@stacksjs/components 0.2.853 → 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.
Files changed (44) hide show
  1. package/dist/CodeEditor-jg4w7e15.stx +497 -0
  2. package/dist/code-editor/editor.js +2166 -0
  3. package/dist/components/CodeBlock.d.ts +12 -5
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.js +29 -12
  6. package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
  7. package/dist/ui/code-editor/controller.d.ts +103 -0
  8. package/dist/ui/code-editor/core/commands.d.ts +55 -0
  9. package/dist/ui/code-editor/core/completion.d.ts +40 -0
  10. package/dist/ui/code-editor/core/editor.d.ts +69 -0
  11. package/dist/ui/code-editor/core/highlight.d.ts +61 -0
  12. package/dist/ui/code-editor/core/history.d.ts +27 -0
  13. package/dist/ui/code-editor/core/index.d.ts +13 -0
  14. package/dist/ui/code-editor/core/keymap.d.ts +38 -0
  15. package/dist/ui/code-editor/core/selection.d.ts +30 -0
  16. package/dist/ui/code-editor/core/text.d.ts +64 -0
  17. package/dist/ui/code-editor/editor.d.ts +15 -0
  18. package/dist/ui/code-editor/index.d.ts +29 -0
  19. package/dist/ui/code-editor/tokenizer.d.ts +18 -0
  20. package/dist/ui/code-editor/view.d.ts +59 -0
  21. package/dist/utils/highlighter.d.ts +19 -1
  22. package/dist/utils/highlighter.js +24 -7
  23. package/dist/utils/syntax-theme.d.ts +44 -0
  24. package/package.json +9 -4
  25. package/src/components/CodeBlock.stx +195 -17
  26. package/src/index.ts +2 -0
  27. package/src/ui/code-editor/CodeEditor.stx +497 -0
  28. package/src/ui/code-editor/README.md +134 -0
  29. package/src/ui/code-editor/controller.ts +517 -0
  30. package/src/ui/code-editor/core/commands.ts +381 -0
  31. package/src/ui/code-editor/core/completion.ts +212 -0
  32. package/src/ui/code-editor/core/editor.ts +319 -0
  33. package/src/ui/code-editor/core/highlight.ts +203 -0
  34. package/src/ui/code-editor/core/history.ts +190 -0
  35. package/src/ui/code-editor/core/index.ts +14 -0
  36. package/src/ui/code-editor/core/keymap.ts +125 -0
  37. package/src/ui/code-editor/core/selection.ts +104 -0
  38. package/src/ui/code-editor/core/text.ts +252 -0
  39. package/src/ui/code-editor/editor.ts +16 -0
  40. package/src/ui/code-editor/index.ts +46 -0
  41. package/src/ui/code-editor/tokenizer.ts +74 -0
  42. package/src/ui/code-editor/view.ts +1066 -0
  43. package/src/utils/highlighter.ts +121 -218
  44. package/src/utils/syntax-theme.ts +144 -0
@@ -0,0 +1,497 @@
1
+ <script server>
2
+ export const value = $props.value || ''
3
+ export const language = $props.language || 'typescript'
4
+ export const readonly = $bool($props.readonly)
5
+ export const disabled = $bool($props.disabled)
6
+ export const lineNumbers = $bool($props.lineNumbers, true)
7
+ export const highlight = $bool($props.highlight, true)
8
+ export const autoComplete = $bool($props.autoComplete, true)
9
+ export const inlineDiagnostics = $bool($props.inlineDiagnostics, true)
10
+ export const insertSpaces = $bool($props.insertSpaces, true)
11
+ export const autofocus = $bool($props.autofocus)
12
+ export const tabSize = $num($props.tabSize) || 2
13
+ export const placeholder = $props.placeholder || ''
14
+ export const label = $props.label || 'Code editor'
15
+ export const name = $props.name || ''
16
+ export const className = $props.className || ''
17
+ /*
18
+ * A CSS length. Left out, the editor is 20rem tall; `height="100%"` fills a
19
+ * sized parent, which is what a pane in a split layout wants.
20
+ */
21
+ export const height = $props.height || '20rem'
22
+ /*
23
+ * `light` or `dark` pins the code colours for this editor whatever the page is
24
+ * doing; anything else follows the page.
25
+ */
26
+ export const theme = ['light', 'dark'].includes($props.theme) ? $props.theme : ''
27
+ /*
28
+ * Ids unique to this instance: the field, the popup it controls and the
29
+ * status line it is described by all have to be findable when two editors
30
+ * share a page.
31
+ */
32
+ export const inputId = $props.id || `code-editor-${$uid}`
33
+ export const popupId = `${inputId}-completions`
34
+ export const statusId = `${inputId}-status`
35
+ export const editorClasses = `relative flex flex-col overflow-hidden rounded-panel border border-line ${className}`.trim()
36
+ /*
37
+ * Arrays that arrive as plain props. A bound one (`:completions="names()"`)
38
+ * is read live by the client script; these are what it starts from.
39
+ */
40
+ export const completions = Array.isArray($props.completions) ? $props.completions : []
41
+ export const widgets = Array.isArray($props.widgets) ? $props.widgets : []
42
+ export const diagnostics = Array.isArray($props.diagnostics) ? $props.diagnostics : []
43
+ export const ghostText = typeof $props.ghostText === 'string' ? $props.ghostText : ''
44
+ </script>
45
+
46
+ <script client>
47
+ /*
48
+ * <CodeEditor>: a code editor made of a text field over a painted copy of it.
49
+ *
50
+ * This script is the wiring: props in, events out, and the state of the
51
+ * completion popup as signals for the template. What a key does is in
52
+ * ./core, how the two layers stay aligned is in ./view, and how they are put
53
+ * together is in ./controller.
54
+ */
55
+ import { CodeEditorController } from './controller'
56
+
57
+ const emit = defineEmits(['input', 'change', 'selection', 'run', 'run-selection', 'complete', 'completion-accept', 'ghost', 'focus', 'blur'])
58
+
59
+ function asBoolean(value) {
60
+ if (value == null) return false
61
+ if (typeof value === 'string') return !['false', '0', 'off', 'no'].includes(value.trim().toLowerCase())
62
+ return Boolean(value)
63
+ }
64
+ function asList(value) {
65
+ if (Array.isArray(value)) return value
66
+ if (typeof value === 'string' && value.charAt(0) === '[') {
67
+ try { return JSON.parse(value) }
68
+ catch (e) { return [] }
69
+ }
70
+ return []
71
+ }
72
+ function asText(value) { return value == null ? '' : String(value) }
73
+
74
+ const model = useModel('value', { default: {{ value }}, parse: asText })
75
+ const liveLanguage = useReactiveProp('language', {{ language }}, { parse: asText })
76
+ const liveReadonly = useReactiveProp('readonly', {{ readonly }}, { parse: asBoolean })
77
+ const liveDisabled = useReactiveProp('disabled', {{ disabled }}, { parse: asBoolean })
78
+ const liveLineNumbers = useReactiveProp('lineNumbers', {{ lineNumbers }}, { parse: asBoolean })
79
+ const liveHighlight = useReactiveProp('highlight', {{ highlight }}, { parse: asBoolean })
80
+ const liveCompletions = useReactiveProp('completions', {{ completions }}, { parse: asList })
81
+ const liveWidgets = useReactiveProp('widgets', {{ widgets }}, { parse: asList })
82
+ const liveDiagnostics = useReactiveProp('diagnostics', {{ diagnostics }}, { parse: asList })
83
+ const liveGhostText = useReactiveProp('ghostText', {{ ghostText }}, { parse: asText })
84
+ // A function can only arrive through a bound attribute, and is read as it is.
85
+ const liveKeymap = useReactiveProp('keymap', null, { parse: v => v })
86
+
87
+ const locked = derived(() => asBoolean(liveReadonly()) || asBoolean(liveDisabled()))
88
+ const isEmpty = state({{ value }} === '')
89
+ const popup = state({ open: false, items: [], activeId: '', left: 0, top: 0, placement: 'below' })
90
+ const popupStyle = derived(() => {
91
+ const current = popup()
92
+ const lift = current.placement === 'above' ? ' translateY(-100%)' : ''
93
+ return 'transform: translate(' + current.left + 'px, ' + current.top + 'px)' + lift
94
+ })
95
+
96
+ const rootRef = useRef('code-editor')
97
+ let editor = null
98
+
99
+ onMount(() => {
100
+ const root = rootRef()
101
+ if (!root) return
102
+
103
+ editor = new CodeEditorController(root, {
104
+ value: model(),
105
+ language: liveLanguage(),
106
+ tabSize: {{ tabSize }},
107
+ insertSpaces: {{ insertSpaces }},
108
+ readOnly: locked(),
109
+ lineNumbers: asBoolean(liveLineNumbers()),
110
+ highlight: asBoolean(liveHighlight()),
111
+ autoComplete: {{ autoComplete }},
112
+ inlineDiagnostics: {{ inlineDiagnostics }},
113
+ completions: asList(liveCompletions()),
114
+ keymap: liveKeymap() || {},
115
+ }, {
116
+ onChange(value) {
117
+ isEmpty.set(value === '')
118
+ model.input(value)
119
+ emit('input', value)
120
+ emit('change', value)
121
+ },
122
+ onSelection(detail) { emit('selection', detail) },
123
+ onRun(detail) { emit('run', detail) },
124
+ onRunSelection(detail) { emit('run-selection', detail) },
125
+ onCompleteRequest(request) { emit('complete', request) },
126
+ onCompletionAccept(item) { emit('completion-accept', item) },
127
+ onGhost(outcome, text) { emit('ghost', { outcome, text }) },
128
+ onPopup(next) { popup.set(next) },
129
+ onFocus() { emit('focus') },
130
+ onBlur() { emit('blur') },
131
+ })
132
+
133
+ // Each prop the parent can change while the editor is up. Registered after
134
+ // the editor exists, so the first run of each finds something to set.
135
+ effect(() => {
136
+ const next = model()
137
+ isEmpty.set(next === '')
138
+ editor.setValue(next)
139
+ })
140
+ effect(() => editor.setLanguage(liveLanguage()))
141
+ effect(() => editor.setReadOnly(locked()))
142
+ effect(() => editor.setLineNumbers(asBoolean(liveLineNumbers())))
143
+ effect(() => editor.setHighlight(asBoolean(liveHighlight())))
144
+ effect(() => editor.setCompletions(asList(liveCompletions())))
145
+ effect(() => editor.setWidgets(asList(liveWidgets())))
146
+ effect(() => editor.setDiagnostics(asList(liveDiagnostics())))
147
+ effect(() => editor.setGhostText(liveGhostText() || null))
148
+ effect(() => editor.setKeymap(liveKeymap() || {}))
149
+
150
+ if ({{ autofocus }}) editor.focus()
151
+ })
152
+
153
+ onDestroy(() => {
154
+ if (editor) editor.destroy()
155
+ editor = null
156
+ })
157
+
158
+ function acceptCompletion(index) {
159
+ if (editor) editor.acceptCompletion(index)
160
+ }
161
+
162
+ defineExpose({
163
+ value: model,
164
+ getValue: () => (editor ? editor.value : model()),
165
+ setValue: (text) => { if (editor) editor.setValue(asText(text)) },
166
+ getSelection: () => (editor ? editor.selection : null),
167
+ setSelection: (anchor, head) => { if (editor) editor.setSelection(anchor, head) },
168
+ focus: () => { if (editor) editor.focus() },
169
+ undo: () => (editor ? editor.core.undo() : false),
170
+ redo: () => (editor ? editor.core.redo() : false),
171
+ exec: name => (editor ? editor.core.exec(name) : false),
172
+ setWidgets: (list) => { if (editor) editor.setWidgets(asList(list)) },
173
+ setDiagnostics: (list) => { if (editor) editor.setDiagnostics(asList(list)) },
174
+ setGhostText: (text) => { if (editor) editor.setGhostText(text || null) },
175
+ showCompletions: (items, options) => { if (editor) editor.showCompletions(asList(items), options || {}) },
176
+ closeCompletions: () => { if (editor) editor.closeCompletions() },
177
+ editor: () => editor,
178
+ })
179
+ </script>
180
+
181
+ <div
182
+ ref="code-editor"
183
+ data-stx-code-editor
184
+ @if(theme)data-syntax-theme="{{ theme }}"@endif
185
+ class="{{ editorClasses }}"
186
+ style="height: {{ height }}"
187
+ :data-readonly="locked() ? '' : null"
188
+ >
189
+ {{-- The scroller. Both layers are ordinary content inside it, so there is
190
+ one scroll position and nothing to copy between them. --}}
191
+ <div data-ce="scroller" class="stx-ce-scroller relative flex-1 min-h-0 overflow-auto">
192
+ <div data-ce="content" class="stx-ce-content relative flex min-w-full min-h-full">
193
+ {{-- Never read aloud: the numbers are positions, and the field already
194
+ reports the line the caret is on. --}}
195
+ <div data-ce="gutter" aria-hidden="true" class="stx-ce-gutter sticky left-0 z-30 shrink-0 select-none">
196
+ <div data-ce="gutter-rows" class="stx-ce-gutter-rows"></div>
197
+ </div>
198
+
199
+ <div data-ce="region" class="stx-ce-region relative flex-1">
200
+ <div data-ce="active-line" aria-hidden="true" class="stx-ce-active-line absolute top-0 left-0 right-0 pointer-events-none" style="display: none"></div>
201
+ <div data-ce="decorations" aria-hidden="true" class="stx-ce-layer absolute top-0 left-0 pointer-events-none"></div>
202
+ {{-- The painted copy. Hidden from assistive technology: the field
203
+ holds the same text and is the thing to read. --}}
204
+ <div data-ce="rows" aria-hidden="true" class="stx-ce-rows absolute top-0 left-0 right-0 pointer-events-none"></div>
205
+
206
+ <textarea
207
+ data-ce="input"
208
+ id="{{ inputId }}"
209
+ @if(name)name="{{ name }}"@endif
210
+ class="stx-ce-input absolute inset-0 z-10 block w-full h-full m-0 border-0 outline-none resize-none overflow-hidden bg-transparent"
211
+ wrap="off"
212
+ spellcheck="false"
213
+ autocomplete="off"
214
+ autocorrect="off"
215
+ autocapitalize="off"
216
+ aria-label="{{ label }}"
217
+ aria-multiline="true"
218
+ aria-describedby="{{ statusId }}"
219
+ aria-autocomplete="list"
220
+ aria-controls="{{ popupId }}"
221
+ :aria-expanded="popup().open ? 'true' : 'false'"
222
+ :aria-activedescendant="popup().activeId || null"
223
+ @if(readonly || disabled)readonly@endif
224
+ :readonly="locked()"
225
+ @if(disabled)aria-disabled="true"@endif
226
+ ></textarea>
227
+
228
+ @if(placeholder)
229
+ <div aria-hidden="true" class="stx-ce-placeholder absolute top-0 left-0 pointer-events-none" :show="isEmpty()">{{ placeholder }}</div>
230
+ @endif
231
+
232
+ <div data-ce="widgets" aria-hidden="true" class="stx-ce-layer absolute top-0 left-0 z-20 pointer-events-none"></div>
233
+ <span data-ce="measure" aria-hidden="true" class="stx-ce-measure absolute top-0 left-0 invisible pointer-events-none"></span>
234
+
235
+ {{-- The completion list. It stays in the document while closed so the
236
+ field's aria-controls always has something to point at. --}}
237
+ <ul
238
+ id="{{ popupId }}"
239
+ role="listbox"
240
+ aria-label="Completions"
241
+ class="stx-ce-popup absolute top-0 left-0 z-40 m-0 p-1 list-none overflow-y-auto rounded-control border border-line bg-panel shadow-lg"
242
+ :show="popup().open"
243
+ :style="popupStyle()"
244
+ >
245
+ <li
246
+ :for="entry in popup().items"
247
+ :key="entry.id"
248
+ :id="entry.id"
249
+ role="option"
250
+ :aria-selected="entry.selected ? 'true' : 'false'"
251
+ :data-selected="entry.selected ? '' : null"
252
+ :data-kind="entry.kind || null"
253
+ class="stx-ce-option flex items-baseline gap-3 px-2 py-0.5 rounded-control cursor-default whitespace-nowrap"
254
+ @mousedown.prevent="acceptCompletion(entry.index)"
255
+ >
256
+ <span class="stx-ce-option-label" x-text="entry.label"></span>
257
+ <span class="stx-ce-option-detail ml-auto text-fg-soft" x-text="entry.detail"></span>
258
+ </li>
259
+ </ul>
260
+ </div>
261
+ </div>
262
+ </div>
263
+
264
+ <div data-ce="status" id="{{ statusId }}" role="status" aria-live="polite" class="sr-only"></div>
265
+ </div>
266
+
267
+ <style>
268
+ /*
269
+ * Everything the editor is drawn with is a custom property, each falling back
270
+ * to one of the library's role tokens. An app that has themed its roles gets a
271
+ * matching editor without doing anything; one that wants the editor different
272
+ * sets these.
273
+ *
274
+ * The font size and the line height are lengths in pixels on purpose. The two
275
+ * layers only stay on the same pixels if a line is a whole number of them.
276
+ */
277
+ :where([data-stx-code-editor]) {
278
+ --stx-code-font: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
279
+ --stx-code-font-size: 13px;
280
+ --stx-code-line-height: 20px;
281
+ --stx-code-padding-y: 8px;
282
+ --stx-code-padding-x: 12px;
283
+ --stx-code-bg: var(--stx-panel, #ffffff);
284
+ --stx-code-fg: var(--stx-fg, #171717);
285
+ --stx-code-gutter-fg: var(--stx-fg-subtle, #a3a3a3);
286
+ --stx-code-gutter-active-fg: var(--stx-fg-strong, #404040);
287
+ --stx-code-caret: var(--stx-accent, #2563eb);
288
+ --stx-code-selection: color-mix(in srgb, var(--stx-accent, #2563eb) 30%, transparent);
289
+ --stx-code-active-line: color-mix(in srgb, var(--stx-fg, #171717) 5%, transparent);
290
+ --stx-code-ghost: var(--stx-fg-subtle, #a3a3a3);
291
+ --stx-code-error: var(--stx-danger, #dc2626);
292
+ --stx-code-warning: var(--stx-warning, #ca8a04);
293
+ --stx-code-info: var(--stx-info, #0891b2);
294
+ --stx-code-result: var(--stx-fg-soft, #737373);
295
+ background: var(--stx-code-bg);
296
+ color: var(--stx-code-fg);
297
+ }
298
+
299
+ [data-stx-code-editor] :is(.stx-ce-rows, .stx-ce-input, .stx-ce-gutter, .stx-ce-layer, .stx-ce-measure, .stx-ce-placeholder, .stx-ce-popup) {
300
+ font-family: var(--stx-code-font);
301
+ font-size: var(--stx-code-font-size);
302
+ line-height: var(--stx-code-line-height);
303
+ font-variant-ligatures: none;
304
+ font-feature-settings: "liga" 0, "calt" 0;
305
+ letter-spacing: 0;
306
+ tab-size: 2;
307
+ }
308
+
309
+ /* The field and its painted copy: the same box, the same text, the same wrap. */
310
+ [data-stx-code-editor] :is(.stx-ce-rows, .stx-ce-input, .stx-ce-placeholder) {
311
+ box-sizing: border-box;
312
+ padding: var(--stx-code-padding-y) var(--stx-code-padding-x);
313
+ white-space: pre;
314
+ overflow-wrap: normal;
315
+ word-break: normal;
316
+ }
317
+ [data-stx-code-editor] :is(.stx-ce-rows) { padding-bottom: 0; padding-top: 0; top: var(--stx-code-padding-y); }
318
+ [data-stx-code-editor] :is(.stx-ce-line) { height: var(--stx-code-line-height); }
319
+ [data-stx-code-editor] :is(.stx-ce-measure) { white-space: pre; }
320
+ [data-stx-code-editor] :is(.stx-ce-placeholder) { color: var(--stx-code-gutter-fg); }
321
+
322
+ [data-stx-code-editor] :is(.stx-ce-input) {
323
+ color: inherit;
324
+ caret-color: var(--stx-code-caret);
325
+ }
326
+ /* Until the view has painted the copy, the field's own text is the text. */
327
+ [data-stx-code-editor][data-ce-ready] :is(.stx-ce-input) {
328
+ color: transparent;
329
+ -webkit-text-fill-color: transparent;
330
+ }
331
+ [data-stx-code-editor] .stx-ce-input::selection {
332
+ background: var(--stx-code-selection);
333
+ color: transparent;
334
+ -webkit-text-fill-color: transparent;
335
+ }
336
+ [data-stx-code-editor][data-readonly] :is(.stx-ce-input) { cursor: default; }
337
+
338
+ [data-stx-code-editor] :is(.stx-ce-gutter) {
339
+ box-sizing: border-box;
340
+ padding-top: var(--stx-code-padding-y);
341
+ background: var(--stx-code-bg);
342
+ color: var(--stx-code-gutter-fg);
343
+ text-align: right;
344
+ cursor: default;
345
+ }
346
+ [data-stx-code-editor] :is(.stx-ce-number) {
347
+ height: var(--stx-code-line-height);
348
+ padding-right: 1ch;
349
+ font-variant-numeric: tabular-nums;
350
+ }
351
+ [data-stx-code-editor] :is(.stx-ce-number[data-active]) { color: var(--stx-code-gutter-active-fg); }
352
+ [data-stx-code-editor] :is(.stx-ce-number[data-severity="error"]) { color: var(--stx-code-error); }
353
+ [data-stx-code-editor] :is(.stx-ce-number[data-severity="warning"]) { color: var(--stx-code-warning); }
354
+ [data-stx-code-editor] :is(.stx-ce-number[data-severity="info"], .stx-ce-number[data-severity="hint"]) { color: var(--stx-code-info); }
355
+
356
+ [data-stx-code-editor] :is(.stx-ce-active-line) {
357
+ height: var(--stx-code-line-height);
358
+ background: var(--stx-code-active-line);
359
+ }
360
+
361
+ /* A squiggle is the text it sits under, invisible, with a wavy line beneath. */
362
+ [data-stx-code-editor] :is(.stx-ce-squiggle) {
363
+ position: absolute;
364
+ top: 0;
365
+ left: 0;
366
+ height: var(--stx-code-line-height);
367
+ color: transparent;
368
+ white-space: pre;
369
+ text-decoration: underline wavy var(--stx-code-error);
370
+ text-decoration-skip-ink: none;
371
+ text-underline-offset: 2px;
372
+ }
373
+ [data-stx-code-editor] :is(.stx-ce-squiggle[data-severity="warning"]) { text-decoration-color: var(--stx-code-warning); }
374
+ [data-stx-code-editor] :is(.stx-ce-squiggle[data-severity="info"], .stx-ce-squiggle[data-severity="hint"]) { text-decoration-color: var(--stx-code-info); }
375
+
376
+ [data-stx-code-editor] :is(.stx-ce-trailing, .stx-ce-widget--placed, .stx-ce-ghost) {
377
+ position: absolute;
378
+ top: 0;
379
+ left: 0;
380
+ white-space: pre;
381
+ }
382
+ [data-stx-code-editor] :is(.stx-ce-trailing) { display: flex; gap: 2ch; }
383
+ [data-stx-code-editor] :is(.stx-ce-widget) { color: var(--stx-code-result); }
384
+ [data-stx-code-editor] :is(.stx-ce-widget[data-kind="error"]) { color: var(--stx-code-error); }
385
+ [data-stx-code-editor] :is(.stx-ce-widget[data-kind="warning"]) { color: var(--stx-code-warning); }
386
+ [data-stx-code-editor] :is(.stx-ce-widget[data-kind="info"], .stx-ce-widget[data-kind="hint"]) { color: var(--stx-code-info); }
387
+ [data-stx-code-editor] :is(.stx-ce-ghost) { color: var(--stx-code-ghost); font-style: italic; }
388
+ [data-stx-code-editor] :is(.stx-ce-ghost--block) { background: var(--stx-code-bg); }
389
+
390
+ [data-stx-code-editor] :is(.stx-ce-popup) {
391
+ max-height: 14rem;
392
+ min-width: 12rem;
393
+ max-width: 32rem;
394
+ color: var(--stx-code-fg);
395
+ }
396
+ [data-stx-code-editor] :is(.stx-ce-option[data-selected]) {
397
+ background: color-mix(in srgb, var(--stx-accent, #2563eb) 18%, transparent);
398
+ }
399
+
400
+ /* Code colours. The same variables <CodeBlock> draws with, and the same text:
401
+ syntaxThemeCss() in utils/syntax-theme.ts, which a test holds this to. */
402
+ :where(:root) {
403
+ --stx-syntax-background: #ffffff;
404
+ --stx-syntax-foreground: #24292f;
405
+ --stx-syntax-keyword: #cf222e;
406
+ --stx-syntax-string: #0a3069;
407
+ --stx-syntax-comment: #6e7781;
408
+ --stx-syntax-numeric: #0550ae;
409
+ --stx-syntax-constant: #0550ae;
410
+ --stx-syntax-function: #8250df;
411
+ --stx-syntax-operator: #cf222e;
412
+ --stx-syntax-punctuation: #24292f;
413
+ --stx-syntax-type: #953800;
414
+ --stx-syntax-variable: #953800;
415
+ --stx-syntax-tag: #116329;
416
+ --stx-syntax-attribute: #0550ae;
417
+ }
418
+ @media (prefers-color-scheme: dark) {
419
+ :where(:root:not(.light)) {
420
+ --stx-syntax-background: #0d1117;
421
+ --stx-syntax-foreground: #c9d1d9;
422
+ --stx-syntax-keyword: #ff7b72;
423
+ --stx-syntax-string: #a5d6ff;
424
+ --stx-syntax-comment: #8b949e;
425
+ --stx-syntax-numeric: #79c0ff;
426
+ --stx-syntax-constant: #79c0ff;
427
+ --stx-syntax-function: #d2a8ff;
428
+ --stx-syntax-operator: #ff7b72;
429
+ --stx-syntax-punctuation: #c9d1d9;
430
+ --stx-syntax-type: #ffa657;
431
+ --stx-syntax-variable: #ffa657;
432
+ --stx-syntax-tag: #7ee787;
433
+ --stx-syntax-attribute: #79c0ff;
434
+ }
435
+ }
436
+ :where(.dark) {
437
+ --stx-syntax-background: #0d1117;
438
+ --stx-syntax-foreground: #c9d1d9;
439
+ --stx-syntax-keyword: #ff7b72;
440
+ --stx-syntax-string: #a5d6ff;
441
+ --stx-syntax-comment: #8b949e;
442
+ --stx-syntax-numeric: #79c0ff;
443
+ --stx-syntax-constant: #79c0ff;
444
+ --stx-syntax-function: #d2a8ff;
445
+ --stx-syntax-operator: #ff7b72;
446
+ --stx-syntax-punctuation: #c9d1d9;
447
+ --stx-syntax-type: #ffa657;
448
+ --stx-syntax-variable: #ffa657;
449
+ --stx-syntax-tag: #7ee787;
450
+ --stx-syntax-attribute: #79c0ff;
451
+ }
452
+ :where([data-syntax-theme="light"]) {
453
+ --stx-syntax-background: #ffffff;
454
+ --stx-syntax-foreground: #24292f;
455
+ --stx-syntax-keyword: #cf222e;
456
+ --stx-syntax-string: #0a3069;
457
+ --stx-syntax-comment: #6e7781;
458
+ --stx-syntax-numeric: #0550ae;
459
+ --stx-syntax-constant: #0550ae;
460
+ --stx-syntax-function: #8250df;
461
+ --stx-syntax-operator: #cf222e;
462
+ --stx-syntax-punctuation: #24292f;
463
+ --stx-syntax-type: #953800;
464
+ --stx-syntax-variable: #953800;
465
+ --stx-syntax-tag: #116329;
466
+ --stx-syntax-attribute: #0550ae;
467
+ }
468
+ :where([data-syntax-theme="dark"]) {
469
+ --stx-syntax-background: #0d1117;
470
+ --stx-syntax-foreground: #c9d1d9;
471
+ --stx-syntax-keyword: #ff7b72;
472
+ --stx-syntax-string: #a5d6ff;
473
+ --stx-syntax-comment: #8b949e;
474
+ --stx-syntax-numeric: #79c0ff;
475
+ --stx-syntax-constant: #79c0ff;
476
+ --stx-syntax-function: #d2a8ff;
477
+ --stx-syntax-operator: #ff7b72;
478
+ --stx-syntax-punctuation: #c9d1d9;
479
+ --stx-syntax-type: #ffa657;
480
+ --stx-syntax-variable: #ffa657;
481
+ --stx-syntax-tag: #7ee787;
482
+ --stx-syntax-attribute: #79c0ff;
483
+ }
484
+ .tok-keyword { color: var(--stx-syntax-keyword); }
485
+ .tok-string { color: var(--stx-syntax-string); }
486
+ .tok-comment { color: var(--stx-syntax-comment); }
487
+ .tok-numeric { color: var(--stx-syntax-numeric); }
488
+ .tok-constant { color: var(--stx-syntax-constant); }
489
+ .tok-function { color: var(--stx-syntax-function); }
490
+ .tok-operator { color: var(--stx-syntax-operator); }
491
+ .tok-punctuation { color: var(--stx-syntax-punctuation); }
492
+ .tok-type { color: var(--stx-syntax-type); }
493
+ .tok-variable { color: var(--stx-syntax-variable); }
494
+ .tok-tag { color: var(--stx-syntax-tag); }
495
+ .tok-attribute { color: var(--stx-syntax-attribute); }
496
+ .tok-comment { font-style: italic; }
497
+ </style>
@@ -0,0 +1,134 @@
1
+ # CodeEditor
2
+
3
+ A code editor: highlighting, a gutter, auto-indent, bracket pairing, comment
4
+ toggle, undo, completions, inline results, diagnostics and ghost text. No
5
+ third-party editor underneath; the tokenizer is `ts-syntax-highlighter`.
6
+
7
+ ```html
8
+ <script client>
9
+ const code = state('const user = await User.find(1)\n')
10
+
11
+ function run(detail) {
12
+ // detail.value is the whole text, detail.selection where the caret is.
13
+ }
14
+ </script>
15
+
16
+ <CodeEditor
17
+ language="typescript"
18
+ height="24rem"
19
+ v-model:value="code"
20
+ @run="run($event)"
21
+ />
22
+ ```
23
+
24
+ A component's event hands its payload to the handler as `$event`, so `run`
25
+ above receives the detail object itself.
26
+
27
+ ## How it works
28
+
29
+ The editor is a real `<textarea>` with its text made transparent, laid over a
30
+ copy of the same text drawn in colour. You type into the field and see the
31
+ copy. Because it is a real field, the browser supplies the caret, selection,
32
+ input methods, dictation, the clipboard, drag and drop, and a control a screen
33
+ reader already understands.
34
+
35
+ Three layers of code, each usable without the ones above it:
36
+
37
+ | File | What it is |
38
+ |---|---|
39
+ | `core/` | The model: document as lines, selection, undo history, commands, keymap, incremental highlighting, the completion list. No DOM and no stx. |
40
+ | `view.ts` | The DOM: keeps the two layers aligned, draws only the lines on screen, positions widgets, squiggles and ghost text. |
41
+ | `controller.ts` | The two joined, plus the highlighter and the run and completion wiring. |
42
+ | `CodeEditor.stx` | The template, the styles, and the props and events. |
43
+
44
+ `@stacksjs/components/code-editor` exports the first three for a host that
45
+ mounts an editor itself.
46
+
47
+ ## Props
48
+
49
+ | Prop | Default | |
50
+ |---|---|---|
51
+ | `value` | `''` | The text. Two-way with `v-model:value`. |
52
+ | `language` | `'typescript'` | Any language id `ts-syntax-highlighter` knows. An unknown one is shown plain. Also sets the comment marker and which brackets pair. |
53
+ | `readonly`, `disabled` | `false` | |
54
+ | `lineNumbers` | `true` | |
55
+ | `highlight` | `true` | Off shows plain text. |
56
+ | `tabSize` | `2` | |
57
+ | `insertSpaces` | `true` | Indent with spaces rather than tab characters. |
58
+ | `autoComplete` | `true` | Offer completions while typing, not only on Ctrl+Space. |
59
+ | `completions` | `[]` | Names always on offer: `{ label, insert?, detail?, kind?, filterText?, boost? }`. |
60
+ | `widgets` | `[]` | Shown beside lines: `{ line, column?, text, kind?, className? }`. `column` defaults to the end of the line. |
61
+ | `diagnostics` | `[]` | `{ from: { line, column }, to: { line, column }, message, severity? }`. Drawn as a squiggle, marked in the gutter, and said after the line. |
62
+ | `inlineDiagnostics` | `true` | Show each diagnostic's message after its line. |
63
+ | `ghostText` | `''` | A suggestion at the caret. Tab takes it, Escape lets it go, typing its first characters keeps it. |
64
+ | `keymap` | `{}` | Bindings laid over the defaults, bound with `:keymap`. An empty command name unbinds a key. |
65
+ | `theme` | follows the page | `light` or `dark` pins the code colours. |
66
+ | `height` | `'20rem'` | Any CSS length. `100%` fills a sized parent. |
67
+ | `placeholder`, `label`, `name`, `id`, `autofocus`, `className` | | `label` is the field's accessible name. |
68
+
69
+ Lines and columns are zero-based everywhere, as a language server counts them.
70
+
71
+ ## Events
72
+
73
+ | Event | Payload |
74
+ |---|---|
75
+ | `@input`, `@change` | The text. |
76
+ | `@selection` | `{ anchor, head, from, to, line, column, text, empty }` |
77
+ | `@run` | `{ value, selection }`. Cmd+Enter, or Ctrl+Enter off a Mac. |
78
+ | `@run-selection` | `{ text, from, to, line, selected }`. With Shift. The caret's line when nothing is selected. |
79
+ | `@complete` | `{ query, from, to, line, column, textBefore, explicit, trigger }`. Answer by calling `showCompletions(items, { from })`. |
80
+ | `@completion-accept` | The item taken. |
81
+ | `@ghost` | `{ outcome: 'accept' \| 'dismiss', text }` |
82
+ | `@focus`, `@blur` | |
83
+
84
+ ## Methods
85
+
86
+ Through a ref on the component: `getValue`, `setValue`, `getSelection`,
87
+ `setSelection(anchor, head)`, `focus`, `undo`, `redo`, `exec(command)`,
88
+ `setWidgets`, `setDiagnostics`, `setGhostText`, `showCompletions(items, { from })`,
89
+ `closeCompletions`, and `editor()` for the controller itself.
90
+
91
+ ## Keys
92
+
93
+ | Key | |
94
+ |---|---|
95
+ | Enter | New line at the same indentation, one level deeper after an opening bracket. |
96
+ | Tab, Shift+Tab | Indent or outdent the selected lines. With no selection, Tab moves to the next tab stop. |
97
+ | Cmd/Ctrl+/ | Comment or uncomment the selected lines. |
98
+ | Cmd/Ctrl+Z, Shift+Cmd/Ctrl+Z, Ctrl+Y | Undo, redo. |
99
+ | Cmd/Ctrl+Enter | Run. |
100
+ | Shift+Cmd/Ctrl+Enter | Run the selection. |
101
+ | Ctrl+Space | Completions. |
102
+ | Escape, then Tab | Leave the editor. Tab indents, so this is the way out by keyboard. |
103
+
104
+ In a binding, `Mod` is Command on a Mac and Control elsewhere:
105
+ `:keymap="{ 'Mod-s': 'run', 'Tab': '' }"`.
106
+
107
+ ## Theming
108
+
109
+ Everything is a CSS custom property that falls back to one of the library's
110
+ role tokens, so an app that has themed its roles already has a matching editor.
111
+
112
+ - The editor: `--stx-code-bg`, `--stx-code-fg`, `--stx-code-gutter-fg`,
113
+ `--stx-code-caret`, `--stx-code-selection`, `--stx-code-active-line`,
114
+ `--stx-code-error`, `--stx-code-warning`, `--stx-code-info`.
115
+ - The code: `--stx-syntax-keyword`, `-string`, `-comment`, `-numeric`,
116
+ `-constant`, `-function`, `-operator`, `-punctuation`, `-type`, `-variable`,
117
+ `-tag`, `-attribute`. `<CodeBlock>` reads the same ones.
118
+ - The type: `--stx-code-font`, `--stx-code-font-size`, `--stx-code-line-height`.
119
+ Give the last two in pixels. The two layers only stay on the same pixels if a
120
+ line is a whole number of them.
121
+
122
+ ## Limits of this version
123
+
124
+ - **One cursor.** The input surface is a textarea and a textarea has one. The
125
+ model already holds a list of ranges, so an input surface of the editor's own
126
+ can add more without changing it.
127
+ - **No soft wrap.** Long lines scroll sideways.
128
+ - **The whole text lives in the field.** In WebKit the field itself spends
129
+ about 7 ms on a typed character once it holds 2,000 lines, before the editor
130
+ does anything; the editor adds between 1 and 2 ms. That is inside a frame, and
131
+ it grows with the size of the file. It is the ceiling of this technique, and
132
+ the reason for the next one.
133
+ - **Grammars ship with the page.** stx inlines a component's imports into the
134
+ page, so a page with an editor carries every grammar the highlighter has.