@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,737 @@
|
|
|
1
|
+
<script module lang='ts'>
|
|
2
|
+
/**
|
|
3
|
+
* 只在浏览器加载的 Monaco 运行时。
|
|
4
|
+
*
|
|
5
|
+
* @example 模块内部上下文
|
|
6
|
+
* ```ts
|
|
7
|
+
* const runtime: Promise<Runtime> = import('./code-editor.js')
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
type Runtime = typeof import('./code-editor.js')
|
|
11
|
+
let runtimePromise: Promise<Runtime> | undefined
|
|
12
|
+
|
|
13
|
+
function loadRuntime(locale: string): Promise<Runtime> {
|
|
14
|
+
return runtimePromise ??= (async () => {
|
|
15
|
+
// Monaco 在首次加载时读取页面级语言包。遵循宿主已有的 Monaco 语言设置。
|
|
16
|
+
if (!('_VSCODE_NLS_MESSAGES' in globalThis)) {
|
|
17
|
+
const language = locale.toLowerCase().split('-')[0]
|
|
18
|
+
if (language === 'zh')
|
|
19
|
+
await import('monaco-editor/nls/lang/zh-cn.js')
|
|
20
|
+
else if (language === 'ja')
|
|
21
|
+
await import('monaco-editor/nls/lang/ja.js')
|
|
22
|
+
}
|
|
23
|
+
return import('./code-editor.js')
|
|
24
|
+
})()
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script lang='ts'>
|
|
29
|
+
import type { IDisposable, editor as MonacoEditor } from 'monaco-editor'
|
|
30
|
+
import type { Snippet } from 'svelte'
|
|
31
|
+
import type {
|
|
32
|
+
CodeWorkspaceAnnotationContext,
|
|
33
|
+
CodeWorkspaceDisplay,
|
|
34
|
+
CodeWorkspaceFile,
|
|
35
|
+
CodeWorkspaceRange,
|
|
36
|
+
CodeWorkspaceSelection,
|
|
37
|
+
CodeWorkspaceSelectionContext,
|
|
38
|
+
CodeWorkspaceTheme,
|
|
39
|
+
} from './code-types.js'
|
|
40
|
+
import { onMount, tick, untrack } from 'svelte'
|
|
41
|
+
import { getUILocale, uiM } from '../../../messages.js'
|
|
42
|
+
import { isMobile } from '../../../utils/platform.svelte.js'
|
|
43
|
+
import Button from '../../primitives/Button.svelte'
|
|
44
|
+
import CodeWorkspaceGutter from './CodeWorkspaceGutter.svelte'
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 编辑器内部的文件、显示选项和宿主回调。
|
|
48
|
+
*
|
|
49
|
+
* @example 模块内部上下文
|
|
50
|
+
* ```ts
|
|
51
|
+
* const props: Props = {
|
|
52
|
+
* file: { node: { kind: 'file', id: 'readme', name: 'README.md' }, text: '# Hello' },
|
|
53
|
+
* options: { lineNumbers: true },
|
|
54
|
+
* theme: 'inherit',
|
|
55
|
+
* onselection: selection => selection?.text,
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
interface Props {
|
|
60
|
+
file: CodeWorkspaceFile
|
|
61
|
+
options: CodeWorkspaceDisplay
|
|
62
|
+
theme: CodeWorkspaceTheme
|
|
63
|
+
navigation?: { range: CodeWorkspaceRange, request: number, focus?: boolean }
|
|
64
|
+
annotationIcon?: Snippet<[CodeWorkspaceAnnotationContext]>
|
|
65
|
+
annotationHover?: Snippet<[CodeWorkspaceAnnotationContext]>
|
|
66
|
+
selectionActions?: Snippet<[CodeWorkspaceSelectionContext]>
|
|
67
|
+
onselection: (selection: CodeWorkspaceSelection | null) => void
|
|
68
|
+
onready?: (ready: boolean) => void
|
|
69
|
+
locale?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const {
|
|
73
|
+
file,
|
|
74
|
+
options,
|
|
75
|
+
theme,
|
|
76
|
+
navigation,
|
|
77
|
+
annotationIcon,
|
|
78
|
+
annotationHover,
|
|
79
|
+
selectionActions,
|
|
80
|
+
onselection,
|
|
81
|
+
onready,
|
|
82
|
+
locale = getUILocale(),
|
|
83
|
+
}: Props = $props()
|
|
84
|
+
|
|
85
|
+
const editorId = $props.id()
|
|
86
|
+
/**
|
|
87
|
+
* 纯文本视图按文件记忆滚动与选区偏移。
|
|
88
|
+
*
|
|
89
|
+
* @example 模块内部上下文
|
|
90
|
+
* ```ts
|
|
91
|
+
* const view: PlainView = { top: 0, left: 0, start: 2, end: 7 }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
type PlainView = { top: number, left: number, start: number, end: number }
|
|
95
|
+
const viewLimit = 24
|
|
96
|
+
const editorViews = new Map<string, MonacoEditor.ICodeEditorViewState>()
|
|
97
|
+
const plainViews = new Map<string, PlainView>()
|
|
98
|
+
const fileKey = $derived(JSON.stringify([file.node.id, file.version ?? file.node.version ?? '']))
|
|
99
|
+
const interactiveAnnotations = $derived(file.annotations?.filter(annotation =>
|
|
100
|
+
annotation.display !== 'range' && (annotation.hoverText !== undefined || annotationIcon || annotationHover),
|
|
101
|
+
) ?? [])
|
|
102
|
+
const editorLabel = $derived(`${uiM('code_editor', undefined, locale)} · ${file.node.name}`)
|
|
103
|
+
const plainText = $derived(file.text.replace(/\r\n?/g, '\n'))
|
|
104
|
+
const lineStarts = $derived([
|
|
105
|
+
0,
|
|
106
|
+
...Array.from(plainText.matchAll(/\n/g), match => match.index + 1),
|
|
107
|
+
])
|
|
108
|
+
|
|
109
|
+
let host: HTMLDivElement | undefined = $state()
|
|
110
|
+
let editorHost: HTMLDivElement
|
|
111
|
+
let plain: HTMLTextAreaElement | undefined = $state()
|
|
112
|
+
let actions: HTMLDivElement | undefined = $state()
|
|
113
|
+
let actionsWidth = $state(0)
|
|
114
|
+
let actionsHeight = $state(0)
|
|
115
|
+
let editor = $state.raw<MonacoEditor.IStandaloneCodeEditor>()
|
|
116
|
+
let runtime: Runtime | undefined
|
|
117
|
+
let collection: MonacoEditor.IEditorDecorationsCollection | undefined
|
|
118
|
+
let mode = $state<'loading' | 'editor' | 'plain'>('loading')
|
|
119
|
+
let selection = $state<CodeWorkspaceSelection | null>(null)
|
|
120
|
+
let anchor = $state<{ top: number, left: number, height: number } | null>(null)
|
|
121
|
+
let currentFileKey: string | undefined
|
|
122
|
+
let currentText: string | undefined
|
|
123
|
+
let currentLanguage: string | undefined
|
|
124
|
+
let currentPlainKey: string | undefined
|
|
125
|
+
let lastNavigation: number | undefined
|
|
126
|
+
let changeFromWorkspace = false
|
|
127
|
+
let actionsDismissed = $state(false)
|
|
128
|
+
let plainTool = $state<'find' | 'goto' | null>(null)
|
|
129
|
+
let plainInput: HTMLInputElement | undefined = $state()
|
|
130
|
+
let plainQuery = $state('')
|
|
131
|
+
let plainFindIndex = $state(-1)
|
|
132
|
+
const plainMatches = $derived.by(() => {
|
|
133
|
+
if (plainTool !== 'find' || !plainQuery)
|
|
134
|
+
return []
|
|
135
|
+
const offsets: number[] = []
|
|
136
|
+
let start = plainText.indexOf(plainQuery)
|
|
137
|
+
while (start !== -1) {
|
|
138
|
+
offsets.push(start)
|
|
139
|
+
start = plainText.indexOf(plainQuery, start + plainQuery.length)
|
|
140
|
+
}
|
|
141
|
+
return offsets
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const canShowActions = $derived(!!selectionActions && !!selection && !actionsDismissed && (mode === 'plain' || !!anchor))
|
|
145
|
+
const actionsLeft = $derived(anchor && host
|
|
146
|
+
? Math.max(4, Math.min(anchor.left, host.clientWidth - actionsWidth - 4))
|
|
147
|
+
: undefined)
|
|
148
|
+
const actionsTop = $derived(anchor
|
|
149
|
+
? (anchor.top > actionsHeight + 6
|
|
150
|
+
? anchor.top - actionsHeight - 4
|
|
151
|
+
: anchor.top + anchor.height + 4)
|
|
152
|
+
: undefined)
|
|
153
|
+
|
|
154
|
+
function remember<T>(views: Map<string, T>, key: string, value: T): void {
|
|
155
|
+
views.delete(key)
|
|
156
|
+
views.set(key, value)
|
|
157
|
+
if (views.size > viewLimit) {
|
|
158
|
+
const oldest = views.keys().next().value
|
|
159
|
+
if (oldest !== undefined)
|
|
160
|
+
views.delete(oldest)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function publish(next: CodeWorkspaceSelection | null): void {
|
|
165
|
+
selection = next
|
|
166
|
+
actionsDismissed = false
|
|
167
|
+
untrack(() => onselection(next))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function readEditorSelection(): void {
|
|
171
|
+
if (changeFromWorkspace || !editor)
|
|
172
|
+
return
|
|
173
|
+
const selected = editor.getSelection()
|
|
174
|
+
const model = editor.getModel()
|
|
175
|
+
if (selected && model && !selected.isEmpty()) {
|
|
176
|
+
publish({
|
|
177
|
+
range: {
|
|
178
|
+
startLine: selected.startLineNumber,
|
|
179
|
+
startColumn: selected.startColumn,
|
|
180
|
+
endLine: selected.endLineNumber,
|
|
181
|
+
endColumn: selected.endColumn,
|
|
182
|
+
},
|
|
183
|
+
text: model.getValueInRange(selected),
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
publish(null)
|
|
188
|
+
}
|
|
189
|
+
positionActions()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function positionActions(): void {
|
|
193
|
+
const instance = editor
|
|
194
|
+
const cursor = instance?.getPosition()
|
|
195
|
+
if (!cursor || !instance || instance.getSelection()?.isEmpty()) {
|
|
196
|
+
anchor = null
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
const position = instance.getScrolledVisiblePosition(cursor)
|
|
200
|
+
const layout = instance.getLayoutInfo()
|
|
201
|
+
anchor = position && position.top >= 0 && position.top < layout.height
|
|
202
|
+
? {
|
|
203
|
+
top: position.top,
|
|
204
|
+
left: Math.max(layout.contentLeft, Math.min(position.left, layout.contentLeft + layout.contentWidth)),
|
|
205
|
+
height: position.height,
|
|
206
|
+
}
|
|
207
|
+
: null
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function themeChanged(): void {
|
|
211
|
+
if (editor && runtime && host)
|
|
212
|
+
runtime.editor.setTheme(runtime.workspaceThemeName(theme, host))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function syncModel(next: CodeWorkspaceFile, key: string): void {
|
|
216
|
+
if (!editor || !runtime)
|
|
217
|
+
return
|
|
218
|
+
|
|
219
|
+
const previous = editor.getModel()
|
|
220
|
+
if (previous && currentFileKey === key && currentText === next.text && currentLanguage === next.language)
|
|
221
|
+
return
|
|
222
|
+
|
|
223
|
+
const view = editor.saveViewState()
|
|
224
|
+
if (currentFileKey !== undefined && view)
|
|
225
|
+
remember(editorViews, currentFileKey, view)
|
|
226
|
+
|
|
227
|
+
changeFromWorkspace = true
|
|
228
|
+
try {
|
|
229
|
+
collection?.clear()
|
|
230
|
+
editor.setModel(null)
|
|
231
|
+
previous?.dispose()
|
|
232
|
+
const uri = runtime.Uri.from({
|
|
233
|
+
scheme: 'inmemory',
|
|
234
|
+
authority: `hai-code-workspace-${editorId}`,
|
|
235
|
+
path: `/${next.node.name}`,
|
|
236
|
+
query: key,
|
|
237
|
+
})
|
|
238
|
+
editor.setModel(runtime.editor.createModel(next.text, next.language, uri))
|
|
239
|
+
const saved = editorViews.get(key)
|
|
240
|
+
if (saved)
|
|
241
|
+
editor.restoreViewState(saved)
|
|
242
|
+
currentFileKey = key
|
|
243
|
+
currentText = next.text
|
|
244
|
+
currentLanguage = next.language
|
|
245
|
+
}
|
|
246
|
+
finally {
|
|
247
|
+
changeFromWorkspace = false
|
|
248
|
+
}
|
|
249
|
+
readEditorSelection()
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function monacoRange(range: CodeWorkspaceRange): import('monaco-editor').IRange {
|
|
253
|
+
return {
|
|
254
|
+
startLineNumber: range.startLine,
|
|
255
|
+
startColumn: range.startColumn,
|
|
256
|
+
endLineNumber: range.endLine,
|
|
257
|
+
endColumn: range.endColumn,
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function syncAnnotations(next: CodeWorkspaceFile): void {
|
|
262
|
+
if (!editor || !collection)
|
|
263
|
+
return
|
|
264
|
+
const model = editor.getModel()
|
|
265
|
+
if (!model)
|
|
266
|
+
return
|
|
267
|
+
|
|
268
|
+
const decorations: MonacoEditor.IModelDeltaDecoration[] = []
|
|
269
|
+
for (const annotation of next.annotations ?? []) {
|
|
270
|
+
const range = model.validateRange(monacoRange(annotation.range))
|
|
271
|
+
const tone = annotation.tone ?? 'neutral'
|
|
272
|
+
const display = annotation.display ?? 'gutter'
|
|
273
|
+
if (display !== 'range') {
|
|
274
|
+
// 排除跨行区间终点所在的空行前缀。
|
|
275
|
+
const gutterRange = range.endColumn === 1 && range.endLineNumber > range.startLineNumber
|
|
276
|
+
? {
|
|
277
|
+
...range,
|
|
278
|
+
endLineNumber: range.endLineNumber - 1,
|
|
279
|
+
endColumn: model.getLineMaxColumn(range.endLineNumber - 1),
|
|
280
|
+
}
|
|
281
|
+
: range
|
|
282
|
+
decorations.push({
|
|
283
|
+
range: gutterRange,
|
|
284
|
+
options: {
|
|
285
|
+
linesDecorationsClassName: `hai-code-gutter hai-code-${tone}`,
|
|
286
|
+
linesDecorationsTooltip: annotation.label,
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
if (display !== 'gutter') {
|
|
291
|
+
decorations.push({
|
|
292
|
+
range,
|
|
293
|
+
options: {
|
|
294
|
+
className: `hai-code-range hai-code-${tone}`,
|
|
295
|
+
isWholeLine: range.isEmpty(),
|
|
296
|
+
},
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
collection.set(decorations)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function offsetAt(line: number, column: number): number {
|
|
304
|
+
const i = Math.max(0, Math.min(lineStarts.length - 1, line - 1))
|
|
305
|
+
const end = i + 1 < lineStarts.length
|
|
306
|
+
? lineStarts[i + 1] - 1
|
|
307
|
+
: plainText.length
|
|
308
|
+
return Math.max(lineStarts[i], Math.min(end, lineStarts[i] + column - 1))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function positionAt(offset: number): { line: number, column: number } {
|
|
312
|
+
let low = 0
|
|
313
|
+
let high = lineStarts.length
|
|
314
|
+
while (low + 1 < high) {
|
|
315
|
+
const middle = Math.floor((low + high) / 2)
|
|
316
|
+
if (lineStarts[middle] > offset)
|
|
317
|
+
high = middle
|
|
318
|
+
else
|
|
319
|
+
low = middle
|
|
320
|
+
}
|
|
321
|
+
return { line: low + 1, column: offset - lineStarts[low] + 1 }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function rememberPlainView(): void {
|
|
325
|
+
if (plain && currentPlainKey !== undefined) {
|
|
326
|
+
remember(plainViews, currentPlainKey, {
|
|
327
|
+
top: plain.scrollTop,
|
|
328
|
+
left: plain.scrollLeft,
|
|
329
|
+
start: plain.selectionStart,
|
|
330
|
+
end: plain.selectionEnd,
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function readPlainSelection(): void {
|
|
336
|
+
if (!plain || plain.selectionStart === plain.selectionEnd) {
|
|
337
|
+
publish(null)
|
|
338
|
+
return
|
|
339
|
+
}
|
|
340
|
+
const start = positionAt(plain.selectionStart)
|
|
341
|
+
const end = positionAt(plain.selectionEnd)
|
|
342
|
+
publish({
|
|
343
|
+
range: {
|
|
344
|
+
startLine: start.line,
|
|
345
|
+
startColumn: start.column,
|
|
346
|
+
endLine: end.line,
|
|
347
|
+
endColumn: end.column,
|
|
348
|
+
},
|
|
349
|
+
text: plainText.slice(plain.selectionStart, plain.selectionEnd),
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function restorePlainView(node: HTMLTextAreaElement, key: string): void {
|
|
354
|
+
currentPlainKey = key
|
|
355
|
+
const previous = plainViews.get(key)
|
|
356
|
+
node.setSelectionRange(previous?.start ?? 0, previous?.end ?? 0)
|
|
357
|
+
node.scrollTop = previous?.top ?? 0
|
|
358
|
+
node.scrollLeft = previous?.left ?? 0
|
|
359
|
+
readPlainSelection()
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function revealPlain(start: number, end: number, focus = false): void {
|
|
363
|
+
if (!plain)
|
|
364
|
+
return
|
|
365
|
+
plain.setSelectionRange(start, end)
|
|
366
|
+
const lineHeight = Number.parseFloat(getComputedStyle(plain).lineHeight)
|
|
367
|
+
plain.scrollTop = Math.max(0, positionAt(start).line - 2) * lineHeight
|
|
368
|
+
if (focus)
|
|
369
|
+
plain.focus({ preventScroll: true })
|
|
370
|
+
readPlainSelection()
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function applyNavigation(next: NonNullable<Props['navigation']>): void {
|
|
374
|
+
if (lastNavigation === next.request)
|
|
375
|
+
return
|
|
376
|
+
if (editor) {
|
|
377
|
+
const range = editor.getModel()?.validateRange(monacoRange(next.range))
|
|
378
|
+
if (!range)
|
|
379
|
+
return
|
|
380
|
+
editor.setSelection(range)
|
|
381
|
+
editor.revealRangeInCenterIfOutsideViewport(range)
|
|
382
|
+
if (next.focus)
|
|
383
|
+
editor.focus()
|
|
384
|
+
}
|
|
385
|
+
else if (plain) {
|
|
386
|
+
revealPlain(
|
|
387
|
+
offsetAt(next.range.startLine, next.range.startColumn),
|
|
388
|
+
offsetAt(next.range.endLine, next.range.endColumn),
|
|
389
|
+
next.focus,
|
|
390
|
+
)
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
lastNavigation = next.request
|
|
396
|
+
actionsDismissed = true
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function showPlainTool(tool: NonNullable<typeof plainTool>): void {
|
|
400
|
+
if (mode !== 'plain')
|
|
401
|
+
return
|
|
402
|
+
plainTool = tool
|
|
403
|
+
plainQuery = ''
|
|
404
|
+
plainFindIndex = -1
|
|
405
|
+
void tick().then(() => plainInput?.focus())
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* 打开当前文件查找入口。
|
|
410
|
+
*
|
|
411
|
+
* @example 模块内部上下文
|
|
412
|
+
* ```ts
|
|
413
|
+
* find()
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
export function find(): void {
|
|
417
|
+
if (editor)
|
|
418
|
+
void editor.getAction('actions.find')?.run()
|
|
419
|
+
else
|
|
420
|
+
showPlainTool('find')
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* 打开当前文件跳行入口。
|
|
425
|
+
*
|
|
426
|
+
* @example 模块内部上下文
|
|
427
|
+
* ```ts
|
|
428
|
+
* goToLine()
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
export function goToLine(): void {
|
|
432
|
+
if (editor) {
|
|
433
|
+
// Monaco 的跳行控件挂到当前聚焦的编辑器,工具栏按钮需先交回焦点。
|
|
434
|
+
editor.focus()
|
|
435
|
+
void editor.getAction('editor.action.gotoLine')?.run()
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
showPlainTool('goto')
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function applyPlainTool(reverse = false): void {
|
|
443
|
+
if (plainTool === 'find' && plainMatches.length) {
|
|
444
|
+
const count = plainMatches.length
|
|
445
|
+
plainFindIndex = plainFindIndex < 0
|
|
446
|
+
? (reverse
|
|
447
|
+
? count - 1
|
|
448
|
+
: 0)
|
|
449
|
+
: (plainFindIndex + (reverse
|
|
450
|
+
? -1
|
|
451
|
+
: 1) + count) % count
|
|
452
|
+
const start = plainMatches[plainFindIndex]
|
|
453
|
+
revealPlain(start, start + plainQuery.length)
|
|
454
|
+
}
|
|
455
|
+
else if (plainTool === 'goto') {
|
|
456
|
+
const line = plainInput?.valueAsNumber
|
|
457
|
+
if (!line || !plainInput?.reportValidity())
|
|
458
|
+
return
|
|
459
|
+
const start = offsetAt(line, 1)
|
|
460
|
+
revealPlain(start, start, true)
|
|
461
|
+
plainTool = null
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function focusActions(): void {
|
|
466
|
+
actionsDismissed = false
|
|
467
|
+
void tick().then(() => {
|
|
468
|
+
const focusable = actions?.querySelector<HTMLElement>(
|
|
469
|
+
'button:not([disabled]),a[href],input:not([disabled]),[tabindex="0"]',
|
|
470
|
+
)
|
|
471
|
+
const target = focusable ?? actions
|
|
472
|
+
target?.focus()
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function handleKeyboard(event: KeyboardEvent): void {
|
|
477
|
+
if (event.altKey && event.shiftKey && event.key === 'Enter' && selectionActions && selection) {
|
|
478
|
+
event.preventDefault()
|
|
479
|
+
event.stopPropagation()
|
|
480
|
+
focusActions()
|
|
481
|
+
}
|
|
482
|
+
else if (event.key === 'Escape' && actions
|
|
483
|
+
&& (actions.contains(document.activeElement) || editor?.hasTextFocus() || plain === document.activeElement)) {
|
|
484
|
+
event.preventDefault()
|
|
485
|
+
event.stopPropagation()
|
|
486
|
+
actionsDismissed = true
|
|
487
|
+
if (editor)
|
|
488
|
+
editor.focus()
|
|
489
|
+
else
|
|
490
|
+
plain?.focus()
|
|
491
|
+
}
|
|
492
|
+
else if (mode !== 'loading' && (event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey) {
|
|
493
|
+
const key = event.key.toLowerCase()
|
|
494
|
+
if (key === 'g' || (mode === 'plain' && key === 'f')) {
|
|
495
|
+
event.preventDefault()
|
|
496
|
+
event.stopPropagation()
|
|
497
|
+
if (key === 'g')
|
|
498
|
+
goToLine()
|
|
499
|
+
else
|
|
500
|
+
find()
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
onMount(() => {
|
|
506
|
+
let disposed = false
|
|
507
|
+
let cleanupEditor: (() => void) | undefined
|
|
508
|
+
const container = host
|
|
509
|
+
if (!container || isMobile()) {
|
|
510
|
+
mode = 'plain'
|
|
511
|
+
onready?.(true)
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
void loadRuntime(locale).then((module) => {
|
|
515
|
+
if (disposed)
|
|
516
|
+
return
|
|
517
|
+
runtime = module
|
|
518
|
+
const created = module.editor.create(editorHost, {
|
|
519
|
+
model: null,
|
|
520
|
+
theme: module.workspaceThemeName(theme, container),
|
|
521
|
+
autoDetectHighContrast: false,
|
|
522
|
+
readOnly: true,
|
|
523
|
+
domReadOnly: true,
|
|
524
|
+
automaticLayout: true,
|
|
525
|
+
tabFocusMode: true,
|
|
526
|
+
scrollBeyondLastLine: false,
|
|
527
|
+
renderValidationDecorations: 'off',
|
|
528
|
+
links: false,
|
|
529
|
+
dragAndDrop: false,
|
|
530
|
+
dropIntoEditor: { enabled: false },
|
|
531
|
+
padding: { top: 8, bottom: 8 },
|
|
532
|
+
})
|
|
533
|
+
const events: IDisposable[] = [
|
|
534
|
+
created.onDidChangeCursorSelection(readEditorSelection),
|
|
535
|
+
created.onDidLayoutChange(positionActions),
|
|
536
|
+
created.onDidScrollChange(positionActions),
|
|
537
|
+
]
|
|
538
|
+
const observer = new MutationObserver(() => {
|
|
539
|
+
if (theme === 'inherit')
|
|
540
|
+
themeChanged()
|
|
541
|
+
})
|
|
542
|
+
// 跟随宿主的容器主题及浏览器系统外观切换。
|
|
543
|
+
for (let node: HTMLElement | null = container; node; node = node.parentElement) {
|
|
544
|
+
observer.observe(node, {
|
|
545
|
+
attributes: true,
|
|
546
|
+
attributeFilter: ['data-theme', 'class', 'style'],
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
const colorQueries = [
|
|
550
|
+
matchMedia('(prefers-color-scheme: dark)'),
|
|
551
|
+
matchMedia('(forced-colors: active)'),
|
|
552
|
+
]
|
|
553
|
+
for (const query of colorQueries)
|
|
554
|
+
query.addEventListener('change', themeChanged)
|
|
555
|
+
cleanupEditor = () => {
|
|
556
|
+
observer.disconnect()
|
|
557
|
+
for (const query of colorQueries)
|
|
558
|
+
query.removeEventListener('change', themeChanged)
|
|
559
|
+
for (const event of events)
|
|
560
|
+
event.dispose()
|
|
561
|
+
const model = created.getModel()
|
|
562
|
+
created.dispose()
|
|
563
|
+
model?.dispose()
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
collection = created.createDecorationsCollection()
|
|
567
|
+
editor = created
|
|
568
|
+
mode = 'editor'
|
|
569
|
+
onready?.(true)
|
|
570
|
+
}).catch(() => {
|
|
571
|
+
cleanupEditor?.()
|
|
572
|
+
cleanupEditor = undefined
|
|
573
|
+
if (!disposed) {
|
|
574
|
+
editor = undefined
|
|
575
|
+
mode = 'plain'
|
|
576
|
+
onready?.(true)
|
|
577
|
+
}
|
|
578
|
+
})
|
|
579
|
+
}
|
|
580
|
+
return () => {
|
|
581
|
+
disposed = true
|
|
582
|
+
cleanupEditor?.()
|
|
583
|
+
onready?.(false)
|
|
584
|
+
}
|
|
585
|
+
})
|
|
586
|
+
|
|
587
|
+
$effect(() => {
|
|
588
|
+
const next = file
|
|
589
|
+
const key = fileKey
|
|
590
|
+
if (editor) {
|
|
591
|
+
// 确保即使宿主只更新标注数组也会重新渲染。
|
|
592
|
+
syncModel(next, key)
|
|
593
|
+
syncAnnotations(next)
|
|
594
|
+
}
|
|
595
|
+
})
|
|
596
|
+
|
|
597
|
+
$effect(() => {
|
|
598
|
+
const label = editorLabel
|
|
599
|
+
const { lineNumbers, minimap, wordWrap } = options
|
|
600
|
+
editor?.updateOptions({
|
|
601
|
+
ariaLabel: label,
|
|
602
|
+
lineNumbers: lineNumbers === 'relative'
|
|
603
|
+
? 'relative'
|
|
604
|
+
: lineNumbers === false
|
|
605
|
+
? 'off'
|
|
606
|
+
: 'on',
|
|
607
|
+
minimap: { enabled: minimap !== false },
|
|
608
|
+
glyphMargin: interactiveAnnotations.length > 0,
|
|
609
|
+
wordWrap: wordWrap ? 'on' : 'off',
|
|
610
|
+
})
|
|
611
|
+
})
|
|
612
|
+
|
|
613
|
+
$effect(() => {
|
|
614
|
+
themeChanged()
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
$effect.pre(() => {
|
|
618
|
+
const key = fileKey
|
|
619
|
+
if (mode === 'plain' && currentPlainKey !== undefined && currentPlainKey !== key)
|
|
620
|
+
untrack(rememberPlainView)
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
$effect(() => {
|
|
624
|
+
const target = navigation
|
|
625
|
+
const key = fileKey
|
|
626
|
+
const node = plain
|
|
627
|
+
if (mode === 'plain' && node && key !== currentPlainKey)
|
|
628
|
+
untrack(() => restorePlainView(node, key))
|
|
629
|
+
if (target && mode !== 'loading')
|
|
630
|
+
untrack(() => applyNavigation(target))
|
|
631
|
+
})
|
|
632
|
+
</script>
|
|
633
|
+
|
|
634
|
+
<div bind:this={host} class='hai-code-editor' onkeydowncapture={handleKeyboard}>
|
|
635
|
+
<div class='hai-code-editor-surface' bind:this={editorHost} hidden={mode !== 'editor'}></div>
|
|
636
|
+
{#if mode === 'loading'}
|
|
637
|
+
<p class='hai-code-editor-message' role='status'>{uiM('code_editorLoading', undefined, locale)}</p>
|
|
638
|
+
{:else if mode === 'plain'}
|
|
639
|
+
<p class='hai-code-editor-message' role='status'>{uiM('code_editorFallback', undefined, locale)}</p>
|
|
640
|
+
{#if plainTool}
|
|
641
|
+
<form class='hai-code-plain-tools' onsubmit={(event) => {
|
|
642
|
+
event.preventDefault()
|
|
643
|
+
applyPlainTool()
|
|
644
|
+
}}>
|
|
645
|
+
{#if plainTool === 'find'}
|
|
646
|
+
<input
|
|
647
|
+
bind:this={plainInput}
|
|
648
|
+
bind:value={plainQuery}
|
|
649
|
+
aria-label={uiM('code_find', undefined, locale)}
|
|
650
|
+
class='input input-bordered input-sm'
|
|
651
|
+
type='search'
|
|
652
|
+
oninput={() => { plainFindIndex = -1 }}
|
|
653
|
+
onkeydown={(event) => {
|
|
654
|
+
if (event.key === 'Enter' && event.shiftKey) {
|
|
655
|
+
event.preventDefault()
|
|
656
|
+
applyPlainTool(true)
|
|
657
|
+
}
|
|
658
|
+
}}
|
|
659
|
+
/>
|
|
660
|
+
<output aria-live='polite'>{plainFindIndex + 1}/{plainMatches.length}</output>
|
|
661
|
+
{:else}
|
|
662
|
+
<input
|
|
663
|
+
bind:this={plainInput}
|
|
664
|
+
aria-label={uiM('code_goto', undefined, locale)}
|
|
665
|
+
class='input input-bordered input-sm'
|
|
666
|
+
type='number'
|
|
667
|
+
min='1'
|
|
668
|
+
max={lineStarts.length}
|
|
669
|
+
required
|
|
670
|
+
/>
|
|
671
|
+
{/if}
|
|
672
|
+
<Button size='sm' type='submit' variant='ghost'>{uiM(plainTool === 'find'
|
|
673
|
+
? 'code_find'
|
|
674
|
+
: 'code_goto', undefined, locale)}</Button>
|
|
675
|
+
<Button size='sm' variant='ghost' onclick={() => {
|
|
676
|
+
plainTool = null
|
|
677
|
+
plain?.focus()
|
|
678
|
+
}}>{uiM('code_close', undefined, locale)}</Button>
|
|
679
|
+
</form>
|
|
680
|
+
{/if}
|
|
681
|
+
<textarea
|
|
682
|
+
bind:this={plain}
|
|
683
|
+
aria-label={editorLabel}
|
|
684
|
+
class='hai-code-plain'
|
|
685
|
+
readonly
|
|
686
|
+
spellcheck='false'
|
|
687
|
+
wrap={options.wordWrap ? 'soft' : 'off'}
|
|
688
|
+
value={plainText}
|
|
689
|
+
onselect={readPlainSelection}
|
|
690
|
+
onpointerup={readPlainSelection}
|
|
691
|
+
></textarea>
|
|
692
|
+
{/if}
|
|
693
|
+
{#if editor && interactiveAnnotations.length}
|
|
694
|
+
{#key fileKey}
|
|
695
|
+
<CodeWorkspaceGutter {editor} {file} annotations={interactiveAnnotations} {annotationIcon} {annotationHover} />
|
|
696
|
+
{/key}
|
|
697
|
+
{/if}
|
|
698
|
+
{#if canShowActions && selection && selectionActions}
|
|
699
|
+
<div
|
|
700
|
+
bind:this={actions}
|
|
701
|
+
bind:offsetWidth={actionsWidth}
|
|
702
|
+
bind:offsetHeight={actionsHeight}
|
|
703
|
+
class='hai-code-selection-actions'
|
|
704
|
+
class:plain-actions={mode === 'plain'}
|
|
705
|
+
style:top={actionsTop === undefined
|
|
706
|
+
? undefined
|
|
707
|
+
: `${actionsTop}px`}
|
|
708
|
+
style:left={actionsLeft === undefined
|
|
709
|
+
? undefined
|
|
710
|
+
: `${actionsLeft}px`}
|
|
711
|
+
role='group'
|
|
712
|
+
aria-label={uiM('code_selectionActions', undefined, locale)}
|
|
713
|
+
aria-keyshortcuts='Alt+Shift+Enter'
|
|
714
|
+
tabindex='-1'
|
|
715
|
+
>
|
|
716
|
+
{@render selectionActions({ file, selection })}
|
|
717
|
+
</div>
|
|
718
|
+
{/if}
|
|
719
|
+
</div>
|
|
720
|
+
|
|
721
|
+
<style>
|
|
722
|
+
.hai-code-editor { position: relative; min-block-size: 0; min-inline-size: 0; flex: 1; display: flex; flex-direction: column; }
|
|
723
|
+
.hai-code-editor-surface { position: absolute; inset: 0; }
|
|
724
|
+
.hai-code-editor-message { margin: 0; padding: 0.6rem 0.75rem; color: color-mix(in srgb, var(--color-base-content) 68%, transparent); font-size: 0.8125rem; }
|
|
725
|
+
.hai-code-plain { flex: 1; min-block-size: 8rem; resize: none; padding: 0.65rem 0.75rem; border: 0; background: var(--color-base-100); color: var(--color-base-content); font: 0.875rem/1.5 ui-monospace, monospace; tab-size: 2; }
|
|
726
|
+
.hai-code-plain-tools { display: flex; flex-wrap: wrap; gap: 0.25rem; align-items: center; padding: 0.25rem 0.5rem; }
|
|
727
|
+
.hai-code-plain-tools input { flex: 1 1 6rem; min-inline-size: 0; }
|
|
728
|
+
.hai-code-selection-actions { position: absolute; z-index: 7; max-inline-size: calc(100% - 0.5rem); display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem; padding: 0.25rem; border: 1px solid var(--color-base-300); border-radius: var(--radius-box); background: var(--color-base-100); color: var(--color-base-content); box-shadow: 0 3px 10px color-mix(in srgb, var(--color-base-content) 20%, transparent); }
|
|
729
|
+
.plain-actions { inset-block-start: 0.25rem; inset-inline-end: 0.25rem; }
|
|
730
|
+
.hai-code-editor :global(.hai-code-neutral) { --hai-annotation-color: var(--color-base-content); }
|
|
731
|
+
.hai-code-editor :global(.hai-code-info) { --hai-annotation-color: var(--color-info); }
|
|
732
|
+
.hai-code-editor :global(.hai-code-success) { --hai-annotation-color: var(--color-success); }
|
|
733
|
+
.hai-code-editor :global(.hai-code-warning) { --hai-annotation-color: var(--color-warning); }
|
|
734
|
+
.hai-code-editor :global(.hai-code-error) { --hai-annotation-color: var(--color-error); }
|
|
735
|
+
.hai-code-editor :global(.hai-code-gutter) { border-inline-start: 3px solid var(--hai-annotation-color); }
|
|
736
|
+
.hai-code-editor :global(.hai-code-range) { background-color: color-mix(in srgb, var(--hai-annotation-color) 23%, transparent); }
|
|
737
|
+
</style>
|