70160
Ce
cel-tui / files
cel-tui / packages / core / src / text-edit.ts
6.7 KB
1import { layoutText } from "./text-layout.js"; 2 3/** 4 * Framework-managed editing state for TextInput. 5 */ 6export interface EditState { 7 /** Current text value. */ 8 value: string; 9 /** Cursor offset in the string (0 = before first char). */ 10 cursor: number; 11} 12 13const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }); 14const WHITESPACE_RE = /^\s+$/u; 15 16/** Clamp a UTF-16 offset backward to a valid grapheme boundary. */ 17export function clampCursorToGraphemeBoundary( 18 value: string, 19 cursor: number, 20): number { 21 const target = Number.isFinite(cursor) ? Math.trunc(cursor) : 0; 22 if (target <= 0) return 0; 23 if (target >= value.length) return value.length; 24 25 for (const { index, segment } of segmenter.segment(value)) { 26 const end = index + segment.length; 27 if (target <= index) return index; 28 if (target < end) return index; 29 if (target === end) return end; 30 } 31 return value.length; 32} 33 34/** 35 * Get the grapheme boundary before the given cursor position. 36 * Returns the start offset of the grapheme that contains or precedes 37 * the cursor, or 0 if at the beginning. 38 */ 39function prevGraphemeBoundary(value: string, cursor: number): number { 40 if (cursor <= 0) return 0; 41 let lastStart = 0; 42 for (const { index, segment } of segmenter.segment(value)) { 43 const end = index + segment.length; 44 if (end >= cursor) return index; 45 lastStart = end; 46 } 47 return lastStart; 48} 49 50/** 51 * Get the grapheme boundary after the given cursor position. 52 * Returns the end offset of the grapheme that starts at or after 53 * the cursor, or value.length if at the end. 54 */ 55function nextGraphemeBoundary(value: string, cursor: number): number { 56 for (const { index, segment } of segmenter.segment(value)) { 57 const end = index + segment.length; 58 if (index >= cursor) return end; 59 } 60 return value.length; 61} 62 63function isWhitespaceGrapheme(segment: string): boolean { 64 return WHITESPACE_RE.test(segment); 65} 66 67function findWordBoundaryBackward(value: string, cursor: number): number { 68 let boundary = cursor; 69 70 while (boundary > 0) { 71 const prev = prevGraphemeBoundary(value, boundary); 72 if (!isWhitespaceGrapheme(value.slice(prev, boundary))) break; 73 boundary = prev; 74 } 75 76 while (boundary > 0) { 77 const prev = prevGraphemeBoundary(value, boundary); 78 if (isWhitespaceGrapheme(value.slice(prev, boundary))) break; 79 boundary = prev; 80 } 81 82 return boundary; 83} 84 85function findWordBoundaryForward(value: string, cursor: number): number { 86 let boundary = cursor; 87 88 while (boundary < value.length) { 89 const next = nextGraphemeBoundary(value, boundary); 90 if (!isWhitespaceGrapheme(value.slice(boundary, next))) break; 91 boundary = next; 92 } 93 94 while (boundary < value.length) { 95 const next = nextGraphemeBoundary(value, boundary); 96 if (isWhitespaceGrapheme(value.slice(boundary, next))) break; 97 boundary = next; 98 } 99 100 return boundary; 101} 102 103/** 104 * Insert a character (or string) at the cursor position. 105 */ 106export function insertChar(state: EditState, char: string): EditState { 107 const { value, cursor } = state; 108 return { 109 value: value.slice(0, cursor) + char + value.slice(cursor), 110 cursor: cursor + char.length, 111 }; 112} 113 114/** 115 * Delete the grapheme before the cursor (Backspace). 116 * Handles multi-codepoint characters (emoji, ZWJ sequences, combining marks). 117 */ 118export function deleteBackward(state: EditState): EditState { 119 const { value, cursor } = state; 120 if (cursor === 0) return state; 121 const boundary = prevGraphemeBoundary(value, cursor); 122 return { 123 value: value.slice(0, boundary) + value.slice(cursor), 124 cursor: boundary, 125 }; 126} 127 128/** 129 * Delete the grapheme after the cursor (Delete key). 130 * Handles multi-codepoint characters (emoji, ZWJ sequences, combining marks). 131 */ 132export function deleteForward(state: EditState): EditState { 133 const { value, cursor } = state; 134 if (cursor >= value.length) return state; 135 const boundary = nextGraphemeBoundary(value, cursor); 136 return { 137 value: value.slice(0, cursor) + value.slice(boundary), 138 cursor, 139 }; 140} 141 142/** 143 * Delete the whitespace-delimited word before the cursor. 144 */ 145export function deleteWordBackward(state: EditState): EditState { 146 const { value, cursor } = state; 147 const boundary = findWordBoundaryBackward(value, cursor); 148 if (boundary === cursor) return state; 149 return { 150 value: value.slice(0, boundary) + value.slice(cursor), 151 cursor: boundary, 152 }; 153} 154 155/** 156 * Delete the whitespace-delimited word after the cursor. 157 */ 158export function deleteWordForward(state: EditState): EditState { 159 const { value, cursor } = state; 160 const boundary = findWordBoundaryForward(value, cursor); 161 if (boundary === cursor) return state; 162 return { 163 value: value.slice(0, cursor) + value.slice(boundary), 164 cursor, 165 }; 166} 167 168/** 169 * Move the cursor in the given direction. 170 * Left/right movement respects grapheme boundaries. 171 * 172 * @param state - Current edit state. 173 * @param direction - Movement direction. 174 * @param width - Available width (for up/down line navigation). 175 */ 176export function moveCursor( 177 state: EditState, 178 direction: "left" | "right" | "up" | "down" | "home" | "end", 179 width?: number, 180): EditState { 181 const { value, cursor } = state; 182 183 switch (direction) { 184 case "left": { 185 if (cursor <= 0) return state; 186 const boundary = prevGraphemeBoundary(value, cursor); 187 return { value, cursor: boundary }; 188 } 189 case "right": { 190 if (cursor >= value.length) return state; 191 const boundary = nextGraphemeBoundary(value, cursor); 192 return { value, cursor: boundary }; 193 } 194 case "home": 195 return { value, cursor: 0 }; 196 case "end": 197 return { value, cursor: value.length }; 198 case "up": 199 case "down": 200 return moveVertical(state, direction, width ?? 80); 201 } 202} 203 204/** 205 * Move the cursor by one whitespace-delimited word. 206 */ 207export function moveCursorByWord( 208 state: EditState, 209 direction: "backward" | "forward", 210): EditState { 211 const { value, cursor } = state; 212 const nextCursor = 213 direction === "backward" 214 ? findWordBoundaryBackward(value, cursor) 215 : findWordBoundaryForward(value, cursor); 216 217 if (nextCursor === cursor) return state; 218 return { value, cursor: nextCursor }; 219} 220 221/** 222 * Vertical cursor movement follows the visual wrapped lines used for painting. 223 */ 224function moveVertical( 225 state: EditState, 226 direction: "up" | "down", 227 width: number, 228): EditState { 229 const { value, cursor } = state; 230 const textLayout = layoutText(value, width, "word"); 231 const pos = textLayout.offsetToPosition(cursor); 232 const targetLine = 233 direction === "up" 234 ? Math.max(0, pos.line - 1) 235 : Math.min(textLayout.lineCount - 1, pos.line + 1); 236 237 if (targetLine === pos.line) { 238 return { 239 value, 240 cursor: direction === "up" ? 0 : value.length, 241 }; 242 } 243 244 return { 245 value, 246 cursor: textLayout.positionToOffset(targetLine, pos.col), 247 }; 248}