ziko 1.7.0 → 1.8.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.
Files changed (54) hide show
  1. package/dist/ziko.cjs +433 -154
  2. package/dist/ziko.js +433 -154
  3. package/dist/ziko.min.js +2 -2
  4. package/dist/ziko.mjs +428 -154
  5. package/package.json +1 -1
  6. package/src/dom/{flex → components/Flex}/index.js +1 -1
  7. package/src/dom/components/{for → For}/index.js +1 -1
  8. package/src/dom/{suspense → components/Suspense}/index.js +3 -7
  9. package/src/dom/components/View/index.js +9 -0
  10. package/src/dom/{graphics/canvas.js → components/graphics/Canvas/index.js} +6 -6
  11. package/src/dom/{graphics/svg.js → components/graphics/Svg/index.js} +4 -4
  12. package/src/dom/components/graphics/index.js +2 -0
  13. package/src/dom/components/index.js +10 -3
  14. package/src/dom/{wrappers/html → components/wrappers/HTMLWrapper}/index.js +1 -1
  15. package/src/dom/{wrappers/svg → components/wrappers/SVGWrapper}/index.js +1 -1
  16. package/src/dom/components/wrappers/index.js +2 -0
  17. package/src/dom/index.js +0 -8
  18. package/src/hooks/index.d.ts +1 -0
  19. package/src/hooks/index.js +1 -0
  20. package/src/hooks/use-effect.d.ts +39 -0
  21. package/src/hooks/use-effect.js +40 -0
  22. package/src/hooks/use-state.d.ts +28 -10
  23. package/src/dom/graphics/index.js +0 -2
  24. package/src/dom/logic/index.js +0 -1
  25. package/src/dom/view/index.js +0 -13
  26. package/src/dom/wrappers/index.js +0 -2
  27. package/src/events-dep/binders/coordinates-based-event.js +0 -25
  28. package/src/events-dep/binders/custom-event.js +0 -33
  29. package/src/events-dep/binders/index.js +0 -105
  30. package/src/events-dep/custom-events-registry/click-away.js +0 -38
  31. package/src/events-dep/custom-events-registry/index.js +0 -3
  32. package/src/events-dep/custom-events-registry/swipe.js +0 -76
  33. package/src/events-dep/custom-events-registry/view.js +0 -74
  34. package/src/events-dep/customizers/normalise-coordinates.js +0 -0
  35. package/src/events-dep/details-setter/index.js +0 -4
  36. package/src/events-dep/details-setter/key.js +0 -14
  37. package/src/events-dep/details-setter/mouse.js +0 -35
  38. package/src/events-dep/details-setter/pointer.js +0 -35
  39. package/src/events-dep/details-setter/touch.js +0 -37
  40. package/src/events-dep/events-map/index.js +0 -70
  41. package/src/events-dep/index.js +0 -3
  42. package/src/events-dep/types/__Shared__.d.ts +0 -155
  43. package/src/events-dep/types/clipboard.d.ts +0 -20
  44. package/src/events-dep/types/focus.d.ts +0 -19
  45. package/src/events-dep/types/pointer.d.ts +0 -45
  46. package/src/events-dep/utils.js +0 -38
  47. package/src/events-dep/ziko-event.js +0 -108
  48. package/src/watch/dom-observer/ziko-observer.js +0 -15
  49. /package/src/dom/{flex → components/Flex}/utils/index.js +0 -0
  50. /package/src/dom/components/{fragment → Fragment}/index.d.ts +0 -0
  51. /package/src/dom/components/{fragment → Fragment}/index.js +0 -0
  52. /package/src/dom/components/{swap → Swap}/index.d.ts +0 -0
  53. /package/src/dom/components/{swap → Swap}/index.js +0 -0
  54. /package/src/dom/{logic/switch → components/Switch}/index.js +0 -0
@@ -1,6 +1,6 @@
1
- import {UIElement} from "../constructors/UIElement.js";
2
- import {Matrix} from "../../math/matrix/Matrix.js"
3
- class ZikoUICanvas extends UIElement{
1
+ import {UIElement} from "../../../constructors/UIElement.js";
2
+ import {Matrix} from "../../../../math/matrix/index.js"
3
+ class UICanvas extends UIElement{
4
4
  constructor(w,h){
5
5
  super("canvas","canvas");
6
6
  this.ctx = this.element?.getContext("2d");
@@ -121,7 +121,7 @@ class ZikoUICanvas extends UIElement{
121
121
  }
122
122
  clone(){
123
123
  console.log(this.width)
124
- const canvas=new ZikoUICanvas();
124
+ const canvas=new UICanvas();
125
125
  canvas.items=this.items;
126
126
  canvas.transformMatrix=this.transformMatrix;
127
127
  canvas.axisMatrix=this.axisMatrix;
@@ -168,8 +168,8 @@ class ZikoUICanvas extends UIElement{
168
168
  }
169
169
  }
170
170
 
171
- const Canvas=(w,h)=>new ZikoUICanvas(w,h);
171
+ const Canvas=(w,h)=>new UICanvas(w,h);
172
172
  export{
173
- ZikoUICanvas,
173
+ UICanvas,
174
174
  Canvas
175
175
  }
@@ -1,5 +1,5 @@
1
- import {UIElement} from "../constructors/UIElement.js";
2
- class ZikoUISvg extends UIElement {
1
+ import {UIElement} from "../../../constructors/UIElement.js";
2
+ class UISvg extends UIElement {
3
3
  constructor(w=360,h=300) {
4
4
  super("svg","svg");
5
5
  //this.cache={};
@@ -62,8 +62,8 @@ class ZikoUISvg extends UIElement {
62
62
 
63
63
  }
64
64
 
65
- const Svg =(w,h)=>new ZikoUISvg(w,h);
65
+ const Svg =(w,h)=>new UISvg(w,h);
66
66
  export{
67
67
  Svg,
68
- ZikoUISvg,
68
+ UISvg,
69
69
  }
@@ -0,0 +1,2 @@
1
+ export * from './Canvas/index.js'
2
+ export * from './Svg/index.js';
@@ -1,3 +1,10 @@
1
- export * from './swap/index.js'
2
- export * from './fragment/index.js'
3
- export * from './for/index.js'
1
+ export * from './View/index.js';
2
+ export * from './Flex/index.js';
3
+ export * from './Suspense/index.js';
4
+ export * from './Swap/index.js';
5
+ export * from './Fragment/index.js';
6
+ export * from './For/index.js';
7
+ export * from './Switch/index.js';
8
+
9
+ export * from './wrappers/index.js'
10
+ export * from './graphics/index.js'
@@ -1,4 +1,4 @@
1
- import { UIElement } from "../../constructors/UIElement.js";
1
+ import { UIElement } from "../../../constructors/UIElement.js";
2
2
 
3
3
  class UIHTMLWrapper extends UIElement {
4
4
  constructor(content){
@@ -1,4 +1,4 @@
1
- import { UIElement } from "../../constructors/UIElement.js";
1
+ import { UIElement } from "../../../constructors/UIElement.js";
2
2
 
3
3
  class UISVGWrapper extends UIElement {
4
4
  constructor(content){
@@ -0,0 +1,2 @@
1
+ export * from './HTMLWrapper/index.js'
2
+ export * from './SVGWrapper/index.js'
package/src/dom/index.js CHANGED
@@ -1,15 +1,7 @@
1
1
  export * from './constructors/UIElement.js'
2
2
  export * from './constructors/UINode.js'
3
3
  export * from './utils/index.js'
4
- export * from './view/index.js'
5
4
  export * from './tags/index.js';
6
5
  export * from './text/index.js';
7
- export * from './flex/index.js';
8
- // export * from './grid/index.js';
9
- export * from './suspense/index.js';
10
- export * from './wrappers/index.js';
11
- // export * from './graphics/index.js'
12
6
  export * from './web-component/index.js'
13
- export * from './logic/index.js'
14
-
15
7
  export * from './components/index.js'
@@ -2,6 +2,7 @@ export type * from './use-ipc'
2
2
  export type * from './use-storage.d.ts'
3
3
  export type * from './use-state.d.ts'
4
4
  export type * from './use-derived.d.ts'
5
+ export type * from './use-effect.d.ts'
5
6
  export type * from './use-reactive.d.ts'
6
7
  export type * from './use-thread.d.ts'
7
8
  export type * from './use-event-emitter.d.ts'
@@ -2,6 +2,7 @@
2
2
  export * from './use-state.js';
3
3
  export * from './use-derived.js';
4
4
  export * from './use-reactive.js';
5
+ export * from './use-effect.js'
5
6
 
6
7
  //
7
8
  export * from './use-ipc.js'
@@ -0,0 +1,39 @@
1
+ import type { StateGetter } from "./use-state.d.ts";
2
+
3
+
4
+ /**
5
+ * Executes a side effect initially and whenever one of the
6
+ * provided reactive state dependencies changes.
7
+ *
8
+ * The current values of the state dependencies are passed
9
+ * to the callback in the same order as the dependency array.
10
+ *
11
+ * @param callback Effect callback receiving the current dependency values.
12
+ * @param deps Reactive state getters to watch.
13
+ * @returns A function that unsubscribes the effect and runs its cleanup.
14
+ *
15
+ * @example
16
+ * const [count, setCount] = useState(0);
17
+ *
18
+ * useEffect(
19
+ * count => {
20
+ * console.log("Count:", count);
21
+ * },
22
+ * [count]
23
+ * );
24
+ *
25
+ * // Count: 0
26
+ *
27
+ * setCount(1);
28
+ * // Count: 1
29
+ *
30
+ * setCount(2);
31
+ * // Count: 2
32
+ *
33
+ * setCount(3);
34
+ * // Count: 3
35
+ */
36
+ export declare function useEffect<T extends readonly unknown[]>(
37
+ callback: (...values: T) => void | (() => void),
38
+ deps: { [K in keyof T]: StateGetter<T[K]> }
39
+ ): () => void;
@@ -0,0 +1,40 @@
1
+ import { isStateGetter } from "./use-state.js";
2
+
3
+ export const useEffect = (callback, deps = []) =>{
4
+ const states = deps.filter(isStateGetter);
5
+
6
+ let cleanup;
7
+
8
+ const execute = () => {
9
+ // Cleanup previous effect
10
+ if (typeof cleanup === "function") {
11
+ cleanup();
12
+ }
13
+
14
+ // Read current state values
15
+ const values = states.map(state => state().value);
16
+
17
+ // Execute effect
18
+ cleanup = callback(...values);
19
+ };
20
+
21
+ // Subscribe to states
22
+ const unsubscribers = states.map(state =>
23
+ state()._subscribe(execute)
24
+ );
25
+
26
+ // Initial execution
27
+ execute();
28
+
29
+ // Return effect cleanup
30
+ return () => {
31
+ unsubscribers.forEach(unsubscribe => {
32
+ unsubscribe();
33
+ });
34
+
35
+ if (typeof cleanup === "function") {
36
+ cleanup();
37
+ cleanup = undefined;
38
+ }
39
+ };
40
+ }
@@ -1,17 +1,33 @@
1
+ export declare const STATE_GETTER: unique symbol;
2
+
3
+ /**
4
+ * Reactive state getter.
5
+ */
6
+ export type StateGetter<T> = (() => {
7
+ value: T;
8
+ _subscribe: (fn: (value: T) => void) => () => void;
9
+ }) & {
10
+ [STATE_GETTER]: true;
11
+ };
12
+
13
+ /**
14
+ * Creates a reactive state.
15
+ *
16
+ * @param initialValue Initial state value.
17
+ *
18
+ * @example
19
+ * const [count, setCount] = useState(0);
20
+ */
1
21
  export function useState<T>(
2
22
  initialValue: T
3
23
  ): [
4
- /** getter function */
5
- () => {
6
- value: T;
7
- isStateGetter: () => true;
8
- _subscribe: (fn: (value: T) => void) => void;
9
- },
24
+ /** Getter function */
25
+ StateGetter<T>,
10
26
 
11
- /** setter function */
27
+ /** Setter function */
12
28
  (newValue: T | ((prev: T) => T)) => void,
13
29
 
14
- /** controller */
30
+ /** Controller */
15
31
  {
16
32
  pause: () => void;
17
33
  resume: () => void;
@@ -21,5 +37,7 @@ export function useState<T>(
21
37
  }
22
38
  ];
23
39
 
24
- /** check if argument is a state getter */
25
- export function isStateGetter(arg: any): boolean;
40
+ /**
41
+ * Checks if a value is a state getter.
42
+ */
43
+ export function isStateGetter(arg: unknown): arg is StateGetter<unknown>;
@@ -1,2 +0,0 @@
1
- export * from './canvas.js'
2
- export * from './svg.js';
@@ -1 +0,0 @@
1
- export * from './switch/index.js'
@@ -1,13 +0,0 @@
1
- import { UIElement } from "../constructors/UIElement.js";
2
- class UIView extends UIElement{
3
- constructor(...items){
4
- super({element : 'div', name : 'view'})
5
- this.append(...items)
6
- }
7
- }
8
-
9
- const View = (...items) => new UIView(...items);
10
- export{
11
- View,
12
- UIView
13
- }
@@ -1,2 +0,0 @@
1
- export * from './html/index.js'
2
- export * from './svg/index.js'
@@ -1,25 +0,0 @@
1
- import { ZikoEvent } from "../ziko-event.js";
2
-
3
- export class CoordinatesBasedEvent extends ZikoEvent{
4
- constructor(signature, target = null, Events = [], details_setter, customizer){
5
- super(signature, target, Events, details_setter, customizer)
6
- Object.assign(this.cache,{
7
- useNormalisedCoordinates : false
8
- })
9
- this.isDown = false;
10
- this.isMoving = false;
11
- this.dx = 0;
12
- this.dy = 0;
13
- this.mx = 0;
14
- this.my = 0;
15
- this.ux = 0;
16
- this.uy = 0;
17
- }
18
- get isDragging(){
19
- return this.isDown && this.isMoving
20
- }
21
- useNormalisedCoordinates(enable = true){
22
- this.cache.useNormalisedCoordinates = enable;
23
- return this;
24
- }
25
- }
@@ -1,33 +0,0 @@
1
- import { ZikoEvent } from "../ziko-event.js";
2
- class ZikoCustomEvent extends ZikoEvent{
3
- constructor(target, events, customizer){
4
- super('custom', target, events, details_setter, customizer)
5
- }
6
- _register_events(events){
7
- super._register_events(events, null, null, false);
8
- return this;
9
- }
10
- emit(event_name, detail = {}){
11
- const event = new CustomEvent(event_name, {
12
- detail,
13
- bubbles: true,
14
- cancelable: true
15
- });
16
- this.targetElement.dispatchEvent(event);
17
- return this;
18
- }
19
- on(event_name, ...callbacks){
20
- if(!this.cache.options.hasOwnProperty(event_name)) this._register_events([event_name]);
21
- this.__onEvent(event_name, this.cache.options[event_name], {}, ...callbacks);
22
- return this;
23
- }
24
- }
25
- function details_setter(){
26
-
27
- }
28
- const bind_custom_event = (target, events, customizer) => new ZikoCustomEvent(target, events, customizer)
29
-
30
- export{
31
- bind_custom_event,
32
- ZikoCustomEvent
33
- }
@@ -1,105 +0,0 @@
1
- import { ZikoEvent } from "../ziko-event.js";
2
- import { EventsMap } from "../events-map/index.js";
3
- import {
4
- ptr_details_setter,
5
- key_details_setter,
6
- mouse_details_setter,
7
- touch_details_setter
8
- } from '../details-setter/index.js'
9
- import {
10
- CoordinatesBasedEvent
11
- } from './coordinates-based-event.js'
12
- import {
13
- register_click_away_event,
14
- register_view_event,
15
- register_swipe_event
16
- } from "../custom-events-registry/index.js";
17
-
18
- export const bind_click_event = (target, customizer) => {
19
- register_click_away_event(target.element)
20
- return new ZikoEvent(
21
- 'click',
22
- target,
23
- EventsMap.Click,
24
- null,
25
- customizer
26
- );
27
- }
28
- export const bind_clipboard_event = (target, customizer) => new ZikoEvent(
29
- 'clipboard',
30
- target,
31
- EventsMap.Clipboard,
32
- null,
33
- customizer
34
- );
35
- export const bind_drag_event = (target, customizer) => new ZikoEvent(
36
- 'drag',
37
- target,
38
- EventsMap.Drag,
39
- null,
40
- customizer
41
- );
42
- export const bind_focus_event = (target, customizer) => new ZikoEvent(
43
- 'focus',
44
- target,
45
- EventsMap.Focus,
46
- null,
47
- customizer
48
- );
49
- export const bind_key_event = (target, customizer) => new ZikoEvent(
50
- 'key',
51
- target,
52
- EventsMap.Key,
53
- key_details_setter,
54
- customizer
55
- );
56
- export const bind_mouse_event = (target, customizer) => new CoordinatesBasedEvent(
57
- 'mouse',
58
- target,
59
- EventsMap.Mouse,
60
- mouse_details_setter,
61
- customizer
62
- );
63
- export const bind_pointer_event = (target, customizer) => new CoordinatesBasedEvent(
64
- 'ptr',
65
- target,
66
- EventsMap.Ptr,
67
- ptr_details_setter,
68
- customizer
69
- );
70
- export const bind_touch_event = (target, customizer) => new CoordinatesBasedEvent(
71
- 'touch',
72
- target,
73
- EventsMap.Touch,
74
- touch_details_setter,
75
- customizer
76
- );
77
- export const bind_wheel_event = (target, customizer) => new ZikoEvent(
78
- 'wheel',
79
- target,
80
- EventsMap.Wheel,
81
- null,
82
- customizer
83
- );
84
-
85
- export const bind_view_event = (target, customizer) => {
86
- register_view_event(target.element)
87
- return new ZikoEvent(
88
- 'view',
89
- target,
90
- EventsMap.View,
91
- null,
92
- customizer
93
- )
94
- }
95
-
96
- export const bind_swipe_event = (target, customizer) => {
97
- register_swipe_event(target.element)
98
- return new ZikoEvent(
99
- 'swipe',
100
- target,
101
- EventsMap.Swipe,
102
- null,
103
- customizer
104
- )
105
- }
@@ -1,38 +0,0 @@
1
- class ClickAwayEvent extends Event {
2
- constructor(originalEvent, targetElement) {
3
- super("clickaway", { bubbles: true, cancelable: true });
4
- this.originalEvent = originalEvent;
5
- this.targetElement = targetElement;
6
- }
7
- }
8
-
9
- function register_click_away_event(element) {
10
- function handler(e) {
11
- if (!element.contains(e.target)) {
12
- const clickAwayEvent = new ClickAwayEvent(e, element);
13
- element.dispatchEvent(clickAwayEvent);
14
- }
15
- }
16
-
17
- globalThis?.document?.addEventListener("click", handler);
18
-
19
- return () => globalThis?.document?.removeEventListener("click", handler);
20
-
21
- }
22
-
23
- export{
24
- ClickAwayEvent,
25
- register_click_away_event
26
- }
27
-
28
- // // Example usage
29
- // const box = document.querySelector("#my-box");
30
-
31
- // const stop = listenClickAway(box);
32
-
33
- // box.addEventListener("clickaway", (e) => {
34
- // console.log("Clicked outside box!", e);
35
- // });
36
-
37
- // // later, you can stop listening:
38
- // // stop();
@@ -1,3 +0,0 @@
1
- export * from './click-away.js'
2
- export * from './view.js'
3
- export * from './swipe.js'
@@ -1,76 +0,0 @@
1
- class SwipeEvent extends CustomEvent {
2
- constructor(type, detail) {
3
- super(type, {
4
- detail,
5
- bubbles: true,
6
- cancelable: true
7
- });
8
- }
9
- }
10
-
11
- function register_swipe_event(
12
- element,
13
- threshold = 50,
14
- restraint = 100,
15
- allowedTime = 500
16
- ) {
17
- let startX = 0,
18
- startY = 0,
19
- startTime = 0,
20
- isPointerDown = false;
21
-
22
- function onPointerDown(e) {
23
- startX = e.clientX;
24
- startY = e.clientY;
25
- startTime = performance.now();
26
- isPointerDown = true;
27
- }
28
-
29
- function onPointerUp(e) {
30
- if (!isPointerDown) return;
31
- isPointerDown = false;
32
-
33
- const distX = e.clientX - startX;
34
- const distY = e.clientY - startY;
35
- const elapsed = performance.now() - startTime;
36
-
37
- let direction = null;
38
- let eventName = null;
39
-
40
- if (elapsed <= allowedTime) {
41
- if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint) {
42
- direction = distX < 0 ? "left" : "right";
43
- eventName = "swipe" + direction;
44
- }
45
- else if (Math.abs(distY) >= threshold && Math.abs(distX) <= restraint) {
46
- direction = distY < 0 ? "up" : "down";
47
- eventName = "swipe" + direction;
48
- }
49
- }
50
-
51
- // Emit event
52
- if (eventName) {
53
- element.dispatchEvent(
54
- new SwipeEvent(eventName, {
55
- direction,
56
- distX,
57
- distY,
58
- originalEvent: e
59
- })
60
- );
61
- }
62
- }
63
-
64
- element.addEventListener("pointerdown", onPointerDown, { passive: true });
65
- element.addEventListener("pointerup", onPointerUp, { passive: true });
66
-
67
- return () => {
68
- element.removeEventListener("pointerdown", onPointerDown);
69
- element.removeEventListener("pointerup", onPointerUp);
70
- };
71
- }
72
-
73
- export {
74
- SwipeEvent,
75
- register_swipe_event
76
- };
@@ -1,74 +0,0 @@
1
- import { throttle } from "../../time/decorators/index.js";
2
- class ViewEvent extends CustomEvent {
3
- constructor(type, detail, { bubbles = true, cancelable = true } = {}) {
4
- super(type, { detail, bubbles, cancelable });
5
- }
6
- }
7
-
8
- function register_view_event(
9
- element,
10
- {
11
- intersection = true,
12
- resize = true,
13
- threshold = 0,
14
- throttleResize = 100,
15
- throttleEnterExit = 0
16
- } = {}
17
- ) {
18
- let intersectionObserver, resizeObserver;
19
- const resizeCallback = entries => {
20
- for (let entry of entries) {
21
- const { width, height } = entry.contentRect;
22
-
23
- element.dispatchEvent(
24
- new ViewEvent("resizeview", {
25
- width,
26
- height,
27
- entry
28
- })
29
- );
30
- }
31
- };
32
-
33
- const throttledResize = throttleResize > 0
34
- ? throttle(resizeCallback, throttleResize)
35
- : resizeCallback;
36
-
37
- const intersectionCallback = entries => {
38
- for (let entry of entries) {
39
- const type = entry.isIntersecting ? "enterview" : "exitview";
40
- element.dispatchEvent(new ViewEvent(type, entry));
41
- }
42
- };
43
-
44
- const throttledIntersections = throttleEnterExit > 0
45
- ? throttle(intersectionCallback, throttleEnterExit)
46
- : intersectionCallback;
47
-
48
- if (intersection) {
49
- intersectionObserver = new IntersectionObserver(throttledIntersections, { threshold });
50
- intersectionObserver.observe(element);
51
- }
52
-
53
- if (resize) {
54
- resizeObserver = new ResizeObserver(throttledResize);
55
- resizeObserver.observe(element);
56
- }
57
-
58
- // ---- UNREGISTER ----
59
- return () => {
60
- if (intersectionObserver) {
61
- intersectionObserver.unobserve(element);
62
- intersectionObserver.disconnect();
63
- }
64
- if (resizeObserver) {
65
- resizeObserver.unobserve(element);
66
- resizeObserver.disconnect();
67
- }
68
- };
69
- }
70
-
71
- export {
72
- ViewEvent,
73
- register_view_event
74
- };
@@ -1,4 +0,0 @@
1
- export * from './key.js'
2
- export * from './pointer.js'
3
- export * from './mouse.js'
4
- export * from './touch.js'
@@ -1,14 +0,0 @@
1
- export function key_details_setter(){
2
- switch(this.currentEvent){
3
- case "keydown" : {
4
- this.kd = this.event.key
5
- }; break;
6
- case "keypress" : {
7
- this.kp = this.event.key
8
- }; break;
9
- case "keyup" : {
10
- this.ku = this.event.key
11
- }; break;
12
-
13
- }
14
- }