webcake-storefront-mcp 2.1.0 → 2.3.0
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/dist/builder/clone.js +638 -0
- package/dist/builder/editor-keys.generated.json +10339 -0
- package/dist/builder/element-schema.js +124 -5
- package/dist/builder/renderer-keys.generated.json +8371 -0
- package/dist/changelog.json +14 -14
- package/dist/tools/builder-extras.js +5 -2
- package/dist/tools/builder.js +14 -4
- package/dist/tools/clone.js +123 -0
- package/dist/tools/families.js +3 -0
- package/dist/tools/registry.js +2 -0
- package/dist/tools/result-cache.js +8 -0
- package/package.json +6 -3
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
// Clone a rendered web page into native BuilderX sections.
|
|
2
|
+
//
|
|
3
|
+
// 1. captureInPage() runs INSIDE the source page (Playwright page.evaluate, or the agent's own
|
|
4
|
+
// browser via the script from get_clone_script): computed styles + real boxes, reduced to a
|
|
5
|
+
// small tree — section → group(row|col) / box → text | image | button | form | video | …
|
|
6
|
+
// Layout is read from geometry (who sits beside whom), not from flex/grid/float CSS, so it
|
|
7
|
+
// works the same for any framework.
|
|
8
|
+
// 2. snapshotToSource() maps that tree onto the builder's own layout helpers (buildSection,
|
|
9
|
+
// rowChildren/stackChildren via buildFromSpec): a row → `layout:"row"` container with
|
|
10
|
+
// column widths from the measured boxes (collapses on tablet/mobile), a column → stack.
|
|
11
|
+
// Nothing becomes custom code; what can't be mapped is counted in `notes`.
|
|
12
|
+
import { buildSection } from "./page.js";
|
|
13
|
+
// ── 1. In-page capture (self-contained: it is serialized with Function#toString) ──────────
|
|
14
|
+
export async function captureInPage(opts = {}) {
|
|
15
|
+
const arr = (x) => Array.from(x);
|
|
16
|
+
const MAX = opts.maxNodes || 2500;
|
|
17
|
+
const W = window.innerWidth;
|
|
18
|
+
let count = 0;
|
|
19
|
+
const skipped = {};
|
|
20
|
+
const skip = (r) => {
|
|
21
|
+
skipped[r] = (skipped[r] || 0) + 1;
|
|
22
|
+
};
|
|
23
|
+
// Lazy content: walk the page once so images/sections that load on scroll exist.
|
|
24
|
+
if (opts.scroll !== false) {
|
|
25
|
+
const step = Math.max(400, window.innerHeight * 0.8);
|
|
26
|
+
for (let y = 0; y < document.body.scrollHeight && y < 40000; y += step) {
|
|
27
|
+
window.scrollTo(0, y);
|
|
28
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
29
|
+
}
|
|
30
|
+
window.scrollTo(0, 0);
|
|
31
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
32
|
+
}
|
|
33
|
+
document.querySelectorAll("details").forEach((d) => (d.open = true));
|
|
34
|
+
const cs = (el) => getComputedStyle(el);
|
|
35
|
+
const box = (el) => {
|
|
36
|
+
const r = el.getBoundingClientRect();
|
|
37
|
+
return { x: Math.round(r.left + window.scrollX), y: Math.round(r.top + window.scrollY), w: Math.round(r.width), h: Math.round(r.height) };
|
|
38
|
+
};
|
|
39
|
+
const visible = (el) => {
|
|
40
|
+
const s = cs(el);
|
|
41
|
+
if (s.display === "none" || s.visibility === "hidden" || Number(s.opacity) === 0)
|
|
42
|
+
return false;
|
|
43
|
+
const r = el.getBoundingClientRect();
|
|
44
|
+
return r.width > 1 && r.height > 1;
|
|
45
|
+
};
|
|
46
|
+
const clear = (c) => !c || c === "transparent" || /rgba\([^)]*,\s*0\)$/.test(c);
|
|
47
|
+
const abs = (u) => {
|
|
48
|
+
try {
|
|
49
|
+
return new URL(u, location.href).href;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return u;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const px = (v) => Math.round(parseFloat(v) || 0);
|
|
56
|
+
const bgUrl = (s) => {
|
|
57
|
+
const m = /url\(["']?([^"')]+)["']?\)/.exec(s.backgroundImage || "");
|
|
58
|
+
return m ? abs(m[1]) : undefined;
|
|
59
|
+
};
|
|
60
|
+
const textStyle = (s) => {
|
|
61
|
+
const o = { fontSize: px(s.fontSize), fontWeight: s.fontWeight, color: s.color, textAlign: s.textAlign };
|
|
62
|
+
if (s.lineHeight !== "normal")
|
|
63
|
+
o.lineHeight = +(parseFloat(s.lineHeight) / parseFloat(s.fontSize)).toFixed(2);
|
|
64
|
+
if (s.letterSpacing !== "normal")
|
|
65
|
+
o.letterSpacing = s.letterSpacing;
|
|
66
|
+
if (s.textTransform !== "none")
|
|
67
|
+
o.textTransform = s.textTransform;
|
|
68
|
+
if (s.fontStyle !== "normal")
|
|
69
|
+
o.fontStyle = s.fontStyle;
|
|
70
|
+
o.fontFamily = s.fontFamily.split(",")[0].replace(/["']/g, "").trim();
|
|
71
|
+
return o;
|
|
72
|
+
};
|
|
73
|
+
const boxStyle = (s) => {
|
|
74
|
+
const o = {};
|
|
75
|
+
if (!clear(s.backgroundColor))
|
|
76
|
+
o.bg = s.backgroundColor;
|
|
77
|
+
const u = bgUrl(s);
|
|
78
|
+
if (u)
|
|
79
|
+
o.bgImage = u;
|
|
80
|
+
if (/gradient\(/.test(s.backgroundImage))
|
|
81
|
+
o.gradient = s.backgroundImage.replace(/,?\s*url\([^)]*\)/g, "");
|
|
82
|
+
if (px(s.borderTopWidth) > 0 && s.borderTopStyle !== "none" && !clear(s.borderTopColor))
|
|
83
|
+
o.border = `${px(s.borderTopWidth)}px ${s.borderTopStyle} ${s.borderTopColor}`;
|
|
84
|
+
if (px(s.borderTopLeftRadius) > 0)
|
|
85
|
+
o.radius = px(s.borderTopLeftRadius);
|
|
86
|
+
if (s.boxShadow && s.boxShadow !== "none")
|
|
87
|
+
o.shadow = s.boxShadow;
|
|
88
|
+
return o;
|
|
89
|
+
};
|
|
90
|
+
const painted = (b) => !!(b.bg || b.bgImage || b.gradient || b.border || b.shadow);
|
|
91
|
+
/** Pinned widgets (chat bubble, cookie bar, back-to-top) — not a pinned full-width header. */
|
|
92
|
+
const overlay = (s, r) => s.position === "fixed" && r.h < window.innerHeight * 0.9 && !(r.w >= W * 0.9 && r.y - window.scrollY <= 8);
|
|
93
|
+
const pads = (s) => ({ t: px(s.paddingTop), r: px(s.paddingRight), b: px(s.paddingBottom), l: px(s.paddingLeft) });
|
|
94
|
+
const INLINE = new Set(["A", "SPAN", "STRONG", "B", "EM", "I", "U", "SMALL", "BR", "SUP", "SUB", "MARK", "CODE", "S", "DEL", "LABEL", "ABBR", "TIME", "FONT", "Q", "CITE"]);
|
|
95
|
+
// Text-only subtree laid out inline. A flex/grid row of spans, or a painted inline-block
|
|
96
|
+
// (a pill button inside a line), is layout, not text.
|
|
97
|
+
const textLeaf = (el) => {
|
|
98
|
+
if (/flex|grid/.test(cs(el).display) && el.children.length > 1)
|
|
99
|
+
return false;
|
|
100
|
+
const links = arr(el.children).filter((c) => c.tagName === "A");
|
|
101
|
+
if (links.length >= 2 && links.length === el.children.length && !arr(el.childNodes).some((n) => n.nodeType === 3 && /[^\s|·•/-]/.test(n.textContent || "")))
|
|
102
|
+
return false;
|
|
103
|
+
for (const c of arr(el.children)) {
|
|
104
|
+
const d = cs(c).display;
|
|
105
|
+
if (!INLINE.has(c.tagName) || /block|flex|grid|table/.test(d) && (d !== "inline-block" || painted(boxStyle(cs(c)))) || !textLeaf(c))
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return (el.innerText || "").trim().length > 0;
|
|
109
|
+
};
|
|
110
|
+
const esc = (t) => t.replace(/[<>&]/g, (c) => ({ "<": "<", ">": ">", "&": "&" }[c]));
|
|
111
|
+
const richText = (el) => {
|
|
112
|
+
const walk = (n) => {
|
|
113
|
+
if (n.nodeType === 3)
|
|
114
|
+
return esc(n.textContent || "");
|
|
115
|
+
if (n.nodeType !== 1)
|
|
116
|
+
return "";
|
|
117
|
+
const e = n;
|
|
118
|
+
const inner = arr(e.childNodes).map(walk).join("");
|
|
119
|
+
const t = e.tagName;
|
|
120
|
+
if (t === "BR")
|
|
121
|
+
return "<br>";
|
|
122
|
+
if (t === "B" || t === "STRONG" || (t === "SPAN" && +cs(e).fontWeight >= 600 && +cs(el).fontWeight < 600))
|
|
123
|
+
return `<b>${inner}</b>`;
|
|
124
|
+
if (t === "I" || t === "EM")
|
|
125
|
+
return `<i>${inner}</i>`;
|
|
126
|
+
if (t === "U")
|
|
127
|
+
return `<u>${inner}</u>`;
|
|
128
|
+
if (t === "A" && e.getAttribute("href"))
|
|
129
|
+
return `<a href="${abs(e.getAttribute("href")).replace(/"/g, """)}">${inner}</a>`;
|
|
130
|
+
return inner;
|
|
131
|
+
};
|
|
132
|
+
return arr(el.childNodes).map(walk).join("").replace(/\s+/g, " ").trim();
|
|
133
|
+
};
|
|
134
|
+
const headingOf = (el) => {
|
|
135
|
+
if (/^H[1-6]$/.test(el.tagName))
|
|
136
|
+
return el.tagName.toLowerCase();
|
|
137
|
+
const h = el.closest("h1,h2,h3,h4,h5,h6");
|
|
138
|
+
return h ? h.tagName.toLowerCase() : "p";
|
|
139
|
+
};
|
|
140
|
+
const isButton = (el, s) => {
|
|
141
|
+
const t = (el.innerText || el.value || "").trim();
|
|
142
|
+
if (!t || t.length > 60 || t.includes("\n"))
|
|
143
|
+
return false;
|
|
144
|
+
if (el.tagName === "BUTTON" || el.getAttribute("role") === "button")
|
|
145
|
+
return true;
|
|
146
|
+
const b = boxStyle(s);
|
|
147
|
+
return el.tagName === "A" && (!!b.bg || !!b.gradient || !!b.border) && px(s.paddingLeft) + px(s.paddingRight) >= 12;
|
|
148
|
+
};
|
|
149
|
+
const form = (el, r, s) => {
|
|
150
|
+
const fields = [];
|
|
151
|
+
let submit = "";
|
|
152
|
+
let submitStyle;
|
|
153
|
+
for (const f of arr(el.querySelectorAll("input,select,textarea,button"))) {
|
|
154
|
+
if (!visible(f))
|
|
155
|
+
continue;
|
|
156
|
+
const type = String(f.type || "").toLowerCase();
|
|
157
|
+
if (f.tagName === "BUTTON" || type === "submit") {
|
|
158
|
+
if (!submit) {
|
|
159
|
+
submit = (f.innerText || f.value || "").trim();
|
|
160
|
+
const fs = cs(f);
|
|
161
|
+
submitStyle = { ...boxStyle(fs), ...textStyle(fs), h: box(f).h };
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (type === "hidden")
|
|
166
|
+
continue;
|
|
167
|
+
if (type === "checkbox" || type === "radio") {
|
|
168
|
+
skip(`form ${type}`);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const lab = (f.id && document.querySelector(`label[for="${CSS.escape(f.id)}"]`)?.innerText) || f.closest("label")?.innerText || f.getAttribute("aria-label") || "";
|
|
172
|
+
fields.push({
|
|
173
|
+
tag: f.tagName.toLowerCase(), type, name: f.name || "", label: lab.trim(), placeholder: f.placeholder || "", required: !!f.required, h: box(f).h,
|
|
174
|
+
style: { ...boxStyle(cs(f)), color: cs(f).color, fontSize: px(cs(f).fontSize) },
|
|
175
|
+
...(f.tagName === "SELECT" ? { options: arr(f.options).map((o) => o.text).slice(0, 30) } : {}),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return { k: "form", ...r, fields, submit: submit || "Submit", submitStyle, style: boxStyle(s) };
|
|
179
|
+
};
|
|
180
|
+
// Lay children out the way they SIT: bands of vertically overlapping boxes → rows.
|
|
181
|
+
const arrange = (kids, r) => {
|
|
182
|
+
const sorted = [...kids].sort((a, b) => a.y - b.y || a.x - b.x);
|
|
183
|
+
const bands = [];
|
|
184
|
+
for (const k of sorted) {
|
|
185
|
+
const band = bands[bands.length - 1];
|
|
186
|
+
const bottom = band ? Math.max(...band.map((b) => b.y + b.h)) : -1;
|
|
187
|
+
if (band && k.y < bottom - 4 && !band.some((b) => k.x < b.x + b.w - 4 && b.x < k.x + k.w - 4))
|
|
188
|
+
band.push(k);
|
|
189
|
+
else
|
|
190
|
+
bands.push([k]);
|
|
191
|
+
}
|
|
192
|
+
const rows = bands.map((b) => {
|
|
193
|
+
if (b.length === 1)
|
|
194
|
+
return b[0];
|
|
195
|
+
b.sort((p, q) => p.x - q.x);
|
|
196
|
+
const gaps = b.slice(1).map((k, i) => k.x - (b[i].x + b[i].w));
|
|
197
|
+
const x = Math.min(...b.map((k) => k.x));
|
|
198
|
+
const y = Math.min(...b.map((k) => k.y));
|
|
199
|
+
return { k: "group", dir: "row", gap: Math.max(0, Math.round(gaps.reduce((n, g) => n + g, 0) / gaps.length)), x, y, w: Math.max(...b.map((k) => k.x + k.w)) - x, h: Math.max(...b.map((k) => k.y + k.h)) - y, kids: b };
|
|
200
|
+
});
|
|
201
|
+
if (rows.length === 1)
|
|
202
|
+
return rows[0].k === "group" ? { ...rows[0], ...r } : rows[0];
|
|
203
|
+
const vg = rows.slice(1).map((k, i) => k.y - (rows[i].y + rows[i].h));
|
|
204
|
+
return { k: "group", dir: "col", gap: Math.max(0, Math.round(vg.reduce((n, g) => n + g, 0) / vg.length)), ...r, kids: rows };
|
|
205
|
+
};
|
|
206
|
+
const node = (el) => {
|
|
207
|
+
if (++count > MAX) {
|
|
208
|
+
skip("node limit");
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
const t = el.tagName.toUpperCase();
|
|
212
|
+
if (/^(SCRIPT|STYLE|NOSCRIPT|TEMPLATE|LINK|META|HEAD)$/.test(t) || !visible(el))
|
|
213
|
+
return null;
|
|
214
|
+
const s = cs(el);
|
|
215
|
+
const r = box(el);
|
|
216
|
+
if (overlay(s, r)) {
|
|
217
|
+
skip("fixed overlay (chat widget, cookie bar, sticky button)");
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const he = el;
|
|
221
|
+
if (t === "IMG" || t === "PICTURE") {
|
|
222
|
+
const img = t === "IMG" ? el : el.querySelector("img");
|
|
223
|
+
const src = img && (img.currentSrc || img.src);
|
|
224
|
+
if (!src)
|
|
225
|
+
return (skip("image without src"), null);
|
|
226
|
+
return { k: "image", ...r, src: abs(src), alt: img.alt || "", radius: px(s.borderTopLeftRadius) || undefined, fit: s.objectFit };
|
|
227
|
+
}
|
|
228
|
+
if (t === "SVG") {
|
|
229
|
+
const html = el.outerHTML;
|
|
230
|
+
if (html.length > 12000)
|
|
231
|
+
return (skip("large svg"), null);
|
|
232
|
+
return { k: "icon", ...r, svg: html, color: s.color };
|
|
233
|
+
}
|
|
234
|
+
if (t === "VIDEO") {
|
|
235
|
+
const v = el;
|
|
236
|
+
const src = v.currentSrc || el.querySelector("source")?.src;
|
|
237
|
+
return src ? { k: "video", ...r, src: abs(src), poster: v.poster || undefined } : (skip("video without src"), null);
|
|
238
|
+
}
|
|
239
|
+
if (t === "IFRAME") {
|
|
240
|
+
const src = el.src || "";
|
|
241
|
+
const yt = /youtube(?:-nocookie)?\.com\/embed\/([\w-]{6,})|youtu\.be\/([\w-]{6,})/.exec(src);
|
|
242
|
+
if (yt)
|
|
243
|
+
return { k: "video", ...r, provider: "youtube", id: yt[1] || yt[2] };
|
|
244
|
+
if (/google\.[^/]+\/maps|maps\.google\./.test(src))
|
|
245
|
+
return { k: "map", ...r, src };
|
|
246
|
+
skip("iframe (third-party widget)");
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
if (t === "HR")
|
|
250
|
+
return { k: "line", ...r, color: s.borderTopColor };
|
|
251
|
+
if (t === "FORM")
|
|
252
|
+
return form(el, r, s);
|
|
253
|
+
if (/^(INPUT|SELECT|TEXTAREA)$/.test(t)) {
|
|
254
|
+
if (/^(submit|button)$/.test(el.type))
|
|
255
|
+
return { k: "button", ...r, text: el.value, style: { ...boxStyle(s), ...textStyle(s) } };
|
|
256
|
+
skip("field outside a form");
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
if ((t === "A" || t === "BUTTON" || el.getAttribute("role") === "button") && isButton(he, s)) {
|
|
260
|
+
const href = el.getAttribute("href");
|
|
261
|
+
return { k: "button", ...r, text: he.innerText.trim(), href: href ? abs(href) : undefined, style: { ...boxStyle(s), ...textStyle(s), padX: px(s.paddingLeft) } };
|
|
262
|
+
}
|
|
263
|
+
const b = boxStyle(s);
|
|
264
|
+
if (textLeaf(el)) {
|
|
265
|
+
const href = t === "A" ? el.getAttribute("href") : null;
|
|
266
|
+
// A whole-text link (menu item, footer link) → plain text + a click event, so it keeps
|
|
267
|
+
// its own colour (rich-text <a> is forced blue/underlined by the storefront's .ql-editor a).
|
|
268
|
+
const n = { k: "text", ...r, tag: headingOf(el), html: richText(el), text: he.innerText.trim(), href: href ? abs(href) : undefined, style: textStyle(s) };
|
|
269
|
+
return painted(b) ? { k: "box", ...r, style: b, pad: pads(s), content: n } : n;
|
|
270
|
+
}
|
|
271
|
+
const kids = [];
|
|
272
|
+
for (const c of arr(el.children)) {
|
|
273
|
+
const n = node(c);
|
|
274
|
+
if (n)
|
|
275
|
+
kids.push(n);
|
|
276
|
+
}
|
|
277
|
+
const loose = arr(el.childNodes).filter((n) => n.nodeType === 3 && (n.textContent || "").trim()).map((n) => (n.textContent || "").trim()).join(" ");
|
|
278
|
+
if (loose)
|
|
279
|
+
kids.unshift({ k: "text", ...r, h: Math.max(16, px(s.lineHeight) || px(s.fontSize) * 1.4), tag: "p", html: esc(loose), text: loose, style: textStyle(s) });
|
|
280
|
+
if (!kids.length)
|
|
281
|
+
return painted(b) && r.h > 3 ? { k: "box", ...r, style: b, pad: pads(s) } : null;
|
|
282
|
+
const content = kids.length === 1 ? kids[0] : arrange(kids, r);
|
|
283
|
+
return painted(b) ? { k: "box", ...r, style: b, pad: pads(s), content } : content;
|
|
284
|
+
};
|
|
285
|
+
// Sections: split the page into its vertical bands. A tall block whose children sit one
|
|
286
|
+
// under the other (app root, <main>, a page wrapper) is replaced by those children; a
|
|
287
|
+
// painted wrapper hands its background down to the bands that have none.
|
|
288
|
+
const inherited = new Map();
|
|
289
|
+
const stacked = (kids) => {
|
|
290
|
+
const rs = kids.map((k) => k.getBoundingClientRect()).sort((a, b) => a.top - b.top);
|
|
291
|
+
return rs.every((r, i) => i === 0 || r.top >= rs[i - 1].bottom - 4);
|
|
292
|
+
};
|
|
293
|
+
// A zero-height wrapper (often around a fixed header) is see-through: take its children.
|
|
294
|
+
const expand = (k, d = 0) => {
|
|
295
|
+
const h = k.getBoundingClientRect().height;
|
|
296
|
+
if (h >= 8)
|
|
297
|
+
return visible(k) ? [k] : [];
|
|
298
|
+
return d < 4 && cs(k).display !== "none" ? arr(k.children).flatMap((c) => expand(c, d + 1)) : [];
|
|
299
|
+
};
|
|
300
|
+
let blocks = arr(document.body.children).flatMap((k) => expand(k));
|
|
301
|
+
for (let i = 0; i < 12; i++) {
|
|
302
|
+
let changed = false;
|
|
303
|
+
const next = [];
|
|
304
|
+
for (const b of blocks) {
|
|
305
|
+
const rb = b.getBoundingClientRect();
|
|
306
|
+
// Rails: empty decorative strips (gutter lines, borders) — not content, not layout.
|
|
307
|
+
const rail = (k) => !(k.innerText || "").trim() && !k.querySelector("img,svg,video,iframe,picture") && k.getBoundingClientRect().width < rb.width * 0.1;
|
|
308
|
+
const kids = arr(b.children).flatMap((k) => expand(k)).filter((k) => !rail(k));
|
|
309
|
+
const bs = boxStyle(cs(b));
|
|
310
|
+
const leaf = /^(IMG|PICTURE|SVG|VIDEO|IFRAME|FORM|HR)$/i.test(b.tagName) || textLeaf(b);
|
|
311
|
+
// Fixed bars (a pinned header) overlap the flow: they are their own band, not layout.
|
|
312
|
+
const flow = kids.filter((k) => cs(k).position !== "fixed");
|
|
313
|
+
const wrapper = !leaf && (kids.length === 1 ? !painted(bs) || rb.height > window.innerHeight * 1.5 : kids.length > 1 && rb.height > window.innerHeight * 1.2 && stacked(flow));
|
|
314
|
+
if (wrapper) {
|
|
315
|
+
const bg = painted(bs) ? bs : inherited.get(b);
|
|
316
|
+
for (const k of kids)
|
|
317
|
+
if (bg && !inherited.has(k))
|
|
318
|
+
inherited.set(k, bg);
|
|
319
|
+
next.push(...kids);
|
|
320
|
+
changed = true;
|
|
321
|
+
}
|
|
322
|
+
else
|
|
323
|
+
next.push(b);
|
|
324
|
+
}
|
|
325
|
+
blocks = next;
|
|
326
|
+
if (!changed)
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
const sections = [];
|
|
330
|
+
for (const b of blocks) {
|
|
331
|
+
const s = cs(b);
|
|
332
|
+
const r = box(b);
|
|
333
|
+
if (r.h < 8)
|
|
334
|
+
continue;
|
|
335
|
+
if (overlay(s, r)) {
|
|
336
|
+
skip("fixed overlay (chat widget, cookie bar, sticky button)");
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const kids = [];
|
|
340
|
+
const self = /^(IMG|PICTURE|SVG|VIDEO|IFRAME|FORM|HR)$/i.test(b.tagName) || textLeaf(b);
|
|
341
|
+
if (self)
|
|
342
|
+
kids.push(node(b));
|
|
343
|
+
else
|
|
344
|
+
for (const c of arr(b.children)) {
|
|
345
|
+
const n = node(c);
|
|
346
|
+
if (n)
|
|
347
|
+
kids.push(n);
|
|
348
|
+
}
|
|
349
|
+
const role = b.tagName === "HEADER" || b.querySelector(":scope > nav, header") || (s.position === "fixed" && r.y <= 8) ? "header" : b.tagName === "FOOTER" ? "footer" : b.tagName === "NAV" ? "nav" : undefined;
|
|
350
|
+
const content = kids.filter(Boolean);
|
|
351
|
+
if (!content.length && !painted(boxStyle(s)))
|
|
352
|
+
continue;
|
|
353
|
+
const own = self ? {} : boxStyle(s);
|
|
354
|
+
const style = own.bg || own.bgImage || own.gradient ? own : { ...(inherited.get(b) || {}), ...own };
|
|
355
|
+
sections.push({ k: "section", ...r, role, style, pad: pads(s), sticky: s.position === "sticky" || s.position === "fixed" || undefined, content: content.length === 1 ? content[0] : content.length ? arrange(content, r) : null });
|
|
356
|
+
}
|
|
357
|
+
// Thin bands with the same background as their neighbour (a section title above its grid)
|
|
358
|
+
// belong to one section.
|
|
359
|
+
const merged = [];
|
|
360
|
+
for (const s of sections) {
|
|
361
|
+
const prev = merged[merged.length - 1];
|
|
362
|
+
if (prev && !prev.role && !s.role && JSON.stringify(prev.style) === JSON.stringify(s.style) && (s.h < 240 || prev.h < 240) && s.content && prev.content) {
|
|
363
|
+
const kids = [...(prev.content.k === "group" && prev.content.dir === "col" ? prev.content.kids : [prev.content]), s.content];
|
|
364
|
+
const y = prev.y;
|
|
365
|
+
const h = s.y + s.h - prev.y;
|
|
366
|
+
Object.assign(prev, { h, w: Math.max(prev.w, s.w), content: { k: "group", dir: "col", gap: Math.max(0, s.y - (prev.y + prev.h)) || 16, x: Math.min(prev.x, s.x), y, w: Math.max(prev.w, s.w), h, kids } });
|
|
367
|
+
}
|
|
368
|
+
else
|
|
369
|
+
merged.push(s);
|
|
370
|
+
}
|
|
371
|
+
sections.length = 0;
|
|
372
|
+
sections.push(...merged);
|
|
373
|
+
const textTotal = (document.body.innerText || "").replace(/\s+/g, "").length;
|
|
374
|
+
// The page colour: body/html, else the colour most of the page's height is painted with
|
|
375
|
+
// (an app root wrapper), so translucent bars and unpainted bands sit on the right colour.
|
|
376
|
+
const weight = {};
|
|
377
|
+
for (const s of sections)
|
|
378
|
+
if (s.style?.bg && !/rgba\([^)]*,\s*0?\.\d+\)$/.test(s.style.bg))
|
|
379
|
+
weight[s.style.bg] = (weight[s.style.bg] || 0) + s.h;
|
|
380
|
+
const dominant = Object.entries(weight).sort((a, b) => b[1] - a[1])[0];
|
|
381
|
+
const pageBg = [document.body, document.documentElement].map((e) => cs(e).backgroundColor).find((c) => !clear(c))
|
|
382
|
+
|| (dominant && dominant[1] > sections.reduce((n, s) => n + s.h, 0) * 0.5 ? dominant[0] : "rgb(255, 255, 255)");
|
|
383
|
+
return { url: location.href, title: document.title, viewport: { w: W, h: window.innerHeight }, pageBg, sections, skipped, textTotal };
|
|
384
|
+
}
|
|
385
|
+
/** The capture as a script an agent can run in any browser tool (evaluate_script / javascript). */
|
|
386
|
+
export const CLONE_SCRIPT = `(${captureInPage.toString()})({})`;
|
|
387
|
+
const note = (c, k) => (c.notes[k] = (c.notes[k] || 0) + 1);
|
|
388
|
+
/** rgba(…, a<1) can't be shown over the storefront's white page: lay it over `base` instead. */
|
|
389
|
+
const over = (color, base) => (base && /rgba\([^)]*,\s*0?\.\d+\)$/.test(color) ? `linear-gradient(${color}, ${color}), ${base}` : color);
|
|
390
|
+
const bgStyle = (st = {}, base) => {
|
|
391
|
+
const o = {};
|
|
392
|
+
if (st.bgImage) {
|
|
393
|
+
// The colour stays under the image: if the image can't load the text keeps its contrast.
|
|
394
|
+
o.background = `${st.gradient ? `${st.gradient}, ` : ""}url(${st.bgImage})${st.bg ? ` ${st.bg}` : ""}`;
|
|
395
|
+
o["background-size"] = "cover";
|
|
396
|
+
o["background-position-x"] = "center";
|
|
397
|
+
o["background-position-y"] = "center";
|
|
398
|
+
o["background-repeat"] = "no-repeat";
|
|
399
|
+
}
|
|
400
|
+
else if (st.gradient)
|
|
401
|
+
o.background = base ? `${st.gradient}, ${base}` : st.gradient;
|
|
402
|
+
else if (st.bg)
|
|
403
|
+
o.background = over(st.bg, base);
|
|
404
|
+
// Split keys: element CSS reads borderWidth/borderStyle/borderColor (real pages use them).
|
|
405
|
+
const b = st.border && /^(\d+)px (\w+) (.+)$/.exec(st.border);
|
|
406
|
+
if (b)
|
|
407
|
+
Object.assign(o, { borderWidth: `${b[1]}px`, borderStyle: b[2], borderColor: b[3] });
|
|
408
|
+
if (st.radius)
|
|
409
|
+
o.borderRadius = `${st.radius}px`;
|
|
410
|
+
if (st.shadow)
|
|
411
|
+
o.boxShadow = st.shadow;
|
|
412
|
+
return o;
|
|
413
|
+
};
|
|
414
|
+
const fontStyle = (c, st = {}) => {
|
|
415
|
+
const o = {};
|
|
416
|
+
if (st.fontSize)
|
|
417
|
+
o.fontSize = `${st.fontSize}px`;
|
|
418
|
+
if (st.fontWeight && st.fontWeight !== "400")
|
|
419
|
+
o.fontWeight = String(st.fontWeight);
|
|
420
|
+
if (st.color)
|
|
421
|
+
o.color = st.color;
|
|
422
|
+
if (st.lineHeight)
|
|
423
|
+
o.lineHeight = String(st.lineHeight);
|
|
424
|
+
if (st.textAlign && st.textAlign !== "start")
|
|
425
|
+
o.textAlign = st.textAlign === "end" ? "right" : st.textAlign;
|
|
426
|
+
if (st.letterSpacing)
|
|
427
|
+
o.letterSpacing = st.letterSpacing;
|
|
428
|
+
if (st.textTransform)
|
|
429
|
+
o.textTransform = st.textTransform;
|
|
430
|
+
if (st.fontStyle)
|
|
431
|
+
o.fontStyle = st.fontStyle;
|
|
432
|
+
if (st.fontFamily)
|
|
433
|
+
c.fonts[st.fontFamily] = (c.fonts[st.fontFamily] || 0) + 1;
|
|
434
|
+
return o;
|
|
435
|
+
};
|
|
436
|
+
const painted = (st = {}) => !!(st.bg || st.bgImage || st.gradient || st.border || st.shadow);
|
|
437
|
+
const link = (href) => {
|
|
438
|
+
if (!href || /^javascript:/i.test(href))
|
|
439
|
+
return undefined;
|
|
440
|
+
if (/^tel:/i.test(href))
|
|
441
|
+
return [{ action: "phone_call", trigger: "click", phone_call_number: href.slice(4) }];
|
|
442
|
+
return [{ action: "open_link", trigger: "click", link_target: href, link_target_url: "_self" }];
|
|
443
|
+
};
|
|
444
|
+
const FIELD = { email: "email", tel: "phone-number", textarea: "text-area", select: "select", date: "input-date", number: "input-number" };
|
|
445
|
+
const inner = (n) => ({ x: n.x + (n.pad?.l || 0), w: Math.max(1, n.w - (n.pad?.l || 0) - (n.pad?.r || 0)) });
|
|
446
|
+
/** Where a box sits inside its area: fill | center | right | left. */
|
|
447
|
+
function alignIn(n, p) {
|
|
448
|
+
if (!p || !n.w)
|
|
449
|
+
return undefined;
|
|
450
|
+
if (n.w >= p.w * 0.92)
|
|
451
|
+
return "fill";
|
|
452
|
+
const off = n.x + n.w / 2 - (p.x + p.w / 2);
|
|
453
|
+
if (Math.abs(off) <= p.w * 0.06)
|
|
454
|
+
return "center";
|
|
455
|
+
if (n.x + n.w >= p.x + p.w * 0.94 && n.x - p.x > p.w * 0.2)
|
|
456
|
+
return "right";
|
|
457
|
+
return "left";
|
|
458
|
+
}
|
|
459
|
+
/** A narrower-than-its-area block keeps its share of the width. */
|
|
460
|
+
const share = (n, p) => (p && n.w && n.w < p.w * 0.92 ? { widthUnit: "%", relWidth: Math.max(5, Math.round((n.w / p.w) * 100)) } : undefined);
|
|
461
|
+
function spec(n, c, area) {
|
|
462
|
+
if (!n)
|
|
463
|
+
return null;
|
|
464
|
+
switch (n.k) {
|
|
465
|
+
case "text":
|
|
466
|
+
c.text += String(n.text || "").replace(/\s+/g, "").length;
|
|
467
|
+
const style = { ...fontStyle(c, n.style), height: n.h };
|
|
468
|
+
const at = area && n.w < area.w * 0.7 ? alignIn(n, area) : undefined;
|
|
469
|
+
// A short label on one line in the source stays on one line (fonts differ in width).
|
|
470
|
+
const oneLine = n.style?.fontSize && n.h <= n.style.fontSize * (n.style.lineHeight || 1.4) * 1.3;
|
|
471
|
+
if (oneLine && String(n.text || "").length <= 40)
|
|
472
|
+
style.whiteSpace = "nowrap";
|
|
473
|
+
if ((at === "right" || at === "center") && (!style.textAlign || style.textAlign === "left"))
|
|
474
|
+
style.textAlign = at;
|
|
475
|
+
if (/<a href/.test(n.html || ""))
|
|
476
|
+
note(c, "links inside a sentence keep the storefront's link style (blue, underlined)");
|
|
477
|
+
const events = link(n.href);
|
|
478
|
+
if (events)
|
|
479
|
+
style.cursor = "pointer";
|
|
480
|
+
return { type: "text", opts: { text: n.html || n.text, specials: { tag: n.tag || "p" }, style, ...(events ? { events } : {}) } };
|
|
481
|
+
case "image":
|
|
482
|
+
c.images.push(n.src);
|
|
483
|
+
return { type: "image", opts: { src: n.src, width: n.w, height: n.h, align: alignIn(n, area), config: share(n, area), specials: n.alt ? { image_alt: n.alt } : undefined, style: { ...(n.radius ? { borderRadius: `${n.radius}px` } : {}), ...(n.fit && n.fit !== "fill" ? { objectFit: n.fit } : {}) } } };
|
|
484
|
+
case "icon": {
|
|
485
|
+
note(c, "svg icon → image (data URI; rehost_images uploads it as PNG)");
|
|
486
|
+
const svg = /xmlns=/.test(n.svg) ? n.svg : n.svg.replace("<svg", '<svg xmlns="http://www.w3.org/2000/svg"');
|
|
487
|
+
const src = `data:image/svg+xml;base64,${Buffer.from(svg.replace(/currentColor/g, n.color || "#000")).toString("base64")}`;
|
|
488
|
+
c.images.push(src);
|
|
489
|
+
return { type: "image", opts: { src, width: n.w, height: n.h, align: alignIn(n, area) ?? "left", config: share(n, area) } };
|
|
490
|
+
}
|
|
491
|
+
case "button": {
|
|
492
|
+
c.text += String(n.text || "").replace(/\s+/g, "").length;
|
|
493
|
+
const st = n.style || {};
|
|
494
|
+
const events = link(n.href);
|
|
495
|
+
if (n.href && /#/.test(n.href) && n.href.split("#")[0] === "")
|
|
496
|
+
note(c, "anchor link kept as a URL — retarget it with scroll_to");
|
|
497
|
+
return {
|
|
498
|
+
type: "button",
|
|
499
|
+
opts: {
|
|
500
|
+
text: n.text,
|
|
501
|
+
style: { ...bgStyle(st, c.pageBg), ...fontStyle(c, st), height: n.h, ...(String(n.text || "").length <= 40 ? { whiteSpace: "nowrap" } : {}), ...(st.padX ? { paddingLeft: `${st.padX}px`, paddingRight: `${st.padX}px` } : {}) },
|
|
502
|
+
...(events ? { events } : {}),
|
|
503
|
+
align: alignIn(n, area) ?? "left",
|
|
504
|
+
},
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
case "line":
|
|
508
|
+
return { type: "line", opts: { style: { borderColor: n.color } } };
|
|
509
|
+
case "video":
|
|
510
|
+
const place = { align: alignIn(n, area), config: share(n, area), style: { height: n.h } };
|
|
511
|
+
if (n.provider === "youtube")
|
|
512
|
+
return { type: "video", opts: { ...place, specials: { type: "youtube", id: n.id, src: `https://www.youtube.com/watch?v=${n.id}` } } };
|
|
513
|
+
return { type: "video", opts: { ...place, src: n.src, thumbnail: n.poster } };
|
|
514
|
+
case "map":
|
|
515
|
+
note(c, "google map: embed iframe kept — set the address in the googlemap element if it differs");
|
|
516
|
+
return { type: "googlemap", opts: { iframe: `<iframe src="${n.src}" width="100%" height="${n.h}" style="border:0" loading="lazy"></iframe>`, width: n.w, height: n.h } };
|
|
517
|
+
case "form": {
|
|
518
|
+
note(c, "form rebuilt natively as a subscribe (lead) form — check its type and field names before going live");
|
|
519
|
+
const children = (n.fields || []).map((f) => {
|
|
520
|
+
const type = FIELD[f.tag === "input" ? f.type : f.tag] || "input";
|
|
521
|
+
const label = f.label || f.placeholder || f.name;
|
|
522
|
+
const fs = f.style || {};
|
|
523
|
+
return { type, opts: { specials: { field_name: f.name || label, label, placeholder: f.placeholder || label, required: !!f.required, show_label: !!f.label }, style: { height: f.h } } };
|
|
524
|
+
});
|
|
525
|
+
const sst = n.submitStyle || {};
|
|
526
|
+
children.push({ type: "submit-button", opts: { specials: { text: n.submit }, style: { ...bgStyle(sst), ...fontStyle(c, sst), ...(sst.h ? { height: sst.h } : {}) } } });
|
|
527
|
+
c.text += String(n.submit || "").length;
|
|
528
|
+
// A lead/contact/newsletter form = "subscribe" (customer_data is a logged-in profile form,
|
|
529
|
+
// hidden for visitors; form_order is a checkout).
|
|
530
|
+
// The renderer styles every field from the FORM's style (+ config.backgroundInput), so the
|
|
531
|
+
// first field's frame goes on the form.
|
|
532
|
+
const f0 = (n.fields || [])[0]?.style || {};
|
|
533
|
+
const { background, ...frame } = bgStyle(f0);
|
|
534
|
+
if (painted(n.style))
|
|
535
|
+
note(c, "form box background/border dropped (the form's style styles its fields)");
|
|
536
|
+
return {
|
|
537
|
+
type: "form",
|
|
538
|
+
opts: { specials: { type: "subscribe" }, style: { ...frame, ...(f0.color ? { color: f0.color } : {}), ...(f0.fontSize ? { fontSize: `${f0.fontSize}px` } : {}) }, ...(background ? { config: { backgroundInput: background } } : {}) },
|
|
539
|
+
children, rowGap: 12,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
case "box": {
|
|
543
|
+
const inside = n.content ? spec(n.content, c, inner(n)) : null;
|
|
544
|
+
const pad = n.pad || {};
|
|
545
|
+
const style = { ...bgStyle(n.style), ...(pad.t || pad.l ? { padding: `${pad.t}px ${pad.r}px ${pad.b}px ${pad.l}px` } : {}), height: n.h };
|
|
546
|
+
if (!inside)
|
|
547
|
+
return { type: "rectangle", opts: { style: { ...bgStyle(n.style), height: n.h, width: n.w } } };
|
|
548
|
+
// A painted wrapper around one group → the group's own container gets the paint.
|
|
549
|
+
const own = { align: alignIn(n, area), config: share(n, area) };
|
|
550
|
+
if (inside.type === "container" && !inside.opts?.style?.background)
|
|
551
|
+
return { ...inside, opts: { ...(inside.opts || {}), ...own, style: { ...(inside.opts?.style || {}), ...style } } };
|
|
552
|
+
return { type: "container", opts: { style, ...own }, children: [inside] };
|
|
553
|
+
}
|
|
554
|
+
case "group": {
|
|
555
|
+
if (n.dir === "row") {
|
|
556
|
+
// Each column runs from its child's left edge to the next child's (the last one to the
|
|
557
|
+
// row's right edge), so space-between / offsets survive; the child is aligned inside it.
|
|
558
|
+
const ks = [...(n.kids || [])].sort((a, b) => a.x - b.x);
|
|
559
|
+
const gap = Math.max(0, Math.min(n.gap ?? 24, 120));
|
|
560
|
+
const left = ks[0].x;
|
|
561
|
+
const right = Math.max(...ks.map((k) => k.x + k.w));
|
|
562
|
+
const cols = ks.map((k, i) => {
|
|
563
|
+
const start = i === 0 ? left : k.x;
|
|
564
|
+
const end = i < ks.length - 1 ? ks[i + 1].x - gap : right;
|
|
565
|
+
return { x: start, w: Math.max(1, end - start) };
|
|
566
|
+
});
|
|
567
|
+
const kids = ks.map((k, i) => spec(k, c, cols[i]));
|
|
568
|
+
const keep = kids.map((k, i) => [k, cols[i]]).filter(([k]) => k);
|
|
569
|
+
if (!keep.length)
|
|
570
|
+
return null;
|
|
571
|
+
if (keep.length === 1)
|
|
572
|
+
return keep[0][0];
|
|
573
|
+
const total = keep.reduce((s, [, a]) => s + a.w, 0) || 1;
|
|
574
|
+
const colWidths = keep.map(([, a]) => ({ unit: "fr", value: Math.max(1, Math.round((a.w / total) * 24)) }));
|
|
575
|
+
const span = { x: left, w: right - left };
|
|
576
|
+
// A row of small pieces (menu links, icons, footer bits) stays a row on phones; real
|
|
577
|
+
// columns (cards, split hero) collapse 2 → 1 as usual.
|
|
578
|
+
const small = ks.every((k) => ["text", "button", "icon", "image"].includes(k.k) && k.w <= 240) && (span.w <= 900 || ks.length <= 4);
|
|
579
|
+
const n2 = keep.length;
|
|
580
|
+
return { type: "container", layout: "row", columnGap: gap, colWidths, ...(small ? { collapse: { bp3: n2, bp4: n2 } } : {}), children: keep.map(([k]) => k), opts: { align: alignIn(span, area), config: share(span, area) } };
|
|
581
|
+
}
|
|
582
|
+
const box = { x: n.x, w: n.w };
|
|
583
|
+
const kids = (n.kids || []).map((k) => spec(k, c, box)).filter(Boolean);
|
|
584
|
+
if (!kids.length)
|
|
585
|
+
return null;
|
|
586
|
+
if (kids.length === 1)
|
|
587
|
+
return kids[0];
|
|
588
|
+
return { type: "container", rowGap: Math.min(n.gap ?? 16, 160), children: kids };
|
|
589
|
+
}
|
|
590
|
+
default:
|
|
591
|
+
note(c, `unmapped ${n.k}`);
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/** A captured page → { sections } built with the builder's own layout helpers. */
|
|
596
|
+
export function snapshotToSource(snap, opts = {}) {
|
|
597
|
+
const c = { pageBg: snap?.pageBg, notes: {}, text: 0, fonts: {}, images: [], W: snap?.viewport?.w || 1440 };
|
|
598
|
+
const white = (col) => !col || /^rgba?\(255, 255, 255(, 1)?\)$/.test(col);
|
|
599
|
+
const sections = [];
|
|
600
|
+
const roles = [];
|
|
601
|
+
for (const s of snap?.sections || []) {
|
|
602
|
+
if (opts.skipChrome && (s.role === "header" || s.role === "footer" || s.role === "nav")) {
|
|
603
|
+
note(c, `${s.role} skipped (the site's own global header/footer is used)`);
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
const content = s.content;
|
|
607
|
+
// The section's children: a column group stacks; anything else is one child.
|
|
608
|
+
const childNodes = content?.k === "group" && content.dir === "col" ? content.kids : content ? [content] : [];
|
|
609
|
+
const children = childNodes.map((n) => spec(n, c, inner(s))).filter(Boolean);
|
|
610
|
+
const pad = s.pad || {};
|
|
611
|
+
const firstY = childNodes.length ? Math.min(...childNodes.map((n) => n.y)) : s.y;
|
|
612
|
+
const lastY = childNodes.length ? Math.max(...childNodes.map((n) => n.y + n.h)) : s.y + s.h;
|
|
613
|
+
const padY = Math.max(0, Math.min(160, Math.round(((firstY - s.y) + (s.y + s.h - lastY)) / 2) || pad.t || 0));
|
|
614
|
+
// A section with no paint of its own shows the source page's background (dark sites).
|
|
615
|
+
const st = s.style?.bg || s.style?.bgImage || s.style?.gradient || white(c.pageBg) ? s.style : { ...s.style, bg: c.pageBg };
|
|
616
|
+
const section = buildSection(children, {
|
|
617
|
+
style: bgStyle(st, white(c.pageBg) ? undefined : c.pageBg),
|
|
618
|
+
padY,
|
|
619
|
+
rowGap: content?.k === "group" && content.dir === "col" ? Math.min(content.gap ?? 16, 160) : undefined,
|
|
620
|
+
});
|
|
621
|
+
if (s.sticky)
|
|
622
|
+
note(c, "sticky/fixed header captured as a normal section — set it sticky in the editor if needed");
|
|
623
|
+
sections.push(section);
|
|
624
|
+
roles.push(s.role || "content");
|
|
625
|
+
}
|
|
626
|
+
for (const [k, n] of Object.entries(snap?.skipped || {}))
|
|
627
|
+
c.notes[`skipped: ${k}`] = n;
|
|
628
|
+
const coverage = snap?.textTotal ? Math.min(100, Math.round((c.text / snap.textTotal) * 100)) : undefined;
|
|
629
|
+
const fonts = Object.entries(c.fonts).sort((a, b) => b[1] - a[1]).map(([f]) => f).slice(0, 4);
|
|
630
|
+
return {
|
|
631
|
+
source: { sections },
|
|
632
|
+
roles,
|
|
633
|
+
stats: { sections: sections.length, text_coverage_pct: coverage, images: c.images.length },
|
|
634
|
+
fonts,
|
|
635
|
+
images: c.images,
|
|
636
|
+
notes: Object.entries(c.notes).map(([k, n]) => `${k}${n > 1 ? ` ×${n}` : ""}`),
|
|
637
|
+
};
|
|
638
|
+
}
|