File size: 4,322 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | "use client";
import { useRef, useState, type CSSProperties, type ReactNode } from "react";
/** Edit a text layer in its own bounds. Enter/blur commits; Escape cancels. */
export function InlineText({ value, label, buttonLabel = value, title, onCommit, onBegin, onEditing, onDraft, formatValue, editorStyle, onMoveStart, onMove, onNudge, className, style, children, multiline = false, maxLength }: {
value: string;
label: string;
buttonLabel?: string;
title?: string;
onCommit: (value: string) => void;
onBegin?: () => void;
onEditing?: (editing: boolean) => void;
onDraft?: (text: string | null) => void;
formatValue?: (text: string) => string;
editorStyle?: CSSProperties;
onMoveStart?: () => void;
onMove?: (dx: number, dy: number) => void;
onNudge?: (dx: number, dy: number) => void;
className?: string;
style?: CSSProperties;
children?: ReactNode;
multiline?: boolean;
maxLength?: number;
}) {
const [editing, setEditing] = useState(false);
const [text, setText] = useState(value);
const finished = useRef(false);
const drag = useRef<{ x: number; y: number; moved: boolean } | null>(null);
const dragged = useRef(false);
const finish = (commit: boolean) => {
if (finished.current) return;
finished.current = true;
if (commit) onCommit(text.trim().replace(/\s+/g, " "));
onDraft?.(null);
setEditing(false);
onEditing?.(false);
};
if (!editing) return (
<button type="button" aria-label={buttonLabel} title={title} className={`${className ?? ""} ${onMove ? "touch-none" : ""}`} style={style}
onKeyDown={event => {
if (!onNudge || !["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) return;
event.preventDefault();
event.stopPropagation();
const step = event.shiftKey ? 10 : 1;
onNudge(event.key === "ArrowLeft" ? -step : event.key === "ArrowRight" ? step : 0, event.key === "ArrowUp" ? -step : event.key === "ArrowDown" ? step : 0);
}}
onPointerDown={event => {
if (!onMove) return;
dragged.current = false;
drag.current = { x: event.clientX, y: event.clientY, moved: false };
onMoveStart?.();
event.currentTarget.setPointerCapture(event.pointerId);
}}
onPointerMove={event => {
const from = drag.current;
if (!from || !(event.buttons & 1)) return;
const dx = event.clientX - from.x, dy = event.clientY - from.y;
if (Math.hypot(dx, dy) > 3) from.moved = true;
if (from.moved) onMove?.(dx, dy);
}}
onPointerUp={() => { dragged.current = drag.current?.moved ?? false; drag.current = null; }}
onPointerCancel={() => { drag.current = null; }}
onClick={() => {
if (dragged.current) { dragged.current = false; return; }
finished.current = false;
const initial = formatValue?.(value) ?? value;
setText(initial);
setEditing(true);
onEditing?.(true);
onBegin?.();
onDraft?.(initial);
}}>{children}</button>
);
const props = {
autoFocus: true,
"aria-label": label,
value: text,
maxLength,
className,
style: { ...style, ...editorStyle },
spellCheck: false,
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const input = event.target, raw = input.value;
const formatted = (event.nativeEvent as InputEvent).isComposing ? raw : formatValue?.(raw) ?? raw;
const start = raw.slice(0, input.selectionStart ?? raw.length).replace(/\s+/g, " ").trimStart().length;
const end = raw.slice(0, input.selectionEnd ?? raw.length).replace(/\s+/g, " ").trimStart().length;
setText(formatted); onDraft?.(formatted);
if (formatted !== raw) requestAnimationFrame(() => {
if (document.activeElement === input) input.setSelectionRange(start, end);
});
},
onBlur: () => finish(true),
onKeyDown: (event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
event.stopPropagation();
if (event.nativeEvent.isComposing) return;
if (event.key === "Escape" || (event.key === "Enter" && !event.shiftKey)) {
event.preventDefault();
finish(event.key === "Enter");
}
},
};
return multiline ? <textarea {...props} /> : <input {...props} />;
}
|