valyrian.js 6.0.4 → 6.0.7

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.
@@ -1,4 +1,5 @@
1
1
  /*** Vnode ***/
2
2
  import { Valyrian } from "./interfaces";
3
3
  /*** Hyperscript ***/
4
- export declare const v: Valyrian;
4
+ declare const v: Valyrian;
5
+ export default v;
@@ -4,7 +4,7 @@ export interface DomElement extends Element {
4
4
  }
5
5
  export interface Props {
6
6
  key?: string | number;
7
- data?: string;
7
+ state?: any;
8
8
  oncreate?: {
9
9
  (vnode: IVnode): never;
10
10
  };
@@ -29,12 +29,12 @@ export interface IVnode {
29
29
  dom?: DomElement;
30
30
  isSVG?: boolean;
31
31
  processed?: boolean;
32
- component?: Component | POJOComponent;
32
+ component?: ValyrianComponent;
33
33
  nodeValue?: string;
34
34
  [key: string | number | symbol]: any;
35
35
  }
36
36
  export interface Component {
37
- (props?: Record<string, any> | null, children?: Children): IVnode | Children;
37
+ (props?: Record<string, any> | null, children?: Children): any | IVnode | Children;
38
38
  [key: string | number | symbol]: any;
39
39
  }
40
40
  export interface POJOComponent {
@@ -52,25 +52,7 @@ export interface VnodeWithDom extends IVnode {
52
52
  export interface Directive {
53
53
  (value: any, vnode: VnodeWithDom, oldVnode?: VnodeWithDom): void;
54
54
  }
55
- export interface ValyrianApp {
56
- isMounted: boolean;
57
- eventListenerNames: Record<string, true>;
58
- onCleanup: Function[];
59
- onUnmount: Function[];
60
- onMount: Function[];
61
- onUpdate: Function[];
62
- eventListener?: EventListener;
63
- mainVnode?: VnodeWithDom;
64
- container?: DomElement;
65
- [key: string | number | symbol]: any;
66
- }
67
- export interface MountedValyrianApp extends ValyrianApp {
68
- eventListener: EventListener;
69
- mainVnode: VnodeWithDom;
70
- container: DomElement;
71
- }
72
55
  export interface Current {
73
- app?: ValyrianApp;
74
56
  component?: ValyrianComponent;
75
57
  vnode?: VnodeWithDom;
76
58
  oldVnode?: VnodeWithDom;
@@ -87,21 +69,25 @@ export interface Plugin {
87
69
  export interface Valyrian {
88
70
  (tagOrComponent: string | ValyrianComponent, props: Props, ...children: Children): IVnode | VnodeComponent;
89
71
  fragment: (props: Props, ...children: Children) => Children;
72
+ isNodeJs: boolean;
73
+ isMounted: boolean;
90
74
  current: Current;
75
+ container?: DomElement;
76
+ mainVnode?: VnodeWithDom;
77
+ component?: null | VnodeComponent;
91
78
  directives: Directives;
92
79
  reservedProps: ReservedProps;
93
80
  isVnode: (object?: unknown | IVnode) => object is IVnode;
94
81
  isComponent: (component?: unknown | ValyrianComponent) => component is ValyrianComponent;
95
82
  isVnodeComponent: (vnode?: unknown | VnodeComponent) => vnode is VnodeComponent;
96
- isNodeJs: boolean;
97
83
  trust: (htmlString: string) => Children;
98
84
  onCleanup: (fn: Function) => void;
99
85
  onUnmount: (fn: Function) => void;
100
86
  onMount: (fn: Function) => void;
101
87
  onUpdate: (fn: Function) => void;
102
88
  mount: (container: DomElement | string, component: ValyrianComponent | IVnode) => void | string;
103
- update: (component: ValyrianComponent | IVnode) => void | string;
104
- unmount: (component: ValyrianComponent | IVnode) => void | string;
89
+ update: () => void | string;
90
+ unmount: () => void | string;
105
91
  setAttribute: (name: string, value: any, vnode: VnodeWithDom, oldVnode?: VnodeWithDom) => void;
106
92
  directive: (name: string, directive: Directive) => void;
107
93
  use: (plugin: Plugin, options?: Record<string | number | symbol, any>) => void | any;
package/dist/index.cjs CHANGED
@@ -24,12 +24,11 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
24
  // lib/index.ts
25
25
  var lib_exports = {};
26
26
  __export(lib_exports, {
27
- v: () => v
27
+ default: () => lib_default
28
28
  });
29
29
  var ComponentString = "__component__";
30
30
  var TextString = "#text";
31
31
  var isNodeJs = Boolean(typeof process !== "undefined" && process.versions && process.versions.node);
32
- var ValyrianSymbol = Symbol("Valyrian");
33
32
  var Und = void 0;
34
33
  var Vnode = function Vnode2(tag, props, children) {
35
34
  this.props = props;
@@ -49,19 +48,23 @@ function createDomElement(tag, isSVG = false) {
49
48
  return isSVG ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag);
50
49
  }
51
50
  function domToVnode(dom) {
52
- if (dom.nodeType === 1 || dom.nodeType === 3) {
53
- let vnode = v(dom.tagName.toLowerCase(), {}, ...Array.from(dom.childNodes).filter((child) => child.nodeType === 1 || child.nodeType === 3).map((child) => {
54
- if (child.nodeType === 1) {
55
- return domToVnode(child);
51
+ if (dom.nodeType === 3) {
52
+ let vnode = v(TextString, {}, []);
53
+ vnode.nodeValue = dom.nodeValue;
54
+ vnode.dom = dom;
55
+ return vnode;
56
+ }
57
+ if (dom.nodeType === 1) {
58
+ let children = [];
59
+ for (let i = 0; i < dom.childNodes.length; i++) {
60
+ let child = domToVnode(dom.childNodes[i]);
61
+ if (child) {
62
+ children.push(child);
56
63
  }
57
- let text = new Vnode(TextString, {}, []);
58
- text.nodeValue = String(child.nodeValue);
59
- text.dom = child;
60
- return text;
61
- }));
62
- if (dom.nodeType === 1) {
63
- [].forEach.call(dom.attributes, (prop) => vnode.props[prop.nodeName] = prop.nodeValue);
64
64
  }
65
+ let props = {};
66
+ [].forEach.call(dom.attributes, (prop) => props[prop.nodeName] = prop.nodeValue);
67
+ let vnode = v(dom.tagName.toLowerCase(), props, ...children);
65
68
  vnode.dom = dom;
66
69
  return vnode;
67
70
  }
@@ -84,27 +87,47 @@ var reservedProps = {
84
87
  "v-for": true,
85
88
  "v-show": true,
86
89
  "v-class": true,
87
- "v-html": true
90
+ "v-html": true,
91
+ "v-model": true
88
92
  };
93
+ var eventListenerNames = {};
94
+ var onCleanupList = [];
95
+ var onMountList = [];
96
+ var onUpdateList = [];
97
+ var onUnmountList = [];
89
98
  var current = {};
99
+ function eventListener(e) {
100
+ let dom = e.target;
101
+ let name = `v-on${e.type}`;
102
+ while (dom) {
103
+ if (dom[name]) {
104
+ dom[name](e, dom);
105
+ if (!e.defaultPrevented) {
106
+ update();
107
+ }
108
+ return;
109
+ }
110
+ dom = dom.parentNode;
111
+ }
112
+ }
90
113
  function onCleanup(callback) {
91
- if (current.app?.onCleanup.indexOf(callback) === -1) {
92
- current.app?.onCleanup.push(callback);
114
+ if (onCleanupList.indexOf(callback) === -1) {
115
+ onCleanupList.push(callback);
93
116
  }
94
117
  }
95
118
  function onUnmount(callback) {
96
- if (current.app?.onUnmount.indexOf(callback) === -1) {
97
- current.app?.onUnmount.push(callback);
119
+ if (onUnmountList.indexOf(callback) === -1) {
120
+ onUnmountList.push(callback);
98
121
  }
99
122
  }
100
123
  function onMount(callback) {
101
- if (current.app?.onMount.indexOf(callback) === -1) {
102
- current.app?.onMount.push(callback);
124
+ if (onMountList.indexOf(callback) === -1) {
125
+ onMountList.push(callback);
103
126
  }
104
127
  }
105
128
  function onUpdate(callback) {
106
- if (current.app?.onUpdate.indexOf(callback) === -1) {
107
- current.app?.onUpdate.push(callback);
129
+ if (onUpdateList.indexOf(callback) === -1) {
130
+ onUpdateList.push(callback);
108
131
  }
109
132
  }
110
133
  function mount(container, component) {
@@ -125,102 +148,54 @@ function mount(container, component) {
125
148
  } else {
126
149
  throw new Error("Component must be a Valyrian Component or a Vnode component");
127
150
  }
128
- if (component[ValyrianSymbol]) {
129
- unmount(component);
130
- } else {
131
- let eventListener = function(e) {
132
- let dom = e.target;
133
- let name = `v-on${e.type}`;
134
- while (dom) {
135
- if (dom[name]) {
136
- dom[name](e, dom);
137
- if (!e.defaultPrevented) {
138
- update(component);
139
- }
140
- return;
141
- }
142
- dom = dom.parentNode;
143
- }
144
- };
145
- component[ValyrianSymbol] = {
146
- isMounted: false,
147
- eventListenerNames: {},
148
- onCleanup: [],
149
- onMount: [],
150
- onUpdate: [],
151
- onUnmount: []
152
- };
153
- component[ValyrianSymbol].eventListener = eventListener;
154
- }
155
- component[ValyrianSymbol].component = vnodeComponent;
156
- component[ValyrianSymbol].container = appContainer;
157
- component[ValyrianSymbol].mainVnode = domToVnode(appContainer);
158
- return update(component);
159
- }
160
- function callCleanup(valyrianApp) {
161
- for (let i = 0; i < valyrianApp.onCleanup.length; i++) {
162
- valyrianApp.onCleanup[i]();
163
- }
164
- valyrianApp.onCleanup = [];
165
- }
166
- function callUnmount(valyrianApp) {
167
- for (let i = 0; i < valyrianApp.onUnmount.length; i++) {
168
- valyrianApp.onUnmount[i]();
169
- }
170
- valyrianApp.onUnmount = [];
171
- }
172
- function callMount(valyrianApp) {
173
- for (let i = 0; i < valyrianApp.onMount.length; i++) {
174
- valyrianApp.onMount[i]();
151
+ if (v.isMounted) {
152
+ unmount();
175
153
  }
176
- valyrianApp.onMount = [];
154
+ v.component = vnodeComponent;
155
+ v.container = appContainer;
156
+ v.mainVnode = domToVnode(appContainer);
157
+ return update();
177
158
  }
178
- function callUpdate(valyrianApp) {
179
- for (let i = 0; i < valyrianApp.onUpdate.length; i++) {
180
- valyrianApp.onUpdate[i]();
159
+ function callCallbackList(list) {
160
+ for (let i = 0; i < list.length; i++) {
161
+ list[i]();
181
162
  }
182
- valyrianApp.onUpdate = [];
163
+ list = [];
183
164
  }
184
- function update(component) {
185
- if (component && component[ValyrianSymbol]) {
186
- let valyrianApp = component[ValyrianSymbol];
187
- current.app = valyrianApp;
188
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
189
- let oldVnode = valyrianApp.mainVnode;
190
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, [valyrianApp.component]);
191
- valyrianApp.mainVnode.dom = oldVnode.dom;
192
- patch(valyrianApp.mainVnode, oldVnode, valyrianApp);
165
+ function update() {
166
+ if (v.component && v.mainVnode) {
167
+ onCleanupList.length && callCallbackList(onCleanupList);
168
+ let oldVnode = v.mainVnode;
169
+ v.mainVnode = new Vnode(v.mainVnode.tag, v.mainVnode.props, [v.component]);
170
+ v.mainVnode.dom = oldVnode.dom;
171
+ patch(v.mainVnode, oldVnode);
193
172
  oldVnode = null;
194
- if (valyrianApp.isMounted === false) {
195
- valyrianApp.onMount.length && callMount(valyrianApp);
196
- valyrianApp.isMounted = true;
173
+ if (v.isMounted === false) {
174
+ onMountList.length && callCallbackList(onMountList);
175
+ v.isMounted = true;
197
176
  } else {
198
- valyrianApp.onUpdate.length && callUpdate(valyrianApp);
177
+ onUpdateList.length && callCallbackList(onUpdateList);
199
178
  }
200
179
  if (isNodeJs) {
201
- return valyrianApp.mainVnode.dom.innerHTML;
180
+ return v.mainVnode.dom.innerHTML;
202
181
  }
203
182
  }
204
183
  }
205
- function unmount(component) {
206
- if (!component || !component[ValyrianSymbol]) {
207
- return;
208
- }
209
- let valyrianApp = component[ValyrianSymbol];
210
- if (valyrianApp.isMounted) {
211
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
212
- valyrianApp.onUnmount.length && callUnmount(valyrianApp);
213
- let oldVnode = valyrianApp.mainVnode;
214
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, []);
215
- valyrianApp.mainVnode.dom = oldVnode.dom;
216
- valyrianApp.mainVnode.isSVG = oldVnode.isSVG;
217
- patch(valyrianApp.mainVnode, oldVnode, valyrianApp);
184
+ function unmount() {
185
+ if (v.isMounted && v.mainVnode && v.component) {
186
+ onCleanupList.length && callCallbackList(onCleanupList);
187
+ onUnmountList.length && callCallbackList(onUnmountList);
188
+ let oldVnode = v.mainVnode;
189
+ v.mainVnode = new Vnode(v.mainVnode.tag, v.mainVnode.props, []);
190
+ v.mainVnode.dom = oldVnode.dom;
191
+ v.mainVnode.isSVG = oldVnode.isSVG;
192
+ patch(v.mainVnode, oldVnode);
218
193
  oldVnode = null;
194
+ v.component = null;
195
+ v.isMounted = false;
219
196
  if (isNodeJs) {
220
- return valyrianApp.mainVnode.dom.innerHTML;
197
+ return v.mainVnode.dom.innerHTML;
221
198
  }
222
- valyrianApp = null;
223
- Reflect.deleteProperty(component, ValyrianSymbol);
224
199
  }
225
200
  }
226
201
  var emptyVnode = new Vnode("__empty__", {}, []);
@@ -238,10 +213,9 @@ function sharedSetAttribute(prop, value, vnode, oldVnode) {
238
213
  return;
239
214
  }
240
215
  if (typeof value === "function") {
241
- let valyrianApp = current.app;
242
- if (prop in valyrianApp.eventListenerNames === false) {
243
- valyrianApp.eventListenerNames[prop] = true;
244
- valyrianApp.container.addEventListener(prop.slice(2), valyrianApp.eventListener);
216
+ if (prop in eventListenerNames === false) {
217
+ eventListenerNames[prop] = true;
218
+ v.container.addEventListener(prop.slice(2), eventListener);
245
219
  }
246
220
  vnode.dom[`v-${prop}`] = value;
247
221
  return;
@@ -282,7 +256,7 @@ function setAttributes(vnode, oldVnode) {
282
256
  }
283
257
  }
284
258
  }
285
- function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
259
+ function patch(newVnode, oldVnode = emptyVnode) {
286
260
  current.vnode = newVnode;
287
261
  current.oldVnode = oldVnode === emptyVnode ? Und : oldVnode;
288
262
  let newTree = newVnode.children;
@@ -338,7 +312,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
338
312
  shouldPatch = false;
339
313
  } else {
340
314
  setAttributes(childVnode, oldChildVnode);
341
- if (valyrianApp.isMounted) {
315
+ if (v.isMounted) {
342
316
  childVnode.props.onupdate && childVnode.props.onupdate(childVnode, oldChildVnode);
343
317
  } else {
344
318
  childVnode.props.oncreate && childVnode.props.oncreate(childVnode);
@@ -355,7 +329,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
355
329
  oldTree[i] && newKeyedList[oldTree[i].props.key] === Und && onremove(oldTree[i]);
356
330
  newVnode.dom.replaceChild(childVnode.dom, newVnode.dom.childNodes[i]);
357
331
  }
358
- shouldPatch && patch(childVnode, oldChildVnode, valyrianApp);
332
+ shouldPatch && patch(childVnode, oldChildVnode);
359
333
  }
360
334
  for (let i = newTreeLength; i < oldTreeLength; i++) {
361
335
  if (newKeyedList[oldTree[i].props.key] === Und) {
@@ -390,12 +364,12 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
390
364
  continue;
391
365
  }
392
366
  setAttributes(newChildVnode, oldChildVnode);
393
- if (valyrianApp.isMounted) {
367
+ if (v.isMounted) {
394
368
  newChildVnode.props.onupdate && newChildVnode.props.onupdate(newChildVnode, oldChildVnode);
395
369
  } else {
396
370
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
397
371
  }
398
- patch(newChildVnode, oldChildVnode, valyrianApp);
372
+ patch(newChildVnode, oldChildVnode);
399
373
  continue;
400
374
  }
401
375
  newChildVnode.dom = createDomElement(newChildVnode.tag, newChildVnode.isSVG);
@@ -403,7 +377,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
403
377
  oldChildVnode.tag !== TextString && onremove(oldChildVnode);
404
378
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
405
379
  newVnode.dom.replaceChild(newChildVnode.dom, oldChildVnode.dom);
406
- patch(newChildVnode, emptyVnode, valyrianApp);
380
+ patch(newChildVnode, emptyVnode);
407
381
  continue;
408
382
  }
409
383
  if (newChildVnode.tag === TextString) {
@@ -415,7 +389,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
415
389
  setAttributes(newChildVnode);
416
390
  newVnode.dom.appendChild(newChildVnode.dom);
417
391
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
418
- patch(newChildVnode, emptyVnode, valyrianApp);
392
+ patch(newChildVnode, emptyVnode);
419
393
  }
420
394
  for (let i = newTreeLength; i < oldTreeLength; i++) {
421
395
  let oldChildVnode = oldTree[i];
@@ -465,7 +439,7 @@ var directives = {
465
439
  "v-model": ([model, property, event], vnode, oldVnode) => {
466
440
  let value;
467
441
  let handler;
468
- if (vnode.name === "input") {
442
+ if (vnode.tag === "input") {
469
443
  event = event || "oninput";
470
444
  switch (vnode.props.type) {
471
445
  case "checkbox": {
@@ -504,7 +478,7 @@ var directives = {
504
478
  setAttribute("value", model[property], vnode, oldVnode);
505
479
  }
506
480
  }
507
- } else if (vnode.name === "select") {
481
+ } else if (vnode.tag === "select") {
508
482
  event = event || "onclick";
509
483
  if (vnode.props.multiple) {
510
484
  handler = (e) => {
@@ -535,7 +509,7 @@ var directives = {
535
509
  }
536
510
  });
537
511
  }
538
- } else if (vnode.name === "textarea") {
512
+ } else if (vnode.tag === "textarea") {
539
513
  event = event || "oninput";
540
514
  vnode.children = [model[property]];
541
515
  }
@@ -573,6 +547,7 @@ v.reservedProps = reservedProps;
573
547
  v.isVnode = isVnode;
574
548
  v.isComponent = isComponent;
575
549
  v.isVnodeComponent = isVnodeComponent;
550
+ v.isMounted = false;
576
551
  v.isNodeJs = isNodeJs;
577
552
  v.trust = trust;
578
553
  v.onCleanup = onCleanup;
@@ -585,4 +560,5 @@ v.update = update;
585
560
  v.setAttribute = setAttribute;
586
561
  v.directive = directive;
587
562
  v.use = use;
563
+ var lib_default = v;
588
564
  module.exports = __toCommonJS(lib_exports);