forked from olliethedev/ui-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer-utils.ts
More file actions
404 lines (348 loc) · 14.4 KB
/
layer-utils.ts
File metadata and controls
404 lines (348 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { LayerStore } from "@/lib/ui-builder/store/layer-store";
import { ComponentLayer, ComponentRegistry } from '@/components/ui/ui-builder/types';
import { getDefaultProps } from '@/lib/ui-builder/store/schema-utils';
/**
* Recursively visits each layer in the layer tree and applies the provided visitor function to each layer.
* The visitor function can modify the layer and its children as needed.
*
* @param layer - The current layer to visit.
* @param visitor - A function that takes a layer and returns a modified layer.
* @returns The modified layer after applying the visitor function.
*/
export const visitLayer = (layer: ComponentLayer, parentLayer: ComponentLayer | null, visitor: (layer: ComponentLayer, parentLayer: ComponentLayer | null) => ComponentLayer): ComponentLayer => {
// Apply the visitor to the current layer
const updatedLayer = visitor(layer, parentLayer);
// Recursively traverse and update children if they exist
if (hasLayerChildren(updatedLayer)) {
const updatedChildren = updatedLayer.children.map((child) =>
visitLayer(child, updatedLayer, visitor)
);
return { ...updatedLayer, children: updatedChildren };
}
return updatedLayer;
};
export const countLayers = (layers: ComponentLayer[] | string): number => {
if (typeof layers === 'string') {
return 0;
}
return layers.reduce((count, layer) => {
if (hasLayerChildren(layer)) {
return count + 1 + countLayers(layer.children);
}
return count + 1;
}, 0);
};
export const addLayer = (layers: ComponentLayer[], newLayer: ComponentLayer, parentId?: string, parentPosition?: number): ComponentLayer[] => {
const updatedPages = layers.map((page) =>
visitLayer(page, null, (layer) => {
if (layer.id === parentId) {
// Handle both layers with existing children and those with undefined/null children
let updatedChildren: ComponentLayer[] = [];
if (hasLayerChildren(layer)) {
updatedChildren = [...layer.children];
} else if (layer.children === undefined || layer.children === null || (Array.isArray(layer.children) && layer.children.length === 0)) {
// Initialize children array for layers with undefined/null children or empty arrays
updatedChildren = [];
} else {
// For layers with string children or other non-array types, we can't add children
return layer;
}
if (parentPosition !== undefined) {
if (parentPosition < 0) {
// If parentPosition is negative, insert at the beginning
updatedChildren = [newLayer, ...updatedChildren];
} else if (parentPosition >= updatedChildren.length) {
// If parentPosition is greater than or equal to the length, append to the end
updatedChildren = [...updatedChildren, newLayer];
} else {
// Insert at the specified position
updatedChildren = [
...updatedChildren.slice(0, parentPosition),
newLayer,
...updatedChildren.slice(parentPosition)
];
}
} else {
// If parentPosition is undefined, append to the end
updatedChildren = [...updatedChildren, newLayer];
}
return { ...layer, children: updatedChildren };
}
return layer;
})
);
return updatedPages;
}
export const findAllParentLayersRecursive = (layers: ComponentLayer[], layerId: string): ComponentLayer[] => {
const parents: ComponentLayer[] = [];
const findParents = (layers: ComponentLayer[], targetId: string): boolean => {
for (const layer of layers) {
if (hasLayerChildren(layer)) {
if (layer.children.some(child => child.id === targetId)) {
parents.push(layer);
// Continue searching upwards
findParents(layers, layer.id);
return true;
}
if (findParents(layer.children, targetId)) {
parents.push(layer);
return true;
}
}
}
return false;
};
findParents(layers, layerId);
return parents;
};
export const findLayerRecursive = (layers: ComponentLayer[], layerId: string): ComponentLayer | undefined => {
for (const layer of layers) {
if (layer.id === layerId) {
return layer;
}
if (hasLayerChildren(layer)) {
const foundInChildren = findLayerRecursive(layer.children, layerId);
if (foundInChildren) {
return foundInChildren;
}
}
}
return undefined;
};
export const duplicateWithNewIdsAndName = (layer: ComponentLayer, addCopySuffix: boolean = true): ComponentLayer => {
const newLayer: ComponentLayer = { ...layer, id: createId() };
if (layer.name) {
newLayer.name = `${ layer.name }${ addCopySuffix ? ' (Copy)' : ''}`;
}
if (hasLayerChildren(newLayer) && hasLayerChildren(layer)) {
newLayer.children = layer.children.map(child => duplicateWithNewIdsAndName(child, false));
}
return newLayer;
};
export function createId(): string {
const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const ID_LENGTH = 7;
let result = '';
const alphabetLength = ALPHABET.length;
for (let i = 0; i < ID_LENGTH; i++) {
const randomIndex = Math.floor(Math.random() * alphabetLength);
result += ALPHABET.charAt(randomIndex);
}
return result;
}
export const hasLayerChildren = (layer: ComponentLayer): layer is ComponentLayer & { children: ComponentLayer[] } => {
return Array.isArray(layer.children) && typeof layer.children !== 'string';
};
export function migrateV1ToV2(persistedState: unknown): LayerStore {
type TextLayer = {
id: string;
name?: string;
type: '_text_';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: Record<string, any>;
text: string;
textType: 'text' | 'markdown';
};
console.log("Migrating store", { persistedState, version: 1 });
const migratedState = persistedState as LayerStore;
// Utilize visitLayer to transform all layers recursively
const transformLayer = (layer: ComponentLayer): ComponentLayer => {
if (layer.type === "_text_") {
const textLayer = layer as unknown as TextLayer;
const transformedTextLayer: ComponentLayer = {
type: textLayer.textType === "markdown" ? "Markdown" : "span",
children: textLayer.text,
id: textLayer.id,
name: textLayer.name,
props: textLayer.props,
};
console.log("Transformed text layer", transformedTextLayer);
return transformedTextLayer;
}
return layer;
};
const migratedPages = migratedState.pages.map((page: ComponentLayer) => {
return visitLayer(page, null, transformLayer) as ComponentLayer;
}) satisfies ComponentLayer[];
return {
...migratedState,
pages: migratedPages,
} satisfies LayerStore;
}
export function migrateV2ToV3(persistedState: unknown): LayerStore {
type PageLayer = {
id: string;
name?: string;
type: '_page_';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: Record<string, any>;
children: ComponentLayer[];
}
console.log("Migrating store", { persistedState, version: 1 });
const migratedState = persistedState as LayerStore;
const keysToMap = {
borderRadius: "data-border-radius",
colorTheme: "data-color-theme",
mode: "data-mode",
};
const migratedPages = migratedState.pages.map((page: ComponentLayer) => {
if (page.type === "_page_") {
const pageLayer = page as unknown as PageLayer;
const transformedPageLayer: ComponentLayer = {
type: "div",
children: pageLayer.children,
id: pageLayer.id,
name: pageLayer.name,
//map keys called borderRadius, colorTheme, mode to data-border-radius, data-color-theme, data-mode
props: Object.fromEntries(Object.entries(pageLayer.props).map(([key, value]) => {
if (keysToMap[key as keyof typeof keysToMap]) {
return [keysToMap[key as keyof typeof keysToMap], value];
}
return [key, value];
})),
};
console.log("Transformed page layer", transformedPageLayer);
return transformedPageLayer;
}
return page;
}) satisfies ComponentLayer[];
return {
...migratedState,
pages: migratedPages,
} satisfies LayerStore;
}
/**
* Creates a new component layer with default props and children initialized from the component registry.
* This utility function consolidates the layer initialization logic used across the application.
*
* @param layerType - The type of component to create
* @param componentRegistry - The component registry containing component definitions
* @param options - Optional configuration for the layer
* @returns A new ComponentLayer with initialized props and children
*/
export const createComponentLayer = (
layerType: string,
componentRegistry: ComponentRegistry,
options: {
id?: string;
name?: string;
applyVariableBindings?: boolean;
variables?: Array<{ id: string; defaultValue: any }>;
} = {}
): ComponentLayer => {
const { id, name, applyVariableBindings = false, variables = [] } = options;
const componentDef = componentRegistry[layerType as keyof typeof componentRegistry];
if (!componentDef) {
throw new Error(`Component definition not found for type: ${layerType}`);
}
const schema = componentDef.schema;
// Safely check if schema has shape property (ZodObject)
const defaultProps = 'shape' in schema && schema.shape ? getDefaultProps(schema as any) : {};
const defaultChildrenRaw = componentDef.defaultChildren;
const defaultChildren = typeof defaultChildrenRaw === "string"
? defaultChildrenRaw
: (defaultChildrenRaw?.map(child => duplicateWithNewIdsAndName(child, false)) || []);
const initialProps = Object.entries(defaultProps).reduce((acc, [key, propDef]) => {
if (key !== "children") {
acc[key] = propDef;
}
return acc;
}, {} as Record<string, any>);
const newLayer: ComponentLayer = {
id: id || createId(),
type: layerType,
name: name || layerType,
props: initialProps,
children: defaultChildren,
};
// Apply default variable bindings if requested
if (applyVariableBindings) {
const defaultVariableBindings = componentDef.defaultVariableBindings || [];
for (const binding of defaultVariableBindings) {
const variable = variables.find(v => v.id === binding.variableId);
if (variable) {
// Set the variable reference in the props
newLayer.props[binding.propName] = { __variableRef: binding.variableId };
}
}
}
return newLayer;
};
/**
* Moves a layer from one position to another in the layer tree.
* This function supports moving layers between different parents and reordering within the same parent.
*
* @param layers - The array of root layers (pages)
* @param sourceLayerId - The ID of the layer to move
* @param targetParentId - The ID of the target parent layer
* @param targetPosition - The position in the target parent's children array (0-based index)
* @returns The updated layers array with the layer moved to its new position
*/
export const moveLayer = (
layers: ComponentLayer[],
sourceLayerId: string,
targetParentId: string,
targetPosition: number
): ComponentLayer[] => {
let layerToMove: ComponentLayer | null = null;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let sourceParentId: string | null = null;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let sourcePosition: number = -1;
// Find the layer to move and its current parent
const findLayerAndParent = (layers: ComponentLayer[], parentId: string | null = null): boolean => {
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
if (layer.id === sourceLayerId) {
layerToMove = layer;
sourceParentId = parentId;
sourcePosition = i;
return true;
}
if (hasLayerChildren(layer)) {
if (findLayerAndParent(layer.children, layer.id)) {
return true;
}
}
}
return false;
};
// Find the layer in the tree
findLayerAndParent(layers);
if (!layerToMove) {
console.warn(`Source layer with ID ${sourceLayerId} not found`);
return layers;
}
// Remove the layer from its current position
const layersWithoutSource = layers.map(page =>
visitLayer(page, null, (layer) => {
if (hasLayerChildren(layer)) {
const updatedChildren = layer.children.filter(child => child.id !== sourceLayerId);
return { ...layer, children: updatedChildren };
}
return layer;
})
);
// Add the layer to its new position
const updatedLayers = addLayer(layersWithoutSource, layerToMove, targetParentId, targetPosition);
return updatedLayers;
};
/**
* Checks if a layer can accept children (has a children property that is an array)
*
* @param layer - The layer to check
* @param componentRegistry - The component registry to check schema
* @returns true if the layer can accept children
*/
export const canLayerAcceptChildren = (
layer: ComponentLayer,
componentRegistry: ComponentRegistry
): boolean => {
const componentDef = componentRegistry[layer.type as keyof typeof componentRegistry];
if (!componentDef) return false;
// Safely check if schema has shape property (ZodObject) and children field
const hasChildrenField = 'shape' in componentDef.schema &&
componentDef.schema.shape &&
componentDef.schema.shape.children !== undefined;
return hasChildrenField && hasLayerChildren(layer);
};