UNPKG

tldraw

Version:

A tiny little drawing editor.

106 lines (105 loc) 2.92 kB
import { getPointerInfo, noop, stopEventPropagation, tlenv, useEditor, useValue } from "@tldraw/editor"; import { useCallback, useEffect, useRef } from "react"; import { INDENT, TextHelpers } from "./TextHelpers.mjs"; function useEditableText(shapeId, type, text) { const editor = useEditor(); const rInput = useRef(null); const isEditing = useValue("isEditing", () => editor.getEditingShapeId() === shapeId, [editor]); const isEditingAnything = useValue("isEditingAnything", () => !!editor.getEditingShapeId(), [ editor ]); useEffect(() => { function selectAllIfEditing(event) { if (event.shapeId === shapeId) { rInput.current?.select(); } } editor.on("select-all-text", selectAllIfEditing); return () => { editor.off("select-all-text", selectAllIfEditing); }; }, [editor, shapeId, isEditing]); useEffect(() => { if (!isEditing) return; if (document.activeElement !== rInput.current) { rInput.current?.focus(); } if (editor.getInstanceState().isCoarsePointer) { rInput.current?.select(); } if (tlenv.isSafari) { rInput.current?.blur(); rInput.current?.focus(); } }, [editor, isEditing]); const handleKeyDown = useCallback( (e) => { if (editor.getEditingShapeId() !== shapeId) return; switch (e.key) { case "Enter": { if (e.ctrlKey || e.metaKey) { editor.complete(); } break; } } }, [editor, shapeId] ); const handleChange = useCallback( (e) => { if (editor.getEditingShapeId() !== shapeId) return; let text2 = TextHelpers.normalizeText(e.currentTarget.value); const untabbedText = text2.replace(/\t/g, INDENT); if (untabbedText !== text2) { const selectionStart = e.currentTarget.selectionStart; e.currentTarget.value = untabbedText; e.currentTarget.selectionStart = selectionStart + (untabbedText.length - text2.length); e.currentTarget.selectionEnd = selectionStart + (untabbedText.length - text2.length); text2 = untabbedText; } editor.updateShape({ id: shapeId, type, props: { text: text2 } }); }, [editor, shapeId, type] ); const handleInputPointerDown = useCallback( (e) => { editor.dispatch({ ...getPointerInfo(e), type: "pointer", name: "pointer_down", target: "shape", shape: editor.getShape(shapeId) }); stopEventPropagation(e); }, [editor, shapeId] ); return { rInput, handleFocus: noop, handleBlur: noop, handleKeyDown, handleChange, handleInputPointerDown, handleDoubleClick: stopEventPropagation, isEmpty: text.trim().length === 0, isEditing, isEditingAnything }; } export { useEditableText }; //# sourceMappingURL=useEditableText.mjs.map