@stacksjs/components 0.2.854 → 0.2.855
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/dist/CodeEditor-jg4w7e15.stx +497 -0
- package/dist/code-editor/editor.js +2166 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +29 -12
- package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
- package/dist/ui/code-editor/controller.d.ts +103 -0
- package/dist/ui/code-editor/core/commands.d.ts +55 -0
- package/dist/ui/code-editor/core/completion.d.ts +40 -0
- package/dist/ui/code-editor/core/editor.d.ts +69 -0
- package/dist/ui/code-editor/core/highlight.d.ts +61 -0
- package/dist/ui/code-editor/core/history.d.ts +27 -0
- package/dist/ui/code-editor/core/index.d.ts +13 -0
- package/dist/ui/code-editor/core/keymap.d.ts +38 -0
- package/dist/ui/code-editor/core/selection.d.ts +30 -0
- package/dist/ui/code-editor/core/text.d.ts +64 -0
- package/dist/ui/code-editor/editor.d.ts +15 -0
- package/dist/ui/code-editor/index.d.ts +29 -0
- package/dist/ui/code-editor/tokenizer.d.ts +18 -0
- package/dist/ui/code-editor/view.d.ts +59 -0
- package/dist/utils/highlighter.d.ts +19 -1
- package/dist/utils/highlighter.js +24 -7
- package/dist/utils/syntax-theme.d.ts +44 -0
- package/package.json +9 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +2 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +134 -0
- package/src/ui/code-editor/controller.ts +517 -0
- package/src/ui/code-editor/core/commands.ts +381 -0
- package/src/ui/code-editor/core/completion.ts +212 -0
- package/src/ui/code-editor/core/editor.ts +319 -0
- package/src/ui/code-editor/core/highlight.ts +203 -0
- package/src/ui/code-editor/core/history.ts +190 -0
- package/src/ui/code-editor/core/index.ts +14 -0
- package/src/ui/code-editor/core/keymap.ts +125 -0
- package/src/ui/code-editor/core/selection.ts +104 -0
- package/src/ui/code-editor/core/text.ts +252 -0
- package/src/ui/code-editor/editor.ts +16 -0
- package/src/ui/code-editor/index.ts +46 -0
- package/src/ui/code-editor/tokenizer.ts +74 -0
- package/src/ui/code-editor/view.ts +1066 -0
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One editor, assembled: the model, the view, the highlighter and the
|
|
3
|
+
* completion list, behind the handful of things a host wants to do with them.
|
|
4
|
+
*
|
|
5
|
+
* The stx component is a template and some signals. Everything that decides
|
|
6
|
+
* something lives here or below, in plain TypeScript, so it can be driven from
|
|
7
|
+
* a test - or from a host that is not an stx page at all.
|
|
8
|
+
*/
|
|
9
|
+
import type { CompletionItem, Keymap, Transaction } from './core'
|
|
10
|
+
import type { EditorDiagnostic, EditorWidget } from './view'
|
|
11
|
+
import { CompletionList, createKeymap, defaultKeymap, EditorCore, languageConfig, rangeEmpty, rangeFrom, rangeTo, registerDefaultCommands, wordStart } from './core'
|
|
12
|
+
import { createLanguageTokenizer } from './tokenizer'
|
|
13
|
+
import { EditorView } from './view'
|
|
14
|
+
|
|
15
|
+
export interface CodeEditorConfig {
|
|
16
|
+
value?: string
|
|
17
|
+
language?: string
|
|
18
|
+
tabSize?: number
|
|
19
|
+
insertSpaces?: boolean
|
|
20
|
+
readOnly?: boolean
|
|
21
|
+
lineNumbers?: boolean
|
|
22
|
+
/** Colour the code. Off shows plain text and loads no grammar. */
|
|
23
|
+
highlight?: boolean
|
|
24
|
+
/** Offer completions while typing, not only on Ctrl-Space. Default true. */
|
|
25
|
+
autoComplete?: boolean
|
|
26
|
+
/** Names always on offer, before any the host supplies on request. */
|
|
27
|
+
completions?: readonly CompletionItem[]
|
|
28
|
+
/** Bindings laid over the defaults. An empty command name unbinds a key. */
|
|
29
|
+
keymap?: Keymap
|
|
30
|
+
inlineDiagnostics?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SelectionDetail {
|
|
34
|
+
anchor: number
|
|
35
|
+
head: number
|
|
36
|
+
from: number
|
|
37
|
+
to: number
|
|
38
|
+
/** Zero-based line and column of the caret. */
|
|
39
|
+
line: number
|
|
40
|
+
column: number
|
|
41
|
+
text: string
|
|
42
|
+
empty: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RunDetail {
|
|
46
|
+
/** The whole document. */
|
|
47
|
+
value: string
|
|
48
|
+
selection: SelectionDetail
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface RunSelectionDetail {
|
|
52
|
+
/** What to run: the selection, or the caret's line when nothing is selected. */
|
|
53
|
+
text: string
|
|
54
|
+
from: number
|
|
55
|
+
to: number
|
|
56
|
+
/** Zero-based line the text starts on, for mapping a result back. */
|
|
57
|
+
line: number
|
|
58
|
+
/** Whether `text` is a real selection rather than the caret's line. */
|
|
59
|
+
selected: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface CompletionRequest {
|
|
63
|
+
/** What has been typed of the word so far. */
|
|
64
|
+
query: string
|
|
65
|
+
/** Offset the word starts at. Pass it back with the items. */
|
|
66
|
+
from: number
|
|
67
|
+
to: number
|
|
68
|
+
line: number
|
|
69
|
+
column: number
|
|
70
|
+
/** The text of the caret's line up to the caret. */
|
|
71
|
+
textBefore: string
|
|
72
|
+
/** True for Ctrl-Space, false for one triggered by typing. */
|
|
73
|
+
explicit: boolean
|
|
74
|
+
/** The character that triggered it, when typing did. */
|
|
75
|
+
trigger: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PopupItem {
|
|
79
|
+
id: string
|
|
80
|
+
index: number
|
|
81
|
+
label: string
|
|
82
|
+
detail: string
|
|
83
|
+
kind: string
|
|
84
|
+
selected: boolean
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface PopupState {
|
|
88
|
+
open: boolean
|
|
89
|
+
items: PopupItem[]
|
|
90
|
+
/** Id of the selected item, for `aria-activedescendant`. Empty when closed. */
|
|
91
|
+
activeId: string
|
|
92
|
+
left: number
|
|
93
|
+
top: number
|
|
94
|
+
/** `above` when there is no room under the caret. */
|
|
95
|
+
placement: 'below' | 'above'
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface CodeEditorHooks {
|
|
99
|
+
onChange?: (value: string, transaction: Transaction) => void
|
|
100
|
+
onSelection?: (detail: SelectionDetail) => void
|
|
101
|
+
onRun?: (detail: RunDetail) => void
|
|
102
|
+
onRunSelection?: (detail: RunSelectionDetail) => void
|
|
103
|
+
onCompleteRequest?: (request: CompletionRequest) => void
|
|
104
|
+
onCompletionAccept?: (item: CompletionItem) => void
|
|
105
|
+
onPopup?: (state: PopupState) => void
|
|
106
|
+
onGhost?: (outcome: 'accept' | 'dismiss', text: string) => void
|
|
107
|
+
onFocus?: () => void
|
|
108
|
+
onBlur?: () => void
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const CLOSED_POPUP: PopupState = { open: false, items: [], activeId: '', left: 0, top: 0, placement: 'below' }
|
|
112
|
+
const WORD = /^[\p{L}\p{N}_$]*$/u
|
|
113
|
+
const TRIGGER = /[\p{L}\p{N}_$.]/u
|
|
114
|
+
/** Rows the popup draws. Narrowing by typing is how a reader gets to the rest. */
|
|
115
|
+
const POPUP_LIMIT = 80
|
|
116
|
+
/** Room the popup wants under the caret before it goes above instead. */
|
|
117
|
+
const POPUP_ROOM = 220
|
|
118
|
+
|
|
119
|
+
let instances = 0
|
|
120
|
+
|
|
121
|
+
export class CodeEditorController {
|
|
122
|
+
readonly core: EditorCore
|
|
123
|
+
readonly view: EditorView
|
|
124
|
+
private readonly hooks: CodeEditorHooks
|
|
125
|
+
private readonly list = new CompletionList()
|
|
126
|
+
private readonly popupId: string
|
|
127
|
+
private completions: readonly CompletionItem[]
|
|
128
|
+
private autoComplete: boolean
|
|
129
|
+
private highlight: boolean
|
|
130
|
+
private language = ''
|
|
131
|
+
private languageRequest = 0
|
|
132
|
+
private userKeymap: Keymap
|
|
133
|
+
private popupWasOpen = false
|
|
134
|
+
private destroyed = false
|
|
135
|
+
|
|
136
|
+
constructor(root: HTMLElement, config: CodeEditorConfig = {}, hooks: CodeEditorHooks = {}) {
|
|
137
|
+
this.hooks = hooks
|
|
138
|
+
this.popupId = `stx-ce-${++instances}`
|
|
139
|
+
this.completions = config.completions ?? []
|
|
140
|
+
this.autoComplete = config.autoComplete ?? true
|
|
141
|
+
this.highlight = config.highlight ?? true
|
|
142
|
+
this.userKeymap = config.keymap ?? {}
|
|
143
|
+
|
|
144
|
+
this.core = new EditorCore({
|
|
145
|
+
value: config.value ?? '',
|
|
146
|
+
tabSize: config.tabSize,
|
|
147
|
+
insertSpaces: config.insertSpaces,
|
|
148
|
+
readOnly: config.readOnly,
|
|
149
|
+
language: languageConfig(config.language),
|
|
150
|
+
})
|
|
151
|
+
registerDefaultCommands(this.core)
|
|
152
|
+
|
|
153
|
+
this.view = new EditorView(root, {
|
|
154
|
+
core: this.core,
|
|
155
|
+
keymap: createKeymap([defaultKeymap, this.userKeymap]),
|
|
156
|
+
inlineDiagnostics: config.inlineDiagnostics,
|
|
157
|
+
onTransaction: transaction => this.onTransaction(transaction),
|
|
158
|
+
onCommand: name => this.onCommand(name),
|
|
159
|
+
onKeydown: event => this.onKeydown(event),
|
|
160
|
+
onGhost: (outcome, text) => this.hooks.onGhost?.(outcome, text),
|
|
161
|
+
onFocus: () => this.hooks.onFocus?.(),
|
|
162
|
+
onBlur: () => {
|
|
163
|
+
this.closeCompletions()
|
|
164
|
+
this.hooks.onBlur?.()
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
this.view.setReadOnly(config.readOnly ?? false)
|
|
168
|
+
this.view.setLineNumbers(config.lineNumbers ?? true)
|
|
169
|
+
this.setLanguage(config.language ?? 'typescript')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ------------------------------------------------------------------- value
|
|
173
|
+
|
|
174
|
+
get value(): string {
|
|
175
|
+
return this.core.value
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Replace the text. A value equal to what is there changes nothing. */
|
|
179
|
+
setValue(value: string): void {
|
|
180
|
+
if (value !== this.core.value)
|
|
181
|
+
this.core.setValue(value)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
get selection(): SelectionDetail {
|
|
185
|
+
const main = this.core.main
|
|
186
|
+
const cursor = this.core.cursor
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
anchor: main.anchor,
|
|
190
|
+
head: main.head,
|
|
191
|
+
from: rangeFrom(main),
|
|
192
|
+
to: rangeTo(main),
|
|
193
|
+
line: cursor.line,
|
|
194
|
+
column: cursor.column,
|
|
195
|
+
text: this.core.selectedText,
|
|
196
|
+
empty: rangeEmpty(main),
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
setSelection(anchor: number, head: number = anchor): void {
|
|
201
|
+
this.core.setSelection(anchor, head)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
focus(): void {
|
|
205
|
+
this.view.focus()
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---------------------------------------------------------------- settings
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Switch language: its comment marker and brackets now, its colours as soon
|
|
212
|
+
* as the grammar has loaded. A language the highlighter does not know is
|
|
213
|
+
* shown plain.
|
|
214
|
+
*/
|
|
215
|
+
setLanguage(language: string): void {
|
|
216
|
+
if (language === this.language)
|
|
217
|
+
return
|
|
218
|
+
|
|
219
|
+
this.language = language
|
|
220
|
+
this.core.language = languageConfig(language)
|
|
221
|
+
this.loadTokenizer()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
setHighlight(highlight: boolean): void {
|
|
225
|
+
if (highlight === this.highlight)
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
this.highlight = highlight
|
|
229
|
+
this.loadTokenizer()
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private loadTokenizer(): void {
|
|
233
|
+
const request = ++this.languageRequest
|
|
234
|
+
if (!this.highlight) {
|
|
235
|
+
this.view.setTokenizer(null)
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
createLanguageTokenizer(this.language)
|
|
240
|
+
.catch(() => null)
|
|
241
|
+
.then((tokenizer) => {
|
|
242
|
+
// A language asked for since then is the one that counts.
|
|
243
|
+
if (!this.destroyed && request === this.languageRequest)
|
|
244
|
+
this.view.setTokenizer(tokenizer)
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
setReadOnly(readOnly: boolean): void {
|
|
249
|
+
this.view.setReadOnly(readOnly)
|
|
250
|
+
if (readOnly)
|
|
251
|
+
this.closeCompletions()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
setLineNumbers(show: boolean): void {
|
|
255
|
+
this.view.setLineNumbers(show)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
setTabSize(tabSize: number): void {
|
|
259
|
+
this.core.tabSize = Math.max(1, Math.floor(tabSize) || 2)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
setInsertSpaces(insertSpaces: boolean): void {
|
|
263
|
+
this.core.insertSpaces = insertSpaces
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
setAutoComplete(autoComplete: boolean): void {
|
|
267
|
+
this.autoComplete = autoComplete
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
setKeymap(keymap: Keymap): void {
|
|
271
|
+
this.userKeymap = keymap
|
|
272
|
+
this.view.setKeymap(createKeymap([defaultKeymap, keymap]))
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
setCompletions(completions: readonly CompletionItem[]): void {
|
|
276
|
+
this.completions = completions
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
setWidgets(widgets: readonly EditorWidget[]): void {
|
|
280
|
+
this.view.setWidgets(widgets)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
setDiagnostics(diagnostics: readonly EditorDiagnostic[]): void {
|
|
284
|
+
this.view.setDiagnostics(diagnostics)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
setGhostText(text: string | null): void {
|
|
288
|
+
this.view.setGhostText(text)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// --------------------------------------------------------------------- run
|
|
292
|
+
|
|
293
|
+
private onCommand(name: string): boolean {
|
|
294
|
+
if (name === 'run') {
|
|
295
|
+
this.hooks.onRun?.({ value: this.core.value, selection: this.selection })
|
|
296
|
+
return true
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (name === 'runSelection') {
|
|
300
|
+
this.hooks.onRunSelection?.(this.runSelectionDetail())
|
|
301
|
+
return true
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (name === 'triggerCompletion') {
|
|
305
|
+
this.requestCompletions(true, '')
|
|
306
|
+
return true
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return false
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private runSelectionDetail(): RunSelectionDetail {
|
|
313
|
+
const main = this.core.main
|
|
314
|
+
if (!rangeEmpty(main)) {
|
|
315
|
+
const from = rangeFrom(main)
|
|
316
|
+
|
|
317
|
+
return { text: this.core.selectedText, from, to: rangeTo(main), line: this.core.doc.positionAt(from).line, selected: true }
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const line = this.core.doc.lineAt(main.head)
|
|
321
|
+
|
|
322
|
+
return { text: line.text, from: line.from, to: line.to, line: line.index, selected: false }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// -------------------------------------------------------------- completion
|
|
326
|
+
|
|
327
|
+
private onTransaction(transaction: Transaction): void {
|
|
328
|
+
if (transaction.docChanged)
|
|
329
|
+
this.hooks.onChange?.(this.core.value, transaction)
|
|
330
|
+
if (transaction.selectionChanged || transaction.docChanged)
|
|
331
|
+
this.hooks.onSelection?.(this.selection)
|
|
332
|
+
|
|
333
|
+
if (this.list.isOpen) {
|
|
334
|
+
this.narrowCompletions()
|
|
335
|
+
}
|
|
336
|
+
else if (this.autoComplete && transaction.origin === 'input' && transaction.kind === 'type' && transaction.changes.length === 1) {
|
|
337
|
+
const typed = transaction.changes[0]!.insert
|
|
338
|
+
if (typed.length === 1 && TRIGGER.test(typed))
|
|
339
|
+
this.requestCompletions(false, typed)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
this.publishPopup()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** The word the caret is at the end of, as an offset and a query. */
|
|
346
|
+
private currentWord(): { from: number, query: string, textBefore: string } {
|
|
347
|
+
const head = this.core.main.head
|
|
348
|
+
const line = this.core.doc.lineAt(head)
|
|
349
|
+
const textBefore = line.text.slice(0, head - line.from)
|
|
350
|
+
const start = wordStart(textBefore, textBefore.length)
|
|
351
|
+
|
|
352
|
+
return { from: line.from + start, query: textBefore.slice(start), textBefore }
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private requestCompletions(explicit: boolean, trigger: string): void {
|
|
356
|
+
if (this.core.readOnly || !rangeEmpty(this.core.main))
|
|
357
|
+
return
|
|
358
|
+
|
|
359
|
+
const head = this.core.main.head
|
|
360
|
+
const cursor = this.core.cursor
|
|
361
|
+
const word = this.currentWord()
|
|
362
|
+
|
|
363
|
+
// Typing offers the standing list only once there is a word to match it
|
|
364
|
+
// against. Asked for outright, it is offered whole.
|
|
365
|
+
if (this.completions.length > 0 && (explicit || word.query !== ''))
|
|
366
|
+
this.list.open(this.completions, word.from, word.query)
|
|
367
|
+
|
|
368
|
+
this.hooks.onCompleteRequest?.({
|
|
369
|
+
query: word.query,
|
|
370
|
+
from: word.from,
|
|
371
|
+
to: head,
|
|
372
|
+
line: cursor.line,
|
|
373
|
+
column: cursor.column,
|
|
374
|
+
textBefore: word.textBefore,
|
|
375
|
+
explicit,
|
|
376
|
+
trigger,
|
|
377
|
+
})
|
|
378
|
+
this.publishPopup()
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Offer these items at the caret.
|
|
383
|
+
*
|
|
384
|
+
* `from` is where the word they complete begins; it defaults to the start of
|
|
385
|
+
* the word the caret is in. Typing after this narrows the list without
|
|
386
|
+
* another request.
|
|
387
|
+
*/
|
|
388
|
+
showCompletions(items: readonly CompletionItem[], options: { from?: number } = {}): void {
|
|
389
|
+
if (this.destroyed || this.core.readOnly || !rangeEmpty(this.core.main))
|
|
390
|
+
return
|
|
391
|
+
|
|
392
|
+
const head = this.core.main.head
|
|
393
|
+
const from = Math.min(head, options.from ?? this.currentWord().from)
|
|
394
|
+
this.list.open(items, from, this.core.doc.slice(from, head))
|
|
395
|
+
this.publishPopup()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
closeCompletions(): void {
|
|
399
|
+
if (!this.list.isOpen && !this.popupWasOpen)
|
|
400
|
+
return
|
|
401
|
+
|
|
402
|
+
this.list.close()
|
|
403
|
+
this.publishPopup()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private narrowCompletions(): void {
|
|
407
|
+
const main = this.core.main
|
|
408
|
+
const from = this.list.from
|
|
409
|
+
const query = rangeEmpty(main) && main.head >= from ? this.core.doc.slice(from, main.head) : null
|
|
410
|
+
|
|
411
|
+
// Still inside the word it was opened for, and still a word.
|
|
412
|
+
if (query === null || !WORD.test(query))
|
|
413
|
+
this.list.close()
|
|
414
|
+
else
|
|
415
|
+
this.list.filter(query)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** Take the selected completion. False when the list is closed. */
|
|
419
|
+
acceptCompletion(index?: number): boolean {
|
|
420
|
+
if (index !== undefined)
|
|
421
|
+
this.list.select(index)
|
|
422
|
+
|
|
423
|
+
const accepted = this.list.accept(this.core.main.head)
|
|
424
|
+
if (!accepted)
|
|
425
|
+
return false
|
|
426
|
+
|
|
427
|
+
const end = accepted.change.from + accepted.change.insert.length
|
|
428
|
+
this.list.close()
|
|
429
|
+
this.core.dispatch({ changes: [accepted.change], selection: { anchor: end, head: end }, kind: 'complete' })
|
|
430
|
+
this.hooks.onCompletionAccept?.(accepted.item)
|
|
431
|
+
this.publishPopup()
|
|
432
|
+
|
|
433
|
+
return true
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
selectCompletion(index: number): void {
|
|
437
|
+
this.list.select(index)
|
|
438
|
+
this.publishPopup()
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private onKeydown(event: KeyboardEvent): boolean {
|
|
442
|
+
if (!this.list.isOpen || event.ctrlKey || event.metaKey || event.altKey)
|
|
443
|
+
return false
|
|
444
|
+
|
|
445
|
+
switch (event.key) {
|
|
446
|
+
case 'ArrowDown':
|
|
447
|
+
this.list.move(1)
|
|
448
|
+
break
|
|
449
|
+
case 'ArrowUp':
|
|
450
|
+
this.list.move(-1)
|
|
451
|
+
break
|
|
452
|
+
case 'PageDown':
|
|
453
|
+
this.list.move(8)
|
|
454
|
+
break
|
|
455
|
+
case 'PageUp':
|
|
456
|
+
this.list.move(-8)
|
|
457
|
+
break
|
|
458
|
+
case 'Enter':
|
|
459
|
+
case 'Tab':
|
|
460
|
+
if (event.shiftKey)
|
|
461
|
+
return false
|
|
462
|
+
return this.acceptCompletion()
|
|
463
|
+
case 'Escape':
|
|
464
|
+
this.list.close()
|
|
465
|
+
break
|
|
466
|
+
default:
|
|
467
|
+
return false
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
this.publishPopup()
|
|
471
|
+
|
|
472
|
+
return true
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
private publishPopup(): void {
|
|
476
|
+
if (!this.hooks.onPopup)
|
|
477
|
+
return
|
|
478
|
+
|
|
479
|
+
if (!this.list.isOpen) {
|
|
480
|
+
if (this.popupWasOpen) {
|
|
481
|
+
this.popupWasOpen = false
|
|
482
|
+
this.hooks.onPopup(CLOSED_POPUP)
|
|
483
|
+
}
|
|
484
|
+
return
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
this.popupWasOpen = true
|
|
488
|
+
const anchor = this.view.caretAnchor(this.list.from)
|
|
489
|
+
const above = anchor.spaceBelow < POPUP_ROOM && anchor.spaceAbove > anchor.spaceBelow
|
|
490
|
+
const selected = this.list.selectedIndex
|
|
491
|
+
const items = this.list.visible.slice(0, POPUP_LIMIT).map((entry, index) => ({
|
|
492
|
+
id: `${this.popupId}-${index}`,
|
|
493
|
+
index,
|
|
494
|
+
label: entry.item.label,
|
|
495
|
+
detail: entry.item.detail ?? '',
|
|
496
|
+
kind: entry.item.kind ?? '',
|
|
497
|
+
selected: index === selected,
|
|
498
|
+
}))
|
|
499
|
+
|
|
500
|
+
this.hooks.onPopup({
|
|
501
|
+
open: true,
|
|
502
|
+
items,
|
|
503
|
+
activeId: items[selected]?.id ?? '',
|
|
504
|
+
left: anchor.left,
|
|
505
|
+
top: above ? anchor.top : anchor.bottom,
|
|
506
|
+
placement: above ? 'above' : 'below',
|
|
507
|
+
})
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// ----------------------------------------------------------------- teardown
|
|
511
|
+
|
|
512
|
+
destroy(): void {
|
|
513
|
+
this.destroyed = true
|
|
514
|
+
this.list.close()
|
|
515
|
+
this.view.destroy()
|
|
516
|
+
}
|
|
517
|
+
}
|