ziko 0.41.2 → 0.42.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ziko",
3
- "version": "0.41.2",
3
+ "version": "0.42.1",
4
4
  "description": "A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...",
5
5
  "keywords": [
6
6
  "front-end",
@@ -60,7 +60,7 @@
60
60
  "./time": {},
61
61
  "./components": {}
62
62
  },
63
- "sideEffects" : false,
63
+ "sideEffects" : true,
64
64
  "bin": {
65
65
  "create-ziko-app": "starter/bin/index.js"
66
66
  },
@@ -2,5 +2,8 @@ export const __CACHE__ = {
2
2
  ui_index : 0,
3
3
  get_ui_index:function(){
4
4
  return this.ui_index ++
5
+ },
6
+ register_ui: function(UIElement){
7
+
5
8
  }
6
9
  }
@@ -1,8 +1,7 @@
1
1
  export const __HYDRATION__ = {
2
- map : new Map(),
2
+ store : new Map(),
3
3
  index : 0,
4
- increment : function(){
5
- return this.index ++
4
+ register: function(component){
5
+ this.store.set(this.index++, component)
6
6
  }
7
- }
8
- export const __HYDRATION_MAP__ = new Map()
7
+ }
@@ -0,0 +1,12 @@
1
+ export const __State__ = {
2
+ store : new Map(),
3
+ index : import.meta.hot?.data?.__Ziko__?.__State__?.index ?? 0,
4
+ register: function(state){
5
+ console.log({
6
+ hmr : import.meta.hot?.data.__Ziko__.__State__.index,
7
+ index : this.index
8
+ })
9
+ this.store.set(this.index++, state)
10
+ }
11
+
12
+ }
@@ -1,22 +1,28 @@
1
- export const __UI__={
2
- __all__(){
3
- return Object.values(this)
4
- .filter(Array.isArray)
5
- .flat();
6
- },
7
- querySelectorAll(){
8
- return this.__all__().filter(n=>n)
9
- },
10
- getElementByIndex(index){
11
- return this.__all__().find(n=>n.ui_index===index);
12
- },
13
- getElementById(id){
14
- return null;
15
- },
16
- getElementsByClass(){
17
-
18
- },
19
- getElementsByTagName(){
20
-
1
+ export class UIStore extends Array {
2
+ constructor(...args) {
3
+ super(...args);
4
+ }
5
+ getItemById(id) {
6
+ return this.find(n => n.element.id === id);
7
+ }
8
+ getItemsByTagName(tag) {
9
+ return this.filter(n => n.element.tagName.toLowerCase() === tag.toLowerCase());
10
+ }
11
+ getElementsByClassName(className) {
12
+ return this.filter(n => n.element.classList?.contains(className));
21
13
  }
22
- }
14
+ querySelector(selector) {
15
+ const el = globalThis?.document?.querySelector(selector);
16
+ if (!el) return null;
17
+ return this.find(ui => ui.element === el) || null;
18
+ }
19
+ querySelectorAll(selector) {
20
+ const els = globalThis?.document?.querySelectorAll(selector);
21
+ return Array.from(els)
22
+ .map(el => this.find(ui => ui.element === el))
23
+ .filter(Boolean);
24
+ }
25
+ }
26
+
27
+ // create the singleton
28
+ export const __UI__ = new UIStore();
@@ -1,17 +1,19 @@
1
1
  import { defineParamsGetter } from './params.js';
2
2
  import { __UI__ } from './__ui__.js';
3
3
  import { __Config__ } from './__config__.js';
4
- import { __HYDRATION__, __HYDRATION_MAP__ } from './__hydration__.js';
4
+ import { __HYDRATION__ } from './__hydration__.js';
5
5
  import { __CACHE__ } from './__cache__.js';
6
+ import { __State__ } from './__state__.js';
6
7
  export function __init__global__(){
7
8
  if ( !globalThis?.__Ziko__ ){
8
9
  globalThis.__Ziko__ = {
9
10
  __UI__,
10
11
  __HYDRATION__,
11
- __HYDRATION_MAP__,
12
+ __State__,
12
13
  __Config__,
13
14
  __CACHE__,
14
15
  };
15
16
  defineParamsGetter(__Ziko__)
16
17
  }
17
18
  }
19
+ export { UIStore } from './__ui__.js'
@@ -3,38 +3,6 @@ export function useDerived(deriveFn, sources) {
3
3
  const subscribers = new Set();
4
4
  let paused = false;
5
5
 
6
- function getValue() {
7
- return {
8
- value,
9
- isStateGetter: () => true,
10
- _subscribe: (fn) => {
11
- subscribers.add(fn);
12
- },
13
- };
14
- }
15
-
16
- function setValue(newValue) {
17
- if (paused) return;
18
- if (typeof newValue === "function") newValue = newValue(value);
19
- if (newValue !== value) {
20
- value = newValue;
21
- subscribers.forEach(fn => fn(value));
22
- }
23
- }
24
-
25
- const controller = {
26
- pause: () => { paused = true; },
27
- resume: () => { paused = false; },
28
- clear: () => { subscribers.clear(); },
29
- force: (newValue) => {
30
- if (typeof newValue === "function") newValue = newValue(value);
31
- value = newValue;
32
- subscribers.forEach(fn => fn(value));
33
- },
34
- getSubscribers: () => new Set(subscribers),
35
- };
36
-
37
- // Subscribe to source states
38
6
  sources.forEach(source => {
39
7
  const srcValue = source(); // getValue()
40
8
  srcValue._subscribe(() => {
@@ -45,8 +13,11 @@ export function useDerived(deriveFn, sources) {
45
13
  subscribers.forEach(fn => fn(value));
46
14
  }
47
15
  }
48
- }, { element: document.body }); // dummy UIElement
16
+ });
49
17
  });
50
-
51
- return [getValue, setValue, controller];
18
+ return () => ({
19
+ value,
20
+ isStateGetter : () => true,
21
+ _subscribe: (fn) => subscribers.add(fn)
22
+ })
52
23
  }
@@ -1,7 +1,17 @@
1
1
  import {mapfun} from '../math'
2
2
  import { useState } from './use-state.js'
3
3
 
4
- const useReactive = (nested_value) => mapfun(n => useState(n), nested_value)
4
+ const useReactive = (nested_value) => mapfun(
5
+ n => {
6
+ const state = useState(n)
7
+ // console.log(state)
8
+ return {
9
+ get : state[0],
10
+ set : state[1],
11
+ }
12
+ },
13
+ nested_value
14
+ )
5
15
  export{
6
16
  useReactive
7
17
  }
@@ -1,55 +1,62 @@
1
+ import { __init__global__ } from "../__ziko__/index.js";
2
+ if(!globalThis.__Ziko__) __init__global__()
3
+
4
+ // HMR persistence
5
+ if (import.meta.hot) {
6
+ import.meta.hot.data.__Ziko__ = import.meta.hot.data.__Ziko__ || globalThis.__Ziko__;
7
+ globalThis.__Ziko__ = import.meta.hot.data.__Ziko__;
8
+ // import.meta.hot.accept(n=>console.log(n));
9
+ // console.log(import.meta.hot.data.__Ziko__.__State__.store)
10
+ }
11
+
12
+
13
+
1
14
  export function useState(initialValue) {
2
- let value = initialValue;
3
- const subscribers = new Set();
4
- let paused = false;
15
+
16
+ // console.log(import.meta.hot.data.__Ziko__.__State__.store.get(0))
17
+
18
+ const {store, index} = __Ziko__.__State__
19
+ __Ziko__.__State__.register({
20
+ value : initialValue,
21
+ subscribers : new Set(),
22
+ paused : false
23
+ })
24
+
25
+ const current = store.get(index);
5
26
 
6
27
  function getValue() {
7
28
  return {
8
- value,
29
+ value: current.value,
9
30
  isStateGetter: () => true,
10
- _subscribe: (
11
- fn,
12
- // UIElement
13
- ) => {
14
- subscribers.add(fn);
15
-
16
- // const observer = new MutationObserver(() => {
17
- // if (!document.body.contains(UIElement.element)) {
18
- // subscribers.delete(fn);
19
- // observer.disconnect();
20
- // }
21
- // });
22
-
23
- // observer.observe(document.body, { childList: true, subtree: true });
24
- },
31
+ _subscribe: (fn) => current.subscribers.add(fn),
25
32
  };
26
33
  }
27
34
 
28
35
  function setValue(newValue) {
29
- if (paused) return;
30
- if (typeof newValue === "function") newValue = newValue(value);
31
- if (newValue !== value) {
32
- value = newValue;
33
- subscribers.forEach(fn => fn(value));
36
+ if (current.paused) return;
37
+ if (typeof newValue === "function") newValue = newValue(current.value);
38
+ if (newValue !== current.value) {
39
+ current.value = newValue;
40
+ current.subscribers.forEach(fn => fn(current.value));
34
41
  }
35
42
  }
36
43
 
37
44
  const controller = {
38
- pause: () => { paused = true; },
39
- resume: () => { paused = false; },
40
- clear: () => { subscribers.clear(); },
41
- force: (newValue) => { // force update even if paused
42
- if (typeof newValue === "function") newValue = newValue(value);
43
- value = newValue;
44
- subscribers.forEach(fn => fn(value));
45
+ pause: () => { current.paused = true; },
46
+ resume: () => { current.paused = false; },
47
+ clear: () => { current.subscribers.clear(); },
48
+ force: (newValue) => {
49
+ if (typeof newValue === "function") newValue = newValue(current.value);
50
+ current.value = newValue;
51
+ current.subscribers.forEach(fn => fn(current.value));
45
52
  },
46
- getSubscribers: () => new Set(subscribers),
53
+ getSubscribers: () => new Set(current.subscribers),
47
54
  };
48
55
 
49
56
  return [getValue, setValue, controller];
50
57
  }
51
58
 
52
- export const isStateGetter = (arg) => {
53
- return typeof(arg) === 'function' && arg?.()?.isStateGetter?.()
54
- }
55
59
 
60
+ export const isStateGetter = (arg) => {
61
+ return typeof arg === 'function' && arg?.()?.isStateGetter?.();
62
+ };
@@ -1,15 +1,26 @@
1
1
  class Tick {
2
- constructor(ms, fn) {
2
+ constructor(fn, ms, count = Infinity, start) {
3
3
  this.ms = ms;
4
4
  this.fn = fn;
5
+ this.count = count;
6
+ this.frame = 1;
5
7
  this.id = null;
6
8
  this.running = false;
9
+ if(start) this.start()
7
10
  }
8
11
 
9
12
  start() {
10
13
  if (!this.running) {
11
14
  this.running = true;
12
- this.id = setInterval(this.fn, this.ms);
15
+ this.frame = 1;
16
+ this.id = setInterval(() => {
17
+ if (this.frame > this.count) {
18
+ this.stop();
19
+ return;
20
+ }
21
+ this.fn.call(null, this);
22
+ this.frame++;
23
+ }, this.ms);
13
24
  }
14
25
  return this;
15
26
  }
@@ -27,8 +38,11 @@ class Tick {
27
38
  return this.running;
28
39
  }
29
40
  }
30
- const tick = (ms, fn) => new Tick(ms, fn);
31
- export{
41
+
42
+ // Helper factory
43
+ const tick = (fn, ms, count = Infinity, start = true) => new Tick(fn, ms, count, start);
44
+
45
+ export {
32
46
  Tick,
33
47
  tick
34
- }
48
+ };
@@ -1,23 +1,23 @@
1
1
  import { isStateGetter } from '../../hooks/use-state.js'
2
2
  export const StyleMethods = {
3
- style(styles){
4
- for(let key in styles){
5
- const value = styles[key];
6
- if(isStateGetter(value)){
7
- const getter = value()
8
- Object.assign(this.element.style, {[key] : getter.value})
9
- getter._subscribe(
10
- (newValue) => {
11
- console.log({newValue})
12
- Object.assign(this.element.style, {[key] : newValue})
13
- },
14
- // this
15
- );
16
- }
17
- else Object.assign(this.element.style, {[key] : value})
18
- }
19
- return this;
20
- },
3
+ // style(styles){
4
+ // for(let key in styles){
5
+ // const value = styles[key];
6
+ // if(isStateGetter(value)){
7
+ // const getter = value()
8
+ // Object.assign(this.element.style, {[key] : getter.value})
9
+ // getter._subscribe(
10
+ // (newValue) => {
11
+ // console.log({newValue})
12
+ // Object.assign(this.element.style, {[key] : newValue})
13
+ // },
14
+ // // this
15
+ // );
16
+ // }
17
+ // else Object.assign(this.element.style, {[key] : value})
18
+ // }
19
+ // return this;
20
+ // },
21
21
  size(width, height){
22
22
  return this.style({width, height})
23
23
  },
@@ -7,7 +7,7 @@ import {
7
7
  EventsMethodes,
8
8
  StyleMethods
9
9
  } from "../__methods__/index.js";
10
-
10
+ import { isStateGetter } from "../../hooks/use-state.js";
11
11
  import {
12
12
  useCustomEvent,
13
13
  useSwipeEvent,
@@ -16,11 +16,11 @@ import {
16
16
  watchAttr,
17
17
  watchChildren
18
18
  } from "../../reactivity/index.js"
19
- import { Random } from "../../math/index.js";
20
- import {__init__global__} from '../../__ziko__/index.js';
19
+ // import { Random } from "../../math/index.js";
20
+ import {__init__global__, UIStore} from '../../__ziko__/index.js';
21
21
  __init__global__()
22
22
  class UIElement extends UINode{
23
- constructor({element, name ='', type="html", useDefaultStyle=false}={}){
23
+ constructor({element, name ='', type="html", render = __Ziko__.__Config__.default.render, useDefaultStyle=false}={}){
24
24
  super()
25
25
  this.target = globalThis.__Ziko__.__Config__.default.target||globalThis?.document?.body;
26
26
  if(typeof element === "string") {
@@ -71,8 +71,8 @@ class UIElement extends UINode{
71
71
  resize:null,
72
72
  intersection:null
73
73
  }
74
- if(element)Object.assign(this.cache,{element});
75
- this.uuid = `${this.cache.name}-${Random.string(16)}`
74
+ if(element) Object.assign(this.cache,{element});
75
+ // this.uuid = `${this.cache.name}-${Random.string(16)}`
76
76
  this.ui_index = globalThis.__Ziko__.__CACHE__.get_ui_index();
77
77
  useDefaultStyle && this.style({
78
78
  position: "relative",
@@ -82,9 +82,9 @@ class UIElement extends UINode{
82
82
  width : "auto",
83
83
  height : "auto"
84
84
  });
85
- this.items = [];
85
+ this.items = new UIStore();
86
86
  globalThis.__Ziko__.__UI__[this.cache.name]?globalThis.__Ziko__.__UI__[this.cache.name]?.push(this):globalThis.__Ziko__.__UI__[this.cache.name]=[this];
87
- element && globalThis.__Ziko__.__Config__.default.render && this?.render?.()
87
+ element && render && this?.render?.()
88
88
  if(
89
89
  // globalThis.__Ziko__.__Config__.renderingMode !== "spa"
90
90
  // &&
@@ -93,9 +93,9 @@ class UIElement extends UINode{
93
93
  this.isInteractive()
94
94
  ){
95
95
  this.setAttr("ziko-hydration-index", globalThis.__Ziko__.__HYDRATION__.index);
96
- globalThis.__Ziko__.__HYDRATION__.map.set(globalThis.__Ziko__.__HYDRATION__.index, ()=>this);
97
- globalThis.__Ziko__.__HYDRATION__.increment()
96
+ globalThis.__Ziko__.__HYDRATION__.register(() => this)
98
97
  }
98
+ globalThis.__Ziko__.__UI__.push(this)
99
99
  }
100
100
  get element(){
101
101
  return this.cache.element;
@@ -106,6 +106,24 @@ class UIElement extends UINode{
106
106
  isZikoUIElement(){
107
107
  return true;
108
108
  }
109
+ style(styles){
110
+ for(let key in styles){
111
+ const value = styles[key];
112
+ if(isStateGetter(value)){
113
+ const getter = value()
114
+ Object.assign(this.element.style, {[key] : getter.value})
115
+ getter._subscribe(
116
+ (newValue) => {
117
+ console.log({newValue})
118
+ Object.assign(this.element.style, {[key] : newValue})
119
+ },
120
+ // this
121
+ );
122
+ }
123
+ else Object.assign(this.element.style, {[key] : value})
124
+ }
125
+ return this;
126
+ }
109
127
  get st(){
110
128
  return this.cache.style;
111
129
  }
@@ -146,14 +164,13 @@ class UIElement extends UINode{
146
164
  return this.element.getBoundingClientRect().left;
147
165
  }
148
166
  clone(render=false) {
149
- const UI = new this.constructor();
150
- UI.__proto__=this.__proto__;
151
- if(this.items.length){
152
- const items = [...this.items].map(n=>n.clone());
153
- UI.append(...items);
154
- }
155
- else UI.element=this.element.cloneNode(true);
156
- return UI.render(render);
167
+ // UI.__proto__=this.__proto__;
168
+ // if(this.items.length){
169
+ // const items = [...this.items].map(n=>n.clone());
170
+ // UI.append(...items);
171
+ // }
172
+ // else UI.element=this.element.cloneNode(true);
173
+ // return UI.render(render);
157
174
  }
158
175
  [Symbol.iterator]() {
159
176
  return this.items[Symbol.iterator]();
@@ -172,14 +189,14 @@ class UIElement extends UINode{
172
189
  this.cache.isFrozzen=freeze;
173
190
  return this;
174
191
  }
175
- setTarget(tg) {
176
- if(this.isBody) return ;
177
- if (tg?.isZikoUIElement) tg = tg.element;
178
- this.unrender();
179
- this.target = tg;
180
- this.render();
181
- return this;
182
- }
192
+ // setTarget(tg) {
193
+ // if(this.isBody) return ;
194
+ // if (tg?.isZikoUIElement) tg = tg.element;
195
+ // this.unrender();
196
+ // this.target = tg;
197
+ // this.render();
198
+ // return this;
199
+ // }
183
200
  describe(label){
184
201
  if(label)this.setAttr("aria-label",label)
185
202
  }
@@ -1,4 +1,12 @@
1
- export function defineWC(name, UIElement, props = {}, { mode = 'open'} = {}) {
1
+ export function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
2
+ if (globalThis.customElements?.get(name)) {
3
+ console.warn(`Custom element "${name}" is already defined`);
4
+ return; // skip redefinition
5
+ }
6
+ if(name.search('-') === -1){
7
+ console.warn(`"${name}" is not a valid custom element name`);
8
+ return;
9
+ }
2
10
  globalThis.customElements?.define(
3
11
  name,
4
12
  class extends HTMLElement {