cogsbox-state 0.5.476 → 0.5.478

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/src/store.ts CHANGED
@@ -136,6 +136,7 @@ export type ShadowMetadata = {
136
136
  stateSource?: 'default' | 'server' | 'localStorage';
137
137
  lastServerSync?: number;
138
138
  isDirty?: boolean;
139
+ isRaw?: boolean;
139
140
  baseServerState?: any;
140
141
  arrayKeys?: string[];
141
142
  fields?: Record<string, any>;
@@ -816,11 +817,24 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
816
817
  newMeta.validation = existingMeta.validation;
817
818
  }
818
819
 
819
- // 3. Preserve component registrations, which only exist on the live target state.
820
+ // 3. Preserve component registrations
820
821
  if (existingMeta.components) {
821
822
  newMeta.components = existingMeta.components;
822
823
  }
823
824
 
825
+ // 4. Preserve clientActivityState (DOM ref registrations)
826
+ if (
827
+ existingMeta.clientActivityState &&
828
+ !sourceMeta.clientActivityState
829
+ ) {
830
+ newMeta.clientActivityState = existingMeta.clientActivityState;
831
+ }
832
+
833
+ // 5. Preserve pluginMetaData
834
+ if (existingMeta.pluginMetaData && !sourceMeta.pluginMetaData) {
835
+ newMeta.pluginMetaData = existingMeta.pluginMetaData;
836
+ }
837
+
824
838
  target._meta = newMeta;
825
839
  }
826
840
  // --- END: CORRECTED METADATA MERGE ---
@@ -906,6 +920,33 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
906
920
  shadowStateStore.get(key) || shadowStateStore.get(`[${key}`);
907
921
  let preservedMetadata: Partial<ShadowMetadata> = {};
908
922
 
923
+ // NEW: Collect runtime metadata (clientActivityState, pluginMetaData) from ALL nodes
924
+ const preservedRuntimeMeta = new Map<
925
+ string,
926
+ {
927
+ clientActivityState?: ClientActivityState;
928
+ pluginMetaData?: Map<string, Record<string, any>>;
929
+ }
930
+ >();
931
+
932
+ if (existingRoot) {
933
+ const collectRuntimeMeta = (node: any, pathSegments: string[]) => {
934
+ if (!node || typeof node !== 'object') return;
935
+ if (node._meta?.clientActivityState || node._meta?.pluginMetaData) {
936
+ preservedRuntimeMeta.set(pathSegments.join('\x00'), {
937
+ clientActivityState: node._meta.clientActivityState,
938
+ pluginMetaData: node._meta.pluginMetaData,
939
+ });
940
+ }
941
+ for (const k in node) {
942
+ if (k !== '_meta') {
943
+ collectRuntimeMeta(node[k], [...pathSegments, k]);
944
+ }
945
+ }
946
+ };
947
+ collectRuntimeMeta(existingRoot, []);
948
+ }
949
+
909
950
  if (existingRoot?._meta) {
910
951
  const {
911
952
  components,
@@ -924,7 +965,6 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
924
965
  shadowStateStore.delete(key);
925
966
  shadowStateStore.delete(`[${key}`);
926
967
 
927
- // Get all available schemas for this state
928
968
  const options = get().getInitialOptions(key);
929
969
  const syncSchemas = get().getInitialOptions('__syncSchemas');
930
970
 
@@ -938,12 +978,31 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
938
978
  },
939
979
  };
940
980
 
941
- // Build with context so type info is stored
942
981
  const newRoot = buildShadowNode(key, initialState, context);
943
982
 
944
983
  if (!newRoot._meta) newRoot._meta = {};
945
984
  Object.assign(newRoot._meta, preservedMetadata);
946
985
 
986
+ // NEW: Restore runtime metadata to matching nodes in rebuilt tree
987
+ const restoreRuntimeMeta = (node: any, pathSegments: string[]) => {
988
+ if (!node || typeof node !== 'object') return;
989
+ const pathKey = pathSegments.join('\x00');
990
+ const saved = preservedRuntimeMeta.get(pathKey);
991
+ if (saved) {
992
+ if (!node._meta) node._meta = {};
993
+ if (saved.clientActivityState)
994
+ node._meta.clientActivityState = saved.clientActivityState;
995
+ if (saved.pluginMetaData)
996
+ node._meta.pluginMetaData = saved.pluginMetaData;
997
+ }
998
+ for (const k in node) {
999
+ if (k !== '_meta') {
1000
+ restoreRuntimeMeta(node[k], [...pathSegments, k]);
1001
+ }
1002
+ }
1003
+ };
1004
+ restoreRuntimeMeta(newRoot, []);
1005
+
947
1006
  const storageKey = Array.isArray(initialState) ? `[${key}` : key;
948
1007
  shadowStateStore.set(storageKey, newRoot);
949
1008
  },
@@ -1354,6 +1413,7 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
1354
1413
  const rootMeta = get().getShadowMetadata(stateKey, []) || {};
1355
1414
  const components = new Map(rootMeta.components);
1356
1415
  components.set(fullComponentId, registration);
1416
+
1357
1417
  get().setShadowMetadata(stateKey, [], { components });
1358
1418
  },
1359
1419
 
@@ -1585,3 +1645,71 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
1585
1645
  }),
1586
1646
  getSyncInfo: (key) => get().syncInfoStore.get(key) || null,
1587
1647
  }));
1648
+
1649
+ export function getAllFieldElements(stateKey: string): HTMLElement[] {
1650
+ const elements: HTMLElement[] = [];
1651
+
1652
+ const rootNode =
1653
+ shadowStateStore.get(stateKey) || shadowStateStore.get(`[${stateKey}`);
1654
+
1655
+ if (!rootNode) return elements;
1656
+
1657
+ const collectElements = (node: any) => {
1658
+ if (!node || typeof node !== 'object') return;
1659
+
1660
+ // Check this node for elements
1661
+ if (node._meta?.clientActivityState?.elements) {
1662
+ node._meta.clientActivityState.elements.forEach((entry: any) => {
1663
+ if (entry.domRef?.current) {
1664
+ elements.push(entry.domRef.current);
1665
+ }
1666
+ });
1667
+ }
1668
+
1669
+ // Recurse into children
1670
+ for (const key in node) {
1671
+ if (key !== '_meta') {
1672
+ collectElements(node[key]);
1673
+ }
1674
+ }
1675
+ };
1676
+
1677
+ collectElements(rootNode);
1678
+
1679
+ return elements;
1680
+ }
1681
+
1682
+ export function setAllFieldsDisabled(
1683
+ stateKey: string,
1684
+ disabled: boolean
1685
+ ): void {
1686
+ const rootNode =
1687
+ shadowStateStore.get(stateKey) || shadowStateStore.get(`[${stateKey}`);
1688
+ if (!rootNode) return;
1689
+
1690
+ const disableElements = (node: any) => {
1691
+ if (!node || typeof node !== 'object') return;
1692
+
1693
+ if (node._meta?.clientActivityState?.elements) {
1694
+ node._meta.clientActivityState.elements.forEach((entry: any) => {
1695
+ const el = entry.domRef?.current;
1696
+ if (!el) return;
1697
+
1698
+ if ('disabled' in el) {
1699
+ (el as HTMLInputElement).disabled = disabled;
1700
+ } else {
1701
+ el.style.pointerEvents = disabled ? 'none' : '';
1702
+ el.setAttribute('aria-disabled', String(disabled));
1703
+ }
1704
+ });
1705
+ }
1706
+
1707
+ for (const key in node) {
1708
+ if (key !== '_meta') {
1709
+ disableElements(node[key]);
1710
+ }
1711
+ }
1712
+ };
1713
+
1714
+ disableElements(rootNode);
1715
+ }
package/src/utility.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { TransformedStateType, CogsInitialState } from './CogsState';
2
-
3
1
  export const isObject = (item: any): item is Record<string, any> => {
4
2
  return (
5
3
  item && typeof item === 'object' && !Array.isArray(item) && item !== null
@@ -346,35 +344,5 @@ export type DebouncedFunction<F extends (...args: any[]) => any> = F & {
346
344
  };
347
345
 
348
346
  export function transformStateFunc<State extends unknown>(initialState: State) {
349
- const isInitialStateType = (state: any): state is CogsInitialState<State> => {
350
- return Object.values(state).some((value) =>
351
- value?.hasOwnProperty('initialState')
352
- );
353
- };
354
- let initalOptions: GenericObject = {};
355
- const transformInitialState = (
356
- state: CogsInitialState<State>
357
- ): GenericObject | GenericObject[] => {
358
- const transformedState: GenericObject | GenericObject[] = {};
359
- Object.entries(state).forEach(([key, value]) => {
360
- if (value?.initialState) {
361
- initalOptions = { ...(initalOptions ?? {}), [key]: value };
362
-
363
- transformedState[key] = value.initialState;
364
- } else {
365
- transformedState[key] = value;
366
- }
367
- });
368
-
369
- return transformedState;
370
- };
371
-
372
- const transformedInitialState = isInitialStateType(initialState)
373
- ? (transformInitialState(initialState) as State)
374
- : (initialState as State);
375
-
376
- return [transformedInitialState, initalOptions] as [
377
- TransformedStateType<State>,
378
- GenericObject,
379
- ];
347
+ return [initialState as State, {}] as [State, GenericObject];
380
348
  }