import QRCode from 'qrcode'; import { appearanceStyle, cssText, frameStyle, initialFrame } from './free-layout.js'; export const blockTypes = { text: 'Text', image: 'Bild', gallery: 'Galerie', button: 'Button', qr: 'QR-Code', video: 'Video', social: 'Social-Media-Link', shape: 'Form', html: 'HTML & CSS', }; export function makeBlock(type) { const defaults = { text: { text: 'Hier steht dein Text.', style: 'body', align: 'left' }, image: { mediaId: '', alt: '', height: 46 }, gallery: { mediaIds: [], height: 29 }, button: { text: 'Mehr erfahren', url: '' }, qr: { text: '', url: '' }, video: { mediaId: '', posterMediaId: '', text: '' }, social: { text: 'Instagram', url: '' }, shape: { shape: 'rectangle', appearance: { fill: '#ddd7ff' } }, html: { markup: '

Eigener Inhalt

\n

Hier kannst du HTML und CSS einfügen.

' }, }; return { id: crypto.randomUUID(), type, width: 'full', ...defaults[type] }; } export function makePage(title = 'Neue Seite') { return { id: crypto.randomUUID(), title, layout: 'free', background: '#fffefa', color: '#20202b', blocks: [] }; } export function escapeHtml(value = '') { return String(value).replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', })[character]); } export function safeUrl(value, webOnly = false) { try { const parsed = new URL(value); return (webOnly ? ['https:', 'http:'] : ['https:', 'http:', 'mailto:', 'tel:']).includes(parsed.protocol) ? parsed.href : ''; } catch { return ''; } } export function mediaRefs(draft) { return [draft.cover, ...draft.pages.flatMap((page) => page.blocks.flatMap((block) => block.type === 'gallery' ? block.mediaIds : [block.mediaId, block.posterMediaId]))].filter(Boolean); } export function pageStyle(page) { return `--paper:${page.background};--ink:${page.color}`; } export async function renderBlock(block, mediaUrl, forPdf = false) { const text = escapeHtml(block.text); const media = (id) => escapeHtml(mediaUrl(id)); const href = escapeHtml(safeUrl(block.url, block.type === 'social')); const link = (label, kind) => href ? `${label}` : `${label || 'Link hinzufügen'}`; const placeholder = (label) => `
${label}
`; switch (block.type) { case 'text': { const tag = block.style === 'title' ? 'h1' : block.style === 'heading' ? 'h2' : 'p'; const align = ['left', 'center', 'right'].includes(block.align) ? block.align : 'left'; return `<${tag} class="folio-text folio-${block.style}" style="text-align:${align}">${text}`; } case 'image': return block.mediaId ? `${escapeHtml(block.alt)}` : placeholder('Bild auswählen'); case 'gallery': return block.mediaIds.length ? `` : placeholder('Galeriebilder auswählen'); case 'button': return link(text, 'button'); case 'social': return link(text, 'social'); case 'shape': return ``; case 'html': { const { htmlDocument } = await import('./inline-html.js'); return ``; } case 'video': { if (!block.mediaId) return placeholder('Video auswählen'); const poster = block.posterMediaId ? media(block.posterMediaId) : ''; return `
${forPdf ? `
${poster ? `` : ''}Video in der Online-Ausgabe ansehen
` : ``}${text ? `
${text}
` : ''}
`; } case 'qr': if (!href) return placeholder('QR-Ziel eingeben'); return `
QR-Code${text ? `
${text}
` : ''}
`; default: return ''; } } export async function renderPages(draft, mediaUrl, forPdf = false) { return Promise.all(draft.pages.map(async (page) => { const blocks = await Promise.all(page.blocks.map(async (block, index) => { const positioning = page.layout === 'free' ? ` style="${frameStyleText(block.frame || initialFrame(block.type, index))}"` : ''; return `
${await renderBlock(block, mediaUrl, forPdf)}
`; })); return { id: page.id, title: page.title, html: `
${blocks.join('')}
`, }; })); } function frameStyleText(frame) { return escapeHtml(cssText(frameStyle(frame))); }