Penpot importer: send images as base64, add reference layer and error report
Mirror to Gitea / mirror (push) Skipped
Mirror to Gitea / mirror (push) Skipped
This commit is contained in:
2 files changed
+56
-10
No files matched your search
@@ -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 }];
|
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();
|
const fontCache = new Map();
|
||||||
/** @param {string} name */
|
/** @param {string} name */
|
||||||
function findFont(name) {
|
function findFont(name) {
|
||||||
@@ -69,9 +86,9 @@ function makeRect(l, ox, oy) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param {any} l @param {number} ox @param {number} oy @param {Uint8Array} bytes */
|
/** @param {any} l @param {number} ox @param {number} oy @param {string} b64 */
|
||||||
async function makeImage(l, ox, oy, bytes) {
|
async function makeImage(l, ox, oy, b64) {
|
||||||
const img = await penpot.uploadMediaData(l.name || 'image', bytes, 'image/jpeg');
|
const img = await penpot.uploadMediaData(String(l.name || 'image').slice(0, 60), fromBase64(b64), 'image/jpeg');
|
||||||
const r = penpot.createRectangle();
|
const r = penpot.createRectangle();
|
||||||
r.name = l.name || 'image';
|
r.name = l.name || 'image';
|
||||||
r.x = ox + l.x;
|
r.x = ox + l.x;
|
||||||
@@ -84,8 +101,12 @@ async function makeImage(l, ox, oy, bytes) {
|
|||||||
/** @type {Record<string, number>} */
|
/** @type {Record<string, number>} */
|
||||||
let nextX = { mobile: 0, desktop: 0 };
|
let nextX = { mobile: 0, desktop: 0 };
|
||||||
|
|
||||||
/** @param {any} b @param {Record<string, Uint8Array>} images */
|
/** @param {any} b @param {Record<string, string>} images */
|
||||||
async function importBoard(b, images) {
|
async function importBoard(b, images) {
|
||||||
|
/** @type {Record<string, number>} */
|
||||||
|
const failed = {};
|
||||||
|
/** @type {string[]} */
|
||||||
|
const errors = [];
|
||||||
const rowY = b.viewport === 'mobile' ? 0 : 7000;
|
const rowY = b.viewport === 'mobile' ? 0 : 7000;
|
||||||
const ox = nextX[b.viewport];
|
const ox = nextX[b.viewport];
|
||||||
nextX[b.viewport] += b.width + 200;
|
nextX[b.viewport] += b.width + 200;
|
||||||
@@ -98,6 +119,19 @@ async function importBoard(b, images) {
|
|||||||
board.fills = [{ fillColor: b.background, fillOpacity: 1 }];
|
board.fills = [{ fillColor: b.background, fillOpacity: 1 }];
|
||||||
board.clipContent = true;
|
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;
|
let made = 0;
|
||||||
for (const sec of b.sections) {
|
for (const sec of b.sections) {
|
||||||
/** @type {any[]} */
|
/** @type {any[]} */
|
||||||
@@ -112,7 +146,8 @@ async function importBoard(b, images) {
|
|||||||
made++;
|
made++;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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) {
|
if (shapes.length > 1) {
|
||||||
@@ -121,14 +156,14 @@ async function importBoard(b, images) {
|
|||||||
} else if (shapes[0]) shapes[0].name = sec.name;
|
} else if (shapes[0]) shapes[0].name = sec.name;
|
||||||
penpot.ui.sendMessage({ type: 'progress', board: b.name, section: 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) => {
|
penpot.ui.onMessage(async (/** @type {any} */ msg) => {
|
||||||
if (msg.type === 'board') {
|
if (msg.type === 'board') {
|
||||||
try {
|
try {
|
||||||
const n = await importBoard(msg.board, msg.images);
|
const r = await importBoard(msg.board, msg.images);
|
||||||
penpot.ui.sendMessage({ type: 'done', board: msg.board.name, layers: n });
|
penpot.ui.sendMessage({ type: 'done', board: msg.board.name, layers: r.made, failed: r.failed, errors: r.errors });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
penpot.ui.sendMessage({ type: 'error', board: msg.board.name, error: String(e) });
|
penpot.ui.sendMessage({ type: 'error', board: msg.board.name, error: String(e) });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,16 +39,27 @@
|
|||||||
const item = queue.shift();
|
const item = queue.shift();
|
||||||
if (!item) { log('All done.'); document.getElementById('go').disabled = false; return; }
|
if (!item) { log('All done.'); document.getElementById('go').disabled = false; return; }
|
||||||
const board = await (await fetch('/baseline/' + item.file)).json();
|
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 = {};
|
const images = {};
|
||||||
for (const sec of board.sections) for (const l of sec.layers) {
|
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 + '…');
|
log('Importing ' + board.name + '…');
|
||||||
parent.postMessage({ type: 'board', board, images }, '*');
|
parent.postMessage({ type: 'board', board, images }, '*');
|
||||||
}
|
}
|
||||||
window.addEventListener('message', (e) => {
|
window.addEventListener('message', (e) => {
|
||||||
const m = e.data;
|
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(); }
|
if (m.type === 'error') { log(`✗ ${m.board}: ${m.error}`); next(); }
|
||||||
});
|
});
|
||||||
document.getElementById('go').onclick = () => {
|
document.getElementById('go').onclick = () => {
|
||||||
|
|||||||
Reference in new issue
Block a user