|
| 1 | +import { generateElementId } from "@webiny/website-builder-sdk"; |
| 2 | +import type { |
| 3 | + Document, |
| 4 | + DocumentElement, |
| 5 | + DocumentElementBindings |
| 6 | +} from "@webiny/website-builder-sdk"; |
| 7 | +import { ElementFactory } from "@webiny/website-builder-sdk"; |
| 8 | +import type { Editor } from "../Editor.js"; |
| 9 | +import { $getElementById } from "./$getElementById.js"; |
| 10 | +import { $addElementReferenceToParent } from "./$addElementReferenceToParent.js"; |
| 11 | +import { executeOnChange, applyAncestorUpdates } from "./executeOnChange.js"; |
| 12 | + |
| 13 | +function collectDescendants(document: Document, elementId: string): string[] { |
| 14 | + const ids: string[] = []; |
| 15 | + const bindings = document.bindings[elementId]; |
| 16 | + if (!bindings?.inputs) { |
| 17 | + return ids; |
| 18 | + } |
| 19 | + |
| 20 | + for (const binding of Object.values(bindings.inputs)) { |
| 21 | + if (binding.type !== "slot") { |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + const children = binding.list |
| 26 | + ? ((binding.static as string[] | undefined) ?? []) |
| 27 | + : binding.static |
| 28 | + ? [binding.static as string] |
| 29 | + : []; |
| 30 | + |
| 31 | + for (const childId of children) { |
| 32 | + ids.push(childId); |
| 33 | + ids.push(...collectDescendants(document, childId)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return ids; |
| 38 | +} |
| 39 | + |
| 40 | +function remapBindings( |
| 41 | + bindings: DocumentElementBindings, |
| 42 | + idMap: Map<string, string> |
| 43 | +): DocumentElementBindings { |
| 44 | + const cloned = structuredClone(bindings); |
| 45 | + |
| 46 | + if (cloned.inputs) { |
| 47 | + for (const [key, binding] of Object.entries(cloned.inputs)) { |
| 48 | + if (binding.type !== "slot") { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + if (binding.list && Array.isArray(binding.static)) { |
| 53 | + cloned.inputs[key] = { |
| 54 | + ...binding, |
| 55 | + static: binding.static.map((id: string) => idMap.get(id) ?? id) |
| 56 | + }; |
| 57 | + } else if (typeof binding.static === "string") { |
| 58 | + const newId = idMap.get(binding.static); |
| 59 | + if (newId) { |
| 60 | + cloned.inputs[key] = { ...binding, static: newId }; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return cloned; |
| 67 | +} |
| 68 | + |
| 69 | +function getSlotIndex(document: Document, element: DocumentElement): number { |
| 70 | + const parent = element.parent; |
| 71 | + if (!parent) { |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + const parentBindings = document.bindings[parent.id]; |
| 76 | + if (!parentBindings?.inputs) { |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + const slotBinding = parentBindings.inputs[parent.slot]; |
| 81 | + if (!slotBinding) { |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + if (slotBinding.list && Array.isArray(slotBinding.static)) { |
| 86 | + return slotBinding.static.indexOf(element.id); |
| 87 | + } |
| 88 | + |
| 89 | + return -1; |
| 90 | +} |
| 91 | + |
| 92 | +export function $cloneElement(editor: Editor, id: string): string | undefined { |
| 93 | + const document = editor.getDocumentState().read(); |
| 94 | + const element = $getElementById(document, id); |
| 95 | + |
| 96 | + if (!element || !element.parent) { |
| 97 | + return undefined; |
| 98 | + } |
| 99 | + |
| 100 | + const allIds = [id, ...collectDescendants(document, id)]; |
| 101 | + const idMap = new Map<string, string>(); |
| 102 | + for (const oldId of allIds) { |
| 103 | + idMap.set(oldId, generateElementId()); |
| 104 | + } |
| 105 | + |
| 106 | + const originalIndex = getSlotIndex(document, element); |
| 107 | + |
| 108 | + editor.updateDocument(doc => { |
| 109 | + for (const oldId of allIds) { |
| 110 | + const original = doc.elements[oldId]; |
| 111 | + const newId = idMap.get(oldId)!; |
| 112 | + |
| 113 | + const clonedElement: DocumentElement = { |
| 114 | + ...structuredClone(original), |
| 115 | + id: newId |
| 116 | + }; |
| 117 | + |
| 118 | + if (clonedElement.parent) { |
| 119 | + const newParentId = idMap.get(clonedElement.parent.id); |
| 120 | + if (newParentId) { |
| 121 | + clonedElement.parent = { |
| 122 | + ...clonedElement.parent, |
| 123 | + id: newParentId |
| 124 | + }; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + doc.elements[newId] = clonedElement; |
| 129 | + |
| 130 | + const originalBindings = doc.bindings[oldId]; |
| 131 | + if (originalBindings) { |
| 132 | + doc.bindings[newId] = remapBindings(originalBindings, idMap); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + const rootNewId = idMap.get(id)!; |
| 137 | + const insertIndex = originalIndex >= 0 ? originalIndex + 1 : originalIndex; |
| 138 | + |
| 139 | + $addElementReferenceToParent(doc, { |
| 140 | + elementId: rootNewId, |
| 141 | + parentId: element.parent!.id, |
| 142 | + slot: element.parent!.slot, |
| 143 | + index: insertIndex |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + const componentsManifest = editor.getEditorState().read().components; |
| 148 | + const editorState = editor.getEditorState().read(); |
| 149 | + const breakpointNames = editorState.viewport.breakpoints.map(bp => bp.name); |
| 150 | + const baseBreakpoint = breakpointNames[0]; |
| 151 | + const elementFactory = new ElementFactory(componentsManifest); |
| 152 | + |
| 153 | + for (const oldId of allIds) { |
| 154 | + const newId = idMap.get(oldId)!; |
| 155 | + const ancestorUpdates = executeOnChange({ |
| 156 | + editor, |
| 157 | + elementId: newId, |
| 158 | + action: "create", |
| 159 | + breakpointNames, |
| 160 | + baseBreakpoint, |
| 161 | + elementFactory |
| 162 | + }); |
| 163 | + |
| 164 | + if (ancestorUpdates.length > 0) { |
| 165 | + editor.updateDocument(doc => { |
| 166 | + applyAncestorUpdates( |
| 167 | + doc, |
| 168 | + ancestorUpdates, |
| 169 | + breakpointNames, |
| 170 | + baseBreakpoint, |
| 171 | + elementFactory |
| 172 | + ); |
| 173 | + }); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return idMap.get(id); |
| 178 | +} |
0 commit comments