@berenjena/react-dev-panel 1.0.3 → 2.0.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.
- package/README.md +32 -171
- package/dist/assets/BooleanControl.css +1 -1
- package/dist/assets/ButtonControl.css +1 -1
- package/dist/assets/ButtonGroupControl.css +1 -1
- package/dist/assets/ColorControl.css +1 -1
- package/dist/assets/ControlRenderer.css +1 -1
- package/dist/assets/DevPanel.css +1 -1
- package/dist/assets/EmptyContent.css +1 -1
- package/dist/assets/Input.css +1 -1
- package/dist/assets/MultiSelectControl.css +1 -0
- package/dist/assets/RangeControl.css +1 -1
- package/dist/assets/Section.css +1 -1
- package/dist/assets/Select.css +1 -1
- package/dist/assets/SeparatorControl.css +1 -1
- package/dist/assets/index.css +1 -1
- package/dist/components/ControlRenderer/ControlRenderer.js +38 -18
- package/dist/components/ControlRenderer/controls/BooleanControl/BooleanControl.d.ts +20 -4
- package/dist/components/ControlRenderer/controls/BooleanControl/BooleanControl.js +16 -14
- package/dist/components/ControlRenderer/controls/ButtonControl/ButtonControl.d.ts +22 -5
- package/dist/components/ControlRenderer/controls/ButtonControl/ButtonControl.js +2 -2
- package/dist/components/ControlRenderer/controls/ButtonGroupControl/ButtonGroupControl.d.ts +35 -0
- package/dist/components/ControlRenderer/controls/ButtonGroupControl/ButtonGroupControl.js +2 -2
- package/dist/components/ControlRenderer/controls/ColorControl/ColorControl.d.ts +13 -6
- package/dist/components/ControlRenderer/controls/ColorControl/ColorControl.js +61 -15
- package/dist/components/ControlRenderer/controls/ColorControl/types.d.ts +14 -0
- package/dist/components/ControlRenderer/controls/DateControl/DateControl.d.ts +17 -5
- package/dist/components/ControlRenderer/controls/DateControl/DateControl.js +12 -12
- package/dist/components/ControlRenderer/controls/MultiSelectControl/MultiSelectControl.d.ts +28 -0
- package/dist/components/ControlRenderer/controls/MultiSelectControl/MultiSelectControl.js +131 -0
- package/dist/components/ControlRenderer/controls/MultiSelectControl/index.d.ts +1 -0
- package/dist/components/ControlRenderer/controls/MultiSelectControl/index.js +4 -0
- package/dist/components/ControlRenderer/controls/MultiSelectControl/types.d.ts +11 -0
- package/dist/components/ControlRenderer/controls/NumberControl/NumberControl.d.ts +26 -11
- package/dist/components/ControlRenderer/controls/NumberControl/NumberControl.js +14 -14
- package/dist/components/ControlRenderer/controls/RangeControl/RangeControl.d.ts +27 -11
- package/dist/components/ControlRenderer/controls/RangeControl/RangeControl.js +19 -19
- package/dist/components/ControlRenderer/controls/SelectControl/SelectControl.d.ts +29 -5
- package/dist/components/ControlRenderer/controls/SeparatorControl/SeparatorControl.d.ts +16 -7
- package/dist/components/ControlRenderer/controls/SeparatorControl/SeparatorControl.js +6 -6
- package/dist/components/ControlRenderer/controls/TextControl/TextControl.d.ts +24 -13
- package/dist/components/ControlRenderer/controls/TextControl/TextControl.js +15 -15
- package/dist/components/ControlRenderer/controls/index.d.ts +2 -0
- package/dist/components/ControlRenderer/controls/index.js +1 -0
- package/dist/components/ControlRenderer/controls/types.d.ts +2 -0
- package/dist/components/DevPanel/DevPanel.js +45 -40
- package/dist/components/DevPanel/types.d.ts +26 -0
- package/dist/components/DevPanelPortal/DevPanelPortal.d.ts +8 -0
- package/dist/components/DevPanelPortal/DevPanelPortal.js +17 -0
- package/dist/components/DevPanelPortal/index.d.ts +1 -0
- package/dist/components/DevPanelPortal/index.js +4 -0
- package/dist/components/EmptyContent/EmptyContent.js +5 -5
- package/dist/components/Icon/index.d.ts +16 -0
- package/dist/components/Icon/index.js +31 -0
- package/dist/components/Input/Input.js +4 -4
- package/dist/components/Section/Section.js +17 -16
- package/dist/components/Select/Select.js +1 -1
- package/dist/components/index.d.ts +3 -2
- package/dist/components/index.js +8 -6
- package/dist/hooks/useDebounceCallback/index.d.ts +1 -0
- package/dist/hooks/useDebounceCallback/index.js +4 -0
- package/dist/hooks/useDebounceCallback/useDebounceCallback.d.ts +8 -0
- package/dist/hooks/useDebounceCallback/useDebounceCallback.js +17 -0
- package/dist/hooks/useDevPanel/useDevPanel.d.ts +10 -5
- package/dist/hooks/useDevPanel/useDevPanel.js +25215 -12
- package/dist/hooks/useDragAndDrop/useDragAndDrop.d.ts +5 -1
- package/dist/hooks/useDragAndDrop/useDragAndDrop.js +24 -24
- package/dist/index.d.ts +0 -1
- package/dist/index.js +2 -8
- package/dist/managers/DevPanelManager.d.ts +85 -0
- package/dist/managers/DevPanelManager.js +119 -0
- package/dist/managers/index.d.ts +1 -0
- package/dist/managers/index.js +4 -0
- package/dist/store/BaseStoreService.d.ts +90 -0
- package/dist/store/BaseStoreService.js +90 -0
- package/dist/store/SectionsStore.d.ts +40 -0
- package/dist/store/SectionsStore.js +114 -0
- package/dist/store/ThemeStore.d.ts +68 -0
- package/dist/store/ThemeStore.js +102 -0
- package/dist/store/{store.d.ts → UIStore.d.ts} +17 -35
- package/dist/store/UIStore.js +117 -0
- package/dist/store/index.d.ts +4 -1
- package/dist/store/index.js +16 -7
- package/package.json +12 -3
- package/dist/assets/index2.css +0 -1
- package/dist/components/Logger/index.d.ts +0 -20
- package/dist/components/Logger/index.js +0 -85
- package/dist/store/store.js +0 -239
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Position } from '../../components/DevPanel/types';
|
|
2
2
|
export interface UseDragAndDropProps {
|
|
3
|
+
/**
|
|
4
|
+
* Callback function to handle position changes during dragging
|
|
5
|
+
* @param position - The new position of the draggable element
|
|
6
|
+
*/
|
|
3
7
|
onPositionChange: (position: Position) => void;
|
|
4
8
|
}
|
|
5
9
|
/**
|
|
@@ -10,7 +14,7 @@ export interface UseDragAndDropProps {
|
|
|
10
14
|
export declare function useDragAndDrop({ onPositionChange }: UseDragAndDropProps): {
|
|
11
15
|
isDragging: boolean;
|
|
12
16
|
elementRef: import('react').RefObject<HTMLDivElement | null>;
|
|
13
|
-
|
|
17
|
+
handlePointerDown: (e: React.PointerEvent) => void;
|
|
14
18
|
stopDragging: () => void;
|
|
15
19
|
adjustPositionForResize: () => void;
|
|
16
20
|
};
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { useRef as
|
|
1
|
+
import { useRef as g, useState as p, useCallback as u, useEffect as f } from "react";
|
|
2
2
|
import { debounce as E } from "../../utils/debounce/debounce.js";
|
|
3
3
|
import { getConstrainedPosition as D } from "../../utils/getConstrainedPosition/getConstrainedPosition.js";
|
|
4
4
|
import { getPositionAdjustment as L } from "../../utils/getPositionAdjustment/getPositionAdjustment.js";
|
|
5
|
-
function
|
|
6
|
-
const o =
|
|
5
|
+
function R({ onPositionChange: s }) {
|
|
6
|
+
const o = g(null), [t, a] = p(!1), [c, l] = p({ x: 0, y: 0 }), d = u(() => {
|
|
7
7
|
const e = o.current;
|
|
8
8
|
if (!e) return;
|
|
9
|
-
const { constrainedPosition: n, needsAdjustment:
|
|
10
|
-
|
|
11
|
-
}, [
|
|
9
|
+
const { constrainedPosition: n, needsAdjustment: m } = L(e);
|
|
10
|
+
m && s(n);
|
|
11
|
+
}, [s]), i = u(
|
|
12
12
|
(e) => {
|
|
13
13
|
if (!t || !o.current) return;
|
|
14
14
|
const n = {
|
|
15
|
-
x: e.clientX -
|
|
16
|
-
y: e.clientY -
|
|
17
|
-
},
|
|
18
|
-
|
|
15
|
+
x: e.clientX - c.x,
|
|
16
|
+
y: e.clientY - c.y
|
|
17
|
+
}, m = D(n, o.current);
|
|
18
|
+
s(m);
|
|
19
19
|
},
|
|
20
|
-
[t,
|
|
21
|
-
), r =
|
|
20
|
+
[t, c.x, c.y, s]
|
|
21
|
+
), r = u(() => {
|
|
22
22
|
a(!1);
|
|
23
|
-
}, []),
|
|
23
|
+
}, []), v = u((e) => {
|
|
24
24
|
if (e.target !== e.currentTarget) return;
|
|
25
25
|
e.preventDefault(), a(!0);
|
|
26
26
|
const n = o.current?.getBoundingClientRect();
|
|
27
|
-
n &&
|
|
27
|
+
n && l({
|
|
28
28
|
x: e.clientX - n.left,
|
|
29
29
|
y: e.clientY - n.top
|
|
30
30
|
});
|
|
@@ -32,24 +32,24 @@ function z({ onPositionChange: i }) {
|
|
|
32
32
|
return f(() => {
|
|
33
33
|
if (!t) return;
|
|
34
34
|
const e = { passive: !0 };
|
|
35
|
-
return document.addEventListener("
|
|
36
|
-
document.removeEventListener("
|
|
35
|
+
return document.addEventListener("pointermove", i, e), document.addEventListener("pointerup", r, e), () => {
|
|
36
|
+
document.removeEventListener("pointermove", i), document.removeEventListener("pointerup", r);
|
|
37
37
|
};
|
|
38
|
-
}, [t,
|
|
39
|
-
const e = E(
|
|
38
|
+
}, [t, i, r]), f(() => {
|
|
39
|
+
const e = E(d, 100);
|
|
40
40
|
return window.addEventListener("resize", e), () => {
|
|
41
41
|
window.removeEventListener("resize", e);
|
|
42
42
|
};
|
|
43
|
-
}, [
|
|
44
|
-
t && (document.removeEventListener("
|
|
45
|
-
}, [t,
|
|
43
|
+
}, [d]), f(() => () => {
|
|
44
|
+
t && (document.removeEventListener("pointermove", i), document.removeEventListener("pointerup", r));
|
|
45
|
+
}, [t, i, r]), {
|
|
46
46
|
isDragging: t,
|
|
47
47
|
elementRef: o,
|
|
48
|
-
|
|
48
|
+
handlePointerDown: v,
|
|
49
49
|
stopDragging: r,
|
|
50
|
-
adjustPositionForResize:
|
|
50
|
+
adjustPositionForResize: d
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
export {
|
|
54
|
-
|
|
54
|
+
R as useDragAndDrop
|
|
55
55
|
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DevPanel as n } from "./components/DevPanel/DevPanel.js";
|
|
3
|
-
import { ControlRenderer as l } from "./components/ControlRenderer/ControlRenderer.js";
|
|
4
|
-
import { useDevPanel as p } from "./hooks/useDevPanel/useDevPanel.js";
|
|
1
|
+
import { useDevPanel as r } from "./hooks/useDevPanel/useDevPanel.js";
|
|
5
2
|
import './assets/index.css';export {
|
|
6
|
-
|
|
7
|
-
n as DevPanel,
|
|
8
|
-
r as controls,
|
|
9
|
-
p as useDevPanel
|
|
3
|
+
r as useDevPanel
|
|
10
4
|
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { DevPanelProps } from '../components/DevPanel/types';
|
|
2
|
+
/**
|
|
3
|
+
* Simplified singleton that manages DevPanel state and props merging
|
|
4
|
+
* Works with createPortal for rendering instead of manual DOM management
|
|
5
|
+
*/
|
|
6
|
+
export declare class DevPanelManager {
|
|
7
|
+
private static instance;
|
|
8
|
+
private currentProps;
|
|
9
|
+
private mountedSections;
|
|
10
|
+
private updateCallback;
|
|
11
|
+
private propsChangeCallback;
|
|
12
|
+
private cleanupTimeoutId;
|
|
13
|
+
private constructor();
|
|
14
|
+
static getInstance(): DevPanelManager;
|
|
15
|
+
/**
|
|
16
|
+
* Sets callback to trigger re-render when state changes
|
|
17
|
+
*
|
|
18
|
+
* @param callback - Function to call when state changes
|
|
19
|
+
*/
|
|
20
|
+
setUpdateCallback(callback: () => void): void;
|
|
21
|
+
/**
|
|
22
|
+
* Updates DevPanel props using merge strategy
|
|
23
|
+
*
|
|
24
|
+
* @param newProps - New props to merge with existing props
|
|
25
|
+
*/
|
|
26
|
+
updateProps(newProps: DevPanelProps): void;
|
|
27
|
+
/**
|
|
28
|
+
* Registers section and updates props
|
|
29
|
+
*
|
|
30
|
+
* @param sectionName - Unique identifier for the section
|
|
31
|
+
* @param props - Optional DevPanel props to merge
|
|
32
|
+
*/
|
|
33
|
+
addSection(sectionName: string, props?: DevPanelProps): void;
|
|
34
|
+
/**
|
|
35
|
+
* Removes section and schedules cleanup if no sections remain
|
|
36
|
+
*
|
|
37
|
+
* @param sectionName - Unique identifier for the section to remove
|
|
38
|
+
*/
|
|
39
|
+
removeSection(sectionName: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* Checks if DevPanel should be rendered
|
|
42
|
+
*
|
|
43
|
+
* @returns True if there are mounted sections, false otherwise
|
|
44
|
+
*/
|
|
45
|
+
shouldRender(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Gets current merged props
|
|
48
|
+
*
|
|
49
|
+
* @returns Copy of the current merged DevPanel props
|
|
50
|
+
*/
|
|
51
|
+
getAllProps(): DevPanelProps;
|
|
52
|
+
/**
|
|
53
|
+
* Subscribe to props changes
|
|
54
|
+
*
|
|
55
|
+
* @param callback - Function to call when props change
|
|
56
|
+
* @returns Unsubscribe function to remove the callback
|
|
57
|
+
*/
|
|
58
|
+
onPropsChange(callback: (props: DevPanelProps) => void): () => void;
|
|
59
|
+
/**
|
|
60
|
+
* Gets active sections for debugging
|
|
61
|
+
*
|
|
62
|
+
* @returns Array of active section names
|
|
63
|
+
*/
|
|
64
|
+
getActiveSections(): string[];
|
|
65
|
+
/**
|
|
66
|
+
* Schedules cleanup with 1s grace period for quick re-mounting
|
|
67
|
+
*/
|
|
68
|
+
private scheduleCleanup;
|
|
69
|
+
/**
|
|
70
|
+
* Notifies subscribers about state changes
|
|
71
|
+
*/
|
|
72
|
+
private notifyUpdate;
|
|
73
|
+
/**
|
|
74
|
+
* Cleans up state and notifies update
|
|
75
|
+
*/
|
|
76
|
+
private cleanup;
|
|
77
|
+
/**
|
|
78
|
+
* Force cleanup - useful for testing
|
|
79
|
+
*/
|
|
80
|
+
forceCleanup(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Reset singleton instance - useful for HMR
|
|
83
|
+
*/
|
|
84
|
+
static resetInstance(): void;
|
|
85
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
class e {
|
|
2
|
+
static instance = null;
|
|
3
|
+
currentProps = {};
|
|
4
|
+
mountedSections = /* @__PURE__ */ new Set();
|
|
5
|
+
updateCallback = null;
|
|
6
|
+
propsChangeCallback = null;
|
|
7
|
+
cleanupTimeoutId = null;
|
|
8
|
+
constructor() {
|
|
9
|
+
}
|
|
10
|
+
static getInstance() {
|
|
11
|
+
return e.instance || (e.instance = new e()), e.instance;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sets callback to trigger re-render when state changes
|
|
15
|
+
*
|
|
16
|
+
* @param callback - Function to call when state changes
|
|
17
|
+
*/
|
|
18
|
+
setUpdateCallback(t) {
|
|
19
|
+
this.updateCallback = t;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Updates DevPanel props using merge strategy
|
|
23
|
+
*
|
|
24
|
+
* @param newProps - New props to merge with existing props
|
|
25
|
+
*/
|
|
26
|
+
updateProps(t) {
|
|
27
|
+
this.currentProps = {
|
|
28
|
+
...this.currentProps,
|
|
29
|
+
...t
|
|
30
|
+
}, this.notifyUpdate();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Registers section and updates props
|
|
34
|
+
*
|
|
35
|
+
* @param sectionName - Unique identifier for the section
|
|
36
|
+
* @param props - Optional DevPanel props to merge
|
|
37
|
+
*/
|
|
38
|
+
addSection(t, i = {}) {
|
|
39
|
+
this.cleanupTimeoutId && (clearTimeout(this.cleanupTimeoutId), this.cleanupTimeoutId = null), this.mountedSections.add(t), Object.keys(i).length > 0 ? this.updateProps(i) : this.notifyUpdate();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Removes section and schedules cleanup if no sections remain
|
|
43
|
+
*
|
|
44
|
+
* @param sectionName - Unique identifier for the section to remove
|
|
45
|
+
*/
|
|
46
|
+
removeSection(t) {
|
|
47
|
+
this.mountedSections.delete(t), this.mountedSections.size === 0 ? this.scheduleCleanup() : this.notifyUpdate();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Checks if DevPanel should be rendered
|
|
51
|
+
*
|
|
52
|
+
* @returns True if there are mounted sections, false otherwise
|
|
53
|
+
*/
|
|
54
|
+
shouldRender() {
|
|
55
|
+
return this.mountedSections.size > 0;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets current merged props
|
|
59
|
+
*
|
|
60
|
+
* @returns Copy of the current merged DevPanel props
|
|
61
|
+
*/
|
|
62
|
+
getAllProps() {
|
|
63
|
+
return { ...this.currentProps };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Subscribe to props changes
|
|
67
|
+
*
|
|
68
|
+
* @param callback - Function to call when props change
|
|
69
|
+
* @returns Unsubscribe function to remove the callback
|
|
70
|
+
*/
|
|
71
|
+
onPropsChange(t) {
|
|
72
|
+
return this.propsChangeCallback = t, () => {
|
|
73
|
+
this.propsChangeCallback = null;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Gets active sections for debugging
|
|
78
|
+
*
|
|
79
|
+
* @returns Array of active section names
|
|
80
|
+
*/
|
|
81
|
+
getActiveSections() {
|
|
82
|
+
return Array.from(this.mountedSections);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Schedules cleanup with 1s grace period for quick re-mounting
|
|
86
|
+
*/
|
|
87
|
+
scheduleCleanup() {
|
|
88
|
+
this.cleanupTimeoutId = setTimeout(() => {
|
|
89
|
+
this.mountedSections.size === 0 && this.cleanup();
|
|
90
|
+
}, 1e3);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Notifies subscribers about state changes
|
|
94
|
+
*/
|
|
95
|
+
notifyUpdate() {
|
|
96
|
+
this.updateCallback?.(), this.propsChangeCallback?.(this.getAllProps());
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Cleans up state and notifies update
|
|
100
|
+
*/
|
|
101
|
+
cleanup() {
|
|
102
|
+
this.currentProps = {}, this.mountedSections.clear(), this.notifyUpdate(), this.cleanupTimeoutId && (clearTimeout(this.cleanupTimeoutId), this.cleanupTimeoutId = null);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Force cleanup - useful for testing
|
|
106
|
+
*/
|
|
107
|
+
forceCleanup() {
|
|
108
|
+
this.cleanupTimeoutId && (clearTimeout(this.cleanupTimeoutId), this.cleanupTimeoutId = null), this.cleanup();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reset singleton instance - useful for HMR
|
|
112
|
+
*/
|
|
113
|
+
static resetInstance() {
|
|
114
|
+
e.instance && (e.instance.forceCleanup(), e.instance = null);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export {
|
|
118
|
+
e as DevPanelManager
|
|
119
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DevPanelManager } from './DevPanelManager';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base class for state management services using the useSyncExternalStore pattern.
|
|
3
|
+
* Provides common functionality for subscription management, state updates, and persistence.
|
|
4
|
+
*
|
|
5
|
+
* @template TState - The type of the state object
|
|
6
|
+
* @template TPersistedState - The type of the persisted state object (usually a subset of TState)
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class BaseStoreService<TState, TPersistedState = TState> {
|
|
9
|
+
/** Set of listeners subscribed to state changes */
|
|
10
|
+
protected readonly listeners: Set<() => void>;
|
|
11
|
+
/** The localStorage key for persisting state */
|
|
12
|
+
protected readonly storageKey: string;
|
|
13
|
+
/** Whether this store should persist state to localStorage */
|
|
14
|
+
protected readonly shouldPersist: boolean;
|
|
15
|
+
/** The current state */
|
|
16
|
+
protected state: TState;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new BaseStoreService instance.
|
|
19
|
+
*
|
|
20
|
+
* @param storageKey - The localStorage key for persisting state (used only if shouldPersist is true)
|
|
21
|
+
* @param defaultState - The default state value
|
|
22
|
+
* @param shouldLoadOnInit - Whether to load persisted state on initialization (only works if shouldPersist is true)
|
|
23
|
+
* @param shouldPersist - Whether to persist state to localStorage (default: true)
|
|
24
|
+
*/
|
|
25
|
+
constructor(storageKey: string, defaultState: TState, shouldLoadOnInit?: boolean, shouldPersist?: boolean);
|
|
26
|
+
/**
|
|
27
|
+
* Abstract method to transform current state to persistable format.
|
|
28
|
+
* Must be implemented by subclasses.
|
|
29
|
+
*
|
|
30
|
+
* @param state - The current state
|
|
31
|
+
* @returns The state in persistable format
|
|
32
|
+
*/
|
|
33
|
+
protected abstract toPersistableState(state: TState): TPersistedState;
|
|
34
|
+
/**
|
|
35
|
+
* Abstract method to transform persisted state back to current state format.
|
|
36
|
+
* Must be implemented by subclasses.
|
|
37
|
+
*
|
|
38
|
+
* @param persistedState - The persisted state
|
|
39
|
+
* @param defaultState - The default state to fall back to
|
|
40
|
+
* @returns The state in current format
|
|
41
|
+
*/
|
|
42
|
+
protected abstract fromPersistedState(persistedState: TPersistedState, defaultState: TState): TState;
|
|
43
|
+
/**
|
|
44
|
+
* Abstract method to get the service name for error messages.
|
|
45
|
+
* Must be implemented by subclasses.
|
|
46
|
+
*
|
|
47
|
+
* @returns The service name
|
|
48
|
+
*/
|
|
49
|
+
protected abstract getServiceName(): string;
|
|
50
|
+
/**
|
|
51
|
+
* Saves the current state to localStorage.
|
|
52
|
+
*
|
|
53
|
+
* @protected
|
|
54
|
+
*/
|
|
55
|
+
protected saveState(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Loads previously persisted state from localStorage.
|
|
58
|
+
* If no state exists or loading fails, keeps the current state.
|
|
59
|
+
*
|
|
60
|
+
* @protected
|
|
61
|
+
*/
|
|
62
|
+
protected loadState(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Notifies all subscribed listeners about state changes.
|
|
65
|
+
*
|
|
66
|
+
* @protected
|
|
67
|
+
*/
|
|
68
|
+
protected notifySubscribers(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Updates the state using an updater function, persists the new state,
|
|
71
|
+
* and notifies all subscribers if the state changed.
|
|
72
|
+
*
|
|
73
|
+
* @param updater - Function that receives current state and returns new state
|
|
74
|
+
* @protected
|
|
75
|
+
*/
|
|
76
|
+
protected setState(updater: (state: TState) => TState): void;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the current state snapshot for useSyncExternalStore.
|
|
79
|
+
*
|
|
80
|
+
* @returns The current state
|
|
81
|
+
*/
|
|
82
|
+
getSnapshot: () => TState;
|
|
83
|
+
/**
|
|
84
|
+
* Subscribes a listener to state changes for useSyncExternalStore.
|
|
85
|
+
*
|
|
86
|
+
* @param listener - Function to call when state changes
|
|
87
|
+
* @returns Unsubscribe function
|
|
88
|
+
*/
|
|
89
|
+
subscribe: (listener: () => void) => (() => void);
|
|
90
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
class r {
|
|
2
|
+
/** Set of listeners subscribed to state changes */
|
|
3
|
+
listeners = /* @__PURE__ */ new Set();
|
|
4
|
+
/** The localStorage key for persisting state */
|
|
5
|
+
storageKey;
|
|
6
|
+
/** Whether this store should persist state to localStorage */
|
|
7
|
+
shouldPersist;
|
|
8
|
+
/** The current state */
|
|
9
|
+
state;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new BaseStoreService instance.
|
|
12
|
+
*
|
|
13
|
+
* @param storageKey - The localStorage key for persisting state (used only if shouldPersist is true)
|
|
14
|
+
* @param defaultState - The default state value
|
|
15
|
+
* @param shouldLoadOnInit - Whether to load persisted state on initialization (only works if shouldPersist is true)
|
|
16
|
+
* @param shouldPersist - Whether to persist state to localStorage (default: true)
|
|
17
|
+
*/
|
|
18
|
+
constructor(t, e, a = !0, s = !0) {
|
|
19
|
+
this.storageKey = t, this.shouldPersist = s, this.state = e, a && s && this.loadState();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Saves the current state to localStorage.
|
|
23
|
+
*
|
|
24
|
+
* @protected
|
|
25
|
+
*/
|
|
26
|
+
saveState() {
|
|
27
|
+
if (this.shouldPersist)
|
|
28
|
+
try {
|
|
29
|
+
const t = this.toPersistableState(this.state);
|
|
30
|
+
localStorage.setItem(this.storageKey, JSON.stringify(t));
|
|
31
|
+
} catch {
|
|
32
|
+
console.warn(`Failed to save ${this.getServiceName()} state to localStorage`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Loads previously persisted state from localStorage.
|
|
37
|
+
* If no state exists or loading fails, keeps the current state.
|
|
38
|
+
*
|
|
39
|
+
* @protected
|
|
40
|
+
*/
|
|
41
|
+
loadState() {
|
|
42
|
+
if (this.shouldPersist)
|
|
43
|
+
try {
|
|
44
|
+
const t = localStorage.getItem(this.storageKey);
|
|
45
|
+
if (t) {
|
|
46
|
+
const e = JSON.parse(t);
|
|
47
|
+
this.state = this.fromPersistedState(e, this.state);
|
|
48
|
+
}
|
|
49
|
+
} catch {
|
|
50
|
+
console.warn(`Failed to load persisted ${this.getServiceName()} state from localStorage`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Notifies all subscribed listeners about state changes.
|
|
55
|
+
*
|
|
56
|
+
* @protected
|
|
57
|
+
*/
|
|
58
|
+
notifySubscribers() {
|
|
59
|
+
this.listeners.forEach((t) => t());
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Updates the state using an updater function, persists the new state,
|
|
63
|
+
* and notifies all subscribers if the state changed.
|
|
64
|
+
*
|
|
65
|
+
* @param updater - Function that receives current state and returns new state
|
|
66
|
+
* @protected
|
|
67
|
+
*/
|
|
68
|
+
setState(t) {
|
|
69
|
+
const e = t(this.state);
|
|
70
|
+
JSON.stringify(e) !== JSON.stringify(this.state) && (this.state = e, this.saveState(), this.notifySubscribers());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns the current state snapshot for useSyncExternalStore.
|
|
74
|
+
*
|
|
75
|
+
* @returns The current state
|
|
76
|
+
*/
|
|
77
|
+
getSnapshot = () => this.state;
|
|
78
|
+
/**
|
|
79
|
+
* Subscribes a listener to state changes for useSyncExternalStore.
|
|
80
|
+
*
|
|
81
|
+
* @param listener - Function to call when state changes
|
|
82
|
+
* @returns Unsubscribe function
|
|
83
|
+
*/
|
|
84
|
+
subscribe = (t) => (this.listeners.add(t), () => {
|
|
85
|
+
this.listeners.delete(t);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
r as BaseStoreService
|
|
90
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ControlsGroup } from '../components/ControlRenderer/controls/types';
|
|
2
|
+
import { DevPanelSection } from '../components/DevPanel/types';
|
|
3
|
+
type SectionsState = Record<string, DevPanelSection>;
|
|
4
|
+
/**
|
|
5
|
+
* React hook that subscribes only to the sections state of the dev panel.
|
|
6
|
+
* This hook will not re-render when UI state (position, visibility) changes.
|
|
7
|
+
*
|
|
8
|
+
* @returns Record of section names to section objects
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const sections = useDevPanelSections();
|
|
13
|
+
* const sectionNames = Object.keys(sections);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function useDevPanelSections(): SectionsState;
|
|
17
|
+
/**
|
|
18
|
+
* React hook that provides access to section actions without subscribing to state.
|
|
19
|
+
* Ideal for components that only need to trigger section actions without rendering on changes.
|
|
20
|
+
*
|
|
21
|
+
* @returns Object containing all available section actions
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const { registerSection, toggleSectionCollapse } = useDevPanelSectionActions();
|
|
26
|
+
*
|
|
27
|
+
* // Add a new section
|
|
28
|
+
* registerSection('mySection', myControls);
|
|
29
|
+
*
|
|
30
|
+
* // Toggle section collapse
|
|
31
|
+
* toggleSectionCollapse('mySection');
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useDevPanelSectionActions(): {
|
|
35
|
+
registerSection: (name: string, controls: ControlsGroup) => void;
|
|
36
|
+
unregisterSection: (name: string) => void;
|
|
37
|
+
toggleSectionCollapse: (name: string) => void;
|
|
38
|
+
reset: () => void;
|
|
39
|
+
};
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useSyncExternalStore as n } from "react";
|
|
2
|
+
import { BaseStoreService as c } from "./BaseStoreService.js";
|
|
3
|
+
const S = "dev-panel-sections-storage";
|
|
4
|
+
class a extends c {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new DevPanelSectionsService instance.
|
|
7
|
+
*/
|
|
8
|
+
constructor() {
|
|
9
|
+
super(S, {}, !1, !1);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Transforms current sections state to persistable format.
|
|
13
|
+
* Since this store doesn't persist, this method returns a dummy value.
|
|
14
|
+
*
|
|
15
|
+
* @param _state - The current sections state (unused)
|
|
16
|
+
* @returns Empty persistable state
|
|
17
|
+
* @protected
|
|
18
|
+
*/
|
|
19
|
+
toPersistableState(e) {
|
|
20
|
+
return { sectionCollapseState: {} };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Transforms persisted state back to current state format.
|
|
24
|
+
* Since this store doesn't persist, this method returns the default state.
|
|
25
|
+
*
|
|
26
|
+
* @param _persistedState - The persisted sections state (unused)
|
|
27
|
+
* @param defaultState - The default state to fall back to
|
|
28
|
+
* @returns The default state
|
|
29
|
+
* @protected
|
|
30
|
+
*/
|
|
31
|
+
fromPersistedState(e, t) {
|
|
32
|
+
return t;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Gets the service name for error messages.
|
|
36
|
+
*
|
|
37
|
+
* @returns The service name
|
|
38
|
+
* @protected
|
|
39
|
+
*/
|
|
40
|
+
getServiceName() {
|
|
41
|
+
return "sections";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Registers a new section with the dev panel.
|
|
45
|
+
* If a section with the same name already exists, it preserves the existing collapse state.
|
|
46
|
+
* New sections start in expanded state (isCollapsed: false).
|
|
47
|
+
*
|
|
48
|
+
* @param name - Unique name for the section
|
|
49
|
+
* @param controls - Array of control configurations for the section
|
|
50
|
+
*/
|
|
51
|
+
registerSection = (e, t) => {
|
|
52
|
+
this.setState((s) => {
|
|
53
|
+
const i = s[e]?.isCollapsed ?? !1;
|
|
54
|
+
return {
|
|
55
|
+
...s,
|
|
56
|
+
[e]: {
|
|
57
|
+
name: e,
|
|
58
|
+
controls: t,
|
|
59
|
+
isCollapsed: i
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Removes a section from the dev panel.
|
|
66
|
+
*
|
|
67
|
+
* @param name - Name of the section to remove
|
|
68
|
+
*/
|
|
69
|
+
unregisterSection = (e) => {
|
|
70
|
+
this.setState((t) => {
|
|
71
|
+
const { [e]: s, ...o } = t;
|
|
72
|
+
return o;
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Toggles the collapsed state of a specific section.
|
|
77
|
+
*
|
|
78
|
+
* @param name - Name of the section to toggle
|
|
79
|
+
*/
|
|
80
|
+
toggleSectionCollapse = (e) => {
|
|
81
|
+
this.setState((t) => {
|
|
82
|
+
const s = t[e];
|
|
83
|
+
return s ? {
|
|
84
|
+
...t,
|
|
85
|
+
[e]: {
|
|
86
|
+
...s,
|
|
87
|
+
isCollapsed: !s.isCollapsed
|
|
88
|
+
}
|
|
89
|
+
} : t;
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Clears all sections.
|
|
94
|
+
*/
|
|
95
|
+
reset = () => {
|
|
96
|
+
this.setState(() => ({}));
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const r = new a();
|
|
100
|
+
function p() {
|
|
101
|
+
return n(r.subscribe, r.getSnapshot);
|
|
102
|
+
}
|
|
103
|
+
function d() {
|
|
104
|
+
return {
|
|
105
|
+
registerSection: r.registerSection,
|
|
106
|
+
unregisterSection: r.unregisterSection,
|
|
107
|
+
toggleSectionCollapse: r.toggleSectionCollapse,
|
|
108
|
+
reset: r.reset
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export {
|
|
112
|
+
d as useDevPanelSectionActions,
|
|
113
|
+
p as useDevPanelSections
|
|
114
|
+
};
|