@stacksjs/components 0.2.854 → 0.2.857

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.
Files changed (139) hide show
  1. package/dist/CodeEditor-jg4w7e15.stx +497 -0
  2. package/dist/ContextMenu-43g3w5tp.stx +188 -0
  3. package/dist/DataGrid-7rt7v865.stx +376 -0
  4. package/dist/GraphView-0t3t9mec.stx +320 -0
  5. package/dist/JsonTree-satc4010.stx +317 -0
  6. package/dist/PreviewFrame-erbgvzj8.stx +257 -0
  7. package/dist/SplitPane-7sqfdwqa.stx +159 -0
  8. package/dist/code-editor/editor.js +2166 -0
  9. package/dist/component-api/component-api.js +36 -0
  10. package/dist/components/CodeBlock.d.ts +12 -5
  11. package/dist/context-menu/context-menu.js +575 -0
  12. package/dist/data-grid/data-grid.js +1244 -0
  13. package/dist/graph-view/graph-view.js +1408 -0
  14. package/dist/index.d.ts +10 -0
  15. package/dist/index.js +34 -15
  16. package/dist/json-tree/json-tree.js +1121 -0
  17. package/dist/preview-frame/preview-frame.js +356 -0
  18. package/dist/split-pane/split-pane.js +364 -0
  19. package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
  20. package/dist/ui/code-editor/controller.d.ts +103 -0
  21. package/dist/ui/code-editor/core/commands.d.ts +55 -0
  22. package/dist/ui/code-editor/core/completion.d.ts +40 -0
  23. package/dist/ui/code-editor/core/editor.d.ts +69 -0
  24. package/dist/ui/code-editor/core/highlight.d.ts +61 -0
  25. package/dist/ui/code-editor/core/history.d.ts +27 -0
  26. package/dist/ui/code-editor/core/index.d.ts +13 -0
  27. package/dist/ui/code-editor/core/keymap.d.ts +38 -0
  28. package/dist/ui/code-editor/core/selection.d.ts +30 -0
  29. package/dist/ui/code-editor/core/text.d.ts +64 -0
  30. package/dist/ui/code-editor/editor.d.ts +15 -0
  31. package/dist/ui/code-editor/index.d.ts +29 -0
  32. package/dist/ui/code-editor/tokenizer.d.ts +18 -0
  33. package/dist/ui/code-editor/view.d.ts +59 -0
  34. package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
  35. package/dist/ui/context-menu/context-menu.d.ts +8 -0
  36. package/dist/ui/context-menu/core.d.ts +74 -0
  37. package/dist/ui/context-menu/index.d.ts +11 -0
  38. package/dist/ui/context-menu/view.d.ts +36 -0
  39. package/dist/ui/data-grid/DataGrid.d.ts +30 -0
  40. package/dist/ui/data-grid/core.d.ts +141 -0
  41. package/dist/ui/data-grid/data-grid.d.ts +9 -0
  42. package/dist/ui/data-grid/index.d.ts +20 -0
  43. package/dist/ui/data-grid/view.d.ts +56 -0
  44. package/dist/ui/graph-view/GraphView.d.ts +21 -0
  45. package/dist/ui/graph-view/core/edges.d.ts +34 -0
  46. package/dist/ui/graph-view/core/force.d.ts +10 -0
  47. package/dist/ui/graph-view/core/graph.d.ts +83 -0
  48. package/dist/ui/graph-view/core/index.d.ts +5 -0
  49. package/dist/ui/graph-view/core/layered.d.ts +24 -0
  50. package/dist/ui/graph-view/core/viewport.d.ts +34 -0
  51. package/dist/ui/graph-view/graph-view.d.ts +9 -0
  52. package/dist/ui/graph-view/index.d.ts +21 -0
  53. package/dist/ui/graph-view/view.d.ts +51 -0
  54. package/dist/ui/json-tree/JsonTree.d.ts +18 -0
  55. package/dist/ui/json-tree/core.d.ts +108 -0
  56. package/dist/ui/json-tree/index.d.ts +18 -0
  57. package/dist/ui/json-tree/json-tree.d.ts +10 -0
  58. package/dist/ui/json-tree/tinker.d.ts +84 -0
  59. package/dist/ui/json-tree/view.d.ts +59 -0
  60. package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
  61. package/dist/ui/preview-frame/core.d.ts +81 -0
  62. package/dist/ui/preview-frame/index.d.ts +19 -0
  63. package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
  64. package/dist/ui/preview-frame/view.d.ts +37 -0
  65. package/dist/ui/split-pane/SplitPane.d.ts +18 -0
  66. package/dist/ui/split-pane/core.d.ts +58 -0
  67. package/dist/ui/split-pane/index.d.ts +15 -0
  68. package/dist/ui/split-pane/split-pane.d.ts +8 -0
  69. package/dist/ui/split-pane/view.d.ts +27 -0
  70. package/dist/ui/virtual-table/index.d.ts +6 -0
  71. package/dist/utils/component-api.d.ts +4 -0
  72. package/dist/utils/highlighter.d.ts +19 -1
  73. package/dist/utils/highlighter.js +24 -7
  74. package/dist/utils/syntax-theme.d.ts +44 -0
  75. package/package.json +44 -4
  76. package/src/components/CodeBlock.stx +195 -17
  77. package/src/index.ts +10 -0
  78. package/src/ui/code-editor/CodeEditor.stx +497 -0
  79. package/src/ui/code-editor/README.md +135 -0
  80. package/src/ui/code-editor/controller.ts +517 -0
  81. package/src/ui/code-editor/core/commands.ts +381 -0
  82. package/src/ui/code-editor/core/completion.ts +212 -0
  83. package/src/ui/code-editor/core/editor.ts +319 -0
  84. package/src/ui/code-editor/core/highlight.ts +203 -0
  85. package/src/ui/code-editor/core/history.ts +190 -0
  86. package/src/ui/code-editor/core/index.ts +14 -0
  87. package/src/ui/code-editor/core/keymap.ts +125 -0
  88. package/src/ui/code-editor/core/selection.ts +104 -0
  89. package/src/ui/code-editor/core/text.ts +252 -0
  90. package/src/ui/code-editor/editor.ts +16 -0
  91. package/src/ui/code-editor/index.ts +46 -0
  92. package/src/ui/code-editor/tokenizer.ts +74 -0
  93. package/src/ui/code-editor/view.ts +1066 -0
  94. package/src/ui/context-menu/ContextMenu.stx +188 -0
  95. package/src/ui/context-menu/README.md +117 -0
  96. package/src/ui/context-menu/context-menu.ts +8 -0
  97. package/src/ui/context-menu/core.ts +250 -0
  98. package/src/ui/context-menu/index.ts +16 -0
  99. package/src/ui/context-menu/view.ts +540 -0
  100. package/src/ui/data-grid/DataGrid.stx +376 -0
  101. package/src/ui/data-grid/README.md +204 -0
  102. package/src/ui/data-grid/core.ts +813 -0
  103. package/src/ui/data-grid/data-grid.ts +9 -0
  104. package/src/ui/data-grid/index.ts +34 -0
  105. package/src/ui/data-grid/view.ts +806 -0
  106. package/src/ui/graph-view/GraphView.stx +320 -0
  107. package/src/ui/graph-view/README.md +210 -0
  108. package/src/ui/graph-view/core/edges.ts +180 -0
  109. package/src/ui/graph-view/core/force.ts +237 -0
  110. package/src/ui/graph-view/core/graph.ts +277 -0
  111. package/src/ui/graph-view/core/index.ts +5 -0
  112. package/src/ui/graph-view/core/layered.ts +256 -0
  113. package/src/ui/graph-view/core/viewport.ts +141 -0
  114. package/src/ui/graph-view/graph-view.ts +9 -0
  115. package/src/ui/graph-view/index.ts +35 -0
  116. package/src/ui/graph-view/view.ts +809 -0
  117. package/src/ui/json-tree/JsonTree.stx +317 -0
  118. package/src/ui/json-tree/README.md +193 -0
  119. package/src/ui/json-tree/core.ts +588 -0
  120. package/src/ui/json-tree/index.ts +28 -0
  121. package/src/ui/json-tree/json-tree.ts +10 -0
  122. package/src/ui/json-tree/tinker.ts +349 -0
  123. package/src/ui/json-tree/view.ts +665 -0
  124. package/src/ui/preview-frame/PreviewFrame.stx +257 -0
  125. package/src/ui/preview-frame/README.md +107 -0
  126. package/src/ui/preview-frame/core.ts +195 -0
  127. package/src/ui/preview-frame/index.ts +31 -0
  128. package/src/ui/preview-frame/preview-frame.ts +8 -0
  129. package/src/ui/preview-frame/view.ts +336 -0
  130. package/src/ui/split-pane/README.md +87 -0
  131. package/src/ui/split-pane/SplitPane.stx +159 -0
  132. package/src/ui/split-pane/core.ts +200 -0
  133. package/src/ui/split-pane/index.ts +24 -0
  134. package/src/ui/split-pane/split-pane.ts +8 -0
  135. package/src/ui/split-pane/view.ts +305 -0
  136. package/src/ui/virtual-table/index.ts +6 -0
  137. package/src/utils/component-api.ts +72 -0
  138. package/src/utils/highlighter.ts +121 -218
  139. 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
+ }