MCPcopy
hub / github.com/puckeditor/puck / walkAppState

Function walkAppState

packages/core/lib/data/walk-app-state.ts:29–222  ·  view source on GitHub ↗
(
  state: PrivateAppState<UserData>,
  config: Config,
  mapContent: (
    content: Content,
    zoneCompound: string,
    zoneType: ZoneType
  ) => Content | void = (content) => content,
  mapNodeOrSkip: (
    item: ComponentData,
    path: string[],
    index: number
  ) => ComponentData | null = (item) => item
)

Source from the content-addressed store, hash-verified

27 * @returns The updated state
28 */
29export function walkAppState<UserData extends Data = Data>(
30 state: PrivateAppState<UserData>,
31 config: Config,
32 mapContent: (
33 content: Content,
34 zoneCompound: string,
35 zoneType: ZoneType
36 ) => Content | void = (content) => content,
37 mapNodeOrSkip: (
38 item: ComponentData,
39 path: string[],
40 index: number
41 ) => ComponentData | null = (item) => item
42): PrivateAppState<UserData> {
43 let newZones: Record<string, Content> = {};
44 const newZoneIndex: ZoneIndex = {};
45 const newNodeIndex: NodeIndex = {};
46
47 const processContent = (
48 path: string[],
49 zoneCompound: string,
50 content: Content,
51 zoneType: ZoneType,
52 newId?: string
53 ): [string, Content] => {
54 const [parentId] = zoneCompound.split(":");
55 const mappedContent =
56 (mapContent(content, zoneCompound, zoneType) ?? content) || [];
57
58 const [_, zone] = zoneCompound.split(":");
59 const newZoneCompound = `${newId || parentId}:${zone}`;
60
61 const newContent = mappedContent.map((zoneChild, index) =>
62 processItem(zoneChild, [...path, newZoneCompound], index)
63 );
64
65 newZoneIndex[newZoneCompound] = {
66 contentIds: newContent.map((item) => item.props.id),
67 type: zoneType,
68 };
69
70 return [newZoneCompound, newContent];
71 };
72
73 const processRelatedZones = (
74 item: ComponentData,
75 newId: string,
76 initialPath: string[]
77 ) => {
78 forRelatedZones(
79 item,
80 state.data,
81 (relatedPath, relatedZoneCompound, relatedContent) => {
82 const [zoneCompound, newContent] = processContent(
83 relatedPath,
84 relatedZoneCompound,
85 relatedContent,
86 "dropzone",

Callers 15

PuckProviderFunction · 0.90
setDataActionFunction · 0.90
duplicateActionFunction · 0.90
removeActionFunction · 0.90
setActionFunction · 0.90
replaceRootActionFunction · 0.90
insertActionFunction · 0.90
replaceActionFunction · 0.90
moveActionFunction · 0.90
remove.spec.tsFile · 0.90
duplicate.spec.tsFile · 0.90
migrate.tsFile · 0.90

Calls 3

toComponentFunction · 0.90
processItemFunction · 0.85
processContentFunction · 0.70

Tested by 6

resetStoresFunction · 0.72
resetStoresFunction · 0.72
resetStoresFunction · 0.72
resetStoresFunction · 0.72
resetStoresFunction · 0.72
selectFirstFunction · 0.72