@h-ai/ui 1.0.0-beta.20 → 1.0.0-beta.21
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.en.md +24 -0
- package/README.ja.md +24 -0
- package/README.md +24 -0
- package/auto-import.d.ts +1 -0
- package/auto-import.js +1 -0
- package/dist/components/scenes/code/CodeWorkspace.svelte +946 -0
- package/dist/components/scenes/code/CodeWorkspace.svelte.d.ts +21 -0
- package/dist/components/scenes/code/CodeWorkspaceEditor.svelte +737 -0
- package/dist/components/scenes/code/CodeWorkspaceEditor.svelte.d.ts +37 -0
- package/dist/components/scenes/code/CodeWorkspaceGutter.svelte +246 -0
- package/dist/components/scenes/code/CodeWorkspaceGutter.svelte.d.ts +23 -0
- package/dist/components/scenes/code/CodeWorkspaceSearch.svelte +234 -0
- package/dist/components/scenes/code/CodeWorkspaceSearch.svelte.d.ts +23 -0
- package/dist/components/scenes/code/CodeWorkspaceTree.svelte +519 -0
- package/dist/components/scenes/code/CodeWorkspaceTree.svelte.d.ts +26 -0
- package/dist/components/scenes/code/code-editor.d.ts +12 -0
- package/dist/components/scenes/code/code-editor.js +93 -0
- package/dist/components/scenes/code/code-types.d.ts +346 -0
- package/dist/components/scenes/code/code-types.js +1 -0
- package/dist/components/scenes/code/index.d.ts +2 -0
- package/dist/components/scenes/code/index.js +1 -0
- package/dist/components/scenes/code/workers/css.worker.d.ts +1 -0
- package/dist/components/scenes/code/workers/css.worker.js +1 -0
- package/dist/components/scenes/code/workers/editor.worker.d.ts +1 -0
- package/dist/components/scenes/code/workers/editor.worker.js +1 -0
- package/dist/components/scenes/code/workers/html.worker.d.ts +1 -0
- package/dist/components/scenes/code/workers/html.worker.js +1 -0
- package/dist/components/scenes/code/workers/json.worker.d.ts +1 -0
- package/dist/components/scenes/code/workers/json.worker.js +1 -0
- package/dist/components/scenes/code/workers/ts.worker.d.ts +1 -0
- package/dist/components/scenes/code/workers/ts.worker.js +1 -0
- package/dist/components/scenes/index.d.ts +1 -0
- package/dist/components/scenes/index.js +2 -0
- package/dist/messages/en-US.json +54 -1
- package/dist/messages/ja-JP.json +54 -1
- package/dist/messages/zh-CN.json +54 -1
- package/package.json +8 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { CodeWorkspaceAnnotationContext, CodeWorkspaceDisplay, CodeWorkspaceFile, CodeWorkspaceRange, CodeWorkspaceSelection, CodeWorkspaceSelectionContext, CodeWorkspaceTheme } from './code-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* 编辑器内部的文件、显示选项和宿主回调。
|
|
5
|
+
*
|
|
6
|
+
* @example 模块内部上下文
|
|
7
|
+
* ```ts
|
|
8
|
+
* const props: Props = {
|
|
9
|
+
* file: { node: { kind: 'file', id: 'readme', name: 'README.md' }, text: '# Hello' },
|
|
10
|
+
* options: { lineNumbers: true },
|
|
11
|
+
* theme: 'inherit',
|
|
12
|
+
* onselection: selection => selection?.text,
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
interface Props {
|
|
17
|
+
file: CodeWorkspaceFile;
|
|
18
|
+
options: CodeWorkspaceDisplay;
|
|
19
|
+
theme: CodeWorkspaceTheme;
|
|
20
|
+
navigation?: {
|
|
21
|
+
range: CodeWorkspaceRange;
|
|
22
|
+
request: number;
|
|
23
|
+
focus?: boolean;
|
|
24
|
+
};
|
|
25
|
+
annotationIcon?: Snippet<[CodeWorkspaceAnnotationContext]>;
|
|
26
|
+
annotationHover?: Snippet<[CodeWorkspaceAnnotationContext]>;
|
|
27
|
+
selectionActions?: Snippet<[CodeWorkspaceSelectionContext]>;
|
|
28
|
+
onselection: (selection: CodeWorkspaceSelection | null) => void;
|
|
29
|
+
onready?: (ready: boolean) => void;
|
|
30
|
+
locale?: string;
|
|
31
|
+
}
|
|
32
|
+
declare const CodeWorkspaceEditor: import("svelte").Component<Props, {
|
|
33
|
+
find: () => void;
|
|
34
|
+
goToLine: () => void;
|
|
35
|
+
}, "">;
|
|
36
|
+
type CodeWorkspaceEditor = ReturnType<typeof CodeWorkspaceEditor>;
|
|
37
|
+
export default CodeWorkspaceEditor;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
<script lang='ts'>
|
|
2
|
+
import type { editor as MonacoEditor } from 'monaco-editor'
|
|
3
|
+
import type { Snippet } from 'svelte'
|
|
4
|
+
import type { CodeWorkspaceAnnotation, CodeWorkspaceAnnotationContext, CodeWorkspaceFile } from './code-types.js'
|
|
5
|
+
import { onDestroy, tick, untrack } from 'svelte'
|
|
6
|
+
import { SvelteMap, SvelteSet } from 'svelte/reactivity'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 渲染当前文件在 Monaco 行前区域的交互标记。
|
|
10
|
+
*
|
|
11
|
+
* @example 模块内部上下文
|
|
12
|
+
* ```ts
|
|
13
|
+
* function hasMarks({ annotations }: Props): boolean {
|
|
14
|
+
* return annotations.length > 0
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
interface Props {
|
|
19
|
+
editor: MonacoEditor.IStandaloneCodeEditor
|
|
20
|
+
file: CodeWorkspaceFile
|
|
21
|
+
annotations: readonly CodeWorkspaceAnnotation[]
|
|
22
|
+
annotationIcon?: Snippet<[CodeWorkspaceAnnotationContext]>
|
|
23
|
+
annotationHover?: Snippet<[CodeWorkspaceAnnotationContext]>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { editor, file, annotations, annotationIcon, annotationHover }: Props = $props()
|
|
27
|
+
const id = $props.id()
|
|
28
|
+
const byLine = $derived.by(() => {
|
|
29
|
+
const groups = new SvelteMap<number, CodeWorkspaceAnnotation[]>()
|
|
30
|
+
for (const annotation of annotations) {
|
|
31
|
+
const line = annotation.range.startLine
|
|
32
|
+
const group = groups.get(line)
|
|
33
|
+
if (group)
|
|
34
|
+
group.push(annotation)
|
|
35
|
+
else
|
|
36
|
+
groups.set(line, [annotation])
|
|
37
|
+
}
|
|
38
|
+
return groups
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
let layout = $state.raw({
|
|
42
|
+
left: 0,
|
|
43
|
+
width: 0,
|
|
44
|
+
height: 0,
|
|
45
|
+
lineHeight: 0,
|
|
46
|
+
rows: [] as { line: number, top: number }[],
|
|
47
|
+
})
|
|
48
|
+
let currentLine = $state<number>()
|
|
49
|
+
let trigger = $state<HTMLButtonElement>()
|
|
50
|
+
let panel = $state<HTMLDivElement>()
|
|
51
|
+
let panelHeight = $state(0)
|
|
52
|
+
let closeTimer: ReturnType<typeof setTimeout> | undefined
|
|
53
|
+
let suppressFocus = false
|
|
54
|
+
const activeRow = $derived(layout.rows.find(row => row.line === currentLine))
|
|
55
|
+
const popupTop = $derived(activeRow
|
|
56
|
+
? Math.max(4, Math.min(activeRow.top, layout.height - panelHeight - 4))
|
|
57
|
+
: 0)
|
|
58
|
+
|
|
59
|
+
$effect(() => {
|
|
60
|
+
const instance = editor
|
|
61
|
+
const groups = byLine
|
|
62
|
+
function refresh() {
|
|
63
|
+
const info = instance.getLayoutInfo()
|
|
64
|
+
const rows: { line: number, top: number }[] = []
|
|
65
|
+
const scrollTop = instance.getScrollTop()
|
|
66
|
+
const lineHeight = instance.getLineHeightForPosition({ lineNumber: 1, column: 1 })
|
|
67
|
+
const seen = new SvelteSet<number>()
|
|
68
|
+
for (const range of instance.getVisibleRanges()) {
|
|
69
|
+
for (let line = range.startLineNumber; line <= range.endLineNumber; line++) {
|
|
70
|
+
if (!groups.has(line) || seen.has(line))
|
|
71
|
+
continue
|
|
72
|
+
seen.add(line)
|
|
73
|
+
const top = instance.getTopForLineNumber(line, true) - scrollTop
|
|
74
|
+
if (top < info.height && top + lineHeight > 0)
|
|
75
|
+
rows.push({ line, top })
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
layout = {
|
|
79
|
+
left: info.glyphMarginLeft,
|
|
80
|
+
width: info.glyphMarginWidth,
|
|
81
|
+
height: info.height,
|
|
82
|
+
lineHeight,
|
|
83
|
+
rows,
|
|
84
|
+
}
|
|
85
|
+
if (untrack(() => currentLine !== undefined && !rows.some(row => row.line === currentLine)))
|
|
86
|
+
dismiss()
|
|
87
|
+
}
|
|
88
|
+
const listeners = [
|
|
89
|
+
instance.onDidScrollChange(refresh),
|
|
90
|
+
instance.onDidLayoutChange(refresh),
|
|
91
|
+
instance.onDidChangeModel(refresh),
|
|
92
|
+
instance.onDidChangeHiddenAreas(refresh),
|
|
93
|
+
instance.onDidChangeConfiguration(refresh),
|
|
94
|
+
]
|
|
95
|
+
untrack(refresh)
|
|
96
|
+
return () => {
|
|
97
|
+
for (const listener of listeners)
|
|
98
|
+
listener.dispose()
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
function cancelClose() {
|
|
103
|
+
clearTimeout(closeTimer)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function dismiss(restoreFocus = false) {
|
|
107
|
+
cancelClose()
|
|
108
|
+
currentLine = undefined
|
|
109
|
+
if (restoreFocus && trigger?.isConnected) {
|
|
110
|
+
suppressFocus = true
|
|
111
|
+
trigger.focus({ preventScroll: true })
|
|
112
|
+
suppressFocus = false
|
|
113
|
+
}
|
|
114
|
+
trigger = undefined
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function show(line: number, button: HTMLButtonElement) {
|
|
118
|
+
if (suppressFocus)
|
|
119
|
+
return
|
|
120
|
+
cancelClose()
|
|
121
|
+
trigger = button
|
|
122
|
+
currentLine = line
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function closeSoon() {
|
|
126
|
+
cancelClose()
|
|
127
|
+
closeTimer = setTimeout(() => {
|
|
128
|
+
if (document.activeElement !== trigger && !panel?.contains(document.activeElement))
|
|
129
|
+
dismiss()
|
|
130
|
+
}, 150)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function activate(line: number, button: HTMLButtonElement) {
|
|
134
|
+
show(line, button)
|
|
135
|
+
await tick()
|
|
136
|
+
if (currentLine === line) {
|
|
137
|
+
const first = panel?.querySelector<HTMLElement>('button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]')
|
|
138
|
+
const target = first ?? panel
|
|
139
|
+
target?.focus({ preventScroll: true })
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function onEscape(event: KeyboardEvent) {
|
|
144
|
+
if (event.key !== 'Escape' || currentLine === undefined)
|
|
145
|
+
return
|
|
146
|
+
const focused = document.activeElement
|
|
147
|
+
const ownsFocus = focused === trigger || Boolean(panel?.contains(focused))
|
|
148
|
+
if (ownsFocus) {
|
|
149
|
+
event.preventDefault()
|
|
150
|
+
event.stopPropagation()
|
|
151
|
+
}
|
|
152
|
+
dismiss(ownsFocus)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function onFocusOut(event: FocusEvent) {
|
|
156
|
+
if (event.relatedTarget === trigger
|
|
157
|
+
|| (event.relatedTarget instanceof Node && panel?.contains(event.relatedTarget))) {
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
closeSoon()
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
onDestroy(cancelClose)
|
|
164
|
+
</script>
|
|
165
|
+
|
|
166
|
+
<svelte:window onkeydowncapture={onEscape} />
|
|
167
|
+
|
|
168
|
+
<div class='annotation-layer'>
|
|
169
|
+
{#if layout.width}
|
|
170
|
+
{#each layout.rows as { line, top } (line)}
|
|
171
|
+
{@const marks = byLine.get(line) ?? []}
|
|
172
|
+
<button
|
|
173
|
+
type='button'
|
|
174
|
+
class='annotation-button'
|
|
175
|
+
style:left={`${layout.left}px`}
|
|
176
|
+
style:top={`${top}px`}
|
|
177
|
+
style:width={`${layout.width}px`}
|
|
178
|
+
style:height={`${layout.lineHeight}px`}
|
|
179
|
+
aria-label={marks.map(mark => mark.label).join(' · ')}
|
|
180
|
+
aria-expanded={currentLine === line}
|
|
181
|
+
aria-controls={currentLine === line
|
|
182
|
+
? id
|
|
183
|
+
: undefined}
|
|
184
|
+
onpointerenter={event => show(line, event.currentTarget)}
|
|
185
|
+
onpointerleave={closeSoon}
|
|
186
|
+
onfocus={event => show(line, event.currentTarget)}
|
|
187
|
+
onfocusout={onFocusOut}
|
|
188
|
+
onclick={event => void activate(line, event.currentTarget)}
|
|
189
|
+
>
|
|
190
|
+
{#each marks as annotation, index (index)}
|
|
191
|
+
<span class='annotation-icon' data-tone={annotation.tone ?? 'neutral'} aria-hidden='true'>
|
|
192
|
+
{#if annotationIcon}
|
|
193
|
+
{@render annotationIcon({ file, annotation })}
|
|
194
|
+
{:else}
|
|
195
|
+
<span class='annotation-dot'></span>
|
|
196
|
+
{/if}
|
|
197
|
+
</span>
|
|
198
|
+
{/each}
|
|
199
|
+
</button>
|
|
200
|
+
{/each}
|
|
201
|
+
{/if}
|
|
202
|
+
{#if activeRow}
|
|
203
|
+
<div
|
|
204
|
+
{id}
|
|
205
|
+
bind:this={panel}
|
|
206
|
+
bind:offsetHeight={panelHeight}
|
|
207
|
+
class='annotation-panel'
|
|
208
|
+
role='group'
|
|
209
|
+
aria-label={trigger?.getAttribute('aria-label') ?? undefined}
|
|
210
|
+
tabindex='-1'
|
|
211
|
+
style:left={`${layout.left + layout.width + 4}px`}
|
|
212
|
+
style:top={`${popupTop}px`}
|
|
213
|
+
style:max-height={`${Math.max(0, layout.height - 8)}px`}
|
|
214
|
+
onpointerenter={cancelClose}
|
|
215
|
+
onpointerleave={closeSoon}
|
|
216
|
+
onfocusout={onFocusOut}
|
|
217
|
+
>
|
|
218
|
+
{#each byLine.get(activeRow.line) ?? [] as annotation, index (index)}
|
|
219
|
+
<div>
|
|
220
|
+
{#if annotation.hoverText !== undefined}
|
|
221
|
+
{annotation.hoverText}
|
|
222
|
+
{:else if annotationHover}
|
|
223
|
+
{@render annotationHover({ file, annotation })}
|
|
224
|
+
{:else}
|
|
225
|
+
{annotation.label}
|
|
226
|
+
{/if}
|
|
227
|
+
</div>
|
|
228
|
+
{/each}
|
|
229
|
+
</div>
|
|
230
|
+
{/if}
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
<style>
|
|
234
|
+
.annotation-layer { position: absolute; inset: 0; z-index: 5; pointer-events: none; overflow: clip; }
|
|
235
|
+
.annotation-button { position: absolute; display: flex; align-items: center; justify-content: center; gap: 1px; padding: 0; cursor: pointer; pointer-events: auto; }
|
|
236
|
+
.annotation-button:hover, .annotation-button:focus-visible { background: color-mix(in srgb, var(--color-primary) 18%, transparent); }
|
|
237
|
+
.annotation-button:focus-visible { outline: 1px solid var(--color-primary); outline-offset: -1px; }
|
|
238
|
+
.annotation-icon { display: grid; place-items: center; min-width: 0; color: var(--color-base-content); }
|
|
239
|
+
.annotation-icon[data-tone='info'] { color: var(--color-info); }
|
|
240
|
+
.annotation-icon[data-tone='success'] { color: var(--color-success); }
|
|
241
|
+
.annotation-icon[data-tone='warning'] { color: var(--color-warning); }
|
|
242
|
+
.annotation-icon[data-tone='error'] { color: var(--color-error); }
|
|
243
|
+
.annotation-icon :global(svg) { max-width: 100%; width: 0.875rem; height: 0.875rem; }
|
|
244
|
+
.annotation-dot { width: .45rem; height: .45rem; border-radius: 100%; background: currentColor; }
|
|
245
|
+
.annotation-panel { position: absolute; display: grid; gap: .35rem; max-width: min(24rem, calc(100% - 3rem)); overflow: auto; border: 1px solid var(--color-base-300); border-radius: var(--radius-box); background: var(--color-base-100); color: var(--color-base-content); padding: .5rem .65rem; box-shadow: 0 3px 12px color-mix(in srgb, var(--color-base-content) 18%, transparent); font-size: .8125rem; overflow-wrap: anywhere; pointer-events: auto; }
|
|
246
|
+
</style>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { editor as MonacoEditor } from 'monaco-editor';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { CodeWorkspaceAnnotation, CodeWorkspaceAnnotationContext, CodeWorkspaceFile } from './code-types.js';
|
|
4
|
+
/**
|
|
5
|
+
* 渲染当前文件在 Monaco 行前区域的交互标记。
|
|
6
|
+
*
|
|
7
|
+
* @example 模块内部上下文
|
|
8
|
+
* ```ts
|
|
9
|
+
* function hasMarks({ annotations }: Props): boolean {
|
|
10
|
+
* return annotations.length > 0
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
interface Props {
|
|
15
|
+
editor: MonacoEditor.IStandaloneCodeEditor;
|
|
16
|
+
file: CodeWorkspaceFile;
|
|
17
|
+
annotations: readonly CodeWorkspaceAnnotation[];
|
|
18
|
+
annotationIcon?: Snippet<[CodeWorkspaceAnnotationContext]>;
|
|
19
|
+
annotationHover?: Snippet<[CodeWorkspaceAnnotationContext]>;
|
|
20
|
+
}
|
|
21
|
+
declare const CodeWorkspaceGutter: import("svelte").Component<Props, {}, "">;
|
|
22
|
+
type CodeWorkspaceGutter = ReturnType<typeof CodeWorkspaceGutter>;
|
|
23
|
+
export default CodeWorkspaceGutter;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
<script lang='ts'>
|
|
2
|
+
import type { CodeWorkspaceSearchHit, CodeWorkspaceSearchQuery, CodeWorkspaceSource } from './code-types.js'
|
|
3
|
+
import { untrack } from 'svelte'
|
|
4
|
+
import { SvelteMap } from 'svelte/reactivity'
|
|
5
|
+
import { uiM } from '../../../messages.js'
|
|
6
|
+
import Button from '../../primitives/Button.svelte'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 工作区搜索的内部接线。
|
|
10
|
+
*
|
|
11
|
+
* @example 模块内部上下文
|
|
12
|
+
* ```ts
|
|
13
|
+
* const props: Props = {
|
|
14
|
+
* source: { listChildren: () => [], readFile: () => ({ text: '' }) },
|
|
15
|
+
* mode: 'files',
|
|
16
|
+
* onopen: hit => hit.node.id,
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
interface Props {
|
|
21
|
+
source: CodeWorkspaceSource
|
|
22
|
+
mode: CodeWorkspaceSearchQuery['mode'] | null
|
|
23
|
+
onopen: (hit: CodeWorkspaceSearchHit) => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { source, mode, onopen }: Props = $props()
|
|
27
|
+
let input = $state<HTMLInputElement>()
|
|
28
|
+
let query = $state('')
|
|
29
|
+
let hits = $state.raw<readonly CodeWorkspaceSearchHit[]>([])
|
|
30
|
+
let cursor = $state<string>()
|
|
31
|
+
let loading = $state(false)
|
|
32
|
+
let failed = $state(false)
|
|
33
|
+
let retry = $state(0)
|
|
34
|
+
let current: AbortController | undefined
|
|
35
|
+
|
|
36
|
+
const groups = $derived.by(() => {
|
|
37
|
+
const grouped = new SvelteMap<string, CodeWorkspaceSearchHit[]>()
|
|
38
|
+
for (const hit of hits) {
|
|
39
|
+
const key = JSON.stringify([hit.node.id, hit.node.version])
|
|
40
|
+
const group = grouped.get(key)
|
|
41
|
+
if (group)
|
|
42
|
+
group.push(hit)
|
|
43
|
+
else
|
|
44
|
+
grouped.set(key, [hit])
|
|
45
|
+
}
|
|
46
|
+
return [
|
|
47
|
+
...grouped.values(),
|
|
48
|
+
]
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const placeholder = $derived(uiM(mode === 'files'
|
|
52
|
+
? 'code_searchPlaceholderFiles'
|
|
53
|
+
: 'code_searchPlaceholderContent'))
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 将焦点移到工作区搜索输入框。
|
|
57
|
+
*
|
|
58
|
+
* @example 模块内部上下文
|
|
59
|
+
* ```ts
|
|
60
|
+
* focus()
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function focus() {
|
|
64
|
+
input?.focus()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function load(
|
|
68
|
+
search: NonNullable<CodeWorkspaceSource['search']>,
|
|
69
|
+
request: CodeWorkspaceSearchQuery,
|
|
70
|
+
controller: AbortController,
|
|
71
|
+
) {
|
|
72
|
+
loading = true
|
|
73
|
+
failed = false
|
|
74
|
+
try {
|
|
75
|
+
const page = await search(request, controller.signal)
|
|
76
|
+
if (controller.signal.aborted)
|
|
77
|
+
return
|
|
78
|
+
hits = request.cursor
|
|
79
|
+
? [
|
|
80
|
+
...hits,
|
|
81
|
+
...page.hits,
|
|
82
|
+
]
|
|
83
|
+
: page.hits
|
|
84
|
+
cursor = page.nextCursor === request.cursor
|
|
85
|
+
? undefined
|
|
86
|
+
: page.nextCursor
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
if (!controller.signal.aborted)
|
|
90
|
+
failed = true
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
if (!controller.signal.aborted)
|
|
94
|
+
loading = false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
$effect(() => {
|
|
99
|
+
const value = query.trim()
|
|
100
|
+
const searchMode = mode
|
|
101
|
+
const search = source.search?.bind(source)
|
|
102
|
+
void retry
|
|
103
|
+
return untrack(() => {
|
|
104
|
+
const controller = new AbortController()
|
|
105
|
+
current = controller
|
|
106
|
+
hits = []
|
|
107
|
+
cursor = undefined
|
|
108
|
+
failed = false
|
|
109
|
+
loading = Boolean(value && searchMode && search)
|
|
110
|
+
const timer = value && searchMode && search
|
|
111
|
+
? setTimeout(() => void load(search, { mode: searchMode, query: value }, controller), 180)
|
|
112
|
+
: undefined
|
|
113
|
+
return () => {
|
|
114
|
+
clearTimeout(timer)
|
|
115
|
+
controller.abort()
|
|
116
|
+
if (current === controller)
|
|
117
|
+
current = undefined
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
function loadMore() {
|
|
123
|
+
if (current && source.search && mode && cursor && !loading) {
|
|
124
|
+
void load(source.search, {
|
|
125
|
+
mode,
|
|
126
|
+
query: query.trim(),
|
|
127
|
+
cursor,
|
|
128
|
+
}, current)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function oninputkey(event: KeyboardEvent) {
|
|
133
|
+
if (event.key === 'ArrowDown') {
|
|
134
|
+
const first = input?.parentElement?.querySelector<HTMLButtonElement>('.result')
|
|
135
|
+
if (first) {
|
|
136
|
+
event.preventDefault()
|
|
137
|
+
first.focus()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (event.key === 'Enter' && hits[0]) {
|
|
141
|
+
event.preventDefault()
|
|
142
|
+
onopen(hits[0])
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
</script>
|
|
146
|
+
|
|
147
|
+
{#if mode}
|
|
148
|
+
<div class='search'>
|
|
149
|
+
<input
|
|
150
|
+
bind:this={input}
|
|
151
|
+
bind:value={query}
|
|
152
|
+
class='input input-sm'
|
|
153
|
+
type='search'
|
|
154
|
+
aria-label={placeholder}
|
|
155
|
+
{placeholder}
|
|
156
|
+
autocomplete='off'
|
|
157
|
+
onkeydown={oninputkey}
|
|
158
|
+
/>
|
|
159
|
+
<div class='results' aria-busy={loading}>
|
|
160
|
+
{#each groups as group (group[0])}
|
|
161
|
+
{@const hit = group[0]!}
|
|
162
|
+
{@const path = [
|
|
163
|
+
...(hit.ancestors ?? []).map(folder => folder.name),
|
|
164
|
+
hit.node.name,
|
|
165
|
+
].join('/')}
|
|
166
|
+
{#if mode === 'files'}
|
|
167
|
+
<button
|
|
168
|
+
type='button'
|
|
169
|
+
class='result file'
|
|
170
|
+
title={path}
|
|
171
|
+
onclick={() => onopen(hit)}
|
|
172
|
+
>
|
|
173
|
+
<strong>{hit.node.name}</strong>
|
|
174
|
+
{#if hit.ancestors?.length}
|
|
175
|
+
<small>{hit.ancestors.map(folder => folder.name).join('/')}</small>
|
|
176
|
+
{/if}
|
|
177
|
+
</button>
|
|
178
|
+
{:else}
|
|
179
|
+
<div class='group' title={path}>{path}</div>
|
|
180
|
+
{#each group as match, index (index)}
|
|
181
|
+
<button
|
|
182
|
+
type='button'
|
|
183
|
+
class='result match'
|
|
184
|
+
aria-label={match.range
|
|
185
|
+
? uiM('code_searchMatch', { name: hit.node.name, line: match.range.startLine, column: match.range.startColumn })
|
|
186
|
+
: hit.node.name}
|
|
187
|
+
onclick={() => onopen(match)}
|
|
188
|
+
>
|
|
189
|
+
{#if match.range}
|
|
190
|
+
<small>{match.range.startLine}:{match.range.startColumn}</small>
|
|
191
|
+
{/if}
|
|
192
|
+
<span>{match.preview ?? match.node.name}</span>
|
|
193
|
+
</button>
|
|
194
|
+
{/each}
|
|
195
|
+
{/if}
|
|
196
|
+
{/each}
|
|
197
|
+
{#if loading}
|
|
198
|
+
<p class='notice' role='status'>{uiM('code_searchLoading')}</p>
|
|
199
|
+
{:else if failed}
|
|
200
|
+
<div class='notice'>
|
|
201
|
+
<p role='alert'>{uiM('code_searchFailed')}</p>
|
|
202
|
+
<Button size='xs' variant='ghost' onclick={() => cursor
|
|
203
|
+
? loadMore()
|
|
204
|
+
: retry++}>{uiM('code_retry')}</Button>
|
|
205
|
+
</div>
|
|
206
|
+
{:else if query.trim() && !hits.length}
|
|
207
|
+
<p class='notice' role='status'>{uiM('code_searchEmpty')}</p>
|
|
208
|
+
{:else if hits.length}
|
|
209
|
+
<span class='sr-only' role='status'>{uiM('code_searchCount', { count: hits.length })}</span>
|
|
210
|
+
{#if cursor}
|
|
211
|
+
<div class='notice'>
|
|
212
|
+
<Button size='xs' variant='ghost' onclick={loadMore}>{uiM('code_more')}</Button>
|
|
213
|
+
</div>
|
|
214
|
+
{/if}
|
|
215
|
+
{/if}
|
|
216
|
+
</div>
|
|
217
|
+
</div>
|
|
218
|
+
{/if}
|
|
219
|
+
|
|
220
|
+
<style>
|
|
221
|
+
.search { display: flex; flex: 1; flex-direction: column; min-height: 0; gap: .45rem; padding: .35rem; }
|
|
222
|
+
input { width: 100%; }
|
|
223
|
+
.results { overflow: auto; }
|
|
224
|
+
.result { display: flex; width: 100%; min-width: 0; align-items: baseline; gap: .4rem; padding: .28rem .45rem; text-align: start; border-radius: .25rem; }
|
|
225
|
+
.result:hover { background: var(--color-base-300); }
|
|
226
|
+
.result:focus-visible { outline: 2px solid var(--color-primary); outline-offset: -2px; }
|
|
227
|
+
.file { flex-direction: column; gap: 0; }
|
|
228
|
+
.result span, .result strong, .result small, .group { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
229
|
+
.result small { opacity: .72; }
|
|
230
|
+
.match small { flex: none; font-variant-numeric: tabular-nums; }
|
|
231
|
+
.match span { font-family: ui-monospace, monospace; font-size: .76rem; }
|
|
232
|
+
.group { padding: .4rem .45rem .1rem; font-size: .75rem; font-weight: 600; }
|
|
233
|
+
.notice { padding: .4rem; font-size: .8rem; }
|
|
234
|
+
</style>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CodeWorkspaceSearchHit, CodeWorkspaceSearchQuery, CodeWorkspaceSource } from './code-types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 工作区搜索的内部接线。
|
|
4
|
+
*
|
|
5
|
+
* @example 模块内部上下文
|
|
6
|
+
* ```ts
|
|
7
|
+
* const props: Props = {
|
|
8
|
+
* source: { listChildren: () => [], readFile: () => ({ text: '' }) },
|
|
9
|
+
* mode: 'files',
|
|
10
|
+
* onopen: hit => hit.node.id,
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
interface Props {
|
|
15
|
+
source: CodeWorkspaceSource;
|
|
16
|
+
mode: CodeWorkspaceSearchQuery['mode'] | null;
|
|
17
|
+
onopen: (hit: CodeWorkspaceSearchHit) => void;
|
|
18
|
+
}
|
|
19
|
+
declare const CodeWorkspaceSearch: import("svelte").Component<Props, {
|
|
20
|
+
focus: () => void;
|
|
21
|
+
}, "">;
|
|
22
|
+
type CodeWorkspaceSearch = ReturnType<typeof CodeWorkspaceSearch>;
|
|
23
|
+
export default CodeWorkspaceSearch;
|