diff --git a/tools/penpot/plugin/plugin.js b/tools/penpot/plugin/plugin.js index 7d4f66b..76d2526 100644 --- a/tools/penpot/plugin/plugin.js +++ b/tools/penpot/plugin/plugin.js @@ -18,6 +18,23 @@ function parseShadow(css) { return [{ style: 'drop-shadow', offsetX: nums[0] || 0, offsetY: nums[1] || 0, blur: nums[2] || 0, spread: nums[3] || 0, color: { color: hex, opacity: a }, hidden: false }]; } +/** Base64 → bytes without relying on atob (not guaranteed in the plugin sandbox). @param {string} b64 */ +function fromBase64(b64) { + const abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + const lut = new Uint8Array(128); + for (let i = 0; i < abc.length; i++) lut[abc.charCodeAt(i)] = i; + const clean = b64.replace(/[^A-Za-z0-9+/]/g, ''); + const out = new Uint8Array(Math.floor((clean.length * 3) / 4)); + let o = 0; + for (let i = 0; i < clean.length; i += 4) { + const n = (lut[clean.charCodeAt(i)] << 18) | (lut[clean.charCodeAt(i + 1)] << 12) | (lut[clean.charCodeAt(i + 2)] << 6) | lut[clean.charCodeAt(i + 3)]; + out[o++] = (n >> 16) & 255; + if (i + 2 < clean.length) out[o++] = (n >> 8) & 255; + if (i + 3 < clean.length) out[o++] = n & 255; + } + return out.subarray(0, o); +} + const fontCache = new Map(); /** @param {string} name */ function findFont(name) { @@ -69,9 +86,9 @@ function makeRect(l, ox, oy) { return r; } -/** @param {any} l @param {number} ox @param {number} oy @param {Uint8Array} bytes */ -async function makeImage(l, ox, oy, bytes) { - const img = await penpot.uploadMediaData(l.name || 'image', bytes, 'image/jpeg'); +/** @param {any} l @param {number} ox @param {number} oy @param {string} b64 */ +async function makeImage(l, ox, oy, b64) { + const img = await penpot.uploadMediaData(String(l.name || 'image').slice(0, 60), fromBase64(b64), 'image/jpeg'); const r = penpot.createRectangle(); r.name = l.name || 'image'; r.x = ox + l.x; @@ -84,8 +101,12 @@ async function makeImage(l, ox, oy, bytes) { /** @type {Record} */ let nextX = { mobile: 0, desktop: 0 }; -/** @param {any} b @param {Record} images */ +/** @param {any} b @param {Record} images */ async function importBoard(b, images) { + /** @type {Record} */ + const failed = {}; + /** @type {string[]} */ + const errors = []; const rowY = b.viewport === 'mobile' ? 0 : 7000; const ox = nextX[b.viewport]; nextX[b.viewport] += b.width + 200; @@ -98,6 +119,19 @@ async function importBoard(b, images) { board.fills = [{ fillColor: b.background, fillOpacity: 1 }]; board.clipContent = true; + // Full-page screenshot of the real site: hidden + locked, toggle it on to compare/trace. + if (images[b.reference]) { + try { + const ref = await makeImage({ name: 'Reference screenshot', x: 0, y: 0, w: b.width, h: b.height }, ox, rowY, images[b.reference]); + board.appendChild(ref); + ref.name = '⧉ Reference screenshot (toggle to compare)'; + ref.hidden = true; + ref.blocked = true; + } catch (e) { + errors.push('reference: ' + String(e)); + } + } + let made = 0; for (const sec of b.sections) { /** @type {any[]} */ @@ -112,7 +146,8 @@ async function importBoard(b, images) { made++; } } catch (e) { - console.warn('layer failed', l.name, e); + failed[l.type] = (failed[l.type] || 0) + 1; + if (errors.length < 5) errors.push(`${l.type} "${String(l.name).slice(0, 30)}": ${String(e)}`); } } if (shapes.length > 1) { @@ -121,14 +156,14 @@ async function importBoard(b, images) { } else if (shapes[0]) shapes[0].name = sec.name; penpot.ui.sendMessage({ type: 'progress', board: b.name, section: sec.name }); } - return made; + return { made, failed, errors }; } penpot.ui.onMessage(async (/** @type {any} */ msg) => { if (msg.type === 'board') { try { - const n = await importBoard(msg.board, msg.images); - penpot.ui.sendMessage({ type: 'done', board: msg.board.name, layers: n }); + const r = await importBoard(msg.board, msg.images); + penpot.ui.sendMessage({ type: 'done', board: msg.board.name, layers: r.made, failed: r.failed, errors: r.errors }); } catch (e) { penpot.ui.sendMessage({ type: 'error', board: msg.board.name, error: String(e) }); } diff --git a/tools/penpot/plugin/ui.html b/tools/penpot/plugin/ui.html index 04deb21..489eca1 100644 --- a/tools/penpot/plugin/ui.html +++ b/tools/penpot/plugin/ui.html @@ -39,16 +39,27 @@ const item = queue.shift(); if (!item) { log('All done.'); document.getElementById('go').disabled = false; return; } const board = await (await fetch('/baseline/' + item.file)).json(); + // Images travel as base64 strings: plain strings survive the plugin message bridge. + const toB64 = async (url) => { + const blob = await (await fetch(url)).blob(); + return await new Promise((ok) => { const r = new FileReader(); r.onload = () => ok(String(r.result).split(',')[1]); r.readAsDataURL(blob); }); + }; const images = {}; for (const sec of board.sections) for (const l of sec.layers) { - if (l.type === 'image' && l.src && !images[l.src]) images[l.src] = new Uint8Array(await (await fetch('/baseline/' + l.src)).arrayBuffer()); + if (l.type === 'image' && l.src && !images[l.src]) images[l.src] = await toB64('/baseline/' + l.src); } + if (board.reference) images[board.reference] = await toB64('/baseline/' + board.reference); log('Importing ' + board.name + '…'); parent.postMessage({ type: 'board', board, images }, '*'); } window.addEventListener('message', (e) => { const m = e.data; - if (m.type === 'done') { log(`✓ ${m.board} (${m.layers} layers)`); next(); } + if (m.type === 'done') { + const f = Object.entries(m.failed || {}).map(([k, v]) => `${v} ${k}`).join(', '); + log(`${f ? '⚠' : '✓'} ${m.board} (${m.layers} layers${f ? ', failed: ' + f : ''})`); + (m.errors || []).forEach((e) => log(' ' + e)); + next(); + } if (m.type === 'error') { log(`✗ ${m.board}: ${m.error}`); next(); } }); document.getElementById('go').onclick = () => {