@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.
- package/dist/CodeEditor-jg4w7e15.stx +497 -0
- package/dist/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/code-editor/editor.js +2166 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +34 -15
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- 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/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -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 +44 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +10 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +135 -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/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo and redo.
|
|
3
|
+
*
|
|
4
|
+
* The editor keeps its own history instead of the textarea's, because the
|
|
5
|
+
* textarea's does not survive the editor: any change written into it from
|
|
6
|
+
* script - an auto-indent, a closed bracket, a toggled comment - either clears
|
|
7
|
+
* the native stack or leaves it describing text that is no longer there.
|
|
8
|
+
*
|
|
9
|
+
* An entry holds its changes twice. `changes` are in the coordinates of the
|
|
10
|
+
* document before them and `inverse` in the coordinates of the document after,
|
|
11
|
+
* so undo and redo are the same operation pointed at different halves, and
|
|
12
|
+
* neither has to work out where an earlier change moved a later one.
|
|
13
|
+
*/
|
|
14
|
+
import type { EditorSelection } from './selection'
|
|
15
|
+
import type { AppliedChange, ChangeSpec } from './text'
|
|
16
|
+
|
|
17
|
+
export interface HistoryEntry {
|
|
18
|
+
changes: ChangeSpec[]
|
|
19
|
+
inverse: ChangeSpec[]
|
|
20
|
+
before: EditorSelection
|
|
21
|
+
after: EditorSelection
|
|
22
|
+
/** What kind of edit, which is what decides whether two entries are one. */
|
|
23
|
+
kind: string
|
|
24
|
+
time: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface HistoryOptions {
|
|
28
|
+
/** Entries kept. Older ones fall off the bottom. Default 500. */
|
|
29
|
+
depth?: number
|
|
30
|
+
/** Edits further apart than this are separate steps. Default 600 ms. */
|
|
31
|
+
groupDelay?: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The changes that put the document back, in after-the-change coordinates. */
|
|
35
|
+
export function invertChanges(applied: readonly AppliedChange[]): ChangeSpec[] {
|
|
36
|
+
let shift = 0
|
|
37
|
+
|
|
38
|
+
return applied.map((change) => {
|
|
39
|
+
const from = change.from + shift
|
|
40
|
+
shift += change.insert.length - (change.to - change.from)
|
|
41
|
+
|
|
42
|
+
return { from, to: from + change.insert.length, insert: change.removed }
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class History {
|
|
47
|
+
private done: HistoryEntry[] = []
|
|
48
|
+
private undone: HistoryEntry[] = []
|
|
49
|
+
private readonly depth: number
|
|
50
|
+
private readonly groupDelay: number
|
|
51
|
+
|
|
52
|
+
constructor(options: HistoryOptions = {}) {
|
|
53
|
+
this.depth = options.depth ?? 500
|
|
54
|
+
this.groupDelay = options.groupDelay ?? 600
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get canUndo(): boolean {
|
|
58
|
+
return this.done.length > 0
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get canRedo(): boolean {
|
|
62
|
+
return this.undone.length > 0
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get undoDepth(): number {
|
|
66
|
+
return this.done.length
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
clear(): void {
|
|
70
|
+
this.done = []
|
|
71
|
+
this.undone = []
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Record an edit, joining it to the previous one when they are one gesture.
|
|
76
|
+
*
|
|
77
|
+
* Typing a word is one undo step, not five. Two entries join when they are
|
|
78
|
+
* the same kind, close together in time, and the second picks up exactly
|
|
79
|
+
* where the first left off. A line break always starts a new step, so undo
|
|
80
|
+
* walks back a line at a time rather than a paragraph.
|
|
81
|
+
*/
|
|
82
|
+
record(applied: readonly AppliedChange[], before: EditorSelection, after: EditorSelection, kind: string, time: number): void {
|
|
83
|
+
if (applied.length === 0)
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
this.undone = []
|
|
87
|
+
|
|
88
|
+
const entry: HistoryEntry = {
|
|
89
|
+
changes: applied.map(change => ({ from: change.from, to: change.to, insert: change.insert })),
|
|
90
|
+
inverse: invertChanges(applied),
|
|
91
|
+
before,
|
|
92
|
+
after,
|
|
93
|
+
kind,
|
|
94
|
+
time,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const previous = this.done[this.done.length - 1]
|
|
98
|
+
if (previous && this.joins(previous, entry)) {
|
|
99
|
+
this.done[this.done.length - 1] = this.join(previous, entry)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
this.done.push(entry)
|
|
104
|
+
if (this.done.length > this.depth)
|
|
105
|
+
this.done.shift()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private joins(previous: HistoryEntry, next: HistoryEntry): boolean {
|
|
109
|
+
if (previous.kind !== next.kind || (next.kind !== 'type' && next.kind !== 'delete'))
|
|
110
|
+
return false
|
|
111
|
+
if (next.time - previous.time > this.groupDelay)
|
|
112
|
+
return false
|
|
113
|
+
if (previous.changes.length !== 1 || next.changes.length !== 1)
|
|
114
|
+
return false
|
|
115
|
+
|
|
116
|
+
const a = previous.changes[0]!
|
|
117
|
+
const b = next.changes[0]!
|
|
118
|
+
|
|
119
|
+
if (next.kind === 'type') {
|
|
120
|
+
// Plain insertions only, each starting where the last one ended.
|
|
121
|
+
if (a.from !== a.to || b.from !== b.to || b.insert.includes('\n') || a.insert.includes('\n'))
|
|
122
|
+
return false
|
|
123
|
+
|
|
124
|
+
return b.from === a.from + a.insert.length
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Deletions: backspace eats leftwards, forward delete stays put.
|
|
128
|
+
if (a.insert !== '' || b.insert !== '')
|
|
129
|
+
return false
|
|
130
|
+
|
|
131
|
+
return b.to === a.from || b.from === a.from
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private join(previous: HistoryEntry, next: HistoryEntry): HistoryEntry {
|
|
135
|
+
const a = previous.changes[0]!
|
|
136
|
+
const b = next.changes[0]!
|
|
137
|
+
const removedA = previous.inverse[0]!.insert
|
|
138
|
+
const removedB = next.inverse[0]!.insert
|
|
139
|
+
|
|
140
|
+
let change: ChangeSpec
|
|
141
|
+
let inverse: ChangeSpec
|
|
142
|
+
|
|
143
|
+
if (next.kind === 'type') {
|
|
144
|
+
change = { from: a.from, to: a.to, insert: a.insert + b.insert }
|
|
145
|
+
inverse = { from: a.from, to: a.from + change.insert.length, insert: removedA }
|
|
146
|
+
}
|
|
147
|
+
else if (b.to === a.from) {
|
|
148
|
+
// Backspace: the new deletion sits just before the old one.
|
|
149
|
+
change = { from: b.from, to: a.to, insert: '' }
|
|
150
|
+
inverse = { from: b.from, to: b.from, insert: removedB + removedA }
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// Forward delete: the new deletion follows the old one in the original text.
|
|
154
|
+
change = { from: a.from, to: a.to + (b.to - b.from), insert: '' }
|
|
155
|
+
inverse = { from: a.from, to: a.from, insert: removedA + removedB }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
changes: [change],
|
|
160
|
+
inverse: [inverse],
|
|
161
|
+
before: previous.before,
|
|
162
|
+
after: next.after,
|
|
163
|
+
kind: next.kind,
|
|
164
|
+
time: next.time,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Stop the next edit joining the last one: a moved caret ends a gesture. */
|
|
169
|
+
breakGroup(): void {
|
|
170
|
+
const previous = this.done[this.done.length - 1]
|
|
171
|
+
if (previous)
|
|
172
|
+
previous.time = Number.NEGATIVE_INFINITY
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
popUndo(): HistoryEntry | undefined {
|
|
176
|
+
const entry = this.done.pop()
|
|
177
|
+
if (entry)
|
|
178
|
+
this.undone.push(entry)
|
|
179
|
+
|
|
180
|
+
return entry
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
popRedo(): HistoryEntry | undefined {
|
|
184
|
+
const entry = this.undone.pop()
|
|
185
|
+
if (entry)
|
|
186
|
+
this.done.push(entry)
|
|
187
|
+
|
|
188
|
+
return entry
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editor core: a document, a selection, a history, commands and a keymap.
|
|
3
|
+
*
|
|
4
|
+
* Framework free and DOM free. `../view.ts` is the half that draws it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from './commands'
|
|
8
|
+
export * from './completion'
|
|
9
|
+
export * from './editor'
|
|
10
|
+
export * from './highlight'
|
|
11
|
+
export * from './history'
|
|
12
|
+
export * from './keymap'
|
|
13
|
+
export * from './selection'
|
|
14
|
+
export * from './text'
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keys to command names.
|
|
3
|
+
*
|
|
4
|
+
* A binding is written the way it is said: `Mod-Shift-Enter`. `Mod` is Command
|
|
5
|
+
* on a Mac and Control everywhere else, resolved when the keymap is built, so
|
|
6
|
+
* one table serves both and neither shows up in the other's shortcuts.
|
|
7
|
+
*
|
|
8
|
+
* The map answers with a name rather than a function. The editor core owns
|
|
9
|
+
* some commands (indent, undo) and the view owns others (run, complete), and
|
|
10
|
+
* neither should have to import the other to be bound to a key.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** The part of a keyboard event a binding is decided on. */
|
|
14
|
+
export interface KeyLike {
|
|
15
|
+
key: string
|
|
16
|
+
ctrlKey?: boolean
|
|
17
|
+
metaKey?: boolean
|
|
18
|
+
altKey?: boolean
|
|
19
|
+
shiftKey?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Keymap = Readonly<Record<string, string>>
|
|
23
|
+
|
|
24
|
+
export const defaultKeymap: Keymap = {
|
|
25
|
+
'Enter': 'insertNewlineAndIndent',
|
|
26
|
+
'Tab': 'indentMore',
|
|
27
|
+
'Shift-Tab': 'indentLess',
|
|
28
|
+
'Backspace': 'deleteBackward',
|
|
29
|
+
'Mod-/': 'toggleLineComment',
|
|
30
|
+
'Mod-z': 'undo',
|
|
31
|
+
'Mod-Shift-z': 'redo',
|
|
32
|
+
'Mod-y': 'redo',
|
|
33
|
+
'Mod-Enter': 'run',
|
|
34
|
+
'Mod-Shift-Enter': 'runSelection',
|
|
35
|
+
'Ctrl-Space': 'triggerCompletion',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const MODIFIER_ORDER = ['Ctrl', 'Alt', 'Shift', 'Meta'] as const
|
|
39
|
+
|
|
40
|
+
/** Whether this is a platform where the command key is Command. */
|
|
41
|
+
export function isApplePlatform(platform?: string): boolean {
|
|
42
|
+
const source = platform
|
|
43
|
+
?? (typeof navigator === 'undefined' ? '' : (navigator.platform || navigator.userAgent || ''))
|
|
44
|
+
|
|
45
|
+
return /Mac|iP(?:hone|ad|od)/i.test(source)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function normalizeKey(key: string): string {
|
|
49
|
+
if (key === ' ' || key === 'Spacebar')
|
|
50
|
+
return 'Space'
|
|
51
|
+
|
|
52
|
+
// A letter is the same binding whether or not Shift made it a capital.
|
|
53
|
+
return key.length === 1 ? key.toLowerCase() : key
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `Mod-Shift-z` and `shift-mod-Z` as the one canonical spelling. */
|
|
57
|
+
export function normalizeBinding(binding: string, apple: boolean): string {
|
|
58
|
+
const parts = binding.split('-')
|
|
59
|
+
// A binding for the minus key ends in an empty part: `Mod--`.
|
|
60
|
+
const key = parts[parts.length - 1] === '' ? '-' : parts[parts.length - 1]!
|
|
61
|
+
const modifiers = new Set<string>()
|
|
62
|
+
|
|
63
|
+
for (const part of parts.slice(0, parts[parts.length - 1] === '' ? -2 : -1)) {
|
|
64
|
+
const name = part.toLowerCase()
|
|
65
|
+
if (name === 'mod')
|
|
66
|
+
modifiers.add(apple ? 'Meta' : 'Ctrl')
|
|
67
|
+
else if (name === 'ctrl' || name === 'control')
|
|
68
|
+
modifiers.add('Ctrl')
|
|
69
|
+
else if (name === 'alt' || name === 'option')
|
|
70
|
+
modifiers.add('Alt')
|
|
71
|
+
else if (name === 'shift')
|
|
72
|
+
modifiers.add('Shift')
|
|
73
|
+
else if (name === 'meta' || name === 'cmd' || name === 'command')
|
|
74
|
+
modifiers.add('Meta')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return [...MODIFIER_ORDER.filter(name => modifiers.has(name)), normalizeKey(key)].join('-')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The canonical name of a key press. */
|
|
81
|
+
export function keyName(event: KeyLike): string {
|
|
82
|
+
const modifiers: string[] = []
|
|
83
|
+
if (event.ctrlKey)
|
|
84
|
+
modifiers.push('Ctrl')
|
|
85
|
+
if (event.altKey)
|
|
86
|
+
modifiers.push('Alt')
|
|
87
|
+
if (event.shiftKey)
|
|
88
|
+
modifiers.push('Shift')
|
|
89
|
+
if (event.metaKey)
|
|
90
|
+
modifiers.push('Meta')
|
|
91
|
+
|
|
92
|
+
return [...modifiers, normalizeKey(event.key)].join('-')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ResolvedKeymap {
|
|
96
|
+
/** The command a key press is bound to, if any. */
|
|
97
|
+
lookup: (event: KeyLike) => string | undefined
|
|
98
|
+
/** Every binding, canonical key name to command name. */
|
|
99
|
+
bindings: ReadonlyMap<string, string>
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Build a keymap for one platform.
|
|
104
|
+
*
|
|
105
|
+
* Later maps win, and binding a key to an empty string unbinds it - which is
|
|
106
|
+
* how a host takes Tab back for focus, or gives `Mod-Enter` to something else.
|
|
107
|
+
*/
|
|
108
|
+
export function createKeymap(maps: readonly Keymap[] = [defaultKeymap], apple: boolean = isApplePlatform()): ResolvedKeymap {
|
|
109
|
+
const bindings = new Map<string, string>()
|
|
110
|
+
|
|
111
|
+
for (const map of maps) {
|
|
112
|
+
for (const [binding, command] of Object.entries(map)) {
|
|
113
|
+
const name = normalizeBinding(binding, apple)
|
|
114
|
+
if (command === '')
|
|
115
|
+
bindings.delete(name)
|
|
116
|
+
else
|
|
117
|
+
bindings.set(name, command)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
bindings,
|
|
123
|
+
lookup: event => bindings.get(keyName(event)),
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selections.
|
|
3
|
+
*
|
|
4
|
+
* A selection is a list of ranges with one of them the main one. This version
|
|
5
|
+
* of the editor only ever holds a single range, because its input surface is a
|
|
6
|
+
* textarea and a textarea has one. The list is there so that the model does not
|
|
7
|
+
* have to change shape when the editor grows its own input surface: every
|
|
8
|
+
* command already asks "for each range", it is just that the answer is one.
|
|
9
|
+
*/
|
|
10
|
+
import type { ChangeSpec } from './text'
|
|
11
|
+
|
|
12
|
+
/** `anchor` is where the selection started, `head` where the caret is. */
|
|
13
|
+
export interface SelectionRange {
|
|
14
|
+
anchor: number
|
|
15
|
+
head: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface EditorSelection {
|
|
19
|
+
ranges: SelectionRange[]
|
|
20
|
+
/** Index of the range the caret, the scroll position and the popup follow. */
|
|
21
|
+
main: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function range(anchor: number, head: number = anchor): SelectionRange {
|
|
25
|
+
return { anchor, head }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function singleSelection(anchor: number, head: number = anchor): EditorSelection {
|
|
29
|
+
return { ranges: [{ anchor, head }], main: 0 }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function rangeFrom(value: SelectionRange): number {
|
|
33
|
+
return Math.min(value.anchor, value.head)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function rangeTo(value: SelectionRange): number {
|
|
37
|
+
return Math.max(value.anchor, value.head)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function rangeEmpty(value: SelectionRange): boolean {
|
|
41
|
+
return value.anchor === value.head
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function mainRange(selection: EditorSelection): SelectionRange {
|
|
45
|
+
return selection.ranges[selection.main] ?? selection.ranges[0]!
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function selectionsEqual(a: EditorSelection, b: EditorSelection): boolean {
|
|
49
|
+
if (a.main !== b.main || a.ranges.length !== b.ranges.length)
|
|
50
|
+
return false
|
|
51
|
+
|
|
52
|
+
return a.ranges.every((value, index) => value.anchor === b.ranges[index]!.anchor && value.head === b.ranges[index]!.head)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function clampSelection(selection: EditorSelection, length: number): EditorSelection {
|
|
56
|
+
const clamp = (offset: number): number => Math.max(0, Math.min(length, offset))
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
main: Math.min(selection.main, selection.ranges.length - 1),
|
|
60
|
+
ranges: selection.ranges.map(value => ({ anchor: clamp(value.anchor), head: clamp(value.head) })),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Where an offset ends up after a set of changes.
|
|
66
|
+
*
|
|
67
|
+
* `changes` are in the coordinates of the document before any of them, sorted
|
|
68
|
+
* and not overlapping, which is the form a transaction holds them in.
|
|
69
|
+
*
|
|
70
|
+
* `assoc` settles the one ambiguous case, an offset exactly where text was
|
|
71
|
+
* inserted: `1` puts it after the insertion, which is what a caret wants (you
|
|
72
|
+
* typed, the caret moved), and `-1` leaves it before.
|
|
73
|
+
*/
|
|
74
|
+
export function mapOffset(offset: number, changes: readonly ChangeSpec[], assoc: -1 | 1 = 1): number {
|
|
75
|
+
let shift = 0
|
|
76
|
+
|
|
77
|
+
for (const change of changes) {
|
|
78
|
+
if (change.from > offset)
|
|
79
|
+
break
|
|
80
|
+
|
|
81
|
+
if (change.to <= offset) {
|
|
82
|
+
if (change.from === offset && change.to === offset && assoc < 0)
|
|
83
|
+
break
|
|
84
|
+
|
|
85
|
+
shift += change.insert.length - (change.to - change.from)
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Inside a replaced range: there is no "same place" any more.
|
|
90
|
+
return change.from + shift + (assoc < 0 ? 0 : change.insert.length)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return offset + shift
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function mapSelection(selection: EditorSelection, changes: readonly ChangeSpec[]): EditorSelection {
|
|
97
|
+
return {
|
|
98
|
+
main: selection.main,
|
|
99
|
+
ranges: selection.ranges.map(value => ({
|
|
100
|
+
anchor: mapOffset(value.anchor, changes, 1),
|
|
101
|
+
head: mapOffset(value.head, changes, 1),
|
|
102
|
+
})),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document: text held as lines.
|
|
3
|
+
*
|
|
4
|
+
* Lines, not one string, because everything an editor asks is a line question.
|
|
5
|
+
* Which lines are on screen, which line changed, what the indentation of this
|
|
6
|
+
* one is. A single string answers each of those with a scan from the top, and a
|
|
7
|
+
* keystroke in a two-thousand-line file would pay for all two thousand.
|
|
8
|
+
*
|
|
9
|
+
* Offsets are still the coordinate everything else speaks - a textarea reports
|
|
10
|
+
* its selection as two of them, and a change is "from here to there" - so the
|
|
11
|
+
* document converts both ways. The table of line starts behind that is rebuilt
|
|
12
|
+
* lazily and only from the first line that changed.
|
|
13
|
+
*
|
|
14
|
+
* Mutable on purpose. An edit touches one or two entries of the array, and the
|
|
15
|
+
* history keeps what it needs to reverse it (the removed text), which is all an
|
|
16
|
+
* immutable document would have bought.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** A place in the document. Both zero-based; `column` counts UTF-16 units. */
|
|
20
|
+
export interface Position {
|
|
21
|
+
line: number
|
|
22
|
+
column: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Replace `from..to` with `insert`. Offsets into the document before the change. */
|
|
26
|
+
export interface ChangeSpec {
|
|
27
|
+
from: number
|
|
28
|
+
to: number
|
|
29
|
+
insert: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What a change did, in the terms each consumer needs. */
|
|
33
|
+
export interface AppliedChange extends ChangeSpec {
|
|
34
|
+
/** The text that was there, which is what undoing it puts back. */
|
|
35
|
+
removed: string
|
|
36
|
+
/** First line touched. */
|
|
37
|
+
fromLine: number
|
|
38
|
+
/** How many lines the replaced range spanned (at least one). */
|
|
39
|
+
removedLines: number
|
|
40
|
+
/** How many lines stand there now (at least one). */
|
|
41
|
+
insertedLines: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface LineInfo {
|
|
45
|
+
index: number
|
|
46
|
+
/** Offset of the line's first character. */
|
|
47
|
+
from: number
|
|
48
|
+
/** Offset just past its last character, before the line break. */
|
|
49
|
+
to: number
|
|
50
|
+
text: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One line ending throughout: a textarea reports `\n` whatever was pasted. */
|
|
54
|
+
export function normalizeNewlines(text: string): string {
|
|
55
|
+
return text.includes('\r') ? text.replace(/\r\n?/g, '\n') : text
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class TextDocument {
|
|
59
|
+
private lines: string[]
|
|
60
|
+
/** `starts[i]` is the offset of line `i`. Valid below `validStarts`. */
|
|
61
|
+
private starts: number[] = [0]
|
|
62
|
+
private validStarts = 1
|
|
63
|
+
private total: number
|
|
64
|
+
private text: string | null
|
|
65
|
+
|
|
66
|
+
constructor(text = '') {
|
|
67
|
+
const normalized = normalizeNewlines(text)
|
|
68
|
+
this.lines = normalized.split('\n')
|
|
69
|
+
this.total = normalized.length
|
|
70
|
+
this.text = normalized
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get lineCount(): number {
|
|
74
|
+
return this.lines.length
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Length in UTF-16 units, line breaks included. */
|
|
78
|
+
get length(): number {
|
|
79
|
+
return this.total
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The text of one line, without its line break. Empty past either end. */
|
|
83
|
+
line(index: number): string {
|
|
84
|
+
return this.lines[index] ?? ''
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The whole document. Cached until the next change. */
|
|
88
|
+
toString(): string {
|
|
89
|
+
if (this.text === null)
|
|
90
|
+
this.text = this.lines.join('\n')
|
|
91
|
+
|
|
92
|
+
return this.text
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Tell the document what its own text is, when the caller already holds it.
|
|
97
|
+
*
|
|
98
|
+
* A textarea hands over its full value on every input event. Joining two
|
|
99
|
+
* thousand lines to compare against it, when that value *is* the answer once
|
|
100
|
+
* the change is applied, would be building the same string twice per key.
|
|
101
|
+
*/
|
|
102
|
+
adoptText(text: string): void {
|
|
103
|
+
if (text.length === this.total)
|
|
104
|
+
this.text = text
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private startOf(index: number): number {
|
|
108
|
+
if (index >= this.validStarts) {
|
|
109
|
+
let offset = this.starts[this.validStarts - 1]!
|
|
110
|
+
for (let line = this.validStarts; line <= index; line++) {
|
|
111
|
+
offset += this.lines[line - 1]!.length + 1
|
|
112
|
+
this.starts[line] = offset
|
|
113
|
+
}
|
|
114
|
+
this.validStarts = index + 1
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return this.starts[index]!
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private clampOffset(offset: number): number {
|
|
121
|
+
return Math.max(0, Math.min(this.total, offset))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The line an offset falls on. A line break belongs to the line it ends. */
|
|
125
|
+
lineIndexAt(offset: number): number {
|
|
126
|
+
const target = this.clampOffset(offset)
|
|
127
|
+
let low = 0
|
|
128
|
+
let high = this.lines.length - 1
|
|
129
|
+
|
|
130
|
+
while (low < high) {
|
|
131
|
+
const middle = (low + high + 1) >> 1
|
|
132
|
+
if (this.startOf(middle) <= target)
|
|
133
|
+
low = middle
|
|
134
|
+
else
|
|
135
|
+
high = middle - 1
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return low
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
lineAt(offset: number): LineInfo {
|
|
142
|
+
return this.lineInfo(this.lineIndexAt(offset))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
lineInfo(index: number): LineInfo {
|
|
146
|
+
const clamped = Math.max(0, Math.min(this.lines.length - 1, index))
|
|
147
|
+
const from = this.startOf(clamped)
|
|
148
|
+
const text = this.lines[clamped]!
|
|
149
|
+
|
|
150
|
+
return { index: clamped, from, to: from + text.length, text }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
positionAt(offset: number): Position {
|
|
154
|
+
const target = this.clampOffset(offset)
|
|
155
|
+
const line = this.lineIndexAt(target)
|
|
156
|
+
|
|
157
|
+
return { line, column: target - this.startOf(line) }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The offset of a position, clamped into the document and into its line. */
|
|
161
|
+
offsetAt(position: Position): number {
|
|
162
|
+
const line = Math.max(0, Math.min(this.lines.length - 1, position.line))
|
|
163
|
+
const column = Math.max(0, Math.min(this.lines[line]!.length, position.column))
|
|
164
|
+
|
|
165
|
+
return this.startOf(line) + column
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
slice(from: number, to: number): string {
|
|
169
|
+
const start = this.clampOffset(Math.min(from, to))
|
|
170
|
+
const end = this.clampOffset(Math.max(from, to))
|
|
171
|
+
if (start === end)
|
|
172
|
+
return ''
|
|
173
|
+
|
|
174
|
+
const first = this.positionAt(start)
|
|
175
|
+
const last = this.positionAt(end)
|
|
176
|
+
if (first.line === last.line)
|
|
177
|
+
return this.lines[first.line]!.slice(first.column, last.column)
|
|
178
|
+
|
|
179
|
+
const parts = [this.lines[first.line]!.slice(first.column)]
|
|
180
|
+
for (let line = first.line + 1; line < last.line; line++)
|
|
181
|
+
parts.push(this.lines[line]!)
|
|
182
|
+
parts.push(this.lines[last.line]!.slice(0, last.column))
|
|
183
|
+
|
|
184
|
+
return parts.join('\n')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Replace a range. The only way the document changes. */
|
|
188
|
+
replace(from: number, to: number, insert: string): AppliedChange {
|
|
189
|
+
const start = this.clampOffset(Math.min(from, to))
|
|
190
|
+
const end = this.clampOffset(Math.max(from, to))
|
|
191
|
+
const text = normalizeNewlines(insert)
|
|
192
|
+
const first = this.positionAt(start)
|
|
193
|
+
const last = this.positionAt(end)
|
|
194
|
+
const removed = this.slice(start, end)
|
|
195
|
+
|
|
196
|
+
const head = this.lines[first.line]!.slice(0, first.column)
|
|
197
|
+
const tail = this.lines[last.line]!.slice(last.column)
|
|
198
|
+
const inserted = text.split('\n')
|
|
199
|
+
inserted[0] = head + inserted[0]!
|
|
200
|
+
inserted[inserted.length - 1] += tail
|
|
201
|
+
|
|
202
|
+
const removedLines = last.line - first.line + 1
|
|
203
|
+
if (removedLines === 1 && inserted.length === 1)
|
|
204
|
+
this.lines[first.line] = inserted[0]!
|
|
205
|
+
else
|
|
206
|
+
this.lines.splice(first.line, removedLines, ...inserted)
|
|
207
|
+
|
|
208
|
+
this.total += text.length - (end - start)
|
|
209
|
+
this.text = null
|
|
210
|
+
// Every line after the first one touched has moved.
|
|
211
|
+
this.validStarts = Math.min(this.validStarts, first.line + 1)
|
|
212
|
+
this.starts.length = this.validStarts
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
from: start,
|
|
216
|
+
to: end,
|
|
217
|
+
insert: text,
|
|
218
|
+
removed,
|
|
219
|
+
fromLine: first.line,
|
|
220
|
+
removedLines,
|
|
221
|
+
insertedLines: inserted.length,
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Leading spaces and tabs of a line. */
|
|
227
|
+
export function leadingWhitespace(text: string): string {
|
|
228
|
+
let end = 0
|
|
229
|
+
while (end < text.length && (text.charCodeAt(end) === 32 || text.charCodeAt(end) === 9))
|
|
230
|
+
end++
|
|
231
|
+
|
|
232
|
+
return text.slice(0, end)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Width of a run of indentation in columns, tabs advancing to the next stop. */
|
|
236
|
+
export function indentWidth(whitespace: string, tabSize: number): number {
|
|
237
|
+
let width = 0
|
|
238
|
+
for (let index = 0; index < whitespace.length; index++)
|
|
239
|
+
width = whitespace.charCodeAt(index) === 9 ? width + tabSize - (width % tabSize) : width + 1
|
|
240
|
+
|
|
241
|
+
return width
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Indentation of a given width, written the way the editor is set to write it. */
|
|
245
|
+
export function indentString(width: number, tabSize: number, insertSpaces: boolean): string {
|
|
246
|
+
if (width <= 0)
|
|
247
|
+
return ''
|
|
248
|
+
if (insertSpaces)
|
|
249
|
+
return ' '.repeat(width)
|
|
250
|
+
|
|
251
|
+
return '\t'.repeat(Math.floor(width / tabSize)) + ' '.repeat(width % tabSize)
|
|
252
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The code editor without its stx template.
|
|
3
|
+
*
|
|
4
|
+
* `@stacksjs/components/code-editor`. Everything `<CodeEditor>` is made of, for
|
|
5
|
+
* a host that mounts one itself or that wants the model on its own:
|
|
6
|
+
*
|
|
7
|
+
* - `EditorCore` and the rest of `./core`: the document, selection, history,
|
|
8
|
+
* commands and keymap. No DOM, so it runs anywhere.
|
|
9
|
+
* - `EditorView`: the text field over its painted copy.
|
|
10
|
+
* - `CodeEditorController`: the two joined, with highlighting and completion.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export * from './controller'
|
|
14
|
+
export * from './core'
|
|
15
|
+
export { createLanguageTokenizer, MAX_TOKENIZED_LINE_LENGTH, tokenizerStateKey } from './tokenizer'
|
|
16
|
+
export * from './view'
|