// @ts-check /// /** * yoyos.sh baseline importer — runs inside Penpot. * The UI (ui.html) fetches captured boards from the local server and posts them here; * this code turns each layer into a native Penpot rectangle, image or text shape. */ penpot.ui.open('yoyos.sh baseline', '/ui.html', { width: 340, height: 520 }); /** @param {string} css e.g. "rgb(20, 20, 18) 6px 6px 0px 0px" */ function parseShadow(css) { const color = css.match(/rgba?\([^)]+\)/)?.[0]; if (!color) return []; const nums = css.replace(color, '').trim().split(/\s+/).map(parseFloat); const [r, g, b, a = 1] = color.replace(/rgba?\(|\)/g, '').split(/[ ,/]+/).filter(Boolean).map(Number); const hex = '#' + [r, g, b].map((n) => Math.round(n).toString(16).padStart(2, '0')).join(''); 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 }]; } const fontCache = new Map(); /** @param {string} name */ function findFont(name) { if (!fontCache.has(name)) { fontCache.set(name, penpot.fonts.findByName(name) || penpot.fonts.findAllByName(name)[0] || null); } return fontCache.get(name); } /** @param {any} l @param {number} ox @param {number} oy */ function makeText(l, ox, oy) { const t = penpot.createText(l.text); if (!t) return null; t.name = l.name || 'text'; const font = findFont(l.font); if (font) { const variant = font.variants.find((/** @type {any} */ v) => v.fontWeight === String(l.weight) && v.fontStyle === (l.italic ? 'italic' : 'normal')) || font.variants.find((/** @type {any} */ v) => v.fontWeight === String(l.weight)) || font.variants[0]; font.applyToText(t, variant); } t.fontSize = String(Math.round(l.size * 100) / 100); t.lineHeight = String(l.lineHeight); t.letterSpacing = String(l.letterSpacing || 0); if (l.transform) t.textTransform = l.transform; t.align = l.align; t.fills = [{ fillColor: l.color, fillOpacity: l.opacity ?? 1 }]; if (l.stroke) t.strokes = [{ strokeColor: l.stroke.color, strokeOpacity: l.stroke.opacity, strokeWidth: l.stroke.width, strokeAlignment: 'outer', strokeStyle: 'solid' }]; if (l.shadow) t.shadows = /** @type {any} */ (parseShadow(l.shadow)); t.growType = 'fixed'; t.x = ox + l.x; t.y = oy + l.y; t.resize(Math.max(l.w + 2, 4), Math.max(l.h, 4)); return t; } /** @param {any} l @param {number} ox @param {number} oy */ function makeRect(l, ox, oy) { const r = penpot.createRectangle(); r.name = l.name || 'box'; r.x = ox + l.x; r.y = oy + l.y; r.resize(Math.max(l.w, 1), Math.max(l.h, 1)); r.fills = l.fill ? [{ fillColor: l.fill.color, fillOpacity: l.fill.opacity }] : []; if (l.stroke) r.strokes = [{ strokeColor: l.stroke.color, strokeOpacity: l.stroke.opacity, strokeWidth: l.stroke.width, strokeAlignment: 'inner', strokeStyle: 'solid' }]; if (l.radius) r.borderRadius = l.radius; if (l.shadow) r.shadows = /** @type {any} */ (parseShadow(l.shadow)); 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'); const r = penpot.createRectangle(); r.name = l.name || 'image'; r.x = ox + l.x; r.y = oy + l.y; r.resize(Math.max(l.w, 1), Math.max(l.h, 1)); r.fills = [{ fillOpacity: 1, fillImage: img }]; return r; } /** @type {Record} */ let nextX = { mobile: 0, desktop: 0 }; /** @param {any} b @param {Record} images */ async function importBoard(b, images) { const rowY = b.viewport === 'mobile' ? 0 : 7000; const ox = nextX[b.viewport]; nextX[b.viewport] += b.width + 200; const board = penpot.createBoard(); board.name = b.name; board.x = ox; board.y = rowY; board.resize(b.width, b.height); board.fills = [{ fillColor: b.background, fillOpacity: 1 }]; board.clipContent = true; let made = 0; for (const sec of b.sections) { /** @type {any[]} */ const shapes = []; for (const l of sec.layers) { try { const s = l.type === 'text' ? makeText(l, ox, rowY) : l.type === 'image' ? (images[l.src] ? await makeImage(l, ox, rowY, images[l.src]) : null) : makeRect(l, ox, rowY); if (s) { board.appendChild(s); shapes.push(s); made++; } } catch (e) { console.warn('layer failed', l.name, e); } } if (shapes.length > 1) { const g = penpot.group(shapes); if (g) g.name = sec.name; } else if (shapes[0]) shapes[0].name = sec.name; penpot.ui.sendMessage({ type: 'progress', board: b.name, section: sec.name }); } return made; } 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 }); } catch (e) { penpot.ui.sendMessage({ type: 'error', board: msg.board.name, error: String(e) }); } } });