|
| 1 | +import { prepareTextInput } from "./text-spans"; |
| 2 | +import type { InputNode } from "./types"; |
| 3 | + |
| 4 | +export type EditorState = { |
| 5 | + text: string; |
| 6 | + cursor: number; |
| 7 | + anchor: number | null; |
| 8 | +}; |
| 9 | + |
| 10 | +export type EditorCommand = |
| 11 | + | { type: "insertText"; text: string } |
| 12 | + | { type: "deleteBackward" } |
| 13 | + | { type: "insertLineBreak" }; |
| 14 | + |
| 15 | +export type EditorResult = { |
| 16 | + state: EditorState; |
| 17 | + changed: boolean; |
| 18 | + submit?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +const editorStateByNode = new WeakMap<InputNode, EditorState>(); |
| 22 | + |
| 23 | +function codepointLength(text: string): number { |
| 24 | + return Array.from(text).length; |
| 25 | +} |
| 26 | + |
| 27 | +function splitCodepoints(text: string): string[] { |
| 28 | + return Array.from(text); |
| 29 | +} |
| 30 | + |
| 31 | +function selectionRange( |
| 32 | + state: EditorState, |
| 33 | +): { start: number; end: number } | null { |
| 34 | + if (state.anchor === null || state.anchor === state.cursor) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + start: Math.min(state.anchor, state.cursor), |
| 40 | + end: Math.max(state.anchor, state.cursor), |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function createCollapsedState(text: string, cursor: number): EditorState { |
| 45 | + return { text, cursor, anchor: null }; |
| 46 | +} |
| 47 | + |
| 48 | +function replaceSelection( |
| 49 | + state: EditorState, |
| 50 | + nextText: string, |
| 51 | +): EditorState { |
| 52 | + const chars = splitCodepoints(state.text); |
| 53 | + const range = selectionRange(state); |
| 54 | + |
| 55 | + if (range === null) { |
| 56 | + const nextCursor = state.cursor + codepointLength(nextText); |
| 57 | + chars.splice(state.cursor, 0, ...splitCodepoints(nextText)); |
| 58 | + return createCollapsedState(chars.join(""), nextCursor); |
| 59 | + } |
| 60 | + |
| 61 | + chars.splice( |
| 62 | + range.start, |
| 63 | + range.end - range.start, |
| 64 | + ...splitCodepoints(nextText), |
| 65 | + ); |
| 66 | + return createCollapsedState( |
| 67 | + chars.join(""), |
| 68 | + range.start + codepointLength(nextText), |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +function deleteSelection(state: EditorState): EditorState { |
| 73 | + const range = selectionRange(state); |
| 74 | + if (range === null) { |
| 75 | + return state; |
| 76 | + } |
| 77 | + |
| 78 | + const chars = splitCodepoints(state.text); |
| 79 | + chars.splice(range.start, range.end - range.start); |
| 80 | + return createCollapsedState(chars.join(""), range.start); |
| 81 | +} |
| 82 | + |
| 83 | +function deletePreviousCodepoint(state: EditorState): EditorState { |
| 84 | + const selected = deleteSelection(state); |
| 85 | + if (selected !== state) { |
| 86 | + return selected; |
| 87 | + } |
| 88 | + |
| 89 | + if (state.cursor === 0) { |
| 90 | + return state; |
| 91 | + } |
| 92 | + |
| 93 | + const chars = splitCodepoints(state.text); |
| 94 | + chars.splice(state.cursor - 1, 1); |
| 95 | + return createCollapsedState(chars.join(""), state.cursor - 1); |
| 96 | +} |
| 97 | + |
| 98 | +export function createEditorState(text: string): EditorState { |
| 99 | + const normalized = prepareTextInput(text).text; |
| 100 | + return createCollapsedState(normalized, codepointLength(normalized)); |
| 101 | +} |
| 102 | + |
| 103 | +export function getInputEditorState(node: InputNode): EditorState { |
| 104 | + return editorStateByNode.get(node) ?? createEditorState(node.props.text()); |
| 105 | +} |
| 106 | + |
| 107 | +export function setInputEditorState( |
| 108 | + node: InputNode, |
| 109 | + state: EditorState, |
| 110 | +): void { |
| 111 | + editorStateByNode.set(node, state); |
| 112 | + node.props.text(state.text); |
| 113 | +} |
| 114 | + |
| 115 | +export function resetInputEditorText(node: InputNode, text: string): void { |
| 116 | + setInputEditorState(node, createEditorState(text)); |
| 117 | +} |
| 118 | + |
| 119 | +export function reduceEditor( |
| 120 | + state: EditorState, |
| 121 | + command: EditorCommand, |
| 122 | + opts: { multiline: boolean }, |
| 123 | +): EditorResult { |
| 124 | + switch (command.type) { |
| 125 | + case "insertText": { |
| 126 | + const nextState = replaceSelection(state, command.text); |
| 127 | + return { |
| 128 | + state: nextState, |
| 129 | + changed: nextState.text !== state.text, |
| 130 | + }; |
| 131 | + } |
| 132 | + case "deleteBackward": { |
| 133 | + const nextState = deletePreviousCodepoint(state); |
| 134 | + return { |
| 135 | + state: nextState, |
| 136 | + changed: nextState.text !== state.text, |
| 137 | + }; |
| 138 | + } |
| 139 | + case "insertLineBreak": { |
| 140 | + if (!opts.multiline) { |
| 141 | + return { |
| 142 | + state, |
| 143 | + changed: false, |
| 144 | + submit: state.text, |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + const nextState = replaceSelection(state, "\n"); |
| 149 | + return { |
| 150 | + state: nextState, |
| 151 | + changed: nextState.text !== state.text, |
| 152 | + }; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments