veles 0.0.7 → 0.0.8

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.cts CHANGED
@@ -1,10 +1,10 @@
1
- import { V as VelesElement, a as VelesComponent } from './types.d-DgVBp6oa.cjs';
2
- export { F as Fragment, c as createElement } from './fragment-CU26z590.cjs';
3
- export { S as State, c as createState } from './create-state-D1JASFVs.cjs';
1
+ import { V as VelesElement, a as VelesComponentObject, c as VelesChildren } from './types.d-CjiJHqth.cjs';
2
+ export { Fragment, jsx as createElement } from './jsx-runtime.cjs';
3
+ export { S as State, c as createState } from './index-BOpDMzdf.cjs';
4
4
 
5
5
  declare function attachComponent({ htmlElement, component, }: {
6
6
  htmlElement: HTMLElement;
7
- component: VelesElement | VelesComponent;
7
+ component: VelesElement | VelesComponentObject;
8
8
  }): () => void;
9
9
 
10
10
  declare function onMount(cb: () => void | Function): void;
@@ -15,4 +15,13 @@ declare function createRef<T>(initialRefValue?: T | null): {
15
15
  current: T | null;
16
16
  };
17
17
 
18
- export { attachComponent, createRef, onMount, onUnmount };
18
+ declare function createContext<T>(): {
19
+ Provider: ({ value, children }: {
20
+ value: T;
21
+ children?: VelesChildren;
22
+ }) => VelesComponentObject | VelesElement;
23
+ addContext: (value: T) => void;
24
+ readContext: () => T;
25
+ };
26
+
27
+ export { attachComponent, createContext, createRef, onMount, onUnmount };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { V as VelesElement, a as VelesComponent } from './types.d-DgVBp6oa.js';
2
- export { F as Fragment, c as createElement } from './fragment-IVSEC7-Q.js';
3
- export { S as State, c as createState } from './create-state-Bo6TT4qP.js';
1
+ import { V as VelesElement, a as VelesComponentObject, c as VelesChildren } from './types.d-CjiJHqth.js';
2
+ export { Fragment, jsx as createElement } from './jsx-runtime.js';
3
+ export { S as State, c as createState } from './index-BA0L7WRK.js';
4
4
 
5
5
  declare function attachComponent({ htmlElement, component, }: {
6
6
  htmlElement: HTMLElement;
7
- component: VelesElement | VelesComponent;
7
+ component: VelesElement | VelesComponentObject;
8
8
  }): () => void;
9
9
 
10
10
  declare function onMount(cb: () => void | Function): void;
@@ -15,4 +15,13 @@ declare function createRef<T>(initialRefValue?: T | null): {
15
15
  current: T | null;
16
16
  };
17
17
 
18
- export { attachComponent, createRef, onMount, onUnmount };
18
+ declare function createContext<T>(): {
19
+ Provider: ({ value, children }: {
20
+ value: T;
21
+ children?: VelesChildren;
22
+ }) => VelesComponentObject | VelesElement;
23
+ addContext: (value: T) => void;
24
+ readContext: () => T;
25
+ };
26
+
27
+ export { attachComponent, createContext, createRef, onMount, onUnmount };
package/dist/index.js CHANGED
@@ -1,16 +1,17 @@
1
- import {
2
- Fragment
3
- } from "./chunk-X6QYYW56.js";
4
- import {
5
- createState
6
- } from "./chunk-MH6DPZ3V.js";
7
1
  import {
8
2
  callMountHandlers,
3
+ callUnmountHandlers,
4
+ createContext,
5
+ createState,
6
+ getExecutedComponentVelesNode,
7
+ renderTree
8
+ } from "./chunk-GVPFIZZW.js";
9
+ import {
10
+ Fragment,
9
11
  createElement,
10
- getComponentVelesNode,
11
12
  onMount,
12
13
  onUnmount
13
- } from "./chunk-ILNLS6QO.js";
14
+ } from "./chunk-4QXZSBVZ.js";
14
15
 
15
16
  // src/attach-component.ts
16
17
  function attachComponent({
@@ -18,10 +19,12 @@ function attachComponent({
18
19
  component
19
20
  }) {
20
21
  const wrappedApp = createElement("div", { children: [component] });
21
- const { velesElementNode } = getComponentVelesNode(wrappedApp);
22
+ const wrappedAppTree = renderTree(wrappedApp);
23
+ const velesElementNode = getExecutedComponentVelesNode(wrappedAppTree);
22
24
  htmlElement.appendChild(velesElementNode.html);
23
- callMountHandlers(wrappedApp);
25
+ callMountHandlers(wrappedAppTree);
24
26
  return () => {
27
+ callUnmountHandlers(wrappedAppTree);
25
28
  velesElementNode.html.remove();
26
29
  };
27
30
  }
@@ -36,6 +39,7 @@ function createRef(initialRefValue = null) {
36
39
  export {
37
40
  Fragment,
38
41
  attachComponent,
42
+ createContext,
39
43
  createElement,
40
44
  createRef,
41
45
  createState,
@@ -26,28 +26,30 @@ __export(jsx_runtime_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(jsx_runtime_exports);
28
28
 
29
- // src/_utils.ts
30
- function getComponentVelesNode(component) {
31
- const componentsTree = [];
32
- if ("velesStringElement" in component) {
33
- return {
34
- velesElementNode: component,
35
- componentsTree: []
36
- };
37
- }
38
- let childNode = component;
39
- while ("velesComponent" in childNode) {
40
- componentsTree.push(childNode);
41
- if ("velesStringElement" in childNode.tree) {
42
- return {
43
- velesElementNode: childNode.tree,
44
- componentsTree
45
- };
46
- } else {
47
- childNode = childNode.tree;
29
+ // src/create-element/create-text-element.ts
30
+ function createTextElement(text) {
31
+ const mountHandlers = [];
32
+ const unmountHandlers = [];
33
+ return {
34
+ velesStringElement: true,
35
+ // in case there is no text, we create an empty Text node, so we still can
36
+ // have a reference to it, replace it, call lifecycle methods, etc
37
+ html: document.createTextNode(text || ""),
38
+ _privateMethods: {
39
+ _addMountHandler(cb) {
40
+ mountHandlers.push(cb);
41
+ },
42
+ _callMountHandlers() {
43
+ mountHandlers.forEach((cb) => cb());
44
+ },
45
+ _addUnmountHandler: (cb) => {
46
+ unmountHandlers.push(cb);
47
+ },
48
+ _callUnmountHandlers: () => {
49
+ unmountHandlers.forEach((cb) => cb());
50
+ }
48
51
  }
49
- }
50
- return { velesElementNode: childNode, componentsTree };
52
+ };
51
53
  }
52
54
 
53
55
  // src/create-element/parse-children.ts
@@ -60,30 +62,36 @@ function parseChildren({
60
62
  if (children === void 0 || children === null) {
61
63
  return childComponents;
62
64
  }
65
+ let lastInsertedNode = null;
63
66
  (Array.isArray(children) ? children : [children]).forEach(
64
67
  (childComponent) => {
65
68
  if (typeof childComponent === "string") {
66
- const text = document.createTextNode(childComponent);
67
- htmlElement.append(text);
69
+ const textNode = createTextElement(childComponent);
70
+ htmlElement.append(textNode.html);
71
+ lastInsertedNode = textNode.html;
72
+ childComponents.push(textNode);
68
73
  } else if (typeof childComponent === "number") {
69
- const text = document.createTextNode(String(childComponent));
70
- htmlElement.append(text);
74
+ const textNode = createTextElement(String(childComponent));
75
+ htmlElement.append(textNode.html);
76
+ lastInsertedNode = textNode.html;
77
+ childComponents.push(textNode);
71
78
  } else if (typeof childComponent === "object" && childComponent && "velesNode" in childComponent && (childComponent == null ? void 0 : childComponent.velesNode)) {
72
79
  if (childComponent.phantom) {
73
80
  childComponent.childComponents.forEach((childComponentofPhantom) => {
74
81
  if ("velesNode" in childComponentofPhantom) {
75
82
  htmlElement.append(childComponentofPhantom.html);
76
83
  childComponentofPhantom.parentVelesElement = velesNode;
77
- } else {
78
- const { velesElementNode } = getComponentVelesNode(
79
- childComponentofPhantom
80
- );
84
+ lastInsertedNode = childComponentofPhantom.html;
85
+ } else if ("velesStringElement" in childComponentofPhantom) {
86
+ const velesElementNode = childComponentofPhantom;
81
87
  if (!velesElementNode) {
82
88
  console.error("can't find HTML tree in a component chain");
83
89
  } else {
84
90
  htmlElement.append(velesElementNode.html);
91
+ lastInsertedNode = velesElementNode.html;
85
92
  velesElementNode.parentVelesElement = velesNode;
86
93
  }
94
+ } else {
87
95
  }
88
96
  });
89
97
  childComponent.parentVelesElement = velesNode;
@@ -92,39 +100,18 @@ function parseChildren({
92
100
  htmlElement.append(childComponent.html);
93
101
  childComponent.parentVelesElement = velesNode;
94
102
  childComponents.push(childComponent);
103
+ lastInsertedNode = childComponent.html;
95
104
  }
96
- } else if (typeof childComponent === "object" && childComponent && "velesComponent" in childComponent && (childComponent == null ? void 0 : childComponent.velesComponent)) {
97
- const { componentsTree, velesElementNode } = getComponentVelesNode(childComponent);
98
- if (!velesElementNode) {
99
- console.error("can't find HTML tree in a component chain");
100
- } else {
101
- if ("velesNode" in velesElementNode && velesElementNode.phantom) {
102
- velesElementNode.childComponents.forEach(
103
- (childComponentofPhantom) => {
104
- if ("velesNode" in childComponentofPhantom) {
105
- htmlElement.append(childComponentofPhantom.html);
106
- childComponentofPhantom.parentVelesElement = velesNode;
107
- } else {
108
- const { componentsTree: componentsTree2, velesElementNode: velesElementNode2 } = getComponentVelesNode(childComponentofPhantom);
109
- if (!velesElementNode2) {
110
- console.error("can't find HTML tree in a component chain");
111
- } else {
112
- htmlElement.append(velesElementNode2.html);
113
- velesElementNode2.parentVelesElement = velesNode;
114
- }
115
- }
116
- }
117
- );
118
- } else {
119
- htmlElement.append(velesElementNode.html);
120
- }
121
- velesElementNode.parentVelesElement = velesNode;
122
- childComponents.push(childComponent);
123
- }
105
+ } else if (typeof childComponent === "object" && childComponent && "velesComponentObject" in childComponent) {
106
+ childComponent.insertAfter = lastInsertedNode;
107
+ childComponent.parentVelesElement = velesNode;
108
+ childComponents.push(childComponent);
109
+ lastInsertedNode = childComponent;
124
110
  } else if (typeof childComponent === "object" && childComponent && "velesStringElement" in childComponent && (childComponent == null ? void 0 : childComponent.velesStringElement)) {
125
111
  htmlElement.append(childComponent.html);
126
112
  childComponent.parentVelesElement = velesNode;
127
113
  childComponents.push(childComponent);
114
+ lastInsertedNode = childComponent.html;
128
115
  }
129
116
  }
130
117
  );
@@ -141,31 +128,33 @@ function assignAttributes({
141
128
  const isFunction = typeof value === "function";
142
129
  if (isFunction && value.velesAttribute === true) {
143
130
  const attributeValue = value(htmlElement, key, velesNode);
144
- htmlElement.setAttribute(key, attributeValue);
145
- } else if (
146
- // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
147
- isFunction && key.length > 2 && key.startsWith("on")
148
- ) {
149
- htmlElement.addEventListener(
150
- key[2].toLocaleLowerCase() + key.slice(3),
151
- value
152
- );
131
+ assignAttribute({ key, value: attributeValue, htmlElement });
153
132
  } else {
154
- htmlElement.setAttribute(key, value);
133
+ assignAttribute({ key, value, htmlElement });
155
134
  }
156
135
  });
157
136
  }
158
-
159
- // src/hooks/lifecycle.ts
160
- var contextStack = [];
161
- var currentContext = null;
162
- function addContext(newContext) {
163
- contextStack.push(newContext);
164
- currentContext = newContext;
165
- }
166
- function popContext() {
167
- contextStack.pop();
168
- currentContext = contextStack[contextStack.length - 1];
137
+ function assignAttribute({
138
+ key,
139
+ value,
140
+ htmlElement
141
+ }) {
142
+ if (
143
+ // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
144
+ typeof value === "function" && key.startsWith("on")
145
+ ) {
146
+ htmlElement.addEventListener(
147
+ key[2].toLocaleLowerCase() + key.slice(3),
148
+ value
149
+ );
150
+ } else {
151
+ if (typeof value === "boolean") {
152
+ if (value)
153
+ htmlElement.setAttribute(key, "");
154
+ } else {
155
+ htmlElement.setAttribute(key, value);
156
+ }
157
+ }
169
158
  }
170
159
 
171
160
  // src/create-element/parse-component.ts
@@ -173,49 +162,27 @@ function parseComponent({
173
162
  element,
174
163
  props
175
164
  }) {
176
- const componentUnmountCbs = [];
177
- const componentMountCbs = [];
178
- const componentAPI = {
179
- onMount: (cb) => {
180
- componentMountCbs.push(cb);
181
- },
182
- onUnmount: (cb) => {
183
- componentUnmountCbs.push(cb);
184
- }
185
- };
186
- addContext(componentAPI);
187
- const _componentTree = element(props, componentAPI);
188
- const componentTree = typeof _componentTree === "string" || !_componentTree ? {
189
- velesStringElement: true,
190
- html: document.createTextNode(
191
- typeof _componentTree === "string" ? _componentTree : ""
192
- )
193
- } : _componentTree;
194
- popContext();
195
- const velesComponent = {
196
- velesComponent: true,
197
- tree: componentTree,
165
+ const mountCbs = [];
166
+ const unmountCbs = [];
167
+ return {
168
+ velesComponentObject: true,
169
+ element,
170
+ props,
198
171
  _privateMethods: {
172
+ _addMountHandler(cb) {
173
+ mountCbs.push(cb);
174
+ },
199
175
  _addUnmountHandler: (cb) => {
200
- componentAPI.onUnmount(cb);
176
+ unmountCbs.push(cb);
201
177
  },
202
178
  _callMountHandlers: () => {
203
- componentMountCbs.forEach((cb) => {
204
- const mountCbResult = cb();
205
- if (typeof mountCbResult === "function") {
206
- componentAPI.onUnmount(mountCbResult);
207
- }
208
- });
179
+ mountCbs.forEach((cb) => cb());
209
180
  },
210
181
  _callUnmountHandlers: () => {
211
- componentUnmountCbs.forEach((cb) => cb());
212
- if ("_privateMethods" in velesComponent.tree) {
213
- velesComponent.tree._privateMethods._callUnmountHandlers();
214
- }
182
+ unmountCbs.forEach((cb) => cb());
215
183
  }
216
184
  }
217
185
  };
218
- return velesComponent;
219
186
  }
220
187
 
221
188
  // src/create-element/create-element.ts
@@ -232,23 +199,25 @@ function createElement(element, props = {}) {
232
199
  htmlElement: newElement,
233
200
  velesNode
234
201
  });
235
- let unmountHandlers = [];
236
- const callUnmountHandlers = () => {
237
- unmountHandlers.forEach((cb) => cb());
238
- unmountHandlers = [];
239
- childComponents.forEach((childComponent) => {
240
- childComponent._privateMethods._callUnmountHandlers();
241
- });
242
- };
202
+ const unmountHandlers = [];
243
203
  velesNode.html = newElement;
244
204
  velesNode.velesNode = true;
245
205
  velesNode.childComponents = childComponents;
246
206
  velesNode.phantom = phantom;
207
+ const mountHandlers = [];
247
208
  velesNode._privateMethods = {
248
- _addUnmountHandler: (cb) => {
209
+ _addMountHandler(cb) {
210
+ mountHandlers.push(cb);
211
+ },
212
+ _callMountHandlers() {
213
+ mountHandlers.forEach((cb) => cb());
214
+ },
215
+ _addUnmountHandler(cb) {
249
216
  unmountHandlers.push(cb);
250
217
  },
251
- _callUnmountHandlers: callUnmountHandlers
218
+ _callUnmountHandlers() {
219
+ unmountHandlers.forEach((cb) => cb());
220
+ }
252
221
  };
253
222
  assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });
254
223
  return velesNode;