@stacksjs/components 0.2.863 → 0.2.865
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/{Button-k3j4cc3h.stx → Button-sebyqeva.stx} +6 -0
- package/dist/{CodeEditor-jg4w7e15.stx → CodeEditor-kaywcb83.stx} +111 -23
- package/dist/code-editor/editor.js +551 -10
- package/dist/index.js +12 -12
- package/dist/ui/code-editor/CodeEditor.d.ts +6 -1
- package/dist/ui/code-editor/controller.d.ts +31 -1
- package/dist/ui/code-editor/core/completion.d.ts +1 -0
- package/dist/ui/code-editor/core/hints.d.ts +68 -0
- package/dist/ui/code-editor/core/index.d.ts +1 -0
- package/dist/ui/code-editor/index.d.ts +6 -3
- package/dist/ui/code-editor/view.d.ts +30 -1
- package/package.json +3 -3
- package/src/ui/button/Button.stx +6 -0
- package/src/ui/code-editor/CodeEditor.stx +111 -23
- package/src/ui/code-editor/README.md +56 -2
- package/src/ui/code-editor/controller.ts +378 -6
- package/src/ui/code-editor/core/completion.ts +2 -0
- package/src/ui/code-editor/core/hints.ts +241 -0
- package/src/ui/code-editor/core/index.ts +1 -0
- package/src/ui/code-editor/core/keymap.ts +2 -0
- package/src/ui/code-editor/index.ts +9 -3
- package/src/ui/code-editor/view.ts +272 -7
- package/src/ui/preview-frame/README.md +15 -7
- package/src/ui/preview-frame/index.ts +6 -1
- package/src/ui/preview-frame/view.ts +5 -3
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a type hint is made of, before anything is drawn.
|
|
3
|
+
*
|
|
4
|
+
* Three questions an editor asks a language server, and the text handling each
|
|
5
|
+
* one needs on this side: which word is under a point (hover), which call the
|
|
6
|
+
* caret is inside and at which argument (signature help), and how the answer
|
|
7
|
+
* that comes back is cut up for display. None of it touches the DOM, so it is
|
|
8
|
+
* tested as plain functions.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const WORD_CHARACTER = /[\p{L}\p{N}_$]/u
|
|
12
|
+
|
|
13
|
+
export interface WordRange {
|
|
14
|
+
/** Columns in the line: `from` inclusive, `to` exclusive. */
|
|
15
|
+
from: number
|
|
16
|
+
to: number
|
|
17
|
+
word: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The word at a column, or null when the column is not on one.
|
|
22
|
+
*
|
|
23
|
+
* A column is on a word when the character at it is part of one. The position
|
|
24
|
+
* just past a word's last character counts too when `inclusiveEnd` is set,
|
|
25
|
+
* which is where a caret rests after typing the word.
|
|
26
|
+
*/
|
|
27
|
+
export function wordAt(text: string, column: number, inclusiveEnd: boolean = false): WordRange | null {
|
|
28
|
+
if (column < 0 || column > text.length)
|
|
29
|
+
return null
|
|
30
|
+
|
|
31
|
+
let at = column
|
|
32
|
+
if (at >= text.length || !WORD_CHARACTER.test(text.charAt(at))) {
|
|
33
|
+
if (!inclusiveEnd || at === 0 || !WORD_CHARACTER.test(text.charAt(at - 1)))
|
|
34
|
+
return null
|
|
35
|
+
at--
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let from = at
|
|
39
|
+
while (from > 0 && WORD_CHARACTER.test(text.charAt(from - 1)))
|
|
40
|
+
from--
|
|
41
|
+
let to = at + 1
|
|
42
|
+
while (to < text.length && WORD_CHARACTER.test(text.charAt(to)))
|
|
43
|
+
to++
|
|
44
|
+
|
|
45
|
+
return { from, to, word: text.slice(from, to) }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface HoverSegment {
|
|
49
|
+
kind: 'code' | 'text'
|
|
50
|
+
text: string
|
|
51
|
+
/** The fence's language for a code segment. Empty when it named none. */
|
|
52
|
+
language: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const FENCE = /^(`{3,}|~{3,})[ \t]*([\w+#.-]*)[^\n]*\n([\s\S]*?)\n?^\1[ \t]*$/gm
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Hover content as the pieces it is drawn in.
|
|
59
|
+
*
|
|
60
|
+
* A language server answers in Markdown, and the part worth keeping exactly is
|
|
61
|
+
* the fenced block: that is the signature. Each fence becomes a code segment,
|
|
62
|
+
* and what lies between fences becomes text, left as written. Nothing here
|
|
63
|
+
* interprets Markdown beyond the fence, on purpose: the text is shown as text.
|
|
64
|
+
*/
|
|
65
|
+
export function parseHoverContent(content: string): HoverSegment[] {
|
|
66
|
+
const source = content.replace(/\r\n?/g, '\n')
|
|
67
|
+
const segments: HoverSegment[] = []
|
|
68
|
+
const text = (value: string): void => {
|
|
69
|
+
const trimmed = value.replace(/^\n+|\s+$/g, '')
|
|
70
|
+
if (trimmed !== '')
|
|
71
|
+
segments.push({ kind: 'text', text: trimmed, language: '' })
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let last = 0
|
|
75
|
+
FENCE.lastIndex = 0
|
|
76
|
+
for (let match = FENCE.exec(source); match; match = FENCE.exec(source)) {
|
|
77
|
+
text(source.slice(last, match.index))
|
|
78
|
+
if (match[3] !== '')
|
|
79
|
+
segments.push({ kind: 'code', text: match[3]!, language: match[2] ?? '' })
|
|
80
|
+
last = match.index + match[0].length
|
|
81
|
+
}
|
|
82
|
+
text(source.slice(last))
|
|
83
|
+
|
|
84
|
+
return segments
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Hover content as one line of plain text, for a screen reader. */
|
|
88
|
+
export function hoverPlainText(content: string): string {
|
|
89
|
+
return parseHoverContent(content)
|
|
90
|
+
.map(segment => segment.text.replace(/\s+/g, ' ').trim())
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join('. ')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface CallContext {
|
|
96
|
+
/** Index in the text of the call's opening parenthesis. */
|
|
97
|
+
open: number
|
|
98
|
+
/** Zero-based index of the argument the text ends in. */
|
|
99
|
+
active: number
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** How far back a call is looked for. A caret is rarely further into one. */
|
|
103
|
+
const CALL_SCAN_LIMIT = 4000
|
|
104
|
+
const CLOSERS: Record<string, string> = { ')': '(', ']': '[', '}': '{' }
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The call the end of `text` is inside, or null when it is inside none.
|
|
108
|
+
*
|
|
109
|
+
* Reads forward, keeping a stack of open brackets and stepping over strings
|
|
110
|
+
* and comments, so a parenthesis or a comma in a string does not count. The
|
|
111
|
+
* call is the nearest unclosed parenthesis; commas directly inside it, and not
|
|
112
|
+
* inside a nested bracket, say which argument.
|
|
113
|
+
*
|
|
114
|
+
* It is an estimate from text alone. A language server knows better, and its
|
|
115
|
+
* answer is what gets shown; this decides when to ask, and keeps the active
|
|
116
|
+
* argument moving between answers.
|
|
117
|
+
*/
|
|
118
|
+
export function callContext(text: string): CallContext | null {
|
|
119
|
+
const start = Math.max(0, text.length - CALL_SCAN_LIMIT)
|
|
120
|
+
const stack: Array<{ bracket: string, index: number, commas: number }> = []
|
|
121
|
+
let quote = ''
|
|
122
|
+
|
|
123
|
+
for (let index = start; index < text.length; index++) {
|
|
124
|
+
const character = text.charAt(index)
|
|
125
|
+
|
|
126
|
+
if (quote) {
|
|
127
|
+
if (character === '\\')
|
|
128
|
+
index++
|
|
129
|
+
else if (character === quote || (character === '\n' && quote !== '`'))
|
|
130
|
+
quote = ''
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (character === '"' || character === '\'' || character === '`') {
|
|
135
|
+
quote = character
|
|
136
|
+
}
|
|
137
|
+
else if (character === '/' && text.charAt(index + 1) === '/') {
|
|
138
|
+
const end = text.indexOf('\n', index)
|
|
139
|
+
index = end === -1 ? text.length : end
|
|
140
|
+
}
|
|
141
|
+
else if (character === '/' && text.charAt(index + 1) === '*') {
|
|
142
|
+
const end = text.indexOf('*/', index + 2)
|
|
143
|
+
index = end === -1 ? text.length : end + 1
|
|
144
|
+
}
|
|
145
|
+
else if (character === '(' || character === '[' || character === '{') {
|
|
146
|
+
stack.push({ bracket: character, index, commas: 0 })
|
|
147
|
+
}
|
|
148
|
+
else if (character in CLOSERS) {
|
|
149
|
+
// A closer with nothing to close began before the scan did. Ignore it.
|
|
150
|
+
if (stack.length > 0 && stack[stack.length - 1]!.bracket === CLOSERS[character])
|
|
151
|
+
stack.pop()
|
|
152
|
+
}
|
|
153
|
+
else if (character === ',' && stack.length > 0) {
|
|
154
|
+
stack[stack.length - 1]!.commas++
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Inside a string or a comment there is no argument being written.
|
|
159
|
+
if (quote)
|
|
160
|
+
return null
|
|
161
|
+
|
|
162
|
+
for (let depth = stack.length - 1; depth >= 0; depth--) {
|
|
163
|
+
const frame = stack[depth]!
|
|
164
|
+
if (frame.bracket === '(')
|
|
165
|
+
return { open: frame.index, active: frame.commas }
|
|
166
|
+
// A block is not an argument: `if (x) { foo` is not inside a call.
|
|
167
|
+
if (frame.bracket === '{' && !/(?:[(,=:]|=>)\s*$/.test(text.slice(Math.max(start, frame.index - 40), frame.index)))
|
|
168
|
+
return null
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** One parameter of a signature: its text in the label, or where it sits in it. */
|
|
175
|
+
export type SignatureParameter = string | [number, number] | { label: string | [number, number], documentation?: string }
|
|
176
|
+
|
|
177
|
+
export interface SignatureHelp {
|
|
178
|
+
/** The whole signature, as one line: `fetchUser(id: string, fresh?: boolean): User`. */
|
|
179
|
+
label: string
|
|
180
|
+
parameters?: readonly SignatureParameter[]
|
|
181
|
+
/** Zero-based index of the parameter being written. */
|
|
182
|
+
active?: number
|
|
183
|
+
documentation?: string
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface SignatureParts {
|
|
187
|
+
before: string
|
|
188
|
+
/** The active parameter's text. Empty when there is none to mark. */
|
|
189
|
+
active: string
|
|
190
|
+
after: string
|
|
191
|
+
/** What the active parameter says about itself, when it does. */
|
|
192
|
+
parameterDocumentation: string
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A signature cut around its active parameter.
|
|
197
|
+
*
|
|
198
|
+
* Parameters are found in the label in order, each search starting where the
|
|
199
|
+
* last one ended, so two parameters with the same text are told apart. A
|
|
200
|
+
* parameter given as a pair of offsets is taken as is.
|
|
201
|
+
*/
|
|
202
|
+
export function signatureParts(help: SignatureHelp): SignatureParts {
|
|
203
|
+
const { label } = help
|
|
204
|
+
const parameters = help.parameters ?? []
|
|
205
|
+
const active = help.active ?? 0
|
|
206
|
+
const none: SignatureParts = { before: label, active: '', after: '', parameterDocumentation: '' }
|
|
207
|
+
let cursor = 0
|
|
208
|
+
|
|
209
|
+
for (let index = 0; index < parameters.length; index++) {
|
|
210
|
+
const parameter = parameters[index]!
|
|
211
|
+
const spec = typeof parameter === 'object' && !Array.isArray(parameter) ? parameter.label : parameter
|
|
212
|
+
let from: number
|
|
213
|
+
let to: number
|
|
214
|
+
|
|
215
|
+
if (Array.isArray(spec)) {
|
|
216
|
+
from = Math.max(0, Math.min(label.length, spec[0]))
|
|
217
|
+
to = Math.max(from, Math.min(label.length, spec[1]))
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
from = spec === '' ? -1 : label.indexOf(spec as string, cursor)
|
|
221
|
+
if (from === -1) {
|
|
222
|
+
if (index === active)
|
|
223
|
+
return none
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
to = from + (spec as string).length
|
|
227
|
+
}
|
|
228
|
+
cursor = to
|
|
229
|
+
|
|
230
|
+
if (index === active) {
|
|
231
|
+
return {
|
|
232
|
+
before: label.slice(0, from),
|
|
233
|
+
active: label.slice(from, to),
|
|
234
|
+
after: label.slice(to),
|
|
235
|
+
parameterDocumentation: typeof parameter === 'object' && !Array.isArray(parameter) ? parameter.documentation ?? '' : '',
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return none
|
|
241
|
+
}
|
|
@@ -33,6 +33,8 @@ export const defaultKeymap: Keymap = {
|
|
|
33
33
|
'Mod-Enter': 'run',
|
|
34
34
|
'Mod-Shift-Enter': 'runSelection',
|
|
35
35
|
'Ctrl-Space': 'triggerCompletion',
|
|
36
|
+
'Mod-i': 'showHover',
|
|
37
|
+
'Mod-Shift-Space': 'triggerSignature',
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
const MODIFIER_ORDER = ['Ctrl', 'Alt', 'Shift', 'Meta'] as const
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type { CodeEditorConfig, CodeEditorHooks, CompletionRequest, PopupItem, PopupState, RunDetail, RunSelectionDetail, SelectionDetail } from './controller'
|
|
2
|
-
export type { CompletionItem, Keymap, Position } from './core'
|
|
3
|
-
export type { CaretAnchor, DiagnosticSeverity, EditorDiagnostic, EditorViewOptions, EditorWidget } from './view'
|
|
1
|
+
export type { CodeEditorConfig, CodeEditorHooks, CompletionRequest, HoverRequest, PopupItem, PopupState, RunDetail, RunSelectionDetail, SelectionDetail, SignatureRequest } from './controller'
|
|
2
|
+
export type { CompletionItem, Keymap, Position, SignatureHelp, SignatureParameter } from './core'
|
|
3
|
+
export type { CaretAnchor, DiagnosticSeverity, EditorDiagnostic, EditorViewOptions, EditorWidget, HintBlock, SignatureDisplay } from './view'
|
|
4
4
|
export { default as CodeEditor } from './CodeEditor.stx'
|
|
5
5
|
|
|
6
6
|
export interface CodeEditorProps {
|
|
@@ -30,6 +30,12 @@ export interface CodeEditorProps {
|
|
|
30
30
|
inlineDiagnostics?: boolean
|
|
31
31
|
/** A suggestion shown at the caret. Tab takes it, Escape lets it go. */
|
|
32
32
|
ghostText?: string
|
|
33
|
+
/** Ask for a hover (`@hover`) when the pointer rests on a word. Default true. */
|
|
34
|
+
hover?: boolean
|
|
35
|
+
/** Milliseconds the pointer rests before a hover is asked for. Default 300. */
|
|
36
|
+
hoverDelay?: number
|
|
37
|
+
/** Ask for signature help (`@signature`) after `(` and `,`. Default true. */
|
|
38
|
+
signatureHelp?: boolean
|
|
33
39
|
/** Bindings laid over the defaults, such as `{ 'Mod-s': 'run' }`. */
|
|
34
40
|
keymap?: import('./core').Keymap
|
|
35
41
|
/** Pin the code colours to `light` or `dark`. Follows the page otherwise. */
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* the elements; this fills and positions them. The model, in `./core`, knows
|
|
24
24
|
* nothing about any of it.
|
|
25
25
|
*/
|
|
26
|
-
import type { EditorCore, HighlightedLine, LineTokenizer, Position, ResolvedKeymap, Transaction } from './core'
|
|
26
|
+
import type { EditorCore, HighlightedLine, HighlightToken, LineTokenizer, Position, ResolvedKeymap, Transaction } from './core'
|
|
27
27
|
import { handleTextInput, LineHighlighter, mapOffset, rangeEmpty, rangeFrom, rangeTo } from './core'
|
|
28
28
|
|
|
29
29
|
/** Something shown beside or after a line: a result, a note, an error. */
|
|
@@ -73,10 +73,37 @@ export interface EditorViewOptions {
|
|
|
73
73
|
onBlur?: () => void
|
|
74
74
|
/** Ghost text was taken or let go. */
|
|
75
75
|
onGhost?: (outcome: 'accept' | 'dismiss', text: string) => void
|
|
76
|
+
/**
|
|
77
|
+
* The pointer has rested over the text: at this offset, or `null` when it is
|
|
78
|
+
* not over a character. Called once per rest, not once per move.
|
|
79
|
+
*/
|
|
80
|
+
onPointerRest?: (offset: number | null) => void
|
|
81
|
+
/** The pointer left the editor, or a button went down. */
|
|
82
|
+
onPointerLeave?: () => void
|
|
83
|
+
/** The text was scrolled. */
|
|
84
|
+
onScroll?: () => void
|
|
85
|
+
/** How long the pointer has to be still before it counts as resting. Default 300 ms. */
|
|
86
|
+
hoverDelay?: number
|
|
76
87
|
/** Show each diagnostic's message after its line. Default true. */
|
|
77
88
|
inlineDiagnostics?: boolean
|
|
78
89
|
}
|
|
79
90
|
|
|
91
|
+
/** One piece of a hint: a block of code, already coloured, or a paragraph. */
|
|
92
|
+
export interface HintBlock {
|
|
93
|
+
kind: 'code' | 'text'
|
|
94
|
+
/** Markup, for a code block that has been coloured. Trusted: the view's own. */
|
|
95
|
+
html?: string
|
|
96
|
+
text: string
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A signature, cut around the parameter being written. */
|
|
100
|
+
export interface SignatureDisplay {
|
|
101
|
+
before: string
|
|
102
|
+
active: string
|
|
103
|
+
after: string
|
|
104
|
+
documentation?: string
|
|
105
|
+
}
|
|
106
|
+
|
|
80
107
|
interface StoredDiagnostic {
|
|
81
108
|
from: number
|
|
82
109
|
to: number
|
|
@@ -105,6 +132,10 @@ interface Metrics {
|
|
|
105
132
|
const OVERSCAN = 12
|
|
106
133
|
/** Lines tokenized past the screen in the pass that follows a change. */
|
|
107
134
|
const SYNC_BUDGET = 200
|
|
135
|
+
/** How long the pointer rests before a hover is asked for. */
|
|
136
|
+
const HOVER_DELAY = 300
|
|
137
|
+
/** Room a hint wants above its line before it goes below instead. */
|
|
138
|
+
const HINT_ROOM = 120
|
|
108
139
|
/** Lines tokenized per idle pass while a long cascade or a first load finishes. */
|
|
109
140
|
const IDLE_BUDGET = 600
|
|
110
141
|
const SEVERITY_RANK: Record<DiagnosticSeverity, number> = { error: 3, warning: 2, info: 1, hint: 0 }
|
|
@@ -118,16 +149,34 @@ function escapeHtml(text: string): string {
|
|
|
118
149
|
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
119
150
|
}
|
|
120
151
|
|
|
152
|
+
/** Coloured runs as HTML: a span per run, plain text left unwrapped. */
|
|
153
|
+
export function renderTokensHtml(tokens: readonly HighlightToken[]): string {
|
|
154
|
+
let html = ''
|
|
155
|
+
for (const token of tokens)
|
|
156
|
+
html += token.type === '' ? escapeHtml(token.content) : `<span class="tok-${token.type}">${escapeHtml(token.content)}</span>`
|
|
157
|
+
|
|
158
|
+
return html
|
|
159
|
+
}
|
|
160
|
+
|
|
121
161
|
/** One line as HTML: a span per coloured run, plain text left unwrapped. */
|
|
122
162
|
export function renderLineHtml(line: HighlightedLine | null, text: string): string {
|
|
123
163
|
if (!line || line.text !== text)
|
|
124
164
|
return escapeHtml(text)
|
|
125
165
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
html += token.type === '' ? escapeHtml(token.content) : `<span class="tok-${token.type}">${escapeHtml(token.content)}</span>`
|
|
166
|
+
return renderTokensHtml(line.tokens)
|
|
167
|
+
}
|
|
129
168
|
|
|
130
|
-
|
|
169
|
+
/** A piece of code as HTML, coloured by a line tokenizer from its first line. */
|
|
170
|
+
export function renderCodeHtml<State>(tokenizer: LineTokenizer<State>, code: string): string {
|
|
171
|
+
let state = tokenizer.initialState()
|
|
172
|
+
const lines: string[] = []
|
|
173
|
+
for (const text of code.split('\n')) {
|
|
174
|
+
const result = tokenizer.tokenizeLine(text, state)
|
|
175
|
+
state = result.endState
|
|
176
|
+
lines.push(renderTokensHtml(result.tokens))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return lines.join('\n')
|
|
131
180
|
}
|
|
132
181
|
|
|
133
182
|
function part(root: HTMLElement, name: string): HTMLElement {
|
|
@@ -154,6 +203,8 @@ export class EditorView {
|
|
|
154
203
|
private readonly widgetLayer: HTMLElement
|
|
155
204
|
private readonly measure: HTMLElement
|
|
156
205
|
private readonly status: HTMLElement | null
|
|
206
|
+
private readonly hover: HTMLElement | null
|
|
207
|
+
private readonly signature: HTMLElement | null
|
|
157
208
|
|
|
158
209
|
private keymap: ResolvedKeymap
|
|
159
210
|
private highlighter: LineHighlighter | null = null
|
|
@@ -175,6 +226,13 @@ export class EditorView {
|
|
|
175
226
|
private ghost: { offset: number, text: string } | null = null
|
|
176
227
|
private announced = ''
|
|
177
228
|
|
|
229
|
+
private hoverOffset = -1
|
|
230
|
+
private signatureOffset = -1
|
|
231
|
+
private pointerX = 0
|
|
232
|
+
private pointerY = 0
|
|
233
|
+
private pointerTimer: ReturnType<typeof setTimeout> | undefined
|
|
234
|
+
private pointerInHint = false
|
|
235
|
+
|
|
178
236
|
private composing = false
|
|
179
237
|
private focused = false
|
|
180
238
|
private tabLeavesNext = false
|
|
@@ -201,6 +259,9 @@ export class EditorView {
|
|
|
201
259
|
this.widgetLayer = part(root, 'widgets')
|
|
202
260
|
this.measure = part(root, 'measure')
|
|
203
261
|
this.status = root.querySelector<HTMLElement>('[data-ce="status"]')
|
|
262
|
+
// Optional: a host with its own template may not want hints at all.
|
|
263
|
+
this.hover = root.querySelector<HTMLElement>('[data-ce="hover"]')
|
|
264
|
+
this.signature = root.querySelector<HTMLElement>('[data-ce="signature"]')
|
|
204
265
|
|
|
205
266
|
// The field may already hold text the server rendered into it. The model is
|
|
206
267
|
// the authority from here on, so whichever differs is brought into line.
|
|
@@ -248,7 +309,38 @@ export class EditorView {
|
|
|
248
309
|
if (root.ownerDocument.activeElement === this.textarea)
|
|
249
310
|
this.syncSelection()
|
|
250
311
|
})
|
|
251
|
-
this.listen(this.scroller, 'scroll', () =>
|
|
312
|
+
this.listen(this.scroller, 'scroll', () => {
|
|
313
|
+
this.scheduleRender()
|
|
314
|
+
this.options.onScroll?.()
|
|
315
|
+
}, { passive: true })
|
|
316
|
+
|
|
317
|
+
// Hover. A move only remembers where the pointer is and restarts a timer:
|
|
318
|
+
// nothing is measured until the pointer has stopped, so moving the mouse
|
|
319
|
+
// across the editor costs no layout at all.
|
|
320
|
+
this.listen(this.textarea, 'mousemove', (event) => {
|
|
321
|
+
const move = event as MouseEvent
|
|
322
|
+
// A drag is a selection being made, not a question being asked.
|
|
323
|
+
if (move.buttons !== 0)
|
|
324
|
+
return
|
|
325
|
+
if (move.clientX === this.pointerX && move.clientY === this.pointerY)
|
|
326
|
+
return
|
|
327
|
+
|
|
328
|
+
this.pointerX = move.clientX
|
|
329
|
+
this.pointerY = move.clientY
|
|
330
|
+
this.schedulePointerRest()
|
|
331
|
+
}, { passive: true })
|
|
332
|
+
this.listen(this.textarea, 'mousedown', () => this.pointerGone())
|
|
333
|
+
this.listen(this.root, 'mouseleave', () => this.pointerGone())
|
|
334
|
+
if (this.hover) {
|
|
335
|
+
// Over the hint itself the pointer is reading it, not leaving the word.
|
|
336
|
+
this.listen(this.hover, 'mouseenter', () => {
|
|
337
|
+
this.pointerInHint = true
|
|
338
|
+
this.cancelPointerRest()
|
|
339
|
+
})
|
|
340
|
+
this.listen(this.hover, 'mouseleave', () => {
|
|
341
|
+
this.pointerInHint = false
|
|
342
|
+
})
|
|
343
|
+
}
|
|
252
344
|
this.listen(this.gutter, 'mousedown', event => this.onGutterMouseDown(event as MouseEvent))
|
|
253
345
|
|
|
254
346
|
if (typeof ResizeObserver !== 'undefined') {
|
|
@@ -874,6 +966,177 @@ export class EditorView {
|
|
|
874
966
|
}, 0)
|
|
875
967
|
}
|
|
876
968
|
|
|
969
|
+
// ------------------------------------------------------------------ pointer
|
|
970
|
+
|
|
971
|
+
private schedulePointerRest(): void {
|
|
972
|
+
this.cancelPointerRest()
|
|
973
|
+
this.pointerTimer = setTimeout(() => {
|
|
974
|
+
this.pointerTimer = undefined
|
|
975
|
+
if (!this.destroyed && !this.pointerInHint)
|
|
976
|
+
this.options.onPointerRest?.(this.offsetAtPoint(this.pointerX, this.pointerY))
|
|
977
|
+
}, this.options.hoverDelay ?? HOVER_DELAY)
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
private cancelPointerRest(): void {
|
|
981
|
+
if (this.pointerTimer !== undefined) {
|
|
982
|
+
clearTimeout(this.pointerTimer)
|
|
983
|
+
this.pointerTimer = undefined
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
private pointerGone(): void {
|
|
988
|
+
this.cancelPointerRest()
|
|
989
|
+
this.pointerInHint = false
|
|
990
|
+
this.options.onPointerLeave?.()
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* The offset of the character under a point on screen, or null when the
|
|
995
|
+
* point is not over one: past the end of its line, or below the last line.
|
|
996
|
+
*
|
|
997
|
+
* The field covers the painted text, so the browser cannot be asked which
|
|
998
|
+
* element is there. The answer comes from the same numbers the text is
|
|
999
|
+
* drawn with: one rectangle read, then arithmetic.
|
|
1000
|
+
*/
|
|
1001
|
+
offsetAtPoint(clientX: number, clientY: number): number | null {
|
|
1002
|
+
const { lineHeight, padTop, padLeft, charWidth } = this.metrics
|
|
1003
|
+
const bounds = this.region.getBoundingClientRect()
|
|
1004
|
+
const x = clientX - bounds.left - padLeft
|
|
1005
|
+
const y = clientY - bounds.top - padTop
|
|
1006
|
+
if (x < 0 || y < 0)
|
|
1007
|
+
return null
|
|
1008
|
+
|
|
1009
|
+
const doc = this.core.doc
|
|
1010
|
+
const line = Math.floor(y / lineHeight)
|
|
1011
|
+
if (line >= doc.lineCount)
|
|
1012
|
+
return null
|
|
1013
|
+
|
|
1014
|
+
const text = doc.line(line)
|
|
1015
|
+
let column: number
|
|
1016
|
+
if (PRINTABLE_ASCII.test(text)) {
|
|
1017
|
+
column = Math.floor(x / charWidth)
|
|
1018
|
+
}
|
|
1019
|
+
else {
|
|
1020
|
+
// Widths differ, so find the last column that starts at or before x.
|
|
1021
|
+
let low = 0
|
|
1022
|
+
let high = text.length
|
|
1023
|
+
while (low < high) {
|
|
1024
|
+
const middle = (low + high + 1) >> 1
|
|
1025
|
+
if (this.widthOf(text, middle) <= x)
|
|
1026
|
+
low = middle
|
|
1027
|
+
else
|
|
1028
|
+
high = middle - 1
|
|
1029
|
+
}
|
|
1030
|
+
column = low
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
return column >= text.length ? null : doc.lineInfo(line).from + column
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
// ------------------------------------------------------------------- hints
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* Put a hint beside an offset: above its line when there is room, below it
|
|
1040
|
+
* otherwise, and never hanging off the right edge of what is visible.
|
|
1041
|
+
*/
|
|
1042
|
+
private placeHint(element: HTMLElement, offset: number, side?: 'above' | 'below'): void {
|
|
1043
|
+
const anchor = this.caretAnchor(offset)
|
|
1044
|
+
const above = side ? side === 'above' : anchor.spaceAbove >= HINT_ROOM || anchor.spaceAbove > anchor.spaceBelow
|
|
1045
|
+
element.style.display = ''
|
|
1046
|
+
element.dataset.placement = above ? 'above' : 'below'
|
|
1047
|
+
|
|
1048
|
+
const visibleRight = this.scroller.scrollLeft + this.scroller.clientWidth - this.gutterWidth
|
|
1049
|
+
const left = Math.max(this.scroller.scrollLeft, Math.min(anchor.left, visibleRight - element.offsetWidth - this.metrics.charWidth))
|
|
1050
|
+
element.style.transform = `translate(${Math.max(0, left)}px, ${above ? anchor.top : anchor.bottom}px)${above ? ' translateY(-100%)' : ''}`
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
private hintBlock(block: HintBlock): HTMLElement {
|
|
1054
|
+
const element = this.root.ownerDocument.createElement(block.kind === 'code' ? 'pre' : 'p')
|
|
1055
|
+
element.className = block.kind === 'code' ? 'stx-ce-hint-code' : 'stx-ce-hint-text'
|
|
1056
|
+
if (block.kind === 'code' && block.html !== undefined)
|
|
1057
|
+
element.innerHTML = block.html
|
|
1058
|
+
else
|
|
1059
|
+
element.textContent = block.text
|
|
1060
|
+
|
|
1061
|
+
return element
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
/** Show a hint anchored to an offset, replacing whatever hint was showing. */
|
|
1065
|
+
showHover(blocks: readonly HintBlock[], offset: number): void {
|
|
1066
|
+
if (!this.hover || this.destroyed)
|
|
1067
|
+
return
|
|
1068
|
+
|
|
1069
|
+
if (blocks.length === 0) {
|
|
1070
|
+
this.hideHover()
|
|
1071
|
+
return
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
this.hoverOffset = Math.max(0, Math.min(this.core.doc.length, offset))
|
|
1075
|
+
this.hover.replaceChildren(...blocks.map(block => this.hintBlock(block)))
|
|
1076
|
+
this.hover.scrollTop = 0
|
|
1077
|
+
this.placeHint(this.hover, this.hoverOffset)
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
hideHover(): void {
|
|
1081
|
+
this.pointerInHint = false
|
|
1082
|
+
if (!this.hover || this.hoverOffset < 0)
|
|
1083
|
+
return
|
|
1084
|
+
|
|
1085
|
+
this.hoverOffset = -1
|
|
1086
|
+
this.hover.style.display = 'none'
|
|
1087
|
+
this.hover.replaceChildren()
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
get hoverOpen(): boolean {
|
|
1091
|
+
return this.hoverOffset >= 0
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* Show a signature beside an offset, with one parameter marked. `side` puts
|
|
1096
|
+
* it there whatever the room, for when the other side is taken.
|
|
1097
|
+
*/
|
|
1098
|
+
showSignature(display: SignatureDisplay, offset: number, side?: 'above' | 'below'): void {
|
|
1099
|
+
if (!this.signature || this.destroyed)
|
|
1100
|
+
return
|
|
1101
|
+
|
|
1102
|
+
const document = this.root.ownerDocument
|
|
1103
|
+
const label = document.createElement('div')
|
|
1104
|
+
label.className = 'stx-ce-signature-label'
|
|
1105
|
+
label.append(display.before)
|
|
1106
|
+
if (display.active !== '') {
|
|
1107
|
+
const active = document.createElement('strong')
|
|
1108
|
+
active.className = 'stx-ce-signature-active'
|
|
1109
|
+
active.textContent = display.active
|
|
1110
|
+
label.append(active)
|
|
1111
|
+
}
|
|
1112
|
+
label.append(display.after)
|
|
1113
|
+
|
|
1114
|
+
const children: HTMLElement[] = [label]
|
|
1115
|
+
if (display.documentation) {
|
|
1116
|
+
const note = document.createElement('p')
|
|
1117
|
+
note.className = 'stx-ce-hint-text'
|
|
1118
|
+
note.textContent = display.documentation
|
|
1119
|
+
children.push(note)
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
this.signatureOffset = Math.max(0, Math.min(this.core.doc.length, offset))
|
|
1123
|
+
this.signature.replaceChildren(...children)
|
|
1124
|
+
this.placeHint(this.signature, this.signatureOffset, side)
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
hideSignature(): void {
|
|
1128
|
+
if (!this.signature || this.signatureOffset < 0)
|
|
1129
|
+
return
|
|
1130
|
+
|
|
1131
|
+
this.signatureOffset = -1
|
|
1132
|
+
this.signature.style.display = 'none'
|
|
1133
|
+
this.signature.replaceChildren()
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
get signatureOpen(): boolean {
|
|
1137
|
+
return this.signatureOffset >= 0
|
|
1138
|
+
}
|
|
1139
|
+
|
|
877
1140
|
// ------------------------------------------------------------------- caret
|
|
878
1141
|
|
|
879
1142
|
/** Where the caret is, in the coordinates of the text area. */
|
|
@@ -1022,7 +1285,8 @@ export class EditorView {
|
|
|
1022
1285
|
|
|
1023
1286
|
// ----------------------------------------------------------- announcements
|
|
1024
1287
|
|
|
1025
|
-
|
|
1288
|
+
/** Say something through the editor's live region. */
|
|
1289
|
+
announce(message: string): void {
|
|
1026
1290
|
if (this.status && this.status.textContent !== message)
|
|
1027
1291
|
this.status.textContent = message
|
|
1028
1292
|
}
|
|
@@ -1057,6 +1321,7 @@ export class EditorView {
|
|
|
1057
1321
|
this.resizeObserver?.disconnect()
|
|
1058
1322
|
if (this.idle !== undefined)
|
|
1059
1323
|
clearTimeout(this.idle)
|
|
1324
|
+
this.cancelPointerRest()
|
|
1060
1325
|
|
|
1061
1326
|
const view = this.root.ownerDocument.defaultView
|
|
1062
1327
|
if (this.frame !== 0 && view?.cancelAnimationFrame)
|
|
@@ -22,10 +22,11 @@ The markup goes into an `<iframe srcdoc>` with a `sandbox` attribute.
|
|
|
22
22
|
DOM, storage or cookies, and none of their own. `allow-same-origin`, top
|
|
23
23
|
navigation and popups are never granted. Links are sent to a new window,
|
|
24
24
|
which the sandbox refuses, so a click cannot navigate the preview away.
|
|
25
|
-
- **Colour scheme.**
|
|
26
|
-
`color-scheme
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
- **Colour scheme.** The scheme is set on the frame element, as its
|
|
26
|
+
`color-scheme`, on the host's side. An engine that hands a frame's
|
|
27
|
+
`color-scheme` to the page inside gives that page its
|
|
28
|
+
`prefers-color-scheme` from it, with no script and no cooperation, and a
|
|
29
|
+
change does not reload it. Chromium does. WebKit does not: see Limits.
|
|
29
30
|
- **Viewport.** The frame is laid out at the device's real size and drawn
|
|
30
31
|
smaller to fit, so the page's own media queries see 390 pixels for a phone
|
|
31
32
|
whatever the box is. It is never enlarged.
|
|
@@ -102,6 +103,13 @@ the ground a device sits on), `--stx-preview-line` (`line`),
|
|
|
102
103
|
its title or its errors. With `scripts` on, a page can `postMessage` its
|
|
103
104
|
parent and the host can listen for it.
|
|
104
105
|
- **No device chrome, pixel ratio or touch emulation.** A viewport is a size.
|
|
105
|
-
- **
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
- **The scheme reaches the page only where the engine passes it on.** In
|
|
107
|
+
Chromium the page's `prefers-color-scheme` follows the frame, and a change
|
|
108
|
+
fires the page's `change` listeners. In WebKit (Safari, and a Craft window,
|
|
109
|
+
which is a WKWebView) the page always sees the system scheme: measured on
|
|
110
|
+
macOS 26.3.1, a frame drawn `light` on a dark system reported dark at load
|
|
111
|
+
and 1.5 seconds later, and a page that declares both schemes was still drawn
|
|
112
|
+
dark. There the `scheme` prop, the toolbar button and the `@scheme` event
|
|
113
|
+
change the frame element and nothing inside it. A preview that has to show
|
|
114
|
+
one scheme on every engine needs markup that does not depend on the media
|
|
115
|
+
query (stacksjs/stx#2186).
|