forked from 777genius/claude-code-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseInputBuffer.ts
More file actions
133 lines (111 loc) · 3.31 KB
/
Copy pathuseInputBuffer.ts
File metadata and controls
133 lines (111 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useCallback, useRef, useState } from 'react'
import type { PastedContent } from '../utils/config.js'
export type BufferEntry = {
text: string
cursorOffset: number
pastedContents: Record<number, PastedContent>
timestamp: number
}
export type UseInputBufferProps = {
maxBufferSize: number
debounceMs: number
}
export type UseInputBufferResult = {
pushToBuffer: (
text: string,
cursorOffset: number,
pastedContents?: Record<number, PastedContent>,
) => void
undo: () => BufferEntry | undefined
canUndo: boolean
clearBuffer: () => void
}
export function useInputBuffer({
maxBufferSize,
debounceMs,
}: UseInputBufferProps): UseInputBufferResult {
const [buffer, setBuffer] = useState<BufferEntry[]>([])
const [currentIndex, setCurrentIndex] = useState(-1)
const lastPushTime = useRef<number>(0)
const pendingPush = useRef<ReturnType<typeof setTimeout> | null>(null)
const pushToBuffer = useCallback(
(
text: string,
cursorOffset: number,
pastedContents: Record<number, PastedContent> = {},
) => {
const now = Date.now()
// Clear any pending push
if (pendingPush.current) {
clearTimeout(pendingPush.current)
pendingPush.current = null
}
// Debounce rapid changes
if (now - lastPushTime.current < debounceMs) {
pendingPush.current = setTimeout(
pushToBuffer,
debounceMs,
text,
cursorOffset,
pastedContents,
)
return
}
lastPushTime.current = now
setBuffer(prevBuffer => {
// If we're not at the end of the buffer, truncate everything after current position
const newBuffer =
currentIndex >= 0 ? prevBuffer.slice(0, currentIndex + 1) : prevBuffer
// Don't add if it's the same as the last entry
const lastEntry = newBuffer[newBuffer.length - 1]
if (lastEntry && lastEntry.text === text) {
return newBuffer
}
// Add new entry
const updatedBuffer = [
...newBuffer,
{ text, cursorOffset, pastedContents, timestamp: now },
]
// Limit buffer size
if (updatedBuffer.length > maxBufferSize) {
return updatedBuffer.slice(-maxBufferSize)
}
return updatedBuffer
})
// Update current index to point to the new entry
setCurrentIndex(prev => {
const newIndex = prev >= 0 ? prev + 1 : buffer.length
return Math.min(newIndex, maxBufferSize - 1)
})
},
[debounceMs, maxBufferSize, currentIndex, buffer.length],
)
const undo = useCallback((): BufferEntry | undefined => {
if (currentIndex < 0 || buffer.length === 0) {
return undefined
}
const targetIndex = Math.max(0, currentIndex - 1)
const entry = buffer[targetIndex]
if (entry) {
setCurrentIndex(targetIndex)
return entry
}
return undefined
}, [buffer, currentIndex])
const clearBuffer = useCallback(() => {
setBuffer([])
setCurrentIndex(-1)
lastPushTime.current = 0
if (pendingPush.current) {
clearTimeout(pendingPush.current)
pendingPush.current = null
}
}, [lastPushTime, pendingPush])
const canUndo = currentIndex > 0 && buffer.length > 1
return {
pushToBuffer,
undo,
canUndo,
clearBuffer,
}
}