All checks were successful
Docker-Image veröffentlichen / publish (push) Successful in 4m8s
113 lines
5.5 KiB
JavaScript
113 lines
5.5 KiB
JavaScript
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: '<h2>Eigener Inhalt</h2>\n<p>Hier kannst du HTML und CSS einfügen.</p>' },
|
|
};
|
|
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
|
|
? `<a class="folio-${kind}" href="${href}" target="_blank" rel="noopener noreferrer">${label}</a>`
|
|
: `<span class="folio-placeholder">${label || 'Link hinzufügen'}</span>`;
|
|
const placeholder = (label) => `<div class="folio-placeholder">${label}</div>`;
|
|
|
|
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}</${tag}>`;
|
|
}
|
|
case 'image':
|
|
return block.mediaId
|
|
? `<img class="folio-image" src="${media(block.mediaId)}" alt="${escapeHtml(block.alt)}" style="height:${block.height}cqw">`
|
|
: placeholder('Bild auswählen');
|
|
case 'gallery':
|
|
return block.mediaIds.length
|
|
? `<div class="folio-gallery" style="--gallery-height:${block.height}cqw">${block.mediaIds.map((id, index) => `<img src="${media(id)}" alt="Galeriebild ${index + 1}">`).join('')}</div>`
|
|
: placeholder('Galeriebilder auswählen');
|
|
case 'button': return link(text, 'button');
|
|
case 'social': return link(text, 'social');
|
|
case 'shape': return `<div class="folio-shape folio-shape-${block.shape}" aria-hidden="true"></div>`;
|
|
case 'html': {
|
|
const { htmlDocument } = await import('./inline-html.js');
|
|
return `<iframe class="folio-html" title="HTML-Inhalt" sandbox="allow-popups allow-popups-to-escape-sandbox" srcdoc="${escapeHtml(htmlDocument(block.markup))}"></iframe>`;
|
|
}
|
|
case 'video': {
|
|
if (!block.mediaId) return placeholder('Video auswählen');
|
|
const poster = block.posterMediaId ? media(block.posterMediaId) : '';
|
|
return `<figure class="folio-video">${forPdf
|
|
? `<div class="folio-video-print">${poster ? `<img src="${poster}" alt="">` : ''}<span>Video in der Online-Ausgabe ansehen</span></div>`
|
|
: `<video controls playsinline preload="metadata" src="${media(block.mediaId)}"${poster ? ` poster="${poster}"` : ''} aria-label="${text || 'Video'}"></video>`}${text ? `<figcaption>${text}</figcaption>` : ''}</figure>`;
|
|
}
|
|
case 'qr':
|
|
if (!href) return placeholder('QR-Ziel eingeben');
|
|
return `<figure class="folio-qr"><img src="${await QRCode.toDataURL(safeUrl(block.url), { margin: 2, width: 384 })}" alt="QR-Code">${text ? `<figcaption>${text}</figcaption>` : ''}</figure>`;
|
|
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 `<div class="folio-block folio-width-${block.width}"${positioning}><div class="folio-item-content" style="${escapeHtml(cssText(appearanceStyle(block.appearance)))}">${await renderBlock(block, mediaUrl, forPdf)}</div></div>`;
|
|
}));
|
|
return {
|
|
id: page.id, title: page.title,
|
|
html: `<article class="folio-page" style="${pageStyle(page)}" aria-label="${escapeHtml(page.title)}"><div class="folio-content folio-layout-${page.layout}">${blocks.join('')}</div></article>`,
|
|
};
|
|
}));
|
|
}
|
|
|
|
function frameStyleText(frame) {
|
|
return escapeHtml(cssText(frameStyle(frame)));
|
|
}
|