'use strict'; /** * UI Canvas in-page editor: the script injected into the mockup iframe. * * Runs inside the sandboxed artifact frame (opaque origin, postMessage only) * and turns the rendered component into something the reviewer can edit * directly: hover and click to select, drag the eight handles to resize, * drag the body to reorder, Alt-drag or arrow keys to nudge, double-click to * edit text, Delete to remove. Every change is recorded as a structured edit * that can be re-applied after the agent live-reloads the file, undone, and * exported together with a clean DOM snapshot. * * All editor UI lives in a shadow root so it never styles, selects, or * exports itself. Served as a string so coverage and lint treat it as data; * it is syntax-checked by tests/lib/ui-canvas-editor.test.js. * * Conventions borrowed, with thanks: Enter/Shift+Enter to walk the tree and * Delete to remove from VisBug (GoogleChromeLabs/ProjectVisBug), the * design/preview mode split from Onlook (onlook-dev/onlook). */ function uiEditorJs() { return String.raw`'use strict'; (() => { if (window.parent === window) return; if (window.__eccUiCanvasEditor) return; window.__eccUiCanvasEditor = true; var post = function (msg) { window.parent.postMessage(msg, '*'); }; // ---- state ------------------------------------------------------------- var mode = 'edit'; var selected = null; var hovered = null; var editing = null; var edits = []; var redoStack = []; var editSeq = 0; var gesture = null; var animationsPaused = false; var ACCENT = '#22c55e'; var RESET_VALUES = { 'background-color': 'transparent', 'background-image': 'none', 'color': 'inherit', 'border': 'none', 'border-radius': '0', 'box-shadow': 'none', 'padding': '0', 'margin': '0', 'gap': '0', 'font-size': 'inherit', 'font-weight': 'normal', 'line-height': 'normal', 'opacity': '1', 'text-align': 'start', 'outline': 'none' }; var NON_TEXT_TAGS = { IMG: 1, VIDEO: 1, AUDIO: 1, SVG: 1, CANVAS: 1, IFRAME: 1, INPUT: 1, SELECT: 1, TEXTAREA: 1, HR: 1, BR: 1, PICTURE: 1, SOURCE: 1, OBJECT: 1, EMBED: 1 }; // ---- overlay host -------------------------------------------------------- var host = document.createElement('div'); host.setAttribute('data-ecc-ui-canvas', 'overlay'); host.style.cssText = 'position:fixed;top:0;left:0;width:0;height:0;z-index:2147483647;pointer-events:none'; var root = host.attachShadow({ mode: 'open' }); root.innerHTML = '' + '' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
'; var hoverBox = root.querySelector('.hover'); var selBox = root.querySelector('.sel'); var labelEl = selBox.querySelector('.label'); var dropEl = root.querySelector('.drop'); var ghostEl = root.querySelector('.ghost'); var toastEl = root.querySelector('.toast'); var pauseStyle = document.createElement('style'); pauseStyle.setAttribute('data-ecc-ui-canvas', 'pause'); pauseStyle.textContent = '*,*::before,*::after{animation-play-state:paused!important;transition:none!important}'; function attachHost() { if (document.body && !host.isConnected) document.body.appendChild(host); } attachHost(); var toastTimer = null; function toast(text) { toastEl.textContent = text; toastEl.style.display = 'block'; clearTimeout(toastTimer); toastTimer = setTimeout(function () { toastEl.style.display = 'none'; }, 1800); } // ---- element helpers --------------------------------------------------------- function isOurs(node) { return node === host || (node && node.getRootNode && node.getRootNode() === root) || (node && node.hasAttribute && node.hasAttribute('data-ecc-ui-canvas')); } function isElement(node) { return node && node.nodeType === 1; } function selectable(node) { return isElement(node) && !isOurs(node) && node !== document.documentElement; } function esc(value) { return (window.CSS && CSS.escape) ? CSS.escape(value) : String(value).replace(/[^a-zA-Z0-9_-]/g, function (c) { return '\\' + c; }); } function elementChildren(parent) { var out = []; for (var i = 0; i < parent.children.length; i++) if (!isOurs(parent.children[i])) out.push(parent.children[i]); return out; } function indexIn(parent, el) { return elementChildren(parent).indexOf(el); } function nthOfType(el) { var n = 1; var sib = el; while ((sib = sib.previousElementSibling)) if (sib.tagName === el.tagName) n++; return n; } function stepFor(el) { var tag = el.tagName.toLowerCase(); if (tag === 'body' || tag === 'html') return tag; var classes = Array.prototype.filter.call(el.classList, function (c) { return /^[A-Za-z_][\w-]*$/.test(c); }).slice(0, 2); var step = tag + classes.map(function (c) { return '.' + esc(c); }).join(''); return step + ':nth-of-type(' + nthOfType(el) + ')'; } // Prefer a unique id, else a type path scoped to the nearest id. Verified // unique so re-application after a reload lands on the same node. function selectorFor(el) { if (!isElement(el)) return 'body'; if (el.id && /^[A-Za-z_][\w-]*$/.test(el.id) && document.querySelectorAll('#' + esc(el.id)).length === 1) return '#' + esc(el.id); var parts = []; var node = el; while (node && isElement(node) && node !== document.documentElement) { if (node !== el && node.id && /^[A-Za-z_][\w-]*$/.test(node.id) && document.querySelectorAll('#' + esc(node.id)).length === 1) { parts.unshift('#' + esc(node.id)); break; } parts.unshift(stepFor(node)); if (node.tagName === 'BODY') break; node = node.parentElement; } var selector = parts.join(' > '); try { if (document.querySelectorAll(selector).length === 1) return selector; } catch (_e) { /* fall through */ } return selector; } function resolve(selector) { try { var el = document.querySelector(selector); return el && !isOurs(el) ? el : null; } catch (_e) { return null; } } function snippetFor(el) { if (!isElement(el)) return ''; if (el.tagName === 'IMG') return el.getAttribute('alt') || (el.getAttribute('src') || '').split('/').pop().slice(0, 80); if (el.tagName === 'INPUT') return el.getAttribute('placeholder') || el.value || el.type; return (el.innerText || el.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 120); } function labelFor(el) { var tag = el.tagName.toLowerCase(); var ident = el.id ? '#' + el.id : (el.classList.length ? '.' + Array.prototype.slice.call(el.classList, 0, 2).join('.') : ''); return tag + ident; } function isTextEditable(el) { if (!isElement(el) || NON_TEXT_TAGS[el.tagName]) return false; if (el === document.body) return false; for (var i = 0; i < el.childNodes.length; i++) { var child = el.childNodes[i]; if (child.nodeType === 3 && child.textContent.trim()) return true; } return el.children.length > 0 && !hasBlockChildren(el) && (el.innerText || '').trim().length > 0; } function hasBlockChildren(el) { for (var i = 0; i < el.children.length; i++) { var display = getComputedStyle(el.children[i]).display; if (display !== 'inline' && display !== 'inline-block') return true; } return false; } function px(value) { var n = parseFloat(value); return isFinite(n) ? n : 0; } function sides(cs, prefix, suffix) { var t = cs.getPropertyValue(prefix + '-top' + suffix), r = cs.getPropertyValue(prefix + '-right' + suffix); var b = cs.getPropertyValue(prefix + '-bottom' + suffix), l = cs.getPropertyValue(prefix + '-left' + suffix); if (t === r && r === b && b === l) return t; if (t === b && r === l) return t + ' ' + r; return t + ' ' + r + ' ' + b + ' ' + l; } function borderSummary(cs) { var width = cs.borderTopWidth; if (px(width) === 0 && px(cs.borderRightWidth) === 0 && px(cs.borderBottomWidth) === 0 && px(cs.borderLeftWidth) === 0) return 'none'; return width + ' ' + cs.borderTopStyle + ' ' + cs.borderTopColor; } function styleReport(el) { var cs = getComputedStyle(el); var isFlexOrGrid = cs.display.indexOf('flex') >= 0 || cs.display.indexOf('grid') >= 0; var rows = [ ['background-color', cs.backgroundColor], ['background-image', cs.backgroundImage === 'none' ? null : cs.backgroundImage], ['color', cs.color], ['border', borderSummary(cs)], ['border-radius', sides(cs, 'border', '-radius').replace(/-top-left|-top-right|-bottom-right|-bottom-left/g, '')], ['box-shadow', cs.boxShadow], ['padding', sides(cs, 'padding', '')], ['margin', sides(cs, 'margin', '')], ['gap', isFlexOrGrid ? (cs.rowGap === cs.columnGap ? cs.rowGap : cs.rowGap + ' ' + cs.columnGap) : null], ['font-size', cs.fontSize], ['font-weight', cs.fontWeight], ['line-height', cs.lineHeight], ['text-align', cs.textAlign], ['opacity', cs.opacity], ['display', cs.display] ]; var out = []; for (var i = 0; i < rows.length; i++) { if (rows[i][1] === null || rows[i][1] === undefined || rows[i][1] === '') continue; out.push({ property: rows[i][0], value: rows[i][1], inline: el.style.getPropertyValue(rows[i][0]) || '', resettable: Object.prototype.hasOwnProperty.call(RESET_VALUES, rows[i][0]) }); } return out; } function describe(el) { var rect = el.getBoundingClientRect(); return { selector: selectorFor(el), tag: el.tagName.toLowerCase(), id: el.id || '', classes: Array.prototype.slice.call(el.classList, 0, 6), snippet: snippetFor(el), size: { width: Math.round(rect.width), height: Math.round(rect.height) }, editable: isTextEditable(el), isImage: el.tagName === 'IMG' }; } function breadcrumbFor(el) { var trail = []; var node = el; while (node && isElement(node) && node !== document.documentElement) { trail.unshift({ selector: selectorFor(node), label: labelFor(node) }); if (node.tagName === 'BODY') break; node = node.parentElement; } return trail.slice(-6); } // ---- overlay positioning ------------------------------------------------- function place(box, rect) { box.style.display = 'block'; box.style.left = rect.left + 'px'; box.style.top = rect.top + 'px'; box.style.width = Math.max(0, rect.width) + 'px'; box.style.height = Math.max(0, rect.height) + 'px'; } function updateBoxes() { if (mode !== 'edit') { hoverBox.style.display = 'none'; selBox.style.display = 'none'; return; } if (hovered && hovered.isConnected && hovered !== selected) place(hoverBox, hovered.getBoundingClientRect()); else hoverBox.style.display = 'none'; if (selected && selected.isConnected) { var rect = selected.getBoundingClientRect(); place(selBox, rect); labelEl.querySelector('b').textContent = labelFor(selected); labelEl.querySelector('span').textContent = Math.round(rect.width) + ' × ' + Math.round(rect.height); labelEl.classList.toggle('inside', rect.top < 30); } else { selBox.style.display = 'none'; if (selected && !selected.isConnected) { selected = null; postSelection(); } } } var rafPending = false; function scheduleBoxes() { if (rafPending) return; rafPending = true; requestAnimationFrame(function () { rafPending = false; updateBoxes(); }); } // ---- selection ----------------------------------------------------------------- function postSelection() { if (!selected || !selected.isConnected) { post({ type: 'uc:selection', selection: null }); return; } var info = describe(selected); info.styles = styleReport(selected); info.breadcrumb = breadcrumbFor(selected); post({ type: 'uc:selection', selection: info }); } function select(el) { if (editing && editing.el !== el) commitTextEdit(); selected = selectable(el) ? el : null; updateBoxes(); postSelection(); } function deselect() { select(null); } // ---- edit log ----------------------------------------------------------------- function newEdit(kind, el, detail) { editSeq += 1; var edit = { id: 'e' + editSeq + '-' + Date.now().toString(36), kind: kind, selector: selectorFor(el), tag: el.tagName.toLowerCase(), snippet: snippetFor(el), at: new Date().toISOString() }; for (var key in detail) if (Object.prototype.hasOwnProperty.call(detail, key)) edit[key] = detail[key]; return edit; } function publicEdit(edit) { var out = {}; for (var key in edit) if (Object.prototype.hasOwnProperty.call(edit, key) && key.charAt(0) !== '_') out[key] = edit[key]; return out; } function postEdits() { post({ type: 'uc:edits', edits: edits.map(publicEdit), canUndo: edits.some(function (e) { return !e.stale; }), canRedo: redoStack.length > 0 }); } function record(edit) { edits.push(edit); redoStack = []; postEdits(); postSelection(); scheduleBoxes(); } function insertAt(parent, el, index) { var kids = elementChildren(parent).filter(function (k) { return k !== el; }); var ref = index < kids.length ? kids[index] : null; parent.insertBefore(el, ref); } function applyInline(el, values) { for (var prop in values) if (Object.prototype.hasOwnProperty.call(values, prop)) { if (values[prop] === '' || values[prop] === null || values[prop] === undefined) el.style.removeProperty(prop); else el.style.setProperty(prop, values[prop]); } } function captureInline(el, props) { var out = {}; for (var i = 0; i < props.length; i++) out[props[i]] = el.style.getPropertyValue(props[i]); return out; } function contentBoxDelta(el, cs) { if (cs.boxSizing === 'border-box') return { w: 0, h: 0 }; return { w: px(cs.paddingLeft) + px(cs.paddingRight) + px(cs.borderLeftWidth) + px(cs.borderRightWidth), h: px(cs.paddingTop) + px(cs.paddingBottom) + px(cs.borderTopWidth) + px(cs.borderBottomWidth) }; } function sizeStyles(el, width, height) { var cs = getComputedStyle(el); var delta = contentBoxDelta(el, cs); var out = {}; if (width !== undefined && width !== null) out.width = Math.max(1, Math.round(width - delta.w)) + 'px'; if (height !== undefined && height !== null) out.height = Math.max(1, Math.round(height - delta.h)) + 'px'; var parent = el.parentElement; if (parent && getComputedStyle(parent).display.indexOf('flex') >= 0 && cs.flexGrow !== '0') out.flex = '0 0 auto'; if (el.tagName === 'IMG' && (width !== undefined || height !== undefined)) { if (width !== undefined && height === undefined) out.height = 'auto'; out['max-width'] = 'none'; } return out; } // Apply a recorded edit to the live document. The el argument is the element the // edit targets (resolved by selector during re-application). Returns false // when the document no longer matches, so the edit can be flagged stale. function applyEdit(edit, el) { el = el || resolve(edit.selector); if (edit.kind === 'remove') { if (!el) return false; var parent = el.parentElement; edit._el = el; edit._parent = parent; edit._index = indexIn(parent, el); el.remove(); return true; } if (!el) return false; if (edit.kind === 'text') { if (edit._htmlBefore === undefined) edit._htmlBefore = el.innerHTML; el.innerHTML = edit.htmlAfter !== undefined ? edit.htmlAfter : ''; if (edit.htmlAfter === undefined) el.textContent = edit.after; edit._el = el; return true; } if (edit.kind === 'resize') { var styles = sizeStyles(el, edit.width, edit.height); if (!edit._inlineBefore) edit._inlineBefore = captureInline(el, Object.keys(styles)); applyInline(el, styles); edit._el = el; return true; } if (edit.kind === 'nudge') { var cs = getComputedStyle(el); if (!edit._inlineBefore) edit._inlineBefore = captureInline(el, ['position', 'left', 'top']); var baseLeft = px(el.style.left), baseTop = px(el.style.top); var next = { left: (baseLeft + edit.dx) + 'px', top: (baseTop + edit.dy) + 'px' }; if (cs.position === 'static') next.position = 'relative'; applyInline(el, next); edit._el = el; return true; } if (edit.kind === 'move') { var target = resolve(edit.to.parent); if (!target) return false; edit._fromParent = el.parentElement; edit._fromIndex = indexIn(el.parentElement, el); insertAt(target, el, edit.to.index); edit._el = el; return true; } if (edit.kind === 'style') { if (!edit._inlineBefore) edit._inlineBefore = captureInline(el, [edit.property]); var value = edit.after === null ? RESET_VALUES[edit.property] : edit.after; if (value === undefined) return false; el.style.setProperty(edit.property, value); edit._el = el; return true; } return false; } function revertEdit(edit) { if (edit.stale) return false; var el = edit._el && edit._el.isConnected ? edit._el : (edit.kind === 'remove' ? edit._el : resolve(edit.selector)); if (edit.kind === 'remove') { if (!el || !edit._parent) return false; insertAt(edit._parent, el, edit._index); return true; } if (!el) return false; if (edit.kind === 'text') { el.innerHTML = edit._htmlBefore !== undefined ? edit._htmlBefore : edit.before; return true; } if (edit.kind === 'resize' || edit.kind === 'nudge' || edit.kind === 'style') { applyInline(el, edit._inlineBefore || {}); return true; } if (edit.kind === 'move') { if (!edit._fromParent) return false; insertAt(edit._fromParent, el, edit._fromIndex); return true; } return false; } function undo() { for (var i = edits.length - 1; i >= 0; i--) { if (edits[i].stale) continue; var edit = edits.splice(i, 1)[0]; revertEdit(edit); redoStack.push(edit); if (edit.kind === 'remove' && edit._el) select(edit._el); toast('Undid ' + edit.kind); break; } postEdits(); postSelection(); scheduleBoxes(); } function redo() { var edit = redoStack.pop(); if (!edit) return; if (applyEdit(edit, edit._el && edit._el.isConnected ? edit._el : null)) { edits.push(edit); toast('Redid ' + edit.kind); } postEdits(); postSelection(); scheduleBoxes(); } function resetAll() { for (var i = edits.length - 1; i >= 0; i--) revertEdit(edits[i]); edits = []; redoStack = []; postEdits(); postSelection(); scheduleBoxes(); toast('All edits reverted'); } // After the agent rewrites the file the frame reloads from scratch; the // shell hands back the edit log and we replay it in order. Edits whose // selector no longer resolves stay in the log, flagged stale, so the human // and the agent both see what did not survive. function restore(payload) { var incoming = Array.isArray(payload.edits) ? payload.edits : []; edits = []; redoStack = []; for (var i = 0; i < incoming.length; i++) { var edit = {}; for (var key in incoming[i]) if (Object.prototype.hasOwnProperty.call(incoming[i], key)) edit[key] = incoming[i][key]; delete edit.stale; if (!applyEdit(edit, null)) edit.stale = true; edits.push(edit); var seq = parseInt(String(edit.id).slice(1), 10); if (seq > editSeq) editSeq = seq; } if (payload.scroll) window.scrollTo(payload.scroll.x || 0, payload.scroll.y || 0); if (payload.selection) { var el = resolve(payload.selection); if (el) selected = el; } updateBoxes(); postEdits(); postSelection(); } // ---- text editing ---------------------------------------------------------- function startTextEdit(el) { if (!isTextEditable(el)) { toast('This element has no editable text'); return; } if (editing) commitTextEdit(); editing = { el: el, before: el.innerText, htmlBefore: el.innerHTML }; el.setAttribute('contenteditable', 'true'); el.setAttribute('data-ecc-ui-canvas-editing', ''); el.focus(); try { var range = document.createRange(); range.selectNodeContents(el); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } catch (_e) { /* selection APIs unavailable */ } selBox.classList.add('editing'); post({ type: 'uc:status', text: 'Editing text · Enter saves · Shift+Enter for a new line · Esc cancels' }); } function finishTextEdit(cancel) { if (!editing) return; var el = editing.el; el.removeAttribute('contenteditable'); el.removeAttribute('data-ecc-ui-canvas-editing'); selBox.classList.remove('editing'); var state = editing; editing = null; if (cancel) { el.innerHTML = state.htmlBefore; post({ type: 'uc:status', text: '' }); scheduleBoxes(); return; } if (el.innerHTML !== state.htmlBefore) { var edit = newEdit('text', el, { before: state.before, after: el.innerText, htmlAfter: el.innerHTML }); edit._htmlBefore = state.htmlBefore; edit._el = el; record(edit); toast('Text updated'); } post({ type: 'uc:status', text: '' }); scheduleBoxes(); } function commitTextEdit() { finishTextEdit(false); } // ---- gestures: resize, move, nudge ----------------------------------------- function startResize(dir, event) { if (!selected) return; var rect = selected.getBoundingClientRect(); gesture = { kind: 'resize', dir: dir, el: selected, x0: event.clientX, y0: event.clientY, w0: rect.width, h0: rect.height, inlineBefore: captureInline(selected, ['width', 'height', 'flex', 'max-width']), ratio: rect.height ? rect.width / rect.height : 1, moved: false }; document.documentElement.style.cursor = getComputedStyle(root.querySelector('.handle[data-dir="' + dir + '"]')).cursor; document.documentElement.setAttribute('data-ecc-ui-canvas-noselect', ''); } function doResize(event) { var g = gesture; var dx = event.clientX - g.x0, dy = event.clientY - g.y0; var width = g.w0, height = g.h0; if (g.dir.indexOf('e') >= 0) width = g.w0 + dx; if (g.dir.indexOf('w') >= 0) width = g.w0 - dx; if (g.dir.indexOf('s') >= 0) height = g.h0 + dy; if (g.dir.indexOf('n') >= 0) height = g.h0 - dy; var corner = g.dir.length === 2; if (g.el.tagName === 'IMG' && corner && !event.shiftKey) { if (Math.abs(width - g.w0) >= Math.abs(height - g.h0)) height = width / g.ratio; else width = height * g.ratio; } width = Math.max(8, Math.round(width)); height = Math.max(8, Math.round(height)); var wantsW = g.dir.indexOf('e') >= 0 || g.dir.indexOf('w') >= 0 || (g.el.tagName === 'IMG' && corner && !event.shiftKey); var wantsH = g.dir.indexOf('n') >= 0 || g.dir.indexOf('s') >= 0 || (g.el.tagName === 'IMG' && corner && !event.shiftKey); g.width = wantsW ? width : undefined; g.height = wantsH ? height : undefined; applyInline(g.el, sizeStyles(g.el, g.width, g.height)); g.moved = true; scheduleBoxes(); } function endResize() { var g = gesture; document.documentElement.style.cursor = ''; document.documentElement.removeAttribute('data-ecc-ui-canvas-noselect'); if (!g.moved) return; var rect = g.el.getBoundingClientRect(); var edit = newEdit('resize', g.el, { width: g.width !== undefined ? Math.round(rect.width) : undefined, height: g.height !== undefined ? Math.round(rect.height) : undefined, before: { width: Math.round(g.w0), height: Math.round(g.h0) } }); if (edit.width === undefined) delete edit.width; if (edit.height === undefined) delete edit.height; edit._inlineBefore = g.inlineBefore; edit._el = g.el; record(edit); toast('Resized to ' + Math.round(rect.width) + ' × ' + Math.round(rect.height)); } function axisOf(container) { var cs = getComputedStyle(container); if (cs.display.indexOf('flex') >= 0) return cs.flexDirection.indexOf('row') === 0 ? 'x' : 'y'; if (cs.display.indexOf('grid') >= 0) return 'grid'; var kids = elementChildren(container); if (kids.length > 1) { var inlineish = kids.every(function (k) { var d = getComputedStyle(k).display; return d.indexOf('inline') === 0 || k.tagName === 'IMG' || k.tagName === 'BUTTON' || k.tagName === 'A' || k.tagName === 'SPAN'; }); if (inlineish) return 'x'; } return 'y'; } function dropPlan(x, y, dragged) { var under = document.elementFromPoint(x, y); if (!under || isOurs(under)) return null; if (under === document.documentElement) under = document.body; if (!under || under === dragged || dragged.contains(under)) return null; var container, index, indicator; if (under === dragged.parentElement || under.contains(dragged)) { container = under; var kids = elementChildren(container).filter(function (k) { return k !== dragged; }); if (!kids.length) { index = 0; indicator = inner(container.getBoundingClientRect()); } else { var axis = axisOf(container); var best = null, bestDist = Infinity; for (var i = 0; i < kids.length; i++) { var r = kids[i].getBoundingClientRect(); var cx = r.left + r.width / 2, cy = r.top + r.height / 2; var dist = axis === 'x' ? Math.abs(cx - x) : axis === 'y' ? Math.abs(cy - y) : Math.hypot(cx - x, cy - y); if (dist < bestDist) { bestDist = dist; best = { el: kids[i], i: i, rect: r }; } } var after = axis === 'x' ? x > best.rect.left + best.rect.width / 2 : axis === 'y' ? y > best.rect.top + best.rect.height / 2 : (x > best.rect.left + best.rect.width / 2); index = best.i + (after ? 1 : 0); indicator = edge(best.rect, axis === 'grid' ? 'x' : axis, after); } } else { container = under.parentElement; if (!container) return null; var axis2 = axisOf(container); var rect = under.getBoundingClientRect(); var after2 = axis2 === 'y' ? y > rect.top + rect.height / 2 : x > rect.left + rect.width / 2; var siblings = elementChildren(container).filter(function (k) { return k !== dragged; }); index = siblings.indexOf(under) + (after2 ? 1 : 0); indicator = edge(rect, axis2 === 'grid' ? 'x' : axis2, after2); } return { container: container, index: index, indicator: indicator }; } function edge(rect, axis, after) { if (axis === 'y') return { left: rect.left, top: (after ? rect.bottom : rect.top) - 1, width: rect.width, height: 3 }; return { left: (after ? rect.right : rect.left) - 1, top: rect.top, width: 3, height: rect.height }; } function inner(rect) { return { left: rect.left + 4, top: rect.top + 4, width: Math.max(0, rect.width - 8), height: 3 }; } function startPress(el, event) { gesture = { kind: 'press', el: el, x0: event.clientX, y0: event.clientY, nudge: event.altKey, started: false }; } function beginDrag(event) { var g = gesture; g.started = true; if (g.nudge) { g.kind = 'nudge'; g.inlineBefore = captureInline(g.el, ['position', 'left', 'top']); g.baseLeft = px(g.el.style.left); g.baseTop = px(g.el.style.top); if (getComputedStyle(g.el).position === 'static') g.el.style.position = 'relative'; post({ type: 'uc:status', text: 'Nudging · release to keep the offset' }); } else { g.kind = 'move'; g.fromParent = g.el.parentElement; g.fromIndex = indexIn(g.fromParent, g.el); g.el.style.pointerEvents = 'none'; selBox.classList.add('dragging'); post({ type: 'uc:status', text: 'Reordering · drop between siblings or into a container · hold Alt to nudge instead' }); } document.documentElement.setAttribute('data-ecc-ui-canvas-noselect', ''); event.preventDefault(); } function doDrag(event) { var g = gesture; if (g.kind === 'nudge') { applyInline(g.el, { left: (g.baseLeft + event.clientX - g.x0) + 'px', top: (g.baseTop + event.clientY - g.y0) + 'px' }); scheduleBoxes(); return; } var rect = g.el.getBoundingClientRect(); place(ghostEl, { left: rect.left + (event.clientX - g.x0), top: rect.top + (event.clientY - g.y0), width: rect.width, height: rect.height }); var plan = dropPlan(event.clientX, event.clientY, g.el); g.plan = plan; if (plan && plan.indicator) place(dropEl, plan.indicator); else dropEl.style.display = 'none'; } function endDrag() { var g = gesture; document.documentElement.removeAttribute('data-ecc-ui-canvas-noselect'); post({ type: 'uc:status', text: '' }); if (g.kind === 'nudge') { var dx = px(g.el.style.left) - g.baseLeft, dy = px(g.el.style.top) - g.baseTop; if (dx === 0 && dy === 0) { applyInline(g.el, g.inlineBefore); return; } var edit = newEdit('nudge', g.el, { dx: dx, dy: dy }); edit._inlineBefore = g.inlineBefore; edit._el = g.el; record(edit); toast('Nudged ' + dx + ', ' + dy + 'px'); return; } g.el.style.pointerEvents = ''; if (!g.el.getAttribute('style')) g.el.removeAttribute('style'); selBox.classList.remove('dragging'); ghostEl.style.display = 'none'; dropEl.style.display = 'none'; var plan = g.plan; if (!plan) return; var sameSpot = plan.container === g.fromParent && (plan.index === g.fromIndex || plan.index === g.fromIndex + 1); if (sameSpot) return; var fromSelector = selectorFor(g.el); var fromParentSelector = selectorFor(g.fromParent); insertAt(plan.container, g.el, plan.index); var moveEdit = newEdit('move', g.el, { from: { parent: fromParentSelector, index: g.fromIndex }, to: { parent: selectorFor(plan.container), index: indexIn(plan.container, g.el) } }); moveEdit.selector = fromSelector; moveEdit.selectorAfter = selectorFor(g.el); moveEdit._fromParent = g.fromParent; moveEdit._fromIndex = g.fromIndex; moveEdit._el = g.el; record(moveEdit); toast('Moved ' + labelFor(g.el)); } function nudgeBy(dx, dy) { if (!selected) return; var last = edits.length ? edits[edits.length - 1] : null; if (last && last.kind === 'nudge' && last._el === selected && !last.stale && Date.now() - Date.parse(last.at) < 1500) { applyInline(selected, { left: (px(selected.style.left) + dx) + 'px', top: (px(selected.style.top) + dy) + 'px' }); last.dx += dx; last.dy += dy; last.at = new Date().toISOString(); postEdits(); scheduleBoxes(); return; } var edit = newEdit('nudge', selected, { dx: dx, dy: dy }); edit._inlineBefore = captureInline(selected, ['position', 'left', 'top']); edit._el = selected; var cs = getComputedStyle(selected); var next = { left: (px(selected.style.left) + dx) + 'px', top: (px(selected.style.top) + dy) + 'px' }; if (cs.position === 'static') next.position = 'relative'; applyInline(selected, next); record(edit); } function removeSelected() { if (!selected || selected === document.body) { toast('Select an element to remove'); return; } var el = selected; var parent = el.parentElement; var edit = newEdit('remove', el, { outerHTML: el.outerHTML.slice(0, 20000), from: { parent: selectorFor(parent), index: indexIn(parent, el) } }); edit._el = el; edit._parent = parent; edit._index = indexIn(parent, el); el.remove(); selected = parent && parent !== document.documentElement ? parent : null; record(edit); toast('Removed ' + edit.tag + ' · Cmd/Ctrl+Z to undo'); } function setStyle(property, value) { if (!selected) return; if (!/^[a-z][a-z0-9-]*$/.test(property)) return; var cs = getComputedStyle(selected); var before = cs.getPropertyValue(property); var last = edits.length ? edits[edits.length - 1] : null; if (last && last.kind === 'style' && last.property === property && last._el === selected && !last.stale && Date.now() - Date.parse(last.at) < 2500) { selected.style.setProperty(property, value === null ? RESET_VALUES[property] : value); last.after = value; last.at = new Date().toISOString(); postEdits(); postSelection(); scheduleBoxes(); return; } var edit = newEdit('style', selected, { property: property, before: before, after: value }); edit._inlineBefore = captureInline(selected, [property]); edit._el = selected; if (!applyEdit(edit, selected)) { toast('Cannot change ' + property); return; } record(edit); } function setSize(width, height) { if (!selected) return; var last = edits.length ? edits[edits.length - 1] : null; if (last && last.kind === 'resize' && last._el === selected && !last.stale && Date.now() - Date.parse(last.at) < 2500) { if (width !== null && width !== undefined) last.width = Math.round(width); if (height !== null && height !== undefined) last.height = Math.round(height); var next = sizeStyles(selected, last.width, last.height); // A dimension first touched now still needs its original value for undo. last._inlineBefore = last._inlineBefore || {}; for (var key in next) if (!Object.prototype.hasOwnProperty.call(last._inlineBefore, key)) last._inlineBefore[key] = selected.style.getPropertyValue(key); applyInline(selected, next); last.at = new Date().toISOString(); postEdits(); postSelection(); scheduleBoxes(); return; } var rect = selected.getBoundingClientRect(); var edit = newEdit('resize', selected, { before: { width: Math.round(rect.width), height: Math.round(rect.height) } }); if (width !== null && width !== undefined) edit.width = Math.round(width); if (height !== null && height !== undefined) edit.height = Math.round(height); if (edit.width === undefined && edit.height === undefined) return; edit._el = selected; applyEdit(edit, selected); record(edit); } // ---- export ------------------------------------------------------------------- function exportHtml() { if (editing) commitTextEdit(); var clone = document.documentElement.cloneNode(true); var junk = clone.querySelectorAll('[data-ecc-ui-canvas],[data-ecc-plan-canvas]'); for (var i = 0; i < junk.length; i++) junk[i].remove(); var scripts = clone.querySelectorAll('script[src*="/ui-editor.js"],script[src*="/sdk.js"]'); for (var j = 0; j < scripts.length; j++) scripts[j].remove(); var marked = clone.querySelectorAll('[contenteditable],[data-ecc-ui-canvas-editing]'); for (var k = 0; k < marked.length; k++) { marked[k].removeAttribute('contenteditable'); marked[k].removeAttribute('data-ecc-ui-canvas-editing'); } clone.removeAttribute('data-ecc-ui-canvas-noselect'); var doctype = document.doctype ? '\n' : '\n'; return doctype + clone.outerHTML + '\n'; } // ---- DOM events ---------------------------------------------------------------- var noSelectStyle = document.createElement('style'); noSelectStyle.setAttribute('data-ecc-ui-canvas', 'noselect'); noSelectStyle.textContent = '[data-ecc-ui-canvas-noselect] *{user-select:none!important;cursor:inherit}'; document.head && document.head.appendChild(noSelectStyle); document.addEventListener('mousemove', function (e) { if (gesture) { if (gesture.kind === 'resize') { doResize(e); return; } if (gesture.kind === 'press') { if (Math.hypot(e.clientX - gesture.x0, e.clientY - gesture.y0) > 4) beginDrag(e); return; } doDrag(e); return; } if (mode !== 'edit' || editing) return; var target = e.target; hovered = selectable(target) && !(editing && editing.el === target) ? target : null; scheduleBoxes(); }, true); document.addEventListener('mousedown', function (e) { if (mode !== 'edit' || e.button !== 0) return; var path = e.composedPath ? e.composedPath() : [e.target]; var first = path[0]; if (first && first.classList && first.classList.contains('handle') && first.getRootNode() === root) { e.preventDefault(); e.stopPropagation(); startResize(first.getAttribute('data-dir'), e); return; } var target = e.target; if (editing && editing.el.contains(target)) return; if (editing) commitTextEdit(); if (!selectable(target)) return; e.preventDefault(); e.stopPropagation(); if (target !== selected) select(target); if (target !== document.body) startPress(target, e); }, true); document.addEventListener('mouseup', function (e) { if (!gesture) return; var g = gesture; gesture = null; if (g.kind === 'resize') { endResize(); e.preventDefault(); e.stopPropagation(); return; } if (g.kind === 'press') return; endDrag(); e.preventDefault(); e.stopPropagation(); }, true); document.addEventListener('click', function (e) { if (mode !== 'edit') return; if (editing && editing.el.contains(e.target)) return; e.preventDefault(); e.stopPropagation(); }, true); document.addEventListener('dblclick', function (e) { if (mode !== 'edit') return; if (editing && editing.el.contains(e.target)) return; if (!selectable(e.target)) return; e.preventDefault(); e.stopPropagation(); select(e.target); startTextEdit(e.target); }, true); document.addEventListener('keydown', function (e) { var meta = e.metaKey || e.ctrlKey; if (meta && e.key.toLowerCase() === 'i' && !editing) { e.preventDefault(); post({ type: 'uc:toggle-mode' }); return; } if (editing) { if (e.key === 'Escape') { e.preventDefault(); finishTextEdit(true); } else if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); commitTextEdit(); } return; } if (mode !== 'edit') return; if (meta && e.key.toLowerCase() === 'z') { e.preventDefault(); if (e.shiftKey) redo(); else undo(); return; } if (meta && e.key.toLowerCase() === 'y') { e.preventDefault(); redo(); return; } if (e.key === 'Escape') { if (selected) { e.preventDefault(); deselect(); } return; } if (!selected) return; if (e.key === 'Delete' || e.key === 'Backspace') { e.preventDefault(); removeSelected(); return; } if (e.key === 'Enter') { e.preventDefault(); if (e.shiftKey) { if (selected.parentElement && selectable(selected.parentElement)) select(selected.parentElement); } else { var kids = elementChildren(selected); if (kids.length) select(kids[0]); else startTextEdit(selected); } return; } if (e.key === 'Tab') { e.preventDefault(); var next = e.shiftKey ? selected.previousElementSibling : selected.nextElementSibling; while (next && isOurs(next)) next = e.shiftKey ? next.previousElementSibling : next.nextElementSibling; if (next) select(next); return; } var step = e.shiftKey ? 10 : 1; if (e.key === 'ArrowLeft') { e.preventDefault(); nudgeBy(-step, 0); } else if (e.key === 'ArrowRight') { e.preventDefault(); nudgeBy(step, 0); } else if (e.key === 'ArrowUp') { e.preventDefault(); nudgeBy(0, -step); } else if (e.key === 'ArrowDown') { e.preventDefault(); nudgeBy(0, step); } }, true); document.addEventListener('focusout', function (e) { if (editing && e.target === editing.el) setTimeout(function () { if (editing && editing.el === e.target) commitTextEdit(); }, 0); }, true); var scrollTimer = null; window.addEventListener('scroll', function () { scheduleBoxes(); if (scrollTimer) return; scrollTimer = setTimeout(function () { scrollTimer = null; post({ type: 'uc:scroll', x: window.scrollX, y: window.scrollY }); }, 120); }, { passive: true, capture: true }); window.addEventListener('resize', function () { scheduleBoxes(); post({ type: 'uc:viewport', width: window.innerWidth, height: window.innerHeight }); }); if (window.ResizeObserver) { var observer = new ResizeObserver(function () { scheduleBoxes(); }); observer.observe(document.documentElement); } if (window.MutationObserver) { var mutations = new MutationObserver(function () { scheduleBoxes(); }); mutations.observe(document.documentElement, { childList: true, subtree: true, attributes: true }); } // ---- shell bridge --------------------------------------------------------------- window.addEventListener('message', function (e) { var msg = e.data || {}; if (typeof msg.type !== 'string' || msg.type.indexOf('uc:') !== 0) return; switch (msg.type) { case 'uc:set-mode': if (editing) commitTextEdit(); mode = msg.mode === 'interact' ? 'interact' : 'edit'; if (mode === 'interact') { hovered = null; } updateBoxes(); break; case 'uc:restore': restore(msg); break; case 'uc:select': { var el = resolve(msg.selector); if (el) { select(el); try { el.scrollIntoView({ block: 'nearest', inline: 'nearest' }); } catch (_e) { /* older engines */ } } break; } case 'uc:deselect': deselect(); break; case 'uc:style': setStyle(msg.property, msg.value === undefined ? null : msg.value); break; case 'uc:size': setSize(msg.width, msg.height); break; case 'uc:remove': removeSelected(); break; case 'uc:edit-text': if (selected) startTextEdit(selected); break; case 'uc:undo': undo(); break; case 'uc:redo': redo(); break; case 'uc:reset': resetAll(); break; case 'uc:forget': // The agent baked the current edits into the file: drop the log // without touching the DOM so nothing is replayed twice. edits = []; redoStack = []; postEdits(); break; case 'uc:pause-animations': animationsPaused = Boolean(msg.paused); if (animationsPaused) document.head.appendChild(pauseStyle); else pauseStyle.remove(); break; case 'uc:export': post({ type: 'uc:export', requestId: msg.requestId, html: exportHtml(), edits: edits.map(publicEdit) }); break; case 'uc:scroll-to': window.scrollTo(msg.x || 0, msg.y || 0); break; default: break; } }); function announceReady() { attachHost(); post({ type: 'uc:ready', title: document.title || '', viewport: { width: window.innerWidth, height: window.innerHeight }, scroll: { x: window.scrollX, y: window.scrollY } }); } if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', announceReady); else announceReady(); })();`; } module.exports = { uiEditorJs };