25 lines
2.1 KiB
TypeScript
25 lines
2.1 KiB
TypeScript
'use client';
|
|
import { useMemo } from 'react';
|
|
import { usePlacementScene } from './usePlacementScene';
|
|
import { api_saveTexts } from '@/actions/texte/api-texte';
|
|
import TextSyncDialog from '@/components/texte/textSyncDialog';
|
|
import type { TextTable } from '@/types/texte';
|
|
import type { GraphicBinding } from '@/types/visualisierung/graphic';
|
|
function sceneTable(table: TextTable) { return { ...table, rows: table.rows.map(row => ({ ...row, beschreibung: row.text, graphics: [] as GraphicBinding[] })) }; }
|
|
function color(value: string) { return /^#[a-f\d]{8}$/i.test(value) ? '#' + value.slice(3) + value.slice(1, 3) : value; }
|
|
export function useTextScene(initial: TextTable, options: { disabled: boolean; snap: boolean; step: number; showHidden: boolean; access?: (key: string) => { visible: boolean; locked: boolean } }) {
|
|
const initialScene = useMemo(() => sceneTable(initial), [initial]);
|
|
const scene = usePlacementScene(initialScene, { ...options, title: 'Texte', link: '/texte/bearbeiten', scope: 'text',
|
|
saveData: async table => sceneTable(await api_saveTexts({ ...table, rows: table.rows.map(({ beschreibung, graphics, ...row }) => row) })),
|
|
baseSlots: [], previewOptions: [{ value: 'standard', label: 'Texte' }], includeRow: row => !!row.text.trim(),
|
|
renderSync: (id, onDone, onClose) => <TextSyncDialog id={id} onClose={onClose} onDone={table => onDone(sceneTable(table))} />,
|
|
renderValue: (row, p) => <svg pointerEvents="none" x={p.x} y={p.y} width={p.width} height={p.height} viewBox={`0 0 ${p.width} ${p.height}`} overflow="hidden">
|
|
<rect width={p.width} height={p.height} fill={color(row.hintergrund)} />
|
|
<text fontFamily={row.schriftart} fontSize={row.schriftgroesse} fontWeight={/bold/i.test(row.schrifttyp) ? 'bold' : row.schrifttyp === 'Normal' ? 'normal' : undefined} fontStyle={/italic/i.test(row.schrifttyp) ? 'italic' : 'normal'} fill={color(row.textfarbe)}>
|
|
{row.text.split(/\r?\n/).map((line, i) => <tspan key={i} x={4} y={row.schriftgroesse + 3 + i * row.schriftgroesse * 1.2}>{line || ' '}</tspan>)}
|
|
</text>
|
|
</svg>,
|
|
});
|
|
return { ...scene, replace: (table: TextTable) => scene.replace(sceneTable(table)) };
|
|
}
|