/** * ============================================================ * src/ui/hooks/useTextBuffer.ts —— 输入框文本缓冲(React 包装) * ------------------------------------------------------------ * 把 textBuffer.ts 里的纯函数包成 React Hook:useState 持有 BufferState, * 暴露一组 callback 给 InputBox 调用。callback 用 setState(prev => ...) * 形式,避免 useInput handler 闭包看到旧 state 的问题。 * ============================================================ */ import { useCallback, useMemo, useRef, useState, type SetStateAction } from 'react'; import { bufClear, bufDeleteBefore, bufDeleteWordBefore, bufInsert, bufKillToLineEnd, bufKillToLineStart, bufMoveBufferEnd, bufMoveBufferStart, bufMoveDown, bufMoveLeft, bufMoveLineEnd, bufMoveLineStart, bufMoveRight, bufMoveUp, bufMoveWordLeft, bufMoveWordRight, bufSetText, EMPTY_BUFFER, layoutBuffer, type BufferLayout, type BufferState, } from '../textBuffer.js'; export interface UseTextBufferResult { state: BufferState; layout: BufferLayout; // 插入 / 重置 insert: (s: string) => void; setText: (s: string) => void; clear: () => void; // 删除 deleteBefore: () => void; deleteWordBefore: () => void; killToLineStart: () => void; killToLineEnd: () => void; // 移动 moveLeft: () => void; moveRight: () => void; moveWordLeft: () => void; moveWordRight: () => void; moveLineStart: () => void; moveLineEnd: () => void; moveBufferStart: () => void; moveBufferEnd: () => void; moveUp: () => void; moveDown: () => void; } export interface DraftStore { current: BufferState } export function useTextBuffer(draft?: DraftStore): UseTextBufferResult { const local = useRef(draft?.current ?? EMPTY_BUFFER); const [state, renderState] = useState(local.current); const setState = useCallback((update: SetStateAction) => { const next = typeof update === 'function' ? update(local.current) : update; local.current = next; if (draft) draft.current = next; renderState(next); }, [draft]); const insert = useCallback((s: string) => setState((p) => bufInsert(p, s)), []); const setText = useCallback((s: string) => setState(bufSetText(s)), []); const clear = useCallback(() => setState(bufClear()), []); const deleteBefore = useCallback(() => setState(bufDeleteBefore), []); const deleteWordBefore = useCallback(() => setState(bufDeleteWordBefore), []); const killToLineStart = useCallback(() => setState(bufKillToLineStart), []); const killToLineEnd = useCallback(() => setState(bufKillToLineEnd), []); const moveLeft = useCallback(() => setState(bufMoveLeft), []); const moveRight = useCallback(() => setState(bufMoveRight), []); const moveWordLeft = useCallback(() => setState(bufMoveWordLeft), []); const moveWordRight = useCallback(() => setState(bufMoveWordRight), []); const moveLineStart = useCallback(() => setState(bufMoveLineStart), []); const moveLineEnd = useCallback(() => setState(bufMoveLineEnd), []); const moveBufferStart = useCallback(() => setState(bufMoveBufferStart), []); const moveBufferEnd = useCallback(() => setState(bufMoveBufferEnd), []); const moveUp = useCallback(() => setState(bufMoveUp), []); const moveDown = useCallback(() => setState(bufMoveDown), []); const layout = useMemo(() => layoutBuffer(state), [state]); return { state, layout, insert, setText, clear, deleteBefore, deleteWordBefore, killToLineStart, killToLineEnd, moveLeft, moveRight, moveWordLeft, moveWordRight, moveLineStart, moveLineEnd, moveBufferStart, moveBufferEnd, moveUp, moveDown, }; }