@h-ai/ui 1.0.0-beta.20 → 1.0.0-beta.22
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/dist/toast.svelte.js +13 -4
- package/package.json +8 -2
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
<script lang='ts'>
|
|
2
|
+
import type { CodeWorkspaceFileNode, CodeWorkspaceFolder, CodeWorkspaceNode, CodeWorkspaceSource } from './code-types.js'
|
|
3
|
+
import { tick, untrack } from 'svelte'
|
|
4
|
+
import { SvelteMap, SvelteSet } from 'svelte/reactivity'
|
|
5
|
+
import { uiM } from '../../../messages.js'
|
|
6
|
+
import Badge from '../../primitives/Badge.svelte'
|
|
7
|
+
import Button from '../../primitives/Button.svelte'
|
|
8
|
+
import Spinner from '../../primitives/Spinner.svelte'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 文件树的内部加载、选中及打开回调。
|
|
12
|
+
*
|
|
13
|
+
* @example 模块内部上下文
|
|
14
|
+
* ```ts
|
|
15
|
+
* const props: Props = {
|
|
16
|
+
* source: { listChildren: () => [], readFile: () => ({ text: '' }) },
|
|
17
|
+
* onopen: node => node.id,
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
interface Props {
|
|
22
|
+
source: CodeWorkspaceSource
|
|
23
|
+
activeId?: string
|
|
24
|
+
locale?: string
|
|
25
|
+
reveal?: { ancestors: readonly CodeWorkspaceFolder[], focus?: boolean, request: number }
|
|
26
|
+
onopen: (node: CodeWorkspaceFileNode, ancestors: readonly CodeWorkspaceFolder[]) => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 可见树节点及其目录链,用于键盘导航。
|
|
31
|
+
*
|
|
32
|
+
* @example 模块内部上下文
|
|
33
|
+
* ```ts
|
|
34
|
+
* const rootFile: TreeEntry = { node: { kind: 'file', id: 'readme', name: 'README.md' }, ancestors: [] }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
interface TreeEntry {
|
|
38
|
+
node: CodeWorkspaceNode
|
|
39
|
+
ancestors: readonly CodeWorkspaceFolder[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 树缓存的父级键,根节点使用 null。
|
|
44
|
+
*
|
|
45
|
+
* @example 模块内部上下文
|
|
46
|
+
* ```ts
|
|
47
|
+
* const root: ParentId = null
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
type ParentId = string | null
|
|
51
|
+
|
|
52
|
+
const { source, activeId, locale, reveal, onopen }: Props = $props()
|
|
53
|
+
const instanceId = $props.id()
|
|
54
|
+
const children = new SvelteMap<ParentId, readonly CodeWorkspaceNode[]>()
|
|
55
|
+
const expanded = new SvelteSet<string>()
|
|
56
|
+
const pending = new SvelteSet<ParentId>()
|
|
57
|
+
const failed = new SvelteSet<ParentId>()
|
|
58
|
+
const requests = new SvelteMap<ParentId, { controller: AbortController, promise: Promise<boolean> }>()
|
|
59
|
+
const elements = new SvelteMap<string, HTMLLIElement>()
|
|
60
|
+
let focusedId = $state<string>()
|
|
61
|
+
let announcement = $state<{ key: 'code_treeFolderLoading' | 'code_treeFolderFailed' | 'code_treeFolderReady', name: string }>()
|
|
62
|
+
let focusChanges = 0
|
|
63
|
+
|
|
64
|
+
function listVisible(parent: ParentId, ancestors: readonly CodeWorkspaceFolder[], output: TreeEntry[]) {
|
|
65
|
+
for (const node of children.get(parent) ?? []) {
|
|
66
|
+
output.push({ node, ancestors })
|
|
67
|
+
if (node.kind === 'folder' && expanded.has(node.id))
|
|
68
|
+
listVisible(node.id, [...ancestors, node], output)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const visible = $derived.by(() => {
|
|
73
|
+
const entries: TreeEntry[] = []
|
|
74
|
+
listVisible(null, [], entries)
|
|
75
|
+
return entries
|
|
76
|
+
})
|
|
77
|
+
const tabStop = $derived(visible.some(entry => entry.node.id === focusedId)
|
|
78
|
+
? focusedId
|
|
79
|
+
: visible.find(entry => entry.node.id === activeId)?.node.id ?? visible[0]?.node.id)
|
|
80
|
+
|
|
81
|
+
$effect(() => {
|
|
82
|
+
const current = source
|
|
83
|
+
untrack(() => {
|
|
84
|
+
children.clear()
|
|
85
|
+
expanded.clear()
|
|
86
|
+
pending.clear()
|
|
87
|
+
failed.clear()
|
|
88
|
+
focusedId = undefined
|
|
89
|
+
announcement = undefined
|
|
90
|
+
void loadChildren(null, current)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
for (const { controller } of requests.values())
|
|
95
|
+
controller.abort()
|
|
96
|
+
requests.clear()
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
$effect(() => {
|
|
101
|
+
const current = source
|
|
102
|
+
const target = reveal
|
|
103
|
+
if (!target)
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
const controller = new AbortController()
|
|
107
|
+
untrack(() => {
|
|
108
|
+
void revealFolders(target, current, controller.signal)
|
|
109
|
+
})
|
|
110
|
+
return () => controller.abort()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
function loadChildren(parent: CodeWorkspaceFolder | null, current = source): Promise<boolean> {
|
|
114
|
+
const id = parent?.id ?? null
|
|
115
|
+
const request = requests.get(id)
|
|
116
|
+
if (request)
|
|
117
|
+
return request.promise
|
|
118
|
+
if (children.has(id))
|
|
119
|
+
return Promise.resolve(true)
|
|
120
|
+
|
|
121
|
+
const controller = new AbortController()
|
|
122
|
+
const { signal } = controller
|
|
123
|
+
const promise = Promise.resolve().then(async () => {
|
|
124
|
+
try {
|
|
125
|
+
if (signal.aborted || current !== source)
|
|
126
|
+
return false
|
|
127
|
+
const entries = await current.listChildren(parent, signal)
|
|
128
|
+
if (signal.aborted || current !== source)
|
|
129
|
+
return false
|
|
130
|
+
children.set(id, entries)
|
|
131
|
+
if (parent && expanded.has(parent.id))
|
|
132
|
+
announcement = { key: 'code_treeFolderReady', name: parent.name }
|
|
133
|
+
return true
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
if (signal.aborted || current !== source)
|
|
137
|
+
return false
|
|
138
|
+
failed.add(id)
|
|
139
|
+
if (parent && expanded.has(parent.id))
|
|
140
|
+
announcement = { key: 'code_treeFolderFailed', name: parent.name }
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
if (requests.get(id)?.controller === controller) {
|
|
145
|
+
requests.delete(id)
|
|
146
|
+
pending.delete(id)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
requests.set(id, { controller, promise })
|
|
152
|
+
pending.add(id)
|
|
153
|
+
failed.delete(id)
|
|
154
|
+
if (parent)
|
|
155
|
+
announcement = { key: 'code_treeFolderLoading', name: parent.name }
|
|
156
|
+
return promise
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function revealFolders(target: NonNullable<Props['reveal']>, current: CodeWorkspaceSource, signal: AbortSignal) {
|
|
160
|
+
const startElement = document.activeElement
|
|
161
|
+
const startFocusChanges = focusChanges
|
|
162
|
+
const ready = await loadChildren(null, current)
|
|
163
|
+
if (!ready || signal.aborted)
|
|
164
|
+
return
|
|
165
|
+
|
|
166
|
+
let parentId: ParentId = null
|
|
167
|
+
for (const ancestor of target.ancestors) {
|
|
168
|
+
const folder: CodeWorkspaceNode | undefined = children.get(parentId)?.find(node => node.id === ancestor.id && node.kind === 'folder')
|
|
169
|
+
if (signal.aborted || folder?.kind !== 'folder')
|
|
170
|
+
return
|
|
171
|
+
expanded.add(folder.id)
|
|
172
|
+
const loaded = await loadChildren(folder, current)
|
|
173
|
+
if (!loaded || !expanded.has(folder.id))
|
|
174
|
+
return
|
|
175
|
+
parentId = folder.id
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const targetId = parentId ?? children.get(null)?.[0]?.id
|
|
179
|
+
if (target.focus && targetId !== undefined) {
|
|
180
|
+
await tick()
|
|
181
|
+
if (!signal.aborted && focusChanges === startFocusChanges && document.activeElement === startElement)
|
|
182
|
+
focusNode(targetId)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function register(node: HTMLLIElement, id: string) {
|
|
187
|
+
elements.set(id, node)
|
|
188
|
+
return {
|
|
189
|
+
destroy() {
|
|
190
|
+
if (elements.get(id) === node)
|
|
191
|
+
elements.delete(id)
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function focusNode(id: string | undefined) {
|
|
197
|
+
if (id !== undefined)
|
|
198
|
+
elements.get(id)?.focus()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function activate(node: CodeWorkspaceNode, ancestors: readonly CodeWorkspaceFolder[]) {
|
|
202
|
+
if (node.kind === 'file') {
|
|
203
|
+
onopen(node, ancestors)
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (expanded.has(node.id) && !failed.has(node.id)) {
|
|
208
|
+
expanded.delete(node.id)
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
expanded.add(node.id)
|
|
213
|
+
void loadChildren(node)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function handleClick(event: MouseEvent, node: CodeWorkspaceNode, ancestors: readonly CodeWorkspaceFolder[]) {
|
|
217
|
+
if (!(event.target instanceof Element) || event.target.closest('[role="treeitem"]') !== event.currentTarget || event.detail > 1)
|
|
218
|
+
return
|
|
219
|
+
|
|
220
|
+
focusNode(node.id)
|
|
221
|
+
activate(node, ancestors)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function handleKey(event: KeyboardEvent, node: CodeWorkspaceNode, ancestors: readonly CodeWorkspaceFolder[]) {
|
|
225
|
+
if (event.currentTarget !== event.target || event.altKey || event.ctrlKey || event.metaKey)
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
switch (event.key) {
|
|
229
|
+
case 'ArrowUp':
|
|
230
|
+
case 'ArrowDown': {
|
|
231
|
+
const index = visible.findIndex(entry => entry.node.id === node.id)
|
|
232
|
+
const offset = event.key === 'ArrowUp'
|
|
233
|
+
? -1
|
|
234
|
+
: 1
|
|
235
|
+
focusNode(visible[index + offset]?.node.id)
|
|
236
|
+
break
|
|
237
|
+
}
|
|
238
|
+
case 'Home':
|
|
239
|
+
focusNode(visible[0]?.node.id)
|
|
240
|
+
break
|
|
241
|
+
case 'End':
|
|
242
|
+
focusNode(visible.at(-1)?.node.id)
|
|
243
|
+
break
|
|
244
|
+
case 'ArrowLeft':
|
|
245
|
+
if (node.kind === 'folder' && expanded.has(node.id))
|
|
246
|
+
expanded.delete(node.id)
|
|
247
|
+
else
|
|
248
|
+
focusNode(ancestors.at(-1)?.id)
|
|
249
|
+
break
|
|
250
|
+
case 'ArrowRight':
|
|
251
|
+
if (node.kind === 'folder') {
|
|
252
|
+
if (!expanded.has(node.id) || failed.has(node.id)) {
|
|
253
|
+
expanded.add(node.id)
|
|
254
|
+
void loadChildren(node)
|
|
255
|
+
}
|
|
256
|
+
else if (!pending.has(node.id)) {
|
|
257
|
+
focusNode(children.get(node.id)?.[0]?.id)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
break
|
|
261
|
+
case 'Enter':
|
|
262
|
+
case ' ':
|
|
263
|
+
if (!event.repeat)
|
|
264
|
+
activate(node, ancestors)
|
|
265
|
+
break
|
|
266
|
+
default:
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
event.preventDefault()
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function handleFocus(event: FocusEvent, nodeId: string) {
|
|
273
|
+
if (event.target !== event.currentTarget)
|
|
274
|
+
return
|
|
275
|
+
focusChanges++
|
|
276
|
+
focusedId = nodeId
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function retryRoot() {
|
|
280
|
+
const trigger = document.activeElement
|
|
281
|
+
const ready = await loadChildren(null)
|
|
282
|
+
if (!ready)
|
|
283
|
+
return
|
|
284
|
+
await tick()
|
|
285
|
+
if (document.activeElement === trigger || document.activeElement === document.body)
|
|
286
|
+
focusNode(tabStop)
|
|
287
|
+
}
|
|
288
|
+
</script>
|
|
289
|
+
|
|
290
|
+
{#snippet branch(entries: readonly CodeWorkspaceNode[], ancestors: readonly CodeWorkspaceFolder[])}
|
|
291
|
+
{#each entries as node (node.id)}
|
|
292
|
+
{@const folder = node.kind === 'folder'}
|
|
293
|
+
{@const open = folder && expanded.has(node.id)}
|
|
294
|
+
{@const busy = pending.has(node.id)}
|
|
295
|
+
{@const error = failed.has(node.id)}
|
|
296
|
+
{@const nodeChildren = children.get(node.id)}
|
|
297
|
+
{@const statusId = `${instanceId}-${encodeURIComponent(node.id)}`}
|
|
298
|
+
<li
|
|
299
|
+
role='treeitem'
|
|
300
|
+
use:register={node.id}
|
|
301
|
+
data-code-node={node.id}
|
|
302
|
+
class='hai-code-tree__item'
|
|
303
|
+
tabindex={node.id === tabStop
|
|
304
|
+
? 0
|
|
305
|
+
: -1}
|
|
306
|
+
aria-label={[
|
|
307
|
+
node.name,
|
|
308
|
+
...(node.markers?.map(marker => marker.label) ?? []),
|
|
309
|
+
].join(', ')}
|
|
310
|
+
aria-current={!folder && activeId === node.id
|
|
311
|
+
? 'true'
|
|
312
|
+
: undefined}
|
|
313
|
+
aria-selected={folder
|
|
314
|
+
? undefined
|
|
315
|
+
: activeId === node.id}
|
|
316
|
+
aria-expanded={folder ? open : undefined}
|
|
317
|
+
aria-busy={busy || undefined}
|
|
318
|
+
aria-describedby={open && (busy || error || nodeChildren?.length === 0)
|
|
319
|
+
? statusId
|
|
320
|
+
: undefined}
|
|
321
|
+
onfocus={event => handleFocus(event, node.id)}
|
|
322
|
+
onclick={event => handleClick(event, node, ancestors)}
|
|
323
|
+
onkeydown={event => handleKey(event, node, ancestors)}
|
|
324
|
+
>
|
|
325
|
+
<div class='hai-code-tree__row' style:--depth={ancestors.length}>
|
|
326
|
+
{#if folder}
|
|
327
|
+
<svg data-code-chevron aria-hidden='true' class:hai-code-tree__chevron-open={open} viewBox='0 0 16 16' fill='none' stroke='currentColor' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'>
|
|
328
|
+
<path d='m6 4 4 4-4 4' />
|
|
329
|
+
</svg>
|
|
330
|
+
<svg data-code-folder aria-hidden='true' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'>
|
|
331
|
+
{#if open}
|
|
332
|
+
<path d='M3 19.5V6a2 2 0 0 1 2-2h4l2 2h7a2 2 0 0 1 2 2v2M3 19.5l2.6-8A2 2 0 0 1 7.5 10H21a1 1 0 0 1 1 1.3l-2.5 7.4a2 2 0 0 1-1.9 1.3H3Z' />
|
|
333
|
+
{:else}
|
|
334
|
+
<path d='M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z' />
|
|
335
|
+
{/if}
|
|
336
|
+
</svg>
|
|
337
|
+
{:else}
|
|
338
|
+
<span class='hai-code-tree__spacer' aria-hidden='true'></span>
|
|
339
|
+
<svg data-code-file aria-hidden='true' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'>
|
|
340
|
+
<path d='M6 3h8l4 4v14H6zM14 3v5h4M9 14l-1.5 1.5L9 17m6-3 1.5 1.5L15 17' />
|
|
341
|
+
</svg>
|
|
342
|
+
{/if}
|
|
343
|
+
<span class='hai-code-tree__name' title={node.name}>{node.name}</span>
|
|
344
|
+
{#if node.markers?.length}
|
|
345
|
+
<span class='hai-code-tree__markers' aria-hidden='true'>
|
|
346
|
+
{#each node.markers as marker, index (index)}
|
|
347
|
+
<span title={marker.label}>
|
|
348
|
+
<Badge variant={marker.tone === 'neutral'
|
|
349
|
+
? 'default'
|
|
350
|
+
: marker.tone} size='xs'>
|
|
351
|
+
{marker.text ?? '●'}
|
|
352
|
+
</Badge>
|
|
353
|
+
</span>
|
|
354
|
+
{/each}
|
|
355
|
+
</span>
|
|
356
|
+
{/if}
|
|
357
|
+
</div>
|
|
358
|
+
{#if open && node.kind === 'folder'}
|
|
359
|
+
<ul role='group'>
|
|
360
|
+
{#if nodeChildren?.length}
|
|
361
|
+
{@render branch(nodeChildren, [...ancestors, node])}
|
|
362
|
+
{:else}
|
|
363
|
+
<li id={statusId} role='none' class='hai-code-tree__status' style:--depth={ancestors.length + 1}>
|
|
364
|
+
{#if busy}
|
|
365
|
+
<Spinner size='xs' variant='ghost' />
|
|
366
|
+
<span>{uiM('code_treeLoading', undefined, locale)}</span>
|
|
367
|
+
{:else if error}
|
|
368
|
+
<span class='text-error'>{uiM('code_treeFolderFailed', { name: node.name }, locale)}</span>
|
|
369
|
+
<span class='hai-code-tree__retry'>{uiM('code_retry', undefined, locale)}</span>
|
|
370
|
+
{:else if nodeChildren}
|
|
371
|
+
<span>{uiM('code_treeEmpty', undefined, locale)}</span>
|
|
372
|
+
{/if}
|
|
373
|
+
</li>
|
|
374
|
+
{/if}
|
|
375
|
+
</ul>
|
|
376
|
+
{/if}
|
|
377
|
+
</li>
|
|
378
|
+
{/each}
|
|
379
|
+
{/snippet}
|
|
380
|
+
|
|
381
|
+
<div class='hai-code-tree'>
|
|
382
|
+
<div role='status' aria-live='polite' aria-atomic='true' class='sr-only'>
|
|
383
|
+
{#if announcement}{uiM(announcement.key, { name: announcement.name }, locale)}{/if}
|
|
384
|
+
</div>
|
|
385
|
+
|
|
386
|
+
{#if failed.has(null)}
|
|
387
|
+
<div class='hai-code-tree__root-status'>
|
|
388
|
+
<span class='text-error'>{uiM('code_treeFailed', undefined, locale)}</span>
|
|
389
|
+
<Button variant='ghost' size='xs' onclick={retryRoot}>{uiM('code_retry', undefined, locale)}</Button>
|
|
390
|
+
</div>
|
|
391
|
+
{:else if children.get(null)?.length === 0}
|
|
392
|
+
<div role='status' class='hai-code-tree__root-status'>{uiM('code_treeEmpty', undefined, locale)}</div>
|
|
393
|
+
{:else if !children.has(null)}
|
|
394
|
+
<div role='status' class='hai-code-tree__root-status'>
|
|
395
|
+
<Spinner size='xs' variant='ghost' />
|
|
396
|
+
<span>{uiM('code_treeLoading', undefined, locale)}</span>
|
|
397
|
+
</div>
|
|
398
|
+
{:else}
|
|
399
|
+
<ul role='tree' aria-label={uiM('code_tree', undefined, locale)}>
|
|
400
|
+
{@render branch(children.get(null) ?? [], [])}
|
|
401
|
+
</ul>
|
|
402
|
+
{/if}
|
|
403
|
+
</div>
|
|
404
|
+
|
|
405
|
+
<style>
|
|
406
|
+
.hai-code-tree {
|
|
407
|
+
min-inline-size: 0;
|
|
408
|
+
padding-inline: .25rem;
|
|
409
|
+
color: var(--color-base-content);
|
|
410
|
+
font-size: .8125rem;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
ul {
|
|
414
|
+
margin: 0;
|
|
415
|
+
padding: 0;
|
|
416
|
+
list-style: none;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
.hai-code-tree__item {
|
|
420
|
+
list-style: none;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
.hai-code-tree__item:focus {
|
|
424
|
+
outline: none;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
.hai-code-tree__item:focus-visible > .hai-code-tree__row {
|
|
428
|
+
outline: 2px solid var(--color-primary);
|
|
429
|
+
outline-offset: -2px;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
.hai-code-tree__row {
|
|
433
|
+
display: flex;
|
|
434
|
+
min-inline-size: 0;
|
|
435
|
+
min-block-size: 1.875rem;
|
|
436
|
+
align-items: center;
|
|
437
|
+
gap: .375rem;
|
|
438
|
+
padding-block: .125rem;
|
|
439
|
+
padding-inline: calc(.25rem + min(var(--depth, 0), 12) * .6875rem) .375rem;
|
|
440
|
+
border-radius: .25rem;
|
|
441
|
+
cursor: pointer;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
.hai-code-tree__row:hover {
|
|
445
|
+
background-color: color-mix(in oklab, var(--color-base-content) 7%, transparent);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
.hai-code-tree__item[aria-current='true'] > .hai-code-tree__row {
|
|
449
|
+
background-color: color-mix(in oklab, var(--color-primary) 18%, var(--color-base-100));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
.hai-code-tree__row svg,
|
|
453
|
+
.hai-code-tree__spacer {
|
|
454
|
+
inline-size: .9375rem;
|
|
455
|
+
block-size: .9375rem;
|
|
456
|
+
flex: none;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
.hai-code-tree__row svg[data-code-chevron],
|
|
460
|
+
.hai-code-tree__spacer {
|
|
461
|
+
inline-size: .75rem;
|
|
462
|
+
color: color-mix(in oklab, var(--color-base-content) 75%, transparent);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
.hai-code-tree__chevron-open {
|
|
466
|
+
transform: rotate(90deg);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
svg[data-code-folder] {
|
|
470
|
+
color: color-mix(in oklab, var(--color-warning) 53%, var(--color-base-content));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
svg[data-code-file] {
|
|
474
|
+
color: color-mix(in oklab, var(--color-info) 55%, var(--color-base-content));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
.hai-code-tree__name {
|
|
478
|
+
flex: 1 1 auto;
|
|
479
|
+
min-inline-size: 1ch;
|
|
480
|
+
overflow: hidden;
|
|
481
|
+
text-overflow: ellipsis;
|
|
482
|
+
white-space: nowrap;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
.hai-code-tree__markers {
|
|
486
|
+
display: flex;
|
|
487
|
+
flex: 0 1 auto;
|
|
488
|
+
min-inline-size: 0;
|
|
489
|
+
gap: .25rem;
|
|
490
|
+
max-inline-size: 55%;
|
|
491
|
+
overflow: hidden;
|
|
492
|
+
white-space: nowrap;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
.hai-code-tree__status,
|
|
496
|
+
.hai-code-tree__root-status {
|
|
497
|
+
display: flex;
|
|
498
|
+
flex-wrap: wrap;
|
|
499
|
+
align-items: center;
|
|
500
|
+
gap: .375rem;
|
|
501
|
+
padding: .25rem .375rem;
|
|
502
|
+
overflow-wrap: anywhere;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
.hai-code-tree__status {
|
|
506
|
+
padding-inline-start: calc(.875rem + min(var(--depth, 0), 12) * .6875rem);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
.hai-code-tree__retry {
|
|
510
|
+
text-decoration: underline;
|
|
511
|
+
cursor: pointer;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
@media (pointer: coarse) {
|
|
515
|
+
.hai-code-tree__row {
|
|
516
|
+
min-block-size: 2.75rem;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
</style>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CodeWorkspaceFileNode, CodeWorkspaceFolder, CodeWorkspaceSource } from './code-types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 文件树的内部加载、选中及打开回调。
|
|
4
|
+
*
|
|
5
|
+
* @example 模块内部上下文
|
|
6
|
+
* ```ts
|
|
7
|
+
* const props: Props = {
|
|
8
|
+
* source: { listChildren: () => [], readFile: () => ({ text: '' }) },
|
|
9
|
+
* onopen: node => node.id,
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
interface Props {
|
|
14
|
+
source: CodeWorkspaceSource;
|
|
15
|
+
activeId?: string;
|
|
16
|
+
locale?: string;
|
|
17
|
+
reveal?: {
|
|
18
|
+
ancestors: readonly CodeWorkspaceFolder[];
|
|
19
|
+
focus?: boolean;
|
|
20
|
+
request: number;
|
|
21
|
+
};
|
|
22
|
+
onopen: (node: CodeWorkspaceFileNode, ancestors: readonly CodeWorkspaceFolder[]) => void;
|
|
23
|
+
}
|
|
24
|
+
declare const CodeWorkspaceTree: import("svelte").Component<Props, {}, "">;
|
|
25
|
+
type CodeWorkspaceTree = ReturnType<typeof CodeWorkspaceTree>;
|
|
26
|
+
export default CodeWorkspaceTree;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CodeWorkspaceTheme } from './code-types.js';
|
|
2
|
+
import { editor, Uri } from 'monaco-editor';
|
|
3
|
+
/**
|
|
4
|
+
* 将工作区主题转换为已注册的 Monaco 主题名称。
|
|
5
|
+
*
|
|
6
|
+
* @example 模块内部上下文
|
|
7
|
+
* ```ts
|
|
8
|
+
* const theme = workspaceThemeName('inherit', document.documentElement)
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare function workspaceThemeName(theme: CodeWorkspaceTheme, host: HTMLElement): string;
|
|
12
|
+
export { editor, Uri };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { editor, Uri } from 'monaco-editor';
|
|
2
|
+
// 使用独立入口和静态 URL,由宿主构建工具生成同源 worker 资源。
|
|
3
|
+
function getWorker(_id, label) {
|
|
4
|
+
switch (label) {
|
|
5
|
+
case 'css':
|
|
6
|
+
case 'less':
|
|
7
|
+
case 'scss':
|
|
8
|
+
return new Worker(new URL('./workers/css.worker.js', import.meta.url), { type: 'module' });
|
|
9
|
+
case 'html':
|
|
10
|
+
case 'handlebars':
|
|
11
|
+
case 'razor':
|
|
12
|
+
return new Worker(new URL('./workers/html.worker.js', import.meta.url), { type: 'module' });
|
|
13
|
+
case 'json':
|
|
14
|
+
return new Worker(new URL('./workers/json.worker.js', import.meta.url), { type: 'module' });
|
|
15
|
+
case 'typescript':
|
|
16
|
+
case 'javascript':
|
|
17
|
+
return new Worker(new URL('./workers/ts.worker.js', import.meta.url), { type: 'module' });
|
|
18
|
+
default:
|
|
19
|
+
return new Worker(new URL('./workers/editor.worker.js', import.meta.url), { type: 'module' });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// 保留宿主已有的 worker 设置。
|
|
23
|
+
if (!globalThis.MonacoEnvironment?.getWorker && !globalThis.MonacoEnvironment?.getWorkerUrl) {
|
|
24
|
+
globalThis.MonacoEnvironment = {
|
|
25
|
+
...globalThis.MonacoEnvironment,
|
|
26
|
+
getWorker,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const builtinThemes = {
|
|
30
|
+
'vs-light': 'vs',
|
|
31
|
+
'vs-dark': 'vs-dark',
|
|
32
|
+
'hc-dark': 'hc-black',
|
|
33
|
+
'hc-light': 'hc-light',
|
|
34
|
+
};
|
|
35
|
+
let githubDefined = false;
|
|
36
|
+
let customSignature;
|
|
37
|
+
function inheritedTheme(host) {
|
|
38
|
+
const scheme = getComputedStyle(host).colorScheme;
|
|
39
|
+
const dark = scheme === 'dark' || (scheme !== 'light' && matchMedia('(prefers-color-scheme: dark)').matches);
|
|
40
|
+
if (matchMedia('(forced-colors: active)').matches)
|
|
41
|
+
return dark ? 'hc-black' : 'hc-light';
|
|
42
|
+
return dark ? 'vs-dark' : 'vs';
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 将工作区主题转换为已注册的 Monaco 主题名称。
|
|
46
|
+
*
|
|
47
|
+
* @example 模块内部上下文
|
|
48
|
+
* ```ts
|
|
49
|
+
* const theme = workspaceThemeName('inherit', document.documentElement)
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function workspaceThemeName(theme, host) {
|
|
53
|
+
if (theme === 'inherit')
|
|
54
|
+
return inheritedTheme(host);
|
|
55
|
+
if (theme === 'github-dark') {
|
|
56
|
+
if (!githubDefined) {
|
|
57
|
+
editor.defineTheme('hai-github-dark', {
|
|
58
|
+
base: 'vs-dark',
|
|
59
|
+
inherit: true,
|
|
60
|
+
colors: {
|
|
61
|
+
'editor.background': '#0D1117',
|
|
62
|
+
'editor.foreground': '#E6EDF3',
|
|
63
|
+
'editor.lineHighlightBackground': '#161B22',
|
|
64
|
+
'editorLineNumber.foreground': '#8B949E',
|
|
65
|
+
'editorLineNumber.activeForeground': '#E6EDF3',
|
|
66
|
+
'editor.selectionBackground': '#264F78',
|
|
67
|
+
},
|
|
68
|
+
rules: [
|
|
69
|
+
{ token: 'comment', foreground: '8B949E' },
|
|
70
|
+
{ token: 'keyword', foreground: 'FF7B72' },
|
|
71
|
+
{ token: 'string', foreground: 'A5D6FF' },
|
|
72
|
+
{ token: 'type', foreground: 'D2A8FF' },
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
githubDefined = true;
|
|
76
|
+
}
|
|
77
|
+
return 'hai-github-dark';
|
|
78
|
+
}
|
|
79
|
+
if (typeof theme === 'string')
|
|
80
|
+
return builtinThemes[theme];
|
|
81
|
+
const signature = JSON.stringify(theme);
|
|
82
|
+
if (signature !== customSignature) {
|
|
83
|
+
editor.defineTheme('hai-code-custom', {
|
|
84
|
+
base: builtinThemes[theme.base],
|
|
85
|
+
inherit: true,
|
|
86
|
+
colors: { ...theme.colors },
|
|
87
|
+
rules: theme.tokens?.map(token => ({ ...token })) ?? [],
|
|
88
|
+
});
|
|
89
|
+
customSignature = signature;
|
|
90
|
+
}
|
|
91
|
+
return 'hai-code-custom';
|
|
92
|
+
}
|
|
93
|
+
export { editor, Uri };
|