"use client"; import { useRef, useState } from "react"; import { useTranslations } from "next-intl"; import { clamp, textTransform, uid, type CaptionTransform, type EditDocument, type TextLayer } from "@/lib/editor/document"; import type { TextBounds, TextEditorLayout } from "@/lib/editor/render"; import { keepTextInFrame, resizeTextScale, TEXT_CORNERS, type TextCorner } from "@/lib/editor/text-transform"; import { InlineText } from "./inline-text"; export function TransformableText({ doc, layer, captionId, label, value, buttonLabel, bounds, width, height, frame, measure, pause, update, onDraft, editorLayout, formatValue, onCommit }: { doc: EditDocument; layer: TextLayer; captionId?: string; label: string; value: string; buttonLabel: string; bounds: TextBounds; width: number; height: number; frame: React.RefObject; measure: (doc: EditDocument) => TextBounds | null; pause: () => void; update: (change: (doc: EditDocument) => EditDocument, group?: string) => void; onDraft: (text: string | null) => void; editorLayout?: TextEditorLayout; formatValue?: (text: string) => string; onCommit: (text: string) => void; }) { const t = useTranslations("editor"); const [editing, setEditing] = useState(false); const drag = useRef<{ doc: EditDocument; bounds: TextBounds; group: string; sx: number; sy: number; corner?: TextCorner; x: number; y: number } | null>(null); const placement = (document: EditDocument): CaptionTransform => (captionId ? document.captionTransforms?.[captionId] : undefined) ?? textTransform(document, layer); const begin = (corner?: TextCorner, x = 0, y = 0) => { pause(); const rect = frame.current?.getBoundingClientRect(); if (!rect?.width || !rect.height) return; drag.current = { doc, bounds, group: `gesture:${uid()}`, sx: width / rect.width, sy: height / rect.height, corner, x, y }; }; const move = (dx: number, dy: number) => { const start = drag.current; if (!start) return; dx *= start.sx; dy *= start.sy; const original = placement(start.doc); let transform = { ...original, x: original.x + dx / width, y: original.y + dy / height }; if (start.corner) transform = { ...original, scale: resizeTextScale(start.bounds, start.corner, dx, dy, original.scale) }; const withTransform = (base: EditDocument): EditDocument => captionId ? { ...base, captionTransforms: { ...base.captionTransforms, [captionId]: { ...transform, size: transform.size ?? null } } } : { ...base, textTransforms: { ...base.textTransforms, [layer]: transform } }; let nextBounds = measure(withTransform(start.doc)); if (!nextBounds) return; if (start.corner) { for (let attempt = 0; attempt < 4 && (nextBounds.width > width - 16 || nextBounds.height > height - 16); attempt++) { transform.scale = clamp(transform.scale * Math.min((width - 16) / nextBounds.width, (height - 16) / nextBounds.height), 0.25, 4); nextBounds = measure(withTransform(start.doc))!; } // Keep the opposite corner fixed, including when larger captions wrap. const east = start.corner.endsWith("e"), south = start.corner.startsWith("s"); const x = start.bounds.x + (east ? 0 : start.bounds.width - nextBounds.width); const y = start.bounds.y + (south ? 0 : start.bounds.height - nextBounds.height); transform.x += (x - nextBounds.x) / width; transform.y += (y - nextBounds.y) / height; nextBounds = { ...nextBounds, x, y }; } transform = keepTextInFrame(transform, nextBounds, width, height); update(current => withTransform(current), start.group); }; const scale = frame.current ? frame.current.getBoundingClientRect().width / width : 0.3; return
begin()} onMove={move} onNudge={(dx, dy) => { begin(); move(dx, dy); }} /> {!editing && TEXT_CORNERS.map(corner =>
; }