(blockId: string)
| 13 | import { useBlockRegistrationContext } from "../components/BlockRegistration"; |
| 14 | |
| 15 | export function useTextInput(blockId: string) { |
| 16 | const { |
| 17 | blocks, |
| 18 | blocksOrder, |
| 19 | focusedBlockId, |
| 20 | setFocusedBlockId, |
| 21 | updateBlock, |
| 22 | mergeBlock, |
| 23 | splitBlock, |
| 24 | removeBlock, |
| 25 | getBlockSnapshot |
| 26 | } = useBlocksContext(); |
| 27 | const { |
| 28 | registerRef, |
| 29 | showSoftInputOnFocus, |
| 30 | inputRefs |
| 31 | } = useTextBlocksContext(); |
| 32 | const { isScrolling } = useScrollContext(); |
| 33 | const { isDragging } = useBlocksMeasuresContext(); |
| 34 | const { textBasedBlocks, defaultBlockType } = useBlockRegistrationContext(); |
| 35 | |
| 36 | const block = getBlockSnapshot(blockId); |
| 37 | const title = block.properties.title; |
| 38 | const inputRef = React.useRef<TextInput>(null); |
| 39 | const selectionRef = React.useRef({ start: title.length, end: title.length }); |
| 40 | const valueRef = React.useRef(title); |
| 41 | const isFocused = focusedBlockId === blockId; |
| 42 | const isEditable = isScrolling === false && isDragging.value === false |
| 43 | ? true |
| 44 | : focusedBlockId === blockId // Keep focusd block editable whn scrolling. |
| 45 | ? true |
| 46 | : false; |
| 47 | |
| 48 | const api = { |
| 49 | current: { |
| 50 | getText: () => valueRef.current, |
| 51 | setText: (text: string) => { |
| 52 | valueRef.current = text; |
| 53 | inputRef.current?.setNativeProps({ text: valueRef.current }); |
| 54 | }, |
| 55 | focus: () => { |
| 56 | inputRef.current?.focus(); |
| 57 | }, |
| 58 | blur: () => { |
| 59 | inputRef.current?.blur(); |
| 60 | }, |
| 61 | setSelection: (selection: { start: number; end: number }) => { |
| 62 | inputRef.current?.setSelection(selection.start, selection.end); |
| 63 | selectionRef.current = selection; |
| 64 | }, |
| 65 | focusWithSelection: (selection: { start: number; end: number }, text?: string) => { |
| 66 | /** Find a better way to sync value on block update */ |
| 67 | if (text !== undefined) { |
| 68 | inputRef.current?.setNativeProps({ text }); |
| 69 | /* valueRef.current = text; */ |
| 70 | } |
| 71 | |
| 72 | inputRef.current?.setSelection(selection.start, selection.end); // Sync native input with selection state |
no test coverage detected