forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilderBlockCopyPaste.ts
More file actions
354 lines (320 loc) · 11.2 KB
/
builderBlockCopyPaste.ts
File metadata and controls
354 lines (320 loc) · 11.2 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
import type Block from "@/block";
import useCanvasStore from "@/stores/canvasStore";
import useComponentStore from "@/stores/componentStore";
import usePageStore from "@/stores/pageStore";
import { BuilderComponent } from "@/types/Builder/BuilderComponent";
import { BuilderPage } from "@/types/Builder/BuilderPage";
import {
copyToClipboard,
detachBlockFromComponent,
getBlockCopy,
getBlockInstance,
getCopyWithoutParent,
isJSONString,
showDialog,
} from "@/utils/helpers";
import { createListResource } from "frappe-ui";
import { nextTick } from "vue";
import { toast } from "vue-sonner";
import { webPages } from "../data/webPage";
import { BuilderClientScript } from "../types/Builder/BuilderClientScript";
type BuilderPageClientScriptRef = { builder_script: string; idx?: number };
type BuilderPageSettings = Pick<
BuilderPage,
| "page_name"
| "route"
| "dynamic_route"
| "is_template"
| "template_name"
| "page_data_script"
| "head_html"
| "body_html"
| "page_title"
| "meta_description"
| "meta_image"
| "canonical_url"
| "authenticated_access"
| "disable_indexing"
| "favicon"
| "blocks"
> & {
client_scripts?: BuilderPageClientScriptRef[];
};
interface BuilderClipboardData {
blocks: (Block | BlockOptions)[];
components: BuilderComponent[];
sourceURL?: string;
pageDoc?: BuilderPageSettings;
pageScripts?: BuilderClientScript[];
}
type BuilderClientScriptDocument = Partial<BuilderClientScript>;
export function copyBuilderBlocks(
e: ClipboardEvent,
currentSiteURL: string,
copyEntirePage: boolean = false,
) {
const canvasStore = useCanvasStore();
const componentStore = useComponentStore();
const pageStore = usePageStore();
if (!canvasStore.activeCanvas?.selectedBlocks.length && !copyEntirePage) return;
e.preventDefault();
const blocks = (
copyEntirePage
? [canvasStore.activeCanvas?.getRootBlock()]
: canvasStore.activeCanvas?.selectedBlocks ?? []
) as Block[];
const componentDocuments: BuilderComponent[] = [];
const blocksToCopy = blocks.map((block) => {
// Collect all used components
const components = block.getUsedComponentNames();
for (const componentName of components) {
const component = componentStore.getComponent(componentName);
if (component) {
componentDocuments.push(component);
}
}
// Handle component children and create copy
let blockCopy = null;
if (!Boolean(block.extendedFromComponent) && block.isChildOfComponent) {
blockCopy = detachBlockFromComponent(block, null);
} else {
blockCopy = getCopyWithoutParent(block);
}
return blockCopy;
});
const dataToCopy: BuilderClipboardData = {
blocks: blocksToCopy,
components: componentDocuments,
sourceURL: currentSiteURL,
};
if (copyEntirePage) {
const currentPage = pageStore.activePage as BuilderPage;
dataToCopy.pageDoc = {
page_name: currentPage.page_name,
route: currentPage.route,
dynamic_route: currentPage.dynamic_route,
page_data_script: currentPage.page_data_script,
head_html: currentPage.head_html,
body_html: currentPage.body_html,
page_title: currentPage.page_title,
meta_description: currentPage.meta_description,
meta_image: currentPage.meta_image,
authenticated_access: currentPage.authenticated_access,
disable_indexing: currentPage.disable_indexing,
favicon: currentPage.favicon,
client_scripts:
currentPage.client_scripts?.map((script) => {
return {
builder_script: script.builder_script,
idx: script.idx,
};
}) || [],
};
dataToCopy.pageScripts = pageStore.activePageScripts;
}
copyToClipboard(dataToCopy, e, "builder-copied-blocks");
copyEntirePage && toast.success("Page Copied");
}
export async function pasteBuilderBlocks(e: ClipboardEvent, currentSiteURL: string): Promise<void> {
const data = e.clipboardData?.getData("builder-copied-blocks") as string;
if (!data || !isJSONString(data)) return;
const clipboardData = JSON.parse(data) as BuilderClipboardData;
const crossSitePaste = Boolean(clipboardData.sourceURL && clipboardData.sourceURL !== currentSiteURL);
if (clipboardData.pageDoc) {
await handlePagePaste(clipboardData, crossSitePaste, currentSiteURL);
} else {
if (clipboardData.components.length) {
toast.loading("Pasting...", {
id: "paste-blocks",
});
await handleComponents(clipboardData, crossSitePaste);
}
await insertBlocks(clipboardData.blocks);
clipboardData.components.length &&
toast.success("Done", {
id: "paste-blocks",
});
}
}
async function handlePagePaste(
clipboardData: BuilderClipboardData,
crossSitePaste: boolean,
currentURL: string | undefined = undefined,
): Promise<void> {
const canvasStore = useCanvasStore();
const pageStore = usePageStore();
await showDialog({
title: "Pasting a page!",
message:
"You are about to paste a page with settings and scripts. Do you want to update the current page or create a new one?",
actions: [
{
label: "Create New Page",
variant: "solid",
async onClick() {
toast.loading("Pasting...", { id: "paste-page" });
await handleComponents(clipboardData, crossSitePaste);
await handlePageScripts(clipboardData, currentURL || "");
if (clipboardData.pageDoc) {
clipboardData.pageDoc.blocks = clipboardData.blocks.map((block) => getCopyWithoutParent(block));
}
const newPage = await webPages.insert.submit(clipboardData.pageDoc);
window.location.href = `/builder/page/${encodeURIComponent(newPage.name)}`;
await pageStore.setPage(newPage.name);
toast.success("Done", { id: "paste-page" });
},
},
{
label: "Update Current Page",
variant: "subtle",
async onClick() {
toast.loading("Pasting...", { id: "paste-page" });
await handleComponents(clipboardData, crossSitePaste);
await handlePageScripts(clipboardData, currentURL || "");
const currentPage = pageStore.activePage as BuilderPage;
await webPages.setValue.submit({
...clipboardData.pageDoc,
name: currentPage.name,
});
await pageStore.setPage(currentPage.name);
nextTick(() => {
canvasStore.pushBlocks(clipboardData.blocks);
pageStore.savePage();
});
toast.success("Done", { id: "paste-page" });
},
},
],
size: "md",
});
}
async function handleComponents(clipboardData: BuilderClipboardData, crossSitePaste: boolean) {
const componentStore = useComponentStore();
const componentIdMap = new Map<string, string>();
let { blocks } = clipboardData;
for (const component of clipboardData.components) {
delete component.for_web_page;
if (crossSitePaste) {
const originalId = component.name;
const newId = generateHash(originalId, clipboardData.sourceURL || "");
componentIdMap.set(originalId, newId);
component.name = newId;
component.component_id = newId;
await componentStore.createComponent(component, true);
} else {
await componentStore.createComponent(component, false);
}
}
if (crossSitePaste) {
blocks.map((block) => {
updateBlockComponentReferences(block, componentIdMap);
const blockCopy = getBlockInstance(block);
updateURLsInBlock(blockCopy, clipboardData.sourceURL as string);
return blockCopy;
});
}
}
async function insertBlocks(blocks: (Block | BlockOptions)[]) {
const canvasStore = useCanvasStore();
if (canvasStore.activeCanvas?.selectedBlocks.length && blocks[0].blockId !== "root") {
let parentBlock = canvasStore.activeCanvas.selectedBlocks[0];
while (parentBlock && !parentBlock.canHaveChildren()) {
parentBlock = parentBlock.getParentBlock() as Block;
}
blocks.forEach((block) => parentBlock.addChild(getBlockCopy(block), null, true));
} else {
canvasStore.pushBlocks(blocks);
}
}
async function handlePageScripts(clipboardData: BuilderClipboardData, currentSiteURL: string): Promise<void> {
if (!clipboardData.pageDoc || !clipboardData.sourceURL || clipboardData.sourceURL === currentSiteURL) {
return;
}
const pageScriptIdMap = new Map<string, string>();
for (const script of clipboardData.pageScripts || []) {
const newScriptId = generateHash(script.name, clipboardData.sourceURL, true);
pageScriptIdMap.set(script.name, newScriptId);
const scriptDoc: BuilderClientScriptDocument = {
...script,
name: newScriptId,
};
const clientScriptResource = createListResource({
doctype: "Builder Client Script",
});
try {
await clientScriptResource.insert.submit(scriptDoc);
} catch (error: { response?: { status: number } } | any) {
if (error?.response?.status === 409) {
// If script already exists, update it
await clientScriptResource.setValue.submit(scriptDoc);
} else {
console.error("Error inserting client script:", error);
}
// pass
}
const clientScript = clipboardData.pageDoc.client_scripts?.find((s) => s.builder_script === script.name);
if (clientScript) {
clientScript.builder_script = newScriptId;
}
}
}
function updateBlockComponentReferences(block: BlockOptions, componentIdMap: Map<string, string>): void {
if (!componentIdMap.size) return;
if (block.extendedFromComponent && componentIdMap.has(block.extendedFromComponent)) {
block.extendedFromComponent = componentIdMap.get(block.extendedFromComponent);
}
if (block.isChildOfComponent && componentIdMap.has(block.isChildOfComponent)) {
block.isChildOfComponent = componentIdMap.get(block.isChildOfComponent);
}
block.children?.forEach((child) => updateBlockComponentReferences(child, componentIdMap));
}
function updateURLsInBlock(block: Block, currentSiteURL: string): void {
// Update image and video sources to absolute URLs
if (block.isImage() || block.isVideo()) {
const src = block.getAttribute("src");
if (src && typeof src === "string" && src.startsWith("/")) {
block.setAttribute("src", `${currentSiteURL}${src}`);
}
}
// Update background image URLs
if (block) {
const bgSrc = block.getStyle("backgroundImage");
if (bgSrc && typeof bgSrc === "string" && bgSrc.startsWith("url(")) {
const urlMatch = bgSrc.match(/url\(["']?([^"']+)["']?\)/);
if (urlMatch && urlMatch[1] && urlMatch[1].startsWith("/")) {
block.setStyle("backgroundImage", `url(${currentSiteURL}${urlMatch[1]})`);
}
}
}
// Update href attributes for links
// if (block.isLink()) {
// const href = block.getAttribute("href");
// if (href && typeof href === "string" && !href.startsWith("http") && !href.startsWith("#")) {
// block.setAttribute("href", `${currentSiteURL}${href}`);
// }
// }
// Recursively process children
block.children.forEach((child) => updateURLsInBlock(child, currentSiteURL));
}
function generateHash(initialId: string, siteURL: string, appendHash = false): string {
if (!appendHash) {
// Original behavior: Generate a deterministic hash with the same length as initialId
const str = `${initialId}_${siteURL}`;
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) + hash + str.charCodeAt(i); // djb2 hash
}
const hashStr = Math.abs(hash).toString(36);
const targetLength = initialId.length;
return hashStr.repeat(Math.ceil(targetLength / hashStr.length)).slice(0, targetLength);
} else {
// New behavior: Retain initialId and append a short hash
const str = `${initialId}_${siteURL}`;
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) + hash + str.charCodeAt(i);
}
const shortHash = Math.abs(hash).toString(36).slice(0, 8);
return `${initialId}_${shortHash}`;
}
}