ks-fwork 4.1.0 → 4.1.2
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/ks-fwork.js +572 -791
- package/package.json +4 -7
- package/src/app.js +0 -36
- package/src/attributes.js +0 -62
- package/src/component.js +0 -159
- package/src/destroy-dom.js +0 -64
- package/src/dispatcher.js +0 -56
- package/src/events.js +0 -28
- package/src/h.js +0 -74
- package/src/index.js +0 -4
- package/src/mount-dom.js +0 -108
- package/src/nodes-equal.js +0 -23
- package/src/patch-dom.js +0 -212
- package/src/scheduler.js +0 -37
- package/src/utils/arrays.js +0 -203
- package/src/utils/objects.js +0 -16
- package/src/utils/props.js +0 -6
- package/src/utils/strings.js +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ks-fwork",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"description": "Demo Frontend Framework",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "",
|
|
@@ -9,20 +9,17 @@
|
|
|
9
9
|
"module": "dist/ks-fwork.js",
|
|
10
10
|
"exports": "./dist/ks-fwork.js",
|
|
11
11
|
"files": [
|
|
12
|
-
"
|
|
12
|
+
"dist"
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
16
|
-
"build": "
|
|
16
|
+
"build": "vite build"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"fast-deep-equal": "^3.1.3"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@rollup/plugin-commonjs": "^29.0.3",
|
|
23
|
-
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
24
|
-
"rollup": "^4.24.0",
|
|
25
22
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
26
|
-
"
|
|
23
|
+
"vite": "^8.1.3"
|
|
27
24
|
}
|
|
28
25
|
}
|
package/src/app.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { destroyDOM } from "./destroy-dom.js";
|
|
2
|
-
import { mountDOM } from "./mount-dom.js";
|
|
3
|
-
import { h } from "./h.js";
|
|
4
|
-
|
|
5
|
-
export function createApp(RootComponent, props = {}) {
|
|
6
|
-
let parentEl = null;
|
|
7
|
-
let rootNode = null;
|
|
8
|
-
let isMounted = false;
|
|
9
|
-
|
|
10
|
-
function reset() {
|
|
11
|
-
parentEl = null;
|
|
12
|
-
rootNode = null;
|
|
13
|
-
isMounted = false;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
// Attach the app to a real DOM element and do the first render.
|
|
18
|
-
mount(_parentEl) {
|
|
19
|
-
if (isMounted) throw new Error('The application is already mounted');
|
|
20
|
-
|
|
21
|
-
parentEl = _parentEl;
|
|
22
|
-
rootNode = h(RootComponent, props);
|
|
23
|
-
mountDOM(rootNode, parentEl);
|
|
24
|
-
|
|
25
|
-
isMounted = true;
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
// Detach the app: remove its DOM and cancel every subscription.
|
|
29
|
-
unmount() {
|
|
30
|
-
if (!isMounted) throw new Error('The application is not mounted');
|
|
31
|
-
|
|
32
|
-
destroyDOM(rootNode);
|
|
33
|
-
reset();
|
|
34
|
-
},
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/attributes.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// Set attributes to an element
|
|
2
|
-
export function setAttributes(el, attrs) {
|
|
3
|
-
const { class: className, style, ...otherAttrs } = attrs;
|
|
4
|
-
|
|
5
|
-
if (className) {
|
|
6
|
-
setClass(el, className);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
if (style) {
|
|
10
|
-
Object.entries(style).forEach(([prop, value]) => {
|
|
11
|
-
setStyle(el, prop, value);
|
|
12
|
-
})
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
for (const [name, value] of Object.entries(otherAttrs)) {
|
|
16
|
-
setAttribute(el, name, value);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Set class or array of classes to an element
|
|
21
|
-
function setClass(el, className) {
|
|
22
|
-
el.className = '';
|
|
23
|
-
|
|
24
|
-
// If classes are set as string
|
|
25
|
-
if (typeof className === 'string') {
|
|
26
|
-
el.className = className;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// If classes are set as an array
|
|
30
|
-
if (Array.isArray(className)) {
|
|
31
|
-
el.classList.add(...className);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Set style to an element
|
|
36
|
-
export function setStyle(el, name, value) {
|
|
37
|
-
el.style[name] = value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Remove style from an element
|
|
41
|
-
export function removeStyle(el, name) {
|
|
42
|
-
el.style[name] = null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Set attribute to an element
|
|
46
|
-
export function setAttribute(el, name, value) {
|
|
47
|
-
// Remove attributes with no values
|
|
48
|
-
if (value == null) {
|
|
49
|
-
removeAttribute(el, name);
|
|
50
|
-
// Set 'data-*' attributes separately
|
|
51
|
-
} else if (name.startsWith('data-')) {
|
|
52
|
-
el.setAttribute(name, value);
|
|
53
|
-
} else {
|
|
54
|
-
el[name] = value;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Remove attribute from an element
|
|
59
|
-
export function removeAttribute(el, name) {
|
|
60
|
-
el[name] = null;
|
|
61
|
-
el.removeAttribute(name);
|
|
62
|
-
}
|
package/src/component.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { mountDOM } from "./mount-dom.js";
|
|
2
|
-
import { destroyDOM } from "./destroy-dom.js";
|
|
3
|
-
import { patchDOM } from "./patch-dom.js";
|
|
4
|
-
import { DOM_TYPES, extractChildren } from "./h.js";
|
|
5
|
-
import { hasOwnProperty } from "./utils/objects.js";
|
|
6
|
-
|
|
7
|
-
import equal from 'fast-deep-equal';
|
|
8
|
-
import { Dispatcher } from "./dispatcher.js";
|
|
9
|
-
|
|
10
|
-
const emptyFn = () => {};
|
|
11
|
-
|
|
12
|
-
export function defineComponent({ render, state, onMounted = emptyFn, onUnmounted = emptyFn, ...methods }) {
|
|
13
|
-
class Component {
|
|
14
|
-
#isMounted = false;
|
|
15
|
-
#vNode = null;
|
|
16
|
-
#hostEl = null;
|
|
17
|
-
#eventHandlers = null;
|
|
18
|
-
#parentComponent = null;
|
|
19
|
-
#dispatcher = new Dispatcher();
|
|
20
|
-
#subscriptions = [];
|
|
21
|
-
|
|
22
|
-
constructor(props = {}, eventHandlers = {}, parentComponent = null) {
|
|
23
|
-
this.props = props;
|
|
24
|
-
this.state = state ? state(props) : {};
|
|
25
|
-
this.#eventHandlers = eventHandlers;
|
|
26
|
-
this.#parentComponent = parentComponent;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
onMounted() {
|
|
30
|
-
return Promise.resolve(onMounted.call(this));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
onUnmounted() {
|
|
34
|
-
return Promise.resolve(onUnmounted.call(this));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get elements() {
|
|
38
|
-
// If vNode is null, return an empty array
|
|
39
|
-
if (this.#vNode == null) {
|
|
40
|
-
return [];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// If vNode's top element is fragment, return the elements inside it
|
|
44
|
-
if (this.#vNode.type === DOM_TYPES.FRAGMENT) {
|
|
45
|
-
return extractChildren(this.#vNode).flatMap((child) => {
|
|
46
|
-
if (child.type === DOM_TYPES.COMPONENT) {
|
|
47
|
-
return child.component.elements;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return [child.el];
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// If vNode's top element is a single node, return its element
|
|
55
|
-
return [this.#vNode.el];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Return component's first element
|
|
59
|
-
get firstElement() {
|
|
60
|
-
return this.elements[0];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Return component's first element offset inside the parent element. If is not fragment, return offset 0
|
|
64
|
-
get offset() {
|
|
65
|
-
if(this.#vNode.type === DOM_TYPES.FRAGMENT) {
|
|
66
|
-
return Array.from(this.#hostEl.children).indexOf(this.firstElement);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return 0;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
updateProps(props) {
|
|
73
|
-
const newProps = { ...this.props, ...props };
|
|
74
|
-
|
|
75
|
-
if (equal(this.props, newProps)) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this.props = newProps;
|
|
80
|
-
this.#patch();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
updateState(state) {
|
|
84
|
-
this.state = { ...this.state, ...state };
|
|
85
|
-
this.#patch();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
render() {
|
|
89
|
-
return render.call(this);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
emit(eventName, payload) {
|
|
93
|
-
this.#dispatcher.dispatch(eventName, payload);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
mount(hostEl, index = null) {
|
|
97
|
-
if (this.#isMounted) {
|
|
98
|
-
throw new Error('Component is already mounted');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
this.#vNode = this.render();
|
|
102
|
-
mountDOM(this.#vNode, hostEl, index, this);
|
|
103
|
-
this.#wireEventHandlers();
|
|
104
|
-
|
|
105
|
-
this.#hostEl = hostEl;
|
|
106
|
-
this.#isMounted = true;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
unmount() {
|
|
110
|
-
if (!this.#isMounted) {
|
|
111
|
-
throw new Error('Component is not mounted');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
destroyDOM(this.#vNode);
|
|
115
|
-
this.#subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
116
|
-
|
|
117
|
-
this.#vNode = null;
|
|
118
|
-
this.#hostEl = null;
|
|
119
|
-
this.#isMounted = false;
|
|
120
|
-
this.#subscriptions = [];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
#patch() {
|
|
124
|
-
if (!this.#isMounted) {
|
|
125
|
-
throw new Error('Component is not mounted');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const vNode = this.render();
|
|
129
|
-
this.#vNode = patchDOM(this.#vNode, vNode, this.#hostEl, this);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
#wireEventHandlers() {
|
|
133
|
-
this.#subscriptions = Object.entries(this.#eventHandlers).map(
|
|
134
|
-
([eventName, handler]) =>
|
|
135
|
-
this.#wireEventHandler(eventName, handler)
|
|
136
|
-
)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
#wireEventHandler(eventName, handler) {
|
|
140
|
-
return this.#dispatcher.subscribe(eventName, (payload) => {
|
|
141
|
-
if (this.#parentComponent) {
|
|
142
|
-
handler.call(this.#parentComponent, payload)
|
|
143
|
-
} else {
|
|
144
|
-
handler(payload)
|
|
145
|
-
}
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
for (const methodName in methods) {
|
|
151
|
-
if (hasOwnProperty(Component, methodName)) {
|
|
152
|
-
throw new Error(`Method "${methodName}()" already exists in the component.`);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
Component.prototype[methodName] = methods[methodName];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return Component;
|
|
159
|
-
}
|
package/src/destroy-dom.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { removeEventListeners } from './events.js'
|
|
2
|
-
import { DOM_TYPES } from "./h.js";
|
|
3
|
-
import { enqueueJob } from "./scheduler.js";
|
|
4
|
-
|
|
5
|
-
// Recursively destroy a virtual node (and its subtree).
|
|
6
|
-
export function destroyDOM(vNode) {
|
|
7
|
-
const { type } = vNode;
|
|
8
|
-
|
|
9
|
-
switch (type) {
|
|
10
|
-
case DOM_TYPES.TEXT: {
|
|
11
|
-
removeTextNode(vNode);
|
|
12
|
-
break;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
case DOM_TYPES.ELEMENT: {
|
|
16
|
-
removeElementNode(vNode);
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
case DOM_TYPES.FRAGMENT: {
|
|
21
|
-
removeFragmentNode(vNode);
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
case DOM_TYPES.COMPONENT: {
|
|
26
|
-
vNode.component.unmount();
|
|
27
|
-
enqueueJob(() => vNode.component.onUnmounted());
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
default: {
|
|
32
|
-
throw new Error(`Can't destroy virtual node of type: ${vNode.type}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Drop the reference to the DOM element
|
|
37
|
-
delete vNode.el;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Detach the text node from the DOM.
|
|
41
|
-
function removeTextNode(vNode) {
|
|
42
|
-
const { el } = vNode;
|
|
43
|
-
el.remove();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Recursively destroy the element's children, remove its event listeners, then detach the element.
|
|
47
|
-
function removeElementNode(vNode) {
|
|
48
|
-
const { el, children, listeners } = vNode;
|
|
49
|
-
|
|
50
|
-
children.forEach(destroyDOM)
|
|
51
|
-
|
|
52
|
-
if (listeners) {
|
|
53
|
-
removeEventListeners(listeners, el);
|
|
54
|
-
delete vNode.listeners;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
el.remove();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Fragment has no DOM element. Destroy fragment node's children recursively.
|
|
61
|
-
function removeFragmentNode(vNode) {
|
|
62
|
-
const { children } = vNode;
|
|
63
|
-
children.forEach(destroyDOM);
|
|
64
|
-
}
|
package/src/dispatcher.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
export class Dispatcher {
|
|
2
|
-
// All registered commands mapped to their handlers (callback functions)
|
|
3
|
-
#handlersByCommand = new Map();
|
|
4
|
-
|
|
5
|
-
// Handlers that run after every dispatch, regardless of command
|
|
6
|
-
#afterHandlers = [];
|
|
7
|
-
|
|
8
|
-
// Subscribe to a command
|
|
9
|
-
subscribe(commandName, handler) {
|
|
10
|
-
// Create an empty handler array for this command if none exists yet
|
|
11
|
-
if (!this.#handlersByCommand.has(commandName)) {
|
|
12
|
-
this.#handlersByCommand.set(commandName, []);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Get the handler array for this command
|
|
16
|
-
const handlers = this.#handlersByCommand.get(commandName);
|
|
17
|
-
|
|
18
|
-
// If this exact handler reference is already registered, skip it and return a no-op unsubscribe
|
|
19
|
-
if (handlers.includes(handler)) {
|
|
20
|
-
return () => {}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Register the handler
|
|
24
|
-
handlers.push(handler);
|
|
25
|
-
|
|
26
|
-
// Return unsubscribe function
|
|
27
|
-
return () => {
|
|
28
|
-
const index = handlers.indexOf(handler);
|
|
29
|
-
handlers.splice(index, 1);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Register a handler that will be called after every dispatch
|
|
34
|
-
afterEveryCommand(handler) {
|
|
35
|
-
this.#afterHandlers.push(handler);
|
|
36
|
-
|
|
37
|
-
// Return unsubscribe function
|
|
38
|
-
return () => {
|
|
39
|
-
const index = this.#afterHandlers.indexOf(handler);
|
|
40
|
-
this.#afterHandlers.splice(index, 1);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Dispatch a command: run its handlers with the payload
|
|
45
|
-
dispatch(commandName, payload) {
|
|
46
|
-
if (this.#handlersByCommand.has(commandName)) {
|
|
47
|
-
// Call every handler subscribed to this command
|
|
48
|
-
this.#handlersByCommand.get(commandName).forEach((handler) => handler(payload))
|
|
49
|
-
} else {
|
|
50
|
-
console.warn(`No handlers for command: ${commandName}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Call all 'afterEveryCommand' handlers (no payload)
|
|
54
|
-
this.#afterHandlers.forEach((handler) => handler());
|
|
55
|
-
}
|
|
56
|
-
}
|
package/src/events.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// Add an object of event listeners to an element
|
|
2
|
-
export function addEventListeners(listeners = {}, el, hostComponent = null) {
|
|
3
|
-
const addedListeners = {};
|
|
4
|
-
|
|
5
|
-
Object.entries(listeners).forEach(([eventName, handler]) => {
|
|
6
|
-
addedListeners[eventName] = addEventListener(eventName, handler, el, hostComponent);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
return addedListeners;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// Add an event listener to an element
|
|
13
|
-
export function addEventListener(eventName, handler, el, hostComponent = null) {
|
|
14
|
-
function boundHandler() {
|
|
15
|
-
hostComponent ? handler.apply(hostComponent, arguments) : handler(...arguments);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
el.addEventListener(eventName, boundHandler);
|
|
19
|
-
|
|
20
|
-
return boundHandler;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Remove an object of event listeners from an element
|
|
24
|
-
export function removeEventListeners(listeners = {}, el) {
|
|
25
|
-
Object.entries(listeners).forEach(([eventName, handler]) => {
|
|
26
|
-
el.removeEventListener(eventName, handler);
|
|
27
|
-
})
|
|
28
|
-
}
|
package/src/h.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { withoutNulls } from './utils/arrays.js';
|
|
2
|
-
|
|
3
|
-
export const DOM_TYPES = {
|
|
4
|
-
TEXT: 'text',
|
|
5
|
-
ELEMENT: 'element',
|
|
6
|
-
FRAGMENT: 'fragment',
|
|
7
|
-
COMPONENT: 'component'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// Create a virtual DOM element
|
|
11
|
-
export function h(tag, props = {}, children = []) {
|
|
12
|
-
const type = typeof tag === 'string' ? DOM_TYPES.ELEMENT : DOM_TYPES.COMPONENT;
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
tag,
|
|
16
|
-
props,
|
|
17
|
-
type,
|
|
18
|
-
children: mapTextNodes(withoutNulls(children)),
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Create a virtual text node
|
|
23
|
-
export function hString(str) {
|
|
24
|
-
return {
|
|
25
|
-
type: DOM_TYPES.TEXT,
|
|
26
|
-
value: str
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Create a fragment node
|
|
31
|
-
export function hFragment(vNodes) {
|
|
32
|
-
return {
|
|
33
|
-
children: mapTextNodes(withoutNulls(vNodes)),
|
|
34
|
-
type: DOM_TYPES.FRAGMENT,
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Transform strings into text virtual nodes
|
|
39
|
-
function mapTextNodes(children) {
|
|
40
|
-
return children.map((child) =>
|
|
41
|
-
typeof child === 'string' ? hString(child) : child
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Create an array with specified amount of empty elements and fill them with 'p' virtual node that has text as children.
|
|
46
|
-
export function lipsum(numberOfParagraphs) {
|
|
47
|
-
const text = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|
|
48
|
-
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
|
|
49
|
-
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
|
|
50
|
-
ut aliquip ex ea commodo consequat.`;
|
|
51
|
-
|
|
52
|
-
return hFragment(
|
|
53
|
-
Array(numberOfParagraphs).fill(
|
|
54
|
-
h('p', {}, [text])
|
|
55
|
-
)
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Recursively extract a node's children into a flat list, dissolving any nested fragments into their child nodes
|
|
60
|
-
export function extractChildren(vNode) {
|
|
61
|
-
if (vNode.children == null) return [];
|
|
62
|
-
|
|
63
|
-
const children = [];
|
|
64
|
-
|
|
65
|
-
for (const child of vNode.children) {
|
|
66
|
-
if (child.type === DOM_TYPES.FRAGMENT) {
|
|
67
|
-
children.push(...extractChildren(child))
|
|
68
|
-
} else {
|
|
69
|
-
children.push(child);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return children;
|
|
74
|
-
}
|
package/src/index.js
DELETED
package/src/mount-dom.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { DOM_TYPES } from "./h.js";
|
|
2
|
-
import { setAttributes } from './attributes.js'
|
|
3
|
-
import { addEventListeners } from "./events.js";
|
|
4
|
-
import { enqueueJob } from "./scheduler.js";
|
|
5
|
-
import { extractPropsAndEvents } from "./utils/props.js";
|
|
6
|
-
|
|
7
|
-
// Recursively mount a virtual node (and its subtree) into a real parent DOM element.
|
|
8
|
-
export function mountDOM(vNode, parentEl, index, hostComponent = null) {
|
|
9
|
-
switch (vNode.type) {
|
|
10
|
-
case DOM_TYPES.TEXT: {
|
|
11
|
-
createTextNode(vNode, parentEl, index);
|
|
12
|
-
break;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
case DOM_TYPES.ELEMENT: {
|
|
16
|
-
createElementNode(vNode, parentEl, index, hostComponent);
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
case DOM_TYPES.FRAGMENT: {
|
|
21
|
-
createFragmentNode(vNode, parentEl, index, hostComponent);
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
case DOM_TYPES.COMPONENT: {
|
|
26
|
-
createComponentNode(vNode, parentEl, index, hostComponent);
|
|
27
|
-
enqueueJob(() => vNode.component.onMounted());
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
default: {
|
|
32
|
-
throw new Error(`Can't mount virtual node of type: ${vNode.type}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Create a real text node, save a reference to it on the vNode, and append it to the parent.
|
|
38
|
-
function createTextNode(vNode, parentEl, index) {
|
|
39
|
-
const { value } = vNode;
|
|
40
|
-
|
|
41
|
-
const textNode = document.createTextNode(value);
|
|
42
|
-
vNode.el = textNode;
|
|
43
|
-
|
|
44
|
-
insert(textNode, parentEl, index);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Create a real element, apply its props, recursively mount its children into it, then append the finished element to the parent.
|
|
48
|
-
function createElementNode(vNode, parentEl, index, hostComponent) {
|
|
49
|
-
const { tag, children } = vNode;
|
|
50
|
-
|
|
51
|
-
const el = document.createElement(tag);
|
|
52
|
-
addProps(el, vNode, hostComponent);
|
|
53
|
-
vNode.el = el;
|
|
54
|
-
|
|
55
|
-
children.forEach((child) => mountDOM(child, el, null, hostComponent));
|
|
56
|
-
insert(el, parentEl, index);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Fragment has no DOM element. Mount its children directly into the parent.
|
|
60
|
-
function createFragmentNode(vNode, parentEl, index, hostComponent) {
|
|
61
|
-
const { children } = vNode;
|
|
62
|
-
vNode.el = parentEl;
|
|
63
|
-
|
|
64
|
-
children.forEach((child, i) => {
|
|
65
|
-
mountDOM(child, parentEl, index ? index + i : null, hostComponent)
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function createComponentNode(vNode, parentEl, index, hostComponent) {
|
|
70
|
-
const Component = vNode.tag;
|
|
71
|
-
const { props, events } = extractPropsAndEvents(vNode);
|
|
72
|
-
const component = new Component(props, events, hostComponent);
|
|
73
|
-
|
|
74
|
-
component.mount(parentEl, index);
|
|
75
|
-
vNode.component = component;
|
|
76
|
-
vNode.el = component.firstElement;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Split props into event listeners ('on') and attributes and add them to DOM element.
|
|
80
|
-
function addProps(el, vNode, hostComponent) {
|
|
81
|
-
const { props: attrs, events} = extractPropsAndEvents(vNode);
|
|
82
|
-
|
|
83
|
-
vNode.listeners = addEventListeners(events, el, hostComponent);
|
|
84
|
-
setAttributes(el, attrs);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Insert element into parent element at the specified index (or append if no index given)
|
|
88
|
-
function insert(el, parentEl, index) {
|
|
89
|
-
// Append if no index specified (null or undefined)
|
|
90
|
-
if (index == null) {
|
|
91
|
-
parentEl.append(el);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (index < 0) {
|
|
96
|
-
throw new Error(`Index must be a positive number, got ${index}`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const children = parentEl.childNodes;
|
|
100
|
-
|
|
101
|
-
if (index >= children.length) {
|
|
102
|
-
// Append if index is greater than or equal to the parent element's length of children
|
|
103
|
-
parentEl.append(el);
|
|
104
|
-
} else {
|
|
105
|
-
// Insert before the element at the specified index
|
|
106
|
-
parentEl.insertBefore(el, children[index]);
|
|
107
|
-
}
|
|
108
|
-
}
|
package/src/nodes-equal.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { DOM_TYPES } from "./h.js";
|
|
2
|
-
|
|
3
|
-
export function areNodesEqual(nodeOne, nodeTwo) {
|
|
4
|
-
// Nodes are different type
|
|
5
|
-
if (nodeOne.type !== nodeTwo.type) return false;
|
|
6
|
-
|
|
7
|
-
if (nodeOne.type === DOM_TYPES.ELEMENT) {
|
|
8
|
-
const { tag: tagOne, props: { key: keyOne } } = nodeOne;
|
|
9
|
-
const { tag: tagTwo, props: { key: keyTwo } } = nodeTwo;
|
|
10
|
-
|
|
11
|
-
return tagOne === tagTwo && keyOne === keyTwo;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (nodeOne.type === DOM_TYPES.COMPONENT) {
|
|
15
|
-
const { tag: componentOne, props: { key: keyOne } } = nodeOne;
|
|
16
|
-
const { tag: componentTwo, props: { key: keyTwo } } = nodeTwo;
|
|
17
|
-
|
|
18
|
-
return componentOne === componentTwo && keyOne === keyTwo;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Text and fragment nodes are always equal
|
|
22
|
-
return true;
|
|
23
|
-
}
|