@kabmo-labs/kabmo 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -0
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +333 -333
- package/dist/client/static/js/index-18xe7655.css +1 -0
- package/dist/client/static/js/{index-bpptfkz2.js → index-nxpe7835.js} +768 -763
- package/package.json +3 -1
- package/src/client/components/common/code-surface/bracket-guide.test.ts +249 -0
- package/src/client/components/common/code-surface/bracket-guide.ts +131 -0
- package/src/client/components/common/code-surface/documents.test.ts +5 -3
- package/src/client/components/common/code-surface/documents.ts +85 -0
- package/src/client/components/common/code-surface/extensions.test.ts +5 -0
- package/src/client/components/common/code-surface/extensions.ts +140 -7
- package/src/client/components/common/code-surface/fold-stack.test.ts +218 -0
- package/src/client/components/common/code-surface/fold.test.ts +148 -0
- package/src/client/components/common/code-surface/fold.ts +118 -0
- package/src/client/components/common/code-surface/indent-guide.ts +104 -0
- package/src/client/components/common/code-surface/indent.test.ts +239 -0
- package/src/client/components/common/code-surface/indent.ts +124 -0
- package/src/client/components/common/code-surface/index.tsx +76 -46
- package/src/client/components/common/code-surface/keymap.ts +12 -23
- package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
- package/src/client/components/common/code-surface/line-commands.ts +103 -0
- package/src/client/components/common/code-surface/props.ts +87 -0
- package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
- package/src/client/components/common/code-surface/stream-language.ts +123 -0
- package/src/client/components/common/code-surface/theme.test.ts +40 -5
- package/src/client/components/common/code-surface/theme.ts +113 -0
- package/src/client/components/common/code-surface/typing.test.ts +97 -0
- package/src/client/components/common/code-surface/typing.ts +111 -0
- package/src/client/components/common/toolbar-row/index.test.tsx +53 -4
- package/src/client/components/common/toolbar-row/index.tsx +101 -30
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +17 -0
- package/src/client/components/layout/desktop-layout/index.tsx +18 -0
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +35 -33
- package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
- package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
- package/src/client/components/settings/categories/AppearanceSettings.tsx +6 -0
- package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
- package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
- package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
- package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +17 -3
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +10 -6
- package/src/client/components/workspace/editor/Body.tsx +24 -0
- package/src/client/components/workspace/editor/CommandPalette.tsx +4 -4
- package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
- package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
- package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
- package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
- package/src/client/components/workspace/editor/FileDocument.tsx +89 -1
- package/src/client/components/workspace/editor/FilePane.tsx +43 -0
- package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
- package/src/client/components/workspace/editor/FindWidget.tsx +29 -0
- package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
- package/src/client/components/workspace/editor/Notices.tsx +71 -0
- package/src/client/components/workspace/editor/Pane.tsx +118 -0
- package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
- package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
- package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
- package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
- package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
- package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
- package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
- package/src/client/components/workspace/editor/Tabs.tsx +195 -18
- package/src/client/components/workspace/editor/Toolbar.tsx +54 -181
- package/src/client/components/workspace/editor/index.tsx +199 -189
- package/src/client/components/workspace/editor/props.ts +64 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
- package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
- package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
- package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
- package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
- package/src/client/components/workspace/file-explorer/index.tsx +10 -48
- package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
- package/src/client/components/workspace/search-panel/Body.tsx +6 -1
- package/src/client/hooks/chat/composer/file-drop.ts +22 -37
- package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
- package/src/client/hooks/chat/composer/pipeline.ts +12 -1
- package/src/client/hooks/editor/caret-position.test.ts +58 -0
- package/src/client/hooks/editor/caret-position.ts +48 -0
- package/src/client/hooks/editor/file-editor-types.ts +200 -0
- package/src/client/hooks/editor/find-history.test.ts +132 -0
- package/src/client/hooks/editor/find-history.ts +46 -0
- package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
- package/src/client/hooks/editor/find-selection-range.ts +49 -0
- package/src/client/hooks/editor/find-state.ts +13 -0
- package/src/client/hooks/editor/font.test.ts +0 -93
- package/src/client/hooks/editor/indent-settings.test.ts +104 -0
- package/src/client/hooks/editor/indent-settings.ts +88 -0
- package/src/client/hooks/editor/quick-open.test.ts +141 -0
- package/src/client/hooks/editor/quick-open.ts +127 -0
- package/src/client/hooks/editor/save-as.test.ts +156 -0
- package/src/client/hooks/editor/save-as.ts +122 -0
- package/src/client/hooks/editor/save-file.test.ts +179 -0
- package/src/client/hooks/editor/save-file.ts +73 -28
- package/src/client/hooks/editor/typing-settings.test.ts +73 -0
- package/src/client/hooks/editor/typing-settings.ts +65 -0
- package/src/client/hooks/editor/use-close-guard.ts +98 -0
- package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
- package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
- package/src/client/hooks/editor/use-editor-drop.ts +58 -68
- package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
- package/src/client/hooks/editor/use-editor-find.ts +38 -3
- package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
- package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
- package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
- package/src/client/hooks/editor/use-external-change.test.ts +88 -0
- package/src/client/hooks/editor/use-external-change.ts +146 -0
- package/src/client/hooks/editor/use-file-editor.ts +83 -116
- package/src/client/hooks/editor/use-file-read.ts +16 -8
- package/src/client/hooks/editor/use-file-writes.ts +28 -3
- package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
- package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
- package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
- package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
- package/src/client/hooks/editor/use-split-editor.ts +91 -0
- package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
- package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
- package/src/client/hooks/editor/use-write-conflict.ts +152 -0
- package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
- package/src/client/hooks/editor/wheel-zoom.ts +96 -0
- package/src/client/hooks/ui/drag-presence.test.ts +193 -0
- package/src/client/hooks/ui/drag-presence.ts +108 -0
- package/src/client/hooks/ui/settings-modal.test.ts +115 -0
- package/src/client/hooks/workspace/file-listing.ts +36 -24
- package/src/client/hooks/workspace/file-tabs.test.ts +90 -0
- package/src/client/hooks/workspace/file-tabs.ts +46 -0
- package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
- package/src/client/lib/desktop/dropped-paths.ts +83 -0
- package/src/client/lib/editor/tab-menu.test.ts +127 -0
- package/src/client/lib/editor/tab-menu.ts +127 -0
- package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
- package/src/client/lib/editor-dirty-tabs.ts +103 -0
- package/src/client/lib/native/resume.test.ts +66 -0
- package/src/client/lib/native/resume.ts +42 -0
- package/src/client/main.tsx +8 -0
- package/src/client/tailwind.css +38 -0
- package/src/server/lib/bundler/css.test.ts +31 -0
- package/src/server/lib/fs/listing.test.ts +81 -16
- package/src/server/lib/fs/listing.ts +72 -5
- package/src/server/lib/fs/write-conflict.test.ts +68 -0
- package/src/server/lib/fs/write-conflict.ts +84 -0
- package/src/server/routes/fs/action-save-as.test.ts +82 -0
- package/src/server/routes/fs/action.test.ts +82 -0
- package/src/server/routes/fs/action.ts +54 -1
- package/src/server/routes/fs/exists.ts +11 -1
- package/src/server/routes/fs/read.ts +6 -1
- package/src/shared/lib/code/editor/find-replace.test.ts +1 -1
- package/src/shared/lib/code/editor/find-scan.test.ts +1 -1
- package/src/shared/lib/code/editor/find-scan.ts +22 -1
- package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
- package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
- package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
- package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
- package/src/shared/lib/code/editor/indent-rules.ts +198 -0
- package/src/shared/lib/code/editor/keymap.test.ts +23 -0
- package/src/shared/lib/code/editor/keymap.ts +40 -3
- package/src/shared/lib/code/editor/palette.test.ts +25 -2
- package/src/shared/lib/code/editor/palette.ts +10 -0
- package/src/shared/lib/code/editor/stream-modes.ts +225 -0
- package/src/shared/lib/code/syntax-highlight.test.ts +1 -1
- package/src/shared/lib/fs/file-tree.test.ts +41 -0
- package/src/shared/lib/fs/file-tree.ts +126 -34
- package/src/shared/lib/fs/folder-chain.test.ts +87 -0
- package/src/shared/lib/fs/folder-chain.ts +91 -0
- package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
- package/src/shared/lib/realtime/connection.test.ts +5 -1
- package/src/shared/lib/realtime/connection.ts +25 -119
- package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
- package/src/shared/lib/realtime/resume.ts +223 -0
- package/src/shared/lib/realtime/state.ts +22 -0
- package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
- package/src/shared/lib/realtime/watchdog.test.ts +99 -0
- package/src/shared/lib/realtime/watchdog.ts +72 -0
- package/src/shared/lib/workspace/split-editor.test.ts +104 -0
- package/src/shared/lib/workspace/split-editor.ts +129 -0
- package/src/test-support/file-explorer-disk.test-util.ts +30 -3
- package/src/test-support/pristine-globals.ts +7 -0
- package/dist/client/static/js/index-k315p1e8.css +0 -1
|
@@ -60,6 +60,7 @@ afterEach(() => {
|
|
|
60
60
|
container?.remove();
|
|
61
61
|
globalThis.fetch = nativeFetch;
|
|
62
62
|
sent.length = 0;
|
|
63
|
+
sentHooks = 0;
|
|
63
64
|
current = [];
|
|
64
65
|
value = '';
|
|
65
66
|
rootPath = null;
|
|
@@ -98,6 +99,8 @@ let current: Attachment[] = [];
|
|
|
98
99
|
let seed: (next: Attachment[]) => void;
|
|
99
100
|
/** Every dispatch, so a test can assert what was NOT sent. */
|
|
100
101
|
const sent: Array<{ attachments: Attachment[] }> = [];
|
|
102
|
+
/** Every `onSent`, so a test can assert the collapse followed a real dispatch. */
|
|
103
|
+
let sentHooks = 0;
|
|
101
104
|
let value = '';
|
|
102
105
|
let rootPath: string | null = null;
|
|
103
106
|
|
|
@@ -112,6 +115,7 @@ function PipelineProbe() {
|
|
|
112
115
|
disabled: false,
|
|
113
116
|
rootPath,
|
|
114
117
|
onSend: (attachmentsOut) => sent.push({ attachments: attachmentsOut }),
|
|
118
|
+
onSent: () => { sentHooks += 1; },
|
|
115
119
|
});
|
|
116
120
|
return null;
|
|
117
121
|
}
|
|
@@ -121,6 +125,15 @@ async function mountPipeline() {
|
|
|
121
125
|
await act(async () => { render(h(PipelineProbe, {}), container); });
|
|
122
126
|
}
|
|
123
127
|
|
|
128
|
+
/** A `DataTransfer` carrying a `text/uri-list` reference, as a Finder drag does. */
|
|
129
|
+
function fileTransfer(reference: string): DataTransfer {
|
|
130
|
+
return {
|
|
131
|
+
types: ['text/uri-list'],
|
|
132
|
+
files: [],
|
|
133
|
+
getData: (type: string) => (type.toLowerCase() === 'text/uri-list' ? reference : ''),
|
|
134
|
+
} as unknown as DataTransfer;
|
|
135
|
+
}
|
|
136
|
+
|
|
124
137
|
function textFile(name: string, body: string): File {
|
|
125
138
|
return new File([body], name, { type: 'text/markdown' });
|
|
126
139
|
}
|
|
@@ -189,6 +202,38 @@ describe('useComposerPipeline', () => {
|
|
|
189
202
|
expect(sent[0].attachments.map((a) => a.id)).toEqual(['i']);
|
|
190
203
|
});
|
|
191
204
|
|
|
205
|
+
test('a dispatched send reports onSent, which is what collapses the stretch', async () => {
|
|
206
|
+
await mountPipeline();
|
|
207
|
+
value = 'collapse me';
|
|
208
|
+
await act(async () => { render(h(PipelineProbe, {}), container); });
|
|
209
|
+
await act(async () => { api.submit(); });
|
|
210
|
+
await waitFor(() => sent.length === 1);
|
|
211
|
+
// `onSent` runs immediately BEFORE the dispatch, so the composer is already
|
|
212
|
+
// docked when the optimistic row lands.
|
|
213
|
+
expect(sentHooks).toBe(1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('a refused send reports nothing, so a stretched composer stays stretched', async () => {
|
|
217
|
+
// The desktop bug this pins: the collapse used to happen in the click
|
|
218
|
+
// handler, so a press that dispatched NOTHING (here: an attachment whose
|
|
219
|
+
// read produced no body) shrank the box over content that was never sent.
|
|
220
|
+
await mountPipeline();
|
|
221
|
+
await act(async () => { api.acceptFiles([new File([], 'empty.md', { type: 'text/markdown' })]); });
|
|
222
|
+
await waitFor(() => api.notice !== null);
|
|
223
|
+
await act(async () => { api.submit(); });
|
|
224
|
+
await settle();
|
|
225
|
+
|
|
226
|
+
expect(sent).toEqual([]);
|
|
227
|
+
expect(sentHooks).toBe(0);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('an empty composer reports nothing either', async () => {
|
|
231
|
+
await mountPipeline();
|
|
232
|
+
await act(async () => { api.submit(); });
|
|
233
|
+
await settle();
|
|
234
|
+
expect(sentHooks).toBe(0);
|
|
235
|
+
});
|
|
236
|
+
|
|
192
237
|
test('a second click during the send does not dispatch the batch twice', async () => {
|
|
193
238
|
await mountPipeline();
|
|
194
239
|
await act(async () => {
|
|
@@ -257,4 +302,35 @@ describe('useComposerPipeline', () => {
|
|
|
257
302
|
expect(api.isDragging).toBe(false);
|
|
258
303
|
expect(typeof api.dropProps.onDrop).toBe('function');
|
|
259
304
|
});
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The overlay's state. The counter this replaced drifted upward on every child
|
|
308
|
+
* crossing, so the card stayed lit after the pointer had left it.
|
|
309
|
+
*/
|
|
310
|
+
test('child crossings keep the drop overlay lit, and one real exit clears it', async () => {
|
|
311
|
+
await mountPipeline();
|
|
312
|
+
const card = document.body.appendChild(document.createElement('div'));
|
|
313
|
+
const child = card.appendChild(document.createElement('div'));
|
|
314
|
+
const transfer = fileTransfer('file:///tmp/note.txt');
|
|
315
|
+
const event = (related?: Node | null) => {
|
|
316
|
+
const ev = new window.Event('dragover', { bubbles: true, cancelable: true });
|
|
317
|
+
Object.defineProperty(ev, 'dataTransfer', { value: transfer });
|
|
318
|
+
Object.defineProperty(ev, 'currentTarget', { value: card });
|
|
319
|
+
Object.defineProperty(ev, 'relatedTarget', { value: related ?? null });
|
|
320
|
+
return ev as never;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
await act(async () => { api.dropProps.onDragEnter(event()); });
|
|
324
|
+
expect(api.isDragging).toBe(true);
|
|
325
|
+
|
|
326
|
+
for (let pass = 0; pass < 5; pass += 1) {
|
|
327
|
+
await act(async () => { api.dropProps.onDragOver(event()); });
|
|
328
|
+
await act(async () => { api.dropProps.onDragLeave(event(child)); });
|
|
329
|
+
}
|
|
330
|
+
expect(api.isDragging).toBe(true);
|
|
331
|
+
|
|
332
|
+
await act(async () => { api.dropProps.onDragLeave(event(null)); });
|
|
333
|
+
expect(api.isDragging).toBe(false);
|
|
334
|
+
card.remove();
|
|
335
|
+
});
|
|
260
336
|
});
|
|
@@ -49,6 +49,15 @@ export interface UseComposerPipelineOptions {
|
|
|
49
49
|
/** Workspace root, used to resolve a dropped path reference. */
|
|
50
50
|
rootPath?: string | null;
|
|
51
51
|
onSend: (attachments: Attachment[], options?: { steering?: boolean }) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Runs when a send is actually DISPATCHED — after every guard, immediately
|
|
54
|
+
* before `onSend`. The composer returns to its docked size from here rather
|
|
55
|
+
* than from its own click handler, so the collapse follows the SEND: a click
|
|
56
|
+
* that dispatched nothing (an empty draft, an unreadable attachment) leaves
|
|
57
|
+
* the stretched box alone, and a send that arrives any other way — Enter,
|
|
58
|
+
* Cmd+Enter, a queued follow-up, a plugin — collapses it too.
|
|
59
|
+
*/
|
|
60
|
+
onSent?: () => void;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
export interface ComposerPipeline {
|
|
@@ -74,6 +83,7 @@ export function useComposerPipeline({
|
|
|
74
83
|
disabled,
|
|
75
84
|
rootPath,
|
|
76
85
|
onSend,
|
|
86
|
+
onSent,
|
|
77
87
|
}: UseComposerPipelineOptions): ComposerPipeline {
|
|
78
88
|
const { addFiles, removeAttachment, readyForSend, peek, clear } = useComposerAttachments({ attachments, setAttachments });
|
|
79
89
|
// Guards a double click: the composer is cleared only after the reads land, so
|
|
@@ -165,11 +175,12 @@ export function useComposerPipeline({
|
|
|
165
175
|
setNotice(describeUnreadable(outgoing.map((a) => attachmentName(a))));
|
|
166
176
|
return;
|
|
167
177
|
}
|
|
178
|
+
onSent?.();
|
|
168
179
|
onSend(outgoing, options);
|
|
169
180
|
}).finally(() => {
|
|
170
181
|
sendingRef.current = false;
|
|
171
182
|
});
|
|
172
|
-
}, [clear, onSend, peek, readyForSend, value]);
|
|
183
|
+
}, [clear, onSend, onSent, peek, readyForSend, value]);
|
|
173
184
|
|
|
174
185
|
return {
|
|
175
186
|
notice,
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The caret readout's arithmetic.
|
|
8
|
+
*
|
|
9
|
+
* Small, and every branch is a number the user reads: `Ln 1, Col 1` at the start of
|
|
10
|
+
* the file, a column that counts a tab as one character (which is what every editor's
|
|
11
|
+
* `Col` shows), and a `null` before the surface has reported anything — a readout
|
|
12
|
+
* that printed `Ln 0, Col 0` would look like a position in an empty file.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { describe, expect, test } from 'bun:test';
|
|
16
|
+
import { EditorSelection, EditorState } from '@codemirror/state';
|
|
17
|
+
|
|
18
|
+
import { caretPositionOf, sameCaret } from '@/client/hooks/editor/caret-position';
|
|
19
|
+
|
|
20
|
+
function stateAt(doc: string, anchor: number): EditorState {
|
|
21
|
+
return EditorState.create({ doc, selection: EditorSelection.single(anchor) });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('caretPositionOf', () => {
|
|
25
|
+
test('the start of the file is line 1, column 1', () => {
|
|
26
|
+
expect(caretPositionOf(stateAt('abc\ndef', 0))).toEqual({ line: 1, column: 1 });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('a position mid-line counts characters from the line start', () => {
|
|
30
|
+
expect(caretPositionOf(stateAt('abc\ndef', 6))).toEqual({ line: 2, column: 3 });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('the very end of a line is its own column, not the next line', () => {
|
|
34
|
+
expect(caretPositionOf(stateAt('abc\ndef', 3))).toEqual({ line: 1, column: 4 });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('a tab counts as ONE character, which is what Col shows', () => {
|
|
38
|
+
expect(caretPositionOf(stateAt('\tabc', 2))).toEqual({ line: 1, column: 3 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('the last line of a file with a trailing newline is an empty line 2', () => {
|
|
42
|
+
expect(caretPositionOf(stateAt('a\n', 2))).toEqual({ line: 2, column: 1 });
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('sameCaret', () => {
|
|
47
|
+
test('compares the two numbers, not the object identity', () => {
|
|
48
|
+
expect(sameCaret({ line: 1, column: 2 }, { line: 1, column: 2 })).toBe(true);
|
|
49
|
+
expect(sameCaret({ line: 1, column: 2 }, { line: 1, column: 3 })).toBe(false);
|
|
50
|
+
expect(sameCaret({ line: 1, column: 2 }, { line: 2, column: 2 })).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('two nulls are the same, and null against a position is not', () => {
|
|
54
|
+
expect(sameCaret(null, null)).toBe(true);
|
|
55
|
+
expect(sameCaret(null, { line: 1, column: 1 })).toBe(false);
|
|
56
|
+
expect(sameCaret({ line: 1, column: 1 }, null)).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The caret's position, in the units a status bar prints.
|
|
8
|
+
*
|
|
9
|
+
* The phone has had a status strip since the editor was rebuilt; the desktop pane
|
|
10
|
+
* had nothing, so the only way to know where the caret was is to count lines.
|
|
11
|
+
*
|
|
12
|
+
* ## Why this is computed from a `State` and not read from the DOM
|
|
13
|
+
*
|
|
14
|
+
* The surface's update listener writes `data-line` and `data-selection` on the
|
|
15
|
+
* content element, and that attribute pair is documented as belonging to the e2e
|
|
16
|
+
* layer — "nothing in the app reads them". A status bar built on it would make an
|
|
17
|
+
* app feature depend on a test fixture. The surface therefore reports the caret
|
|
18
|
+
* through its own prop, from the same listener, and this module is the arithmetic.
|
|
19
|
+
*
|
|
20
|
+
* ## Column is 1-based and counts CHARACTERS, not display columns
|
|
21
|
+
*
|
|
22
|
+
* A tab counts as one, which is what an editor's "Col" shows and what the user's own
|
|
23
|
+
* selection arithmetic produces. Counting display columns would need the tab size
|
|
24
|
+
* and the font, and would disagree with the number under their cursor.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { EditorState } from '@codemirror/state';
|
|
28
|
+
|
|
29
|
+
/** Where the caret is, in the units the readout prints. */
|
|
30
|
+
export interface CaretPosition {
|
|
31
|
+
/** 1-based line number. */
|
|
32
|
+
line: number;
|
|
33
|
+
/** 1-based character offset within the line. */
|
|
34
|
+
column: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The caret's position in `state`. */
|
|
38
|
+
export function caretPositionOf(state: EditorState): CaretPosition {
|
|
39
|
+
const head = state.selection.main.head;
|
|
40
|
+
const line = state.doc.lineAt(head);
|
|
41
|
+
return { line: line.number, column: head - line.from + 1 };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Whether two positions print the same thing. */
|
|
45
|
+
export function sameCaret(a: CaretPosition | null, b: CaretPosition | null): boolean {
|
|
46
|
+
if (a === null || b === null) return a === b;
|
|
47
|
+
return a.line === b.line && a.column === b.column;
|
|
48
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The file-editing contract's own types.
|
|
8
|
+
*
|
|
9
|
+
* Split out of `use-file-editor.ts` for the repository's per-file ceiling, and they
|
|
10
|
+
* belong here anyway: the phone's full-screen editor, the desktop pane and the
|
|
11
|
+
* write path all name these, and importing a type from a hook module couples them
|
|
12
|
+
* to an implementation file.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type FileEditorSaveStatus = 'idle' | 'saving' | 'saved' | 'error' | 'needs-permission';
|
|
16
|
+
|
|
17
|
+
import type { ViewerKind } from '@/shared/lib/fs/document-kind';
|
|
18
|
+
import type { LineEnding } from '@/shared/lib/code/line-endings';
|
|
19
|
+
import type { ByteSource } from '@/client/components/workspace/editor/viewers/bytes';
|
|
20
|
+
|
|
21
|
+
/** The single file a code surface is editing; `id` keys the in-memory cache. */
|
|
22
|
+
export interface FileEditorTarget {
|
|
23
|
+
id?: number | string;
|
|
24
|
+
name: string;
|
|
25
|
+
path?: string;
|
|
26
|
+
content?: string;
|
|
27
|
+
root?: string;
|
|
28
|
+
repo?: string;
|
|
29
|
+
/** The live handle a PWA launch opened this file through, when there is one. */
|
|
30
|
+
handleId?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface UseFileEditorOptions {
|
|
34
|
+
/** Flush a pending debounced write on unmount so closing the editor cannot drop an edit. */
|
|
35
|
+
flushOnUnmount?: boolean;
|
|
36
|
+
/** Mark a failed write as `'error'`; otherwise the status returns to `'idle'`. */
|
|
37
|
+
reportSaveError?: boolean;
|
|
38
|
+
/** Blob MIME type for downloads. */
|
|
39
|
+
downloadMimeType?: string;
|
|
40
|
+
/** Fired after a write lands, so workspace panels can refresh. */
|
|
41
|
+
onFileSaved?: () => void;
|
|
42
|
+
/**
|
|
43
|
+
* The file this tab names is gone from disk (the tab strip strikes it
|
|
44
|
+
* through; see `useMissingFiles`).
|
|
45
|
+
*
|
|
46
|
+
* A save then means something the user did not ask for — writing the buffer
|
|
47
|
+
* back would CREATE the file again — so the debounced autosave stands down
|
|
48
|
+
* and an explicit Save asks first. Without this, a keystroke in a tab whose
|
|
49
|
+
* file was deleted a moment ago silently resurrected it on disk.
|
|
50
|
+
*/
|
|
51
|
+
missing?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface UseFileEditorResult {
|
|
55
|
+
content: string;
|
|
56
|
+
onChange: (next: string) => void;
|
|
57
|
+
isLoading: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* The current file's bytes have arrived (or it is an image / carries inline
|
|
60
|
+
* content). Distinguishes "the buffer is empty" from "the buffer is not read
|
|
61
|
+
* yet" — `isLoading` cannot, because it starts `false` on the first render and
|
|
62
|
+
* a consumer that trusted it would act on an empty buffer.
|
|
63
|
+
*/
|
|
64
|
+
loaded: boolean;
|
|
65
|
+
loadError: string | null;
|
|
66
|
+
/**
|
|
67
|
+
* The server refused this file as a document (binary, or larger than the
|
|
68
|
+
* editor opens), with its own sentence. Distinct from `loadError` only in
|
|
69
|
+
* WORDING — both leave an empty buffer and disable Save — but the difference
|
|
70
|
+
* is what the user needs: a zip was read fine and is simply not text.
|
|
71
|
+
*/
|
|
72
|
+
unsupported: string | null;
|
|
73
|
+
saveStatus: FileEditorSaveStatus;
|
|
74
|
+
copied: boolean;
|
|
75
|
+
isDirty: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* The ACTIVE buffer's own line ending, which the next save restores.
|
|
78
|
+
*
|
|
79
|
+
* Detected from the bytes on read, never from a setting: a CRLF file must stay
|
|
80
|
+
* CRLF, and the buffer itself is LF-only so this is the only place the style
|
|
81
|
+
* survives.
|
|
82
|
+
*/
|
|
83
|
+
eol: LineEnding;
|
|
84
|
+
/**
|
|
85
|
+
* Change the ending the NEXT save writes.
|
|
86
|
+
*
|
|
87
|
+
* A UI over an existing capability: the write path has always carried `eol` (a
|
|
88
|
+
* multipart body normalizes bare LF, so the ending cannot ride in the payload). What
|
|
89
|
+
* was missing was a way to CHOOSE it — a file that arrived as CRLF stayed CRLF and
|
|
90
|
+
* one that arrived as LF stayed LF, with no way to convert either.
|
|
91
|
+
*/
|
|
92
|
+
setEol: (eol: LineEnding) => void;
|
|
93
|
+
/**
|
|
94
|
+
* The mtime the ACTIVE buffer was read at, or null when it has none (an inline
|
|
95
|
+
* attachment, a handle-backed file, a read that failed).
|
|
96
|
+
*
|
|
97
|
+
* The conflict guard sends it as `baseMtime` and the change watcher compares the
|
|
98
|
+
* disk against it.
|
|
99
|
+
*/
|
|
100
|
+
baseMtimeMs: number | null;
|
|
101
|
+
/**
|
|
102
|
+
* EVERY cached buffer holding edits that are not on disk — not just the active
|
|
103
|
+
* one.
|
|
104
|
+
*
|
|
105
|
+
* `isDirty` answers for the tab on screen, which is all a single hook can see;
|
|
106
|
+
* the tab strip needs the answer for the tab the user is about to CLOSE, and a
|
|
107
|
+
* background buffer is exactly what `isDirty` cannot describe. Keys are the
|
|
108
|
+
* buffer keys, which for an opened tab is `String(tab.id)`.
|
|
109
|
+
*/
|
|
110
|
+
dirtyKeys: ReadonlySet<string>;
|
|
111
|
+
language: string;
|
|
112
|
+
/**
|
|
113
|
+
* A handle-backed tab whose live handle is gone — the page reloaded since the
|
|
114
|
+
* file was launched, and a `FileSystemFileHandle` cannot be persisted. Save
|
|
115
|
+
* is refused (there is nothing to write through), so the surface says why
|
|
116
|
+
* instead of showing a button that silently fails.
|
|
117
|
+
*/
|
|
118
|
+
handleMissing: boolean;
|
|
119
|
+
/** True for raster images: the surface renders `imageUrl`, not `content`. */
|
|
120
|
+
isImage: boolean;
|
|
121
|
+
/** Raw-byte URL of the active image (cache-busted per revision), else null. */
|
|
122
|
+
imageUrl: string | null;
|
|
123
|
+
/**
|
|
124
|
+
* Which surface draws this file (see `shared/lib/fs/document-kind`). The pane
|
|
125
|
+
* dispatches on it, and `isImage` is now one case of it rather than its own
|
|
126
|
+
* axis.
|
|
127
|
+
*/
|
|
128
|
+
viewerKind: ViewerKind;
|
|
129
|
+
/**
|
|
130
|
+
* The bytes a byte-drawing viewer reads, from the tab's path or its live
|
|
131
|
+
* handle. `loadBytes` is null for a text viewer and for an inline attachment
|
|
132
|
+
* (nothing to fetch); `url` is additionally null when the file came from a
|
|
133
|
+
* handle rather than a workspace path.
|
|
134
|
+
*/
|
|
135
|
+
byteSource: ByteSource;
|
|
136
|
+
/**
|
|
137
|
+
* True for the viewers that read BYTES rather than the text buffer (image,
|
|
138
|
+
* PDF, DOCX, workbook). Their read path is skipped and their writes refused,
|
|
139
|
+
* because a byte surface has no buffer to save.
|
|
140
|
+
*/
|
|
141
|
+
isBinaryViewer: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Bumped by `reset()` (a workspace refresh). A byte viewer that fetches its
|
|
144
|
+
* own bytes has no read effect to re-run, so it takes this as its re-read
|
|
145
|
+
* lever; the text path already re-reads through `useFileRead`.
|
|
146
|
+
*/
|
|
147
|
+
revision: number;
|
|
148
|
+
saveNow: () => void;
|
|
149
|
+
copy: () => void;
|
|
150
|
+
download: () => void;
|
|
151
|
+
/**
|
|
152
|
+
* Re-read every cached file from disk (e.g. a workspace refresh). Content
|
|
153
|
+
* already on screen stays until the fresh bytes land, and a file with
|
|
154
|
+
* unsaved edits is left alone.
|
|
155
|
+
*/
|
|
156
|
+
reset: () => void;
|
|
157
|
+
/** Drop cached content for files no longer open. */
|
|
158
|
+
retain: (ids: Array<number | string>) => void;
|
|
159
|
+
/** The file was deleted, so a Save is waiting on the user's answer. */
|
|
160
|
+
saveMissingConfirm: boolean;
|
|
161
|
+
/** Write the buffer anyway — this recreates the file on disk. */
|
|
162
|
+
confirmSaveMissing: () => void;
|
|
163
|
+
/** Dismiss the dialog; nothing is written and the buffer stays dirty. */
|
|
164
|
+
cancelSaveMissing: () => void;
|
|
165
|
+
/**
|
|
166
|
+
* The disk copy moved since this buffer was read, so the last write was refused.
|
|
167
|
+
*
|
|
168
|
+
* `null` for the active file means there is no conflict to report. The panel
|
|
169
|
+
* offers two answers — reload the disk copy, or write over it — because
|
|
170
|
+
* reporting it as a failed save would leave the user with no way forward.
|
|
171
|
+
*/
|
|
172
|
+
conflict: { diskMtimeMs: number | null } | null;
|
|
173
|
+
/** Take the disk copy, discarding the buffer's edits. */
|
|
174
|
+
resolveConflictByReload: () => void;
|
|
175
|
+
/**
|
|
176
|
+
* Write the buffer anyway, adopting the disk mtime as the new base.
|
|
177
|
+
*
|
|
178
|
+
* This is the "Keep mine" answer: the user has seen that the disk moved and
|
|
179
|
+
* chosen their buffer over it.
|
|
180
|
+
*/
|
|
181
|
+
resolveConflictByKeepingMine: () => void;
|
|
182
|
+
/**
|
|
183
|
+
* Throw the buffer away and take the file from disk.
|
|
184
|
+
*
|
|
185
|
+
* The user-facing Revert. Distinct from `reset()` (a workspace refresh, which
|
|
186
|
+
* deliberately KEEPS a dirty buffer) and from `resolveConflictByReload` only in
|
|
187
|
+
* that it needs no conflict to exist — the reason to revert is usually that the
|
|
188
|
+
* user wants the disk copy back, not that anyone else wrote it.
|
|
189
|
+
*/
|
|
190
|
+
revertToDisk: () => void;
|
|
191
|
+
/**
|
|
192
|
+
* Adopt the disk's mtime as this buffer's base WITHOUT writing anything.
|
|
193
|
+
*
|
|
194
|
+
* The answer to an external-change notice when the user wants to keep editing:
|
|
195
|
+
* their next save must not be refused with a `409` over a change they have
|
|
196
|
+
* already been shown and accepted. Nothing is written here — the buffer stays
|
|
197
|
+
* dirty and the autosave owns the write.
|
|
198
|
+
*/
|
|
199
|
+
adoptDiskMtime: (diskMtimeMs: number) => void;
|
|
200
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The find bar's recent queries.
|
|
8
|
+
*
|
|
9
|
+
* Newest first, no duplicates, bounded — and only queries that were actually USED, which
|
|
10
|
+
* is why `remember` is called from the bar's close rather than from every keystroke. A
|
|
11
|
+
* half-written regex that never ran is noise in a list whose value is that each entry is
|
|
12
|
+
* something you searched for.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { afterAll, afterEach, describe, expect, test } from 'bun:test';
|
|
16
|
+
import { Window } from 'happy-dom';
|
|
17
|
+
import { h, render } from 'preact';
|
|
18
|
+
import { act } from 'preact/test-utils';
|
|
19
|
+
|
|
20
|
+
import { FIND_HISTORY_LIMIT, useFindHistory, type FindHistory } from '@/client/hooks/editor/find-history';
|
|
21
|
+
import { forgetSession } from '@/shared/lib/workspace/session-state/store';
|
|
22
|
+
import type { ComponentChildren } from 'preact';
|
|
23
|
+
import type { ComponentType } from 'preact/compat';
|
|
24
|
+
|
|
25
|
+
const SESSION = 'find-history-test';
|
|
26
|
+
const DOM_GLOBALS = ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'CustomEvent'] as const;
|
|
27
|
+
const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
|
|
28
|
+
const win = new Window({ url: 'http://localhost' });
|
|
29
|
+
for (const key of DOM_GLOBALS) {
|
|
30
|
+
const target = globalThis as unknown as Record<string, unknown>;
|
|
31
|
+
native[key] = target[key];
|
|
32
|
+
target[key] = (win as unknown as Record<string, unknown>)[key];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
afterAll(() => {
|
|
36
|
+
const target = globalThis as unknown as Record<string, unknown>;
|
|
37
|
+
for (const key of DOM_GLOBALS) target[key] = native[key];
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
forgetSession(SESSION);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Mounted through the REAL session-state provider: `useFindHistory` persists through
|
|
46
|
+
* `useSessionState`, and a local stub would not exercise the store the history actually
|
|
47
|
+
* lives in.
|
|
48
|
+
*/
|
|
49
|
+
let SessionStateProvider: ComponentType<{ sessionId: string; children: ComponentChildren }>;
|
|
50
|
+
|
|
51
|
+
async function mount(): Promise<{ api: () => FindHistory; unmount: () => void }> {
|
|
52
|
+
globalThis.fetch = (async () => ({
|
|
53
|
+
ok: true,
|
|
54
|
+
json: async () => ({ sessionId: SESSION, state: {} }),
|
|
55
|
+
text: async () => JSON.stringify({ sessionId: SESSION, state: {} }),
|
|
56
|
+
})) as unknown as typeof fetch;
|
|
57
|
+
const provider = await import('@/client/components/common/session-state-provider/index');
|
|
58
|
+
SessionStateProvider = provider.SessionStateProvider as typeof SessionStateProvider;
|
|
59
|
+
|
|
60
|
+
let slot: FindHistory | null = null;
|
|
61
|
+
function Probe() {
|
|
62
|
+
slot = useFindHistory();
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const host = document.createElement('div');
|
|
66
|
+
document.body.appendChild(host);
|
|
67
|
+
await act(async () => {
|
|
68
|
+
render(h(SessionStateProvider, { sessionId: SESSION, children: h(Probe, {}) }), host);
|
|
69
|
+
});
|
|
70
|
+
for (let turn = 0; turn < 12; turn++) {
|
|
71
|
+
await act(async () => { for (let inner = 0; inner < 8; inner++) await Promise.resolve(); });
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
api: () => {
|
|
75
|
+
if (!slot) throw new Error('not mounted');
|
|
76
|
+
return slot;
|
|
77
|
+
},
|
|
78
|
+
unmount: () => render(null, host),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
describe('useFindHistory', () => {
|
|
83
|
+
test('starts empty', async () => {
|
|
84
|
+
const { api, unmount } = await mount();
|
|
85
|
+
expect(api().entries).toEqual([]);
|
|
86
|
+
unmount();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('records newest first', async () => {
|
|
90
|
+
const { api, unmount } = await mount();
|
|
91
|
+
act(() => api().remember('alpha'));
|
|
92
|
+
act(() => api().remember('beta'));
|
|
93
|
+
expect(api().entries).toEqual(['beta', 'alpha']);
|
|
94
|
+
unmount();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('re-searching an entry moves it to the front instead of duplicating it', async () => {
|
|
98
|
+
const { api, unmount } = await mount();
|
|
99
|
+
act(() => api().remember('alpha'));
|
|
100
|
+
act(() => api().remember('beta'));
|
|
101
|
+
act(() => api().remember('alpha'));
|
|
102
|
+
expect(api().entries).toEqual(['alpha', 'beta']);
|
|
103
|
+
unmount();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('an empty or whitespace query is not recorded', async () => {
|
|
107
|
+
const { api, unmount } = await mount();
|
|
108
|
+
act(() => api().remember(' '));
|
|
109
|
+
act(() => api().remember(''));
|
|
110
|
+
expect(api().entries).toEqual([]);
|
|
111
|
+
unmount();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('trims before comparing, so " alpha " and "alpha" are one entry', async () => {
|
|
115
|
+
const { api, unmount } = await mount();
|
|
116
|
+
act(() => api().remember(' alpha '));
|
|
117
|
+
act(() => api().remember('alpha'));
|
|
118
|
+
expect(api().entries).toEqual(['alpha']);
|
|
119
|
+
unmount();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('is bounded, dropping the oldest', async () => {
|
|
123
|
+
const { api, unmount } = await mount();
|
|
124
|
+
for (let index = 0; index < FIND_HISTORY_LIMIT + 5; index++) {
|
|
125
|
+
act(() => api().remember(`query-${index}`));
|
|
126
|
+
}
|
|
127
|
+
expect(api().entries).toHaveLength(FIND_HISTORY_LIMIT);
|
|
128
|
+
expect(api().entries[0]).toBe(`query-${FIND_HISTORY_LIMIT + 4}`);
|
|
129
|
+
expect(api().entries).not.toContain('query-0');
|
|
130
|
+
unmount();
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The find bar's recent queries.
|
|
8
|
+
*
|
|
9
|
+
* Persisted per session like the query itself — a reload should not forget the string
|
|
10
|
+
* you were hunting for — and bounded, because a long editing session would otherwise
|
|
11
|
+
* keep every search it ever ran.
|
|
12
|
+
*
|
|
13
|
+
* A query is remembered when it was actually USED (the bar closing, or a history entry
|
|
14
|
+
* being picked again), not when it was typed: a half-written regex that never ran is
|
|
15
|
+
* noise in a list whose whole value is that each entry is something you searched for.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { useCallback } from 'preact/hooks';
|
|
19
|
+
|
|
20
|
+
import { useSessionState } from '@/client/hooks/workspace/session-state';
|
|
21
|
+
|
|
22
|
+
/** How many recent queries the bar remembers. */
|
|
23
|
+
export const FIND_HISTORY_LIMIT = 20;
|
|
24
|
+
|
|
25
|
+
export interface FindHistory {
|
|
26
|
+
entries: readonly string[];
|
|
27
|
+
/** Record a query as the most recent, without duplicating it. */
|
|
28
|
+
remember: (query: string) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useFindHistory(): FindHistory {
|
|
32
|
+
const [entries, setEntries] = useSessionState<string[]>('editor.findHistory', []);
|
|
33
|
+
|
|
34
|
+
const remember = useCallback(
|
|
35
|
+
(query: string) => {
|
|
36
|
+
const trimmed = query.trim();
|
|
37
|
+
if (!trimmed) return;
|
|
38
|
+
setEntries((previous) =>
|
|
39
|
+
[trimmed, ...previous.filter((entry) => entry !== trimmed)].slice(0, FIND_HISTORY_LIMIT),
|
|
40
|
+
);
|
|
41
|
+
},
|
|
42
|
+
[setEntries],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return { entries, remember };
|
|
46
|
+
}
|