@lvce-editor/virtual-dom 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +15 -2
- package/dist/index.js +89 -1
- package/package.json +1 -1
- package/src/parts/AttachEvent/AttachEvent.ts +20 -2
- package/src/parts/ComponentUid/ComponentUid.ts +25 -0
- package/src/parts/GetEventListenerOptions/GetEventListenerOptions.ts +10 -0
- package/src/parts/GetWrappedListener/GetWrappedListener.ts +26 -0
- package/src/parts/IpcState/IpcState.ts +11 -0
- package/src/parts/Main/Main.ts +2 -0
- package/src/parts/NameAnonymousFunction/NameAnonymousFunction.ts +5 -0
- package/src/parts/RememberFocus/RememberFocus.ts +57 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
export const render: (elements: any[], eventMap?: any) => HTMLElement
|
|
1
2
|
|
|
3
|
+
export const renderInto: (
|
|
4
|
+
$Parent: HTMLElement,
|
|
5
|
+
elements: any[],
|
|
6
|
+
eventMap?: any,
|
|
7
|
+
) => void
|
|
2
8
|
|
|
3
|
-
export const
|
|
9
|
+
export const setIpc: (ipc: any) => void
|
|
10
|
+
|
|
11
|
+
export const setComponentUid: ($Element: HTMLElement) => void
|
|
4
12
|
|
|
5
|
-
export const
|
|
13
|
+
export const rememberFocus: (
|
|
14
|
+
$Viewlet: HTMLElement,
|
|
15
|
+
dom: any[],
|
|
16
|
+
eventMap: any,
|
|
17
|
+
uid: number,
|
|
18
|
+
) => void
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
// src/parts/ComponentUid/ComponentUid.ts
|
|
2
|
+
var uidSymbol = Symbol("uid");
|
|
3
|
+
var setComponentUid = ($Element, uid) => {
|
|
4
|
+
$Element[uidSymbol] = uid;
|
|
5
|
+
};
|
|
6
|
+
var getUidTarget = ($Element) => {
|
|
7
|
+
while ($Element) {
|
|
8
|
+
if ($Element[uidSymbol]) {
|
|
9
|
+
return $Element;
|
|
10
|
+
}
|
|
11
|
+
$Element = $Element.parentNode;
|
|
12
|
+
}
|
|
13
|
+
return void 0;
|
|
14
|
+
};
|
|
15
|
+
var get = ($Element) => {
|
|
16
|
+
const $Target = getUidTarget($Element);
|
|
17
|
+
return $Target[uidSymbol];
|
|
18
|
+
};
|
|
19
|
+
var fromEvent = (event) => {
|
|
20
|
+
const { target, currentTarget } = event;
|
|
21
|
+
return get(currentTarget || target);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/parts/IpcState/IpcState.ts
|
|
25
|
+
var state = {
|
|
26
|
+
ipc: void 0
|
|
27
|
+
};
|
|
28
|
+
var getIpc = () => {
|
|
29
|
+
return state.ipc;
|
|
30
|
+
};
|
|
31
|
+
var setIpc = (value) => {
|
|
32
|
+
state.ipc = value;
|
|
33
|
+
};
|
|
34
|
+
|
|
1
35
|
// src/parts/ClearNode/ClearNode.ts
|
|
2
36
|
var clearNode = ($Node) => {
|
|
3
37
|
$Node.textContent = "";
|
|
@@ -220,8 +254,60 @@ var getElementTag = (type) => {
|
|
|
220
254
|
}
|
|
221
255
|
};
|
|
222
256
|
|
|
257
|
+
// src/parts/GetEventListenerOptions/GetEventListenerOptions.ts
|
|
258
|
+
var getEventListenerOptions = (eventName) => {
|
|
259
|
+
switch (eventName) {
|
|
260
|
+
case "wheel":
|
|
261
|
+
return {
|
|
262
|
+
passive: true
|
|
263
|
+
};
|
|
264
|
+
default:
|
|
265
|
+
return void 0;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/parts/NameAnonymousFunction/NameAnonymousFunction.ts
|
|
270
|
+
var nameAnonymousFunction = (fn, name) => {
|
|
271
|
+
Object.defineProperty(fn, "name", {
|
|
272
|
+
value: name
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/parts/GetWrappedListener/GetWrappedListener.ts
|
|
277
|
+
var cache = /* @__PURE__ */ new Map();
|
|
278
|
+
var getWrappedListener = (listener, returnValue) => {
|
|
279
|
+
if (!returnValue) {
|
|
280
|
+
return listener;
|
|
281
|
+
}
|
|
282
|
+
if (!cache.has(listener)) {
|
|
283
|
+
const wrapped = (event) => {
|
|
284
|
+
const uid = fromEvent(event);
|
|
285
|
+
const result = listener(event);
|
|
286
|
+
if (result.length === 0) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const ipc = getIpc();
|
|
290
|
+
ipc.send("Viewlet.executeViewletCommand", uid, ...result);
|
|
291
|
+
};
|
|
292
|
+
nameAnonymousFunction(wrapped, listener.name);
|
|
293
|
+
cache.set(listener, wrapped);
|
|
294
|
+
}
|
|
295
|
+
return cache.get(listener);
|
|
296
|
+
};
|
|
297
|
+
|
|
223
298
|
// src/parts/AttachEvent/AttachEvent.ts
|
|
224
299
|
var attachEvent = ($Node, eventMap, key, value) => {
|
|
300
|
+
const listener = eventMap[value];
|
|
301
|
+
if (!listener) {
|
|
302
|
+
console.warn("listener not found", value);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const options = getEventListenerOptions(eventMap);
|
|
306
|
+
const wrapped = getWrappedListener(
|
|
307
|
+
listener,
|
|
308
|
+
eventMap.returnValue
|
|
309
|
+
);
|
|
310
|
+
$Node.addEventListener(key, wrapped, options);
|
|
225
311
|
};
|
|
226
312
|
|
|
227
313
|
// src/parts/VirtualDomElementProp/VirtualDomElementProp.ts
|
|
@@ -354,5 +440,7 @@ var render2 = (elements, eventMap = {}) => {
|
|
|
354
440
|
};
|
|
355
441
|
export {
|
|
356
442
|
render2 as render,
|
|
357
|
-
renderInto
|
|
443
|
+
renderInto,
|
|
444
|
+
setComponentUid,
|
|
445
|
+
setIpc
|
|
358
446
|
};
|
package/package.json
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import * as GetEventListeneroptions from '../GetEventListenerOptions/GetEventListenerOptions.ts'
|
|
2
|
+
import * as GetWrappedListener from '../GetWrappedListener/GetWrappedListener.ts'
|
|
3
|
+
|
|
4
|
+
export const attachEvent = (
|
|
5
|
+
$Node: HTMLElement,
|
|
6
|
+
eventMap: any,
|
|
7
|
+
key: string,
|
|
8
|
+
value: string,
|
|
9
|
+
) => {
|
|
10
|
+
const listener = eventMap[value]
|
|
11
|
+
if (!listener) {
|
|
12
|
+
console.warn('listener not found', value)
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
const options = GetEventListeneroptions.getEventListenerOptions(eventMap)
|
|
16
|
+
const wrapped = GetWrappedListener.getWrappedListener(
|
|
17
|
+
listener,
|
|
18
|
+
eventMap.returnValue,
|
|
19
|
+
)
|
|
20
|
+
$Node.addEventListener(key, wrapped, options)
|
|
3
21
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const uidSymbol = Symbol('uid')
|
|
2
|
+
|
|
3
|
+
export const setComponentUid = ($Element, uid) => {
|
|
4
|
+
$Element[uidSymbol] = uid
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const getUidTarget = ($Element) => {
|
|
8
|
+
while ($Element) {
|
|
9
|
+
if ($Element[uidSymbol]) {
|
|
10
|
+
return $Element
|
|
11
|
+
}
|
|
12
|
+
$Element = $Element.parentNode
|
|
13
|
+
}
|
|
14
|
+
return undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const get = ($Element) => {
|
|
18
|
+
const $Target = getUidTarget($Element)
|
|
19
|
+
return $Target[uidSymbol]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const fromEvent = (event) => {
|
|
23
|
+
const { target, currentTarget } = event
|
|
24
|
+
return get(currentTarget || target)
|
|
25
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as ComponentUid from '../ComponentUid/ComponentUid.ts'
|
|
2
|
+
import * as NameAnonymousFunction from '../NameAnonymousFunction/NameAnonymousFunction.ts'
|
|
3
|
+
import * as IpcState from '../IpcState/IpcState.ts'
|
|
4
|
+
|
|
5
|
+
const cache = new Map()
|
|
6
|
+
|
|
7
|
+
export const getWrappedListener = (listener, returnValue) => {
|
|
8
|
+
if (!returnValue) {
|
|
9
|
+
return listener
|
|
10
|
+
}
|
|
11
|
+
if (!cache.has(listener)) {
|
|
12
|
+
const wrapped = (event) => {
|
|
13
|
+
const uid = ComponentUid.fromEvent(event)
|
|
14
|
+
const result = listener(event)
|
|
15
|
+
// TODO check for empty array by value
|
|
16
|
+
if (result.length === 0) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
const ipc = IpcState.getIpc()
|
|
20
|
+
ipc.send('Viewlet.executeViewletCommand', uid, ...result)
|
|
21
|
+
}
|
|
22
|
+
NameAnonymousFunction.nameAnonymousFunction(wrapped, listener.name)
|
|
23
|
+
cache.set(listener, wrapped)
|
|
24
|
+
}
|
|
25
|
+
return cache.get(listener)
|
|
26
|
+
}
|
package/src/parts/Main/Main.ts
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as ComponentUid from '../ComponentUid/ComponentUid.ts'
|
|
2
|
+
import * as VirtualDom from '../VirtualDom/VirtualDom.ts'
|
|
3
|
+
|
|
4
|
+
const queryInputs = ($Viewlet: HTMLElement) => {
|
|
5
|
+
return [...$Viewlet.querySelectorAll('input, textarea')]
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const rememberFocus = (
|
|
9
|
+
$Viewlet: HTMLElement,
|
|
10
|
+
dom: any[],
|
|
11
|
+
eventMap: any,
|
|
12
|
+
uid = 0,
|
|
13
|
+
) => {
|
|
14
|
+
// TODO replace this workaround with
|
|
15
|
+
// virtual dom diffing
|
|
16
|
+
const oldLeft = $Viewlet.style.left
|
|
17
|
+
const oldTop = $Viewlet.style.top
|
|
18
|
+
const oldWidth = $Viewlet.style.width
|
|
19
|
+
const oldHeight = $Viewlet.style.height
|
|
20
|
+
// @ts-expect-error
|
|
21
|
+
const focused = document.activeElement.getAttribute('name')
|
|
22
|
+
const $$Inputs = queryInputs($Viewlet)
|
|
23
|
+
const inputMap = Object.create(null)
|
|
24
|
+
for (const $Input of $$Inputs) {
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
inputMap[$Input.name] = $Input.value
|
|
27
|
+
}
|
|
28
|
+
if (uid) {
|
|
29
|
+
const $New = VirtualDom.render(dom, eventMap).firstChild
|
|
30
|
+
ComponentUid.setComponentUid($New, uid)
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
$Viewlet.replaceWith($New)
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
$Viewlet = $New
|
|
35
|
+
} else {
|
|
36
|
+
VirtualDom.renderInto($Viewlet, dom, eventMap)
|
|
37
|
+
}
|
|
38
|
+
const $$NewInputs = queryInputs($Viewlet)
|
|
39
|
+
for (const $Input of $$NewInputs) {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
$Input.value = inputMap[$Input.name] || $Input.value || ''
|
|
42
|
+
}
|
|
43
|
+
if (focused) {
|
|
44
|
+
const $Focused = $Viewlet.querySelector(`[name="${focused}"]`)
|
|
45
|
+
if ($Focused) {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
$Focused.focus()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
$Viewlet.style.top = oldTop
|
|
52
|
+
$Viewlet.style.left = oldLeft
|
|
53
|
+
$Viewlet.style.height = oldHeight
|
|
54
|
+
$Viewlet.style.width = oldWidth
|
|
55
|
+
|
|
56
|
+
return $Viewlet
|
|
57
|
+
}
|