@nibling/editor 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/commands/autoclose.d.ts +13 -0
- package/dist/commands/autoclose.js +78 -0
- package/dist/commands/commands.d.ts +104 -0
- package/dist/commands/commands.js +442 -0
- package/dist/commands/comments.d.ts +3 -0
- package/dist/commands/comments.js +9 -0
- package/dist/commands/fold.d.ts +18 -0
- package/dist/commands/fold.js +75 -0
- package/dist/commands/keymap.d.ts +40 -0
- package/dist/commands/keymap.js +118 -0
- package/dist/complete/autocomplete.d.ts +35 -0
- package/dist/complete/autocomplete.js +386 -0
- package/dist/complete/source.d.ts +72 -0
- package/dist/complete/source.js +52 -0
- package/dist/complete/words.d.ts +53 -0
- package/dist/complete/words.js +203 -0
- package/dist/element.d.ts +119 -0
- package/dist/element.js +1011 -0
- package/dist/expand/expansions.d.ts +68 -0
- package/dist/expand/expansions.js +143 -0
- package/dist/expand/projection.d.ts +73 -0
- package/dist/expand/projection.js +189 -0
- package/dist/expand/resolver.d.ts +42 -0
- package/dist/expand/resolver.js +585 -0
- package/dist/expand/slice-tokens.d.ts +25 -0
- package/dist/expand/slice-tokens.js +79 -0
- package/dist/extension.d.ts +58 -0
- package/dist/extension.js +1 -0
- package/dist/find/find.d.ts +12 -0
- package/dist/find/find.js +469 -0
- package/dist/find/search.d.ts +81 -0
- package/dist/find/search.js +308 -0
- package/dist/headless.d.ts +16 -0
- package/dist/headless.js +19 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inline-expansion.d.ts +89 -0
- package/dist/inline-expansion.js +324 -0
- package/dist/input/edit-context.d.ts +23 -0
- package/dist/input/edit-context.js +102 -0
- package/dist/input/surface.d.ts +101 -0
- package/dist/input/surface.js +84 -0
- package/dist/input/textarea.d.ts +26 -0
- package/dist/input/textarea.js +125 -0
- package/dist/sheet.d.ts +6 -0
- package/dist/sheet.js +12 -0
- package/dist/state/core.d.ts +73 -0
- package/dist/state/core.js +86 -0
- package/dist/state/fold-ranges.d.ts +41 -0
- package/dist/state/fold-ranges.js +167 -0
- package/dist/state/folds.d.ts +45 -0
- package/dist/state/folds.js +240 -0
- package/dist/state/history.d.ts +25 -0
- package/dist/state/history.js +127 -0
- package/dist/state/indent.d.ts +33 -0
- package/dist/state/indent.js +121 -0
- package/dist/state/selection.d.ts +45 -0
- package/dist/state/selection.js +106 -0
- package/dist/state/whitespace.d.ts +16 -0
- package/dist/state/whitespace.js +54 -0
- package/dist/text/changeset.d.ts +56 -0
- package/dist/text/changeset.js +321 -0
- package/dist/text/text.d.ts +30 -0
- package/dist/text/text.js +321 -0
- package/dist/tokens/bridge.d.ts +107 -0
- package/dist/tokens/bridge.js +461 -0
- package/dist/tokens/engine.d.ts +39 -0
- package/dist/tokens/engine.js +360 -0
- package/dist/tokens/protocol.d.ts +113 -0
- package/dist/tokens/protocol.js +38 -0
- package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
- package/dist/tokens/textmate/ascii-regex.js +156 -0
- package/dist/tokens/textmate/index.d.ts +7 -0
- package/dist/tokens/textmate/index.js +19 -0
- package/dist/tokens/textmate/linker.d.ts +67 -0
- package/dist/tokens/textmate/linker.js +238 -0
- package/dist/tokens/textmate/scopes.d.ts +14 -0
- package/dist/tokens/textmate/scopes.js +54 -0
- package/dist/tokens/textmate/theme.d.ts +54 -0
- package/dist/tokens/textmate/theme.js +138 -0
- package/dist/tokens/textmate/tokenizer.d.ts +29 -0
- package/dist/tokens/textmate/tokenizer.js +684 -0
- package/dist/tokens/textmate.d.ts +49 -0
- package/dist/tokens/textmate.js +4 -0
- package/dist/view/decorations.d.ts +183 -0
- package/dist/view/decorations.js +51 -0
- package/dist/view/guides.d.ts +58 -0
- package/dist/view/guides.js +174 -0
- package/dist/view/heightmap.d.ts +97 -0
- package/dist/view/heightmap.js +229 -0
- package/dist/view/line-dom.d.ts +54 -0
- package/dist/view/line-dom.js +168 -0
- package/dist/view/measure.d.ts +37 -0
- package/dist/view/measure.js +136 -0
- package/dist/view/overlays.d.ts +32 -0
- package/dist/view/overlays.js +135 -0
- package/dist/view/rail.d.ts +27 -0
- package/dist/view/rail.js +232 -0
- package/dist/view/remote.d.ts +24 -0
- package/dist/view/remote.js +143 -0
- package/dist/view/scope-focus.d.ts +27 -0
- package/dist/view/scope-focus.js +255 -0
- package/dist/view/view.d.ts +114 -0
- package/dist/view/view.js +1231 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +66 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +64 -0
- package/grammars/astro.mjs +2 -0
- package/grammars/chunks/r-90cb351dba.mjs +1 -0
- package/grammars/chunks/r-b92a500aab.mjs +1 -0
- package/grammars/chunks/r-ced5311992.mjs +1 -0
- package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
- package/grammars/chunks/s-1821e66170.mjs +1 -0
- package/grammars/css.mjs +1 -0
- package/grammars/diff.mjs +1 -0
- package/grammars/docker.mjs +1 -0
- package/grammars/dotenv.mjs +1 -0
- package/grammars/graphql.mjs +1 -0
- package/grammars/html.mjs +2 -0
- package/grammars/ini.mjs +1 -0
- package/grammars/javascript.mjs +5 -0
- package/grammars/json.mjs +1 -0
- package/grammars/json5.mjs +1 -0
- package/grammars/jsonc.mjs +1 -0
- package/grammars/less.mjs +1 -0
- package/grammars/manifest.json +58 -0
- package/grammars/markdown.mjs +1 -0
- package/grammars/mdx.mjs +1 -0
- package/grammars/pug.mjs +1 -0
- package/grammars/scss.mjs +1 -0
- package/grammars/shellscript.mjs +1 -0
- package/grammars/sql.mjs +1 -0
- package/grammars/stylus.mjs +1 -0
- package/grammars/svelte.mjs +1 -0
- package/grammars/toml.mjs +1 -0
- package/grammars/tsx.mjs +5 -0
- package/grammars/typescript.mjs +2 -0
- package/grammars/vue.mjs +1 -0
- package/grammars/yaml.mjs +1 -0
- package/package.json +71 -3
- package/themes/dark-plus.json +1 -0
- package/themes/light-plus.json +1 -0
- package/themes/monokai.json +1 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { nextVisibleLine } from "./state/folds.js";
|
|
2
|
+
import { Selection, isCursor } from "./state/selection.js";
|
|
3
|
+
import { SliceTokens } from "./expand/slice-tokens.js";
|
|
4
|
+
import { sheetFor } from "./sheet.js";
|
|
5
|
+
/** Lines an expansion shows before it scrolls inside itself. */
|
|
6
|
+
const MAX_LINES = 24;
|
|
7
|
+
/**
|
|
8
|
+
* Events that must not leave an expansion, because a host editor listens for them on an ancestor:
|
|
9
|
+
* its own mousedown (on the host element), its rail's (on its shadow root), and in EditContext
|
|
10
|
+
* mode its keydown and clipboard (on the host element). Everything else still bubbles to the page.
|
|
11
|
+
*/
|
|
12
|
+
const CONTAINED = ['mousedown', 'keydown', 'keyup', 'copy', 'cut', 'paste'];
|
|
13
|
+
/**
|
|
14
|
+
* The frame. Quiet: a faint inset band (the gutter included, so the source's line numbers read as
|
|
15
|
+
* borrowed), a hairline rule in the rail gap that brightens while the expansion has focus, and a
|
|
16
|
+
* one-line header with the definition's name, where it lives, and a collapse affordance. It
|
|
17
|
+
* fades in and unrolls downward over the medium duration, and only under no-preference.
|
|
18
|
+
*
|
|
19
|
+
* Every color is a `--nib-editor-*` knob (docs/design/theming.md) over nibling's tokens, then over
|
|
20
|
+
* the editor's own background and foreground.
|
|
21
|
+
*/
|
|
22
|
+
const css = `
|
|
23
|
+
:host{
|
|
24
|
+
--nib-editor-expansion-bg:var(--nib-color-surface,color-mix(in srgb,var(--nib-fg,#d4d4d4) 4%,var(--nib-bg,#1e1e1e)));
|
|
25
|
+
--nib-editor-expansion-rule:var(--nib-color-border,color-mix(in srgb,var(--nib-fg,#d4d4d4) 16%,var(--nib-bg,#1e1e1e)));
|
|
26
|
+
--nib-editor-expansion-rule-active:var(--nib-color-text-muted,color-mix(in srgb,var(--nib-fg,#d4d4d4) 45%,var(--nib-bg,#1e1e1e)));
|
|
27
|
+
--nib-editor-expansion-fg:var(--nib-color-text-muted,color-mix(in srgb,var(--nib-fg,#d4d4d4) 50%,var(--nib-bg,#1e1e1e)));
|
|
28
|
+
--nib-editor-motion-duration-medium:var(--nib-motion-duration-medium,250ms);
|
|
29
|
+
}
|
|
30
|
+
.nib-x{position:sticky;left:0;width:var(--nib-view-w,100%);box-sizing:border-box;padding-bottom:4px;
|
|
31
|
+
--nib-x-band:var(--nib-editor-expansion-bg);background:var(--nib-x-band);color:var(--nib-editor-expansion-fg)}
|
|
32
|
+
.nib-x::before{content:'';position:absolute;z-index:3;top:0;bottom:0;left:calc(var(--nib-gutter) - 8px);width:1px;
|
|
33
|
+
background:var(--nib-editor-expansion-rule);pointer-events:none}
|
|
34
|
+
.nib-x:focus-within::before{background:var(--nib-editor-expansion-rule-active)}
|
|
35
|
+
.nib-x-head{display:flex;align-items:center;gap:1ch;height:var(--nib-lh,18px);padding-left:var(--nib-gutter);padding-right:1ch;
|
|
36
|
+
font-size:.85em;white-space:nowrap;user-select:none;-webkit-user-select:none;cursor:default}
|
|
37
|
+
.nib-x-name{color:color-mix(in srgb,var(--nib-fg,#d4d4d4) 80%,var(--nib-bg,#1e1e1e))}
|
|
38
|
+
.nib-x-where{flex:1;overflow:hidden;text-overflow:ellipsis}
|
|
39
|
+
.nib-x-close{all:unset;position:relative;width:var(--nib-lh,18px);height:var(--nib-lh,18px);border-radius:4px;cursor:pointer;
|
|
40
|
+
color:var(--nib-editor-expansion-fg)}
|
|
41
|
+
.nib-x-close::before{content:'';position:absolute;inset:0;background:currentColor;
|
|
42
|
+
-webkit-mask:var(--nib-x-glyph) center/8px 8px no-repeat;mask:var(--nib-x-glyph) center/8px 8px no-repeat}
|
|
43
|
+
.nib-x-close:hover{color:var(--nib-fg,#d4d4d4);background:color-mix(in srgb,var(--nib-fg,#d4d4d4) 8%,transparent)}
|
|
44
|
+
.nib-x-close:focus-visible{outline:1px solid var(--nib-editor-expansion-rule-active);outline-offset:-1px}
|
|
45
|
+
.nib-x{--nib-x-glyph:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath d='M1 1l6 6M7 1L1 7' stroke='%23000' stroke-width='1.2' stroke-linecap='round'/%3E%3C/svg%3E")}
|
|
46
|
+
/* The nested editor paints the band, and its borrowed line numbers sit a step quieter. The band
|
|
47
|
+
is resolved on the frame: the nested editor defines the knobs again for its own expansions, in
|
|
48
|
+
terms of its own --nib-bg, so reading the knob here would be a cycle. Nested expansions band a
|
|
49
|
+
step deeper for the same reason. */
|
|
50
|
+
.nib-x-editor{display:block;--nib-bg:var(--nib-x-band);
|
|
51
|
+
--nib-gutter-fg:color-mix(in srgb,var(--nib-fg,#d4d4d4) 30%,var(--nib-x-band))}
|
|
52
|
+
/* The reveal: the frame fades in while a curtain in the band's color rolls up off the body, so
|
|
53
|
+
it reads as unrolling downward. Only opacity and a pointer-events:none overlay move: clipping
|
|
54
|
+
the frame itself would also clip hit testing, and a click right after opening would fall
|
|
55
|
+
through to the line underneath. */
|
|
56
|
+
.nib-x.nib-x-enter::after{content:'';position:absolute;z-index:4;inset:var(--nib-lh,18px) 0 0 0;background:var(--nib-x-band);
|
|
57
|
+
pointer-events:none;transform:scaleY(0);transform-origin:bottom}
|
|
58
|
+
@media (prefers-reduced-motion:no-preference){
|
|
59
|
+
.nib-x.nib-x-enter{animation:nib-x-in var(--nib-editor-motion-duration-medium) var(--nib-editor-motion-ease-out)}
|
|
60
|
+
.nib-x.nib-x-enter::after{animation:nib-x-unroll var(--nib-editor-motion-duration-medium) var(--nib-editor-motion-ease-out)}
|
|
61
|
+
.nib-x::before{transition:background var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
62
|
+
.nib-x-close{transition:color var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out),background var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
63
|
+
}
|
|
64
|
+
@keyframes nib-x-in{from{opacity:0}}
|
|
65
|
+
@keyframes nib-x-unroll{from{transform:scaleY(1)}}
|
|
66
|
+
`;
|
|
67
|
+
/**
|
|
68
|
+
* The element side of inline expansion: one block widget per open expansion, each a frame holding
|
|
69
|
+
* a nested editor over the expansion's projection. The headless parts (Projection,
|
|
70
|
+
* InlineExpansions, the resolver, SliceTokens) know nothing of this.
|
|
71
|
+
*
|
|
72
|
+
* Lifetime: a widget's nested editor lives as long as its expansion, so its folds and selection
|
|
73
|
+
* survive scrolling. It is in the DOM only while the call site's line is mounted; unmounting
|
|
74
|
+
* disconnects it, which ends its view, its input surface, and its token pin, and reconnecting
|
|
75
|
+
* builds them again. Closing the expansion drops it.
|
|
76
|
+
*
|
|
77
|
+
* Focus and keys: each nested editor has its own textarea, keymap, and selection. The frame stops
|
|
78
|
+
* every surface event at its edge, so nothing typed or clicked inside reaches the host editor's
|
|
79
|
+
* listeners (its EditContext host, its rail, its mousedown) and the host's never reach inside.
|
|
80
|
+
*/
|
|
81
|
+
export class ExpansionWidgets {
|
|
82
|
+
#host;
|
|
83
|
+
#widgets = new Map();
|
|
84
|
+
#list = [];
|
|
85
|
+
#listeners = [];
|
|
86
|
+
constructor(host, signal) {
|
|
87
|
+
this.#host = host;
|
|
88
|
+
const sheet = sheetFor(host.root);
|
|
89
|
+
sheet.replaceSync(css);
|
|
90
|
+
host.root.adoptedStyleSheets = [...host.root.adoptedStyleSheets, sheet];
|
|
91
|
+
const off = host.expansions.subscribe(() => this.#sync());
|
|
92
|
+
this.#sync();
|
|
93
|
+
signal.addEventListener('abort', () => {
|
|
94
|
+
off();
|
|
95
|
+
host.root.adoptedStyleSheets = host.root.adoptedStyleSheets.filter((s) => s !== sheet);
|
|
96
|
+
}, { once: true });
|
|
97
|
+
}
|
|
98
|
+
widgets() {
|
|
99
|
+
return this.#list;
|
|
100
|
+
}
|
|
101
|
+
subscribe(invalidate) {
|
|
102
|
+
this.#listeners.push(invalidate);
|
|
103
|
+
return () => (this.#listeners = this.#listeners.filter((l) => l !== invalidate));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* ArrowDown on the call site's line, or ArrowUp from the line below it: put the cursor in the
|
|
107
|
+
* nearest expansion at the same column. True when it moved.
|
|
108
|
+
*/
|
|
109
|
+
enterFrom(line, dir, column) {
|
|
110
|
+
const list = this.#host.expansions.at(line);
|
|
111
|
+
const e = dir > 0 ? list[0] : list.at(-1);
|
|
112
|
+
const w = e && this.#widgets.get(e.key);
|
|
113
|
+
if (!w)
|
|
114
|
+
return false;
|
|
115
|
+
w.enter(dir > 0 ? 'first' : 'last', column);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
#sync() {
|
|
119
|
+
const open = this.#host.expansions.list;
|
|
120
|
+
const live = new Set(open.map((e) => e.key));
|
|
121
|
+
for (const [key, w] of this.#widgets) {
|
|
122
|
+
if (live.has(key))
|
|
123
|
+
continue;
|
|
124
|
+
w.dispose();
|
|
125
|
+
this.#widgets.delete(key);
|
|
126
|
+
}
|
|
127
|
+
for (const e of open)
|
|
128
|
+
if (!this.#widgets.has(e.key))
|
|
129
|
+
this.#widgets.set(e.key, new ExpansionWidget(this.#host, e, this));
|
|
130
|
+
this.#list = open.map((e) => this.#widgets.get(e.key));
|
|
131
|
+
for (const l of this.#listeners)
|
|
132
|
+
l();
|
|
133
|
+
}
|
|
134
|
+
/** From inside expansion `key`: the next one on its line in `dir`, if any. */
|
|
135
|
+
sibling(key, dir) {
|
|
136
|
+
const e = this.#host.expansions.get(key);
|
|
137
|
+
if (!e)
|
|
138
|
+
return undefined;
|
|
139
|
+
const list = this.#host.expansions.at(e.line);
|
|
140
|
+
const next = list[list.findIndex((x) => x.key === key) + dir];
|
|
141
|
+
return next && this.#widgets.get(next.key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** One expansion's frame and nested editor. */
|
|
145
|
+
export class ExpansionWidget {
|
|
146
|
+
key;
|
|
147
|
+
#host;
|
|
148
|
+
#expansion;
|
|
149
|
+
#frame;
|
|
150
|
+
#where;
|
|
151
|
+
editor;
|
|
152
|
+
#lifetime = new AbortController();
|
|
153
|
+
#entered = false;
|
|
154
|
+
constructor(host, expansion, all) {
|
|
155
|
+
this.key = expansion.key;
|
|
156
|
+
this.#host = host;
|
|
157
|
+
this.#expansion = expansion;
|
|
158
|
+
const doc = host.root.ownerDocument;
|
|
159
|
+
const p = expansion.projection;
|
|
160
|
+
const inner = p.inner;
|
|
161
|
+
const frame = (this.#frame = doc.createElement('div'));
|
|
162
|
+
frame.className = 'nib-x nib-x-enter';
|
|
163
|
+
frame.part.add('expansion');
|
|
164
|
+
frame.addEventListener('animationend', () => frame.classList.remove('nib-x-enter'), { once: true });
|
|
165
|
+
for (const type of CONTAINED)
|
|
166
|
+
frame.addEventListener(type, (e) => e.stopPropagation(), { signal: this.#lifetime.signal });
|
|
167
|
+
const head = doc.createElement('div');
|
|
168
|
+
head.className = 'nib-x-head';
|
|
169
|
+
const name = doc.createElement('span');
|
|
170
|
+
name.className = 'nib-x-name';
|
|
171
|
+
name.textContent = expansion.name;
|
|
172
|
+
this.#where = doc.createElement('span');
|
|
173
|
+
this.#where.className = 'nib-x-where';
|
|
174
|
+
const close = doc.createElement('button');
|
|
175
|
+
close.className = 'nib-x-close';
|
|
176
|
+
close.part.add('expansion-close');
|
|
177
|
+
close.setAttribute('aria-label', `Collapse ${expansion.name}`);
|
|
178
|
+
close.title = 'Collapse (Esc)';
|
|
179
|
+
// Pressing it must not move focus out of whichever editor has it.
|
|
180
|
+
close.addEventListener('mousedown', (e) => e.preventDefault());
|
|
181
|
+
close.addEventListener('click', () => this.collapse(false));
|
|
182
|
+
head.append(name, this.#where, close);
|
|
183
|
+
const source = expansion.doc;
|
|
184
|
+
const resolve = host.resolverIn(source);
|
|
185
|
+
const atTop = () => inner.state.text.lineAt(inner.state.selection.primary.head).number === 1;
|
|
186
|
+
const atBottom = () => inner.state.text.lineAt(inner.state.selection.primary.head).number === inner.state.text.lines;
|
|
187
|
+
const single = () => inner.state.selection.ranges.length === 1 && isCursor(inner.state.selection.primary);
|
|
188
|
+
const column = () => {
|
|
189
|
+
const { text, selection } = inner.state;
|
|
190
|
+
return selection.primary.head - text.lineAt(selection.primary.head).from;
|
|
191
|
+
};
|
|
192
|
+
const claimed = { origin: 'select' };
|
|
193
|
+
const keymap = [
|
|
194
|
+
{
|
|
195
|
+
key: 'Escape',
|
|
196
|
+
run: () => (single() ? (this.collapse(true), claimed) : null),
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
key: 'ArrowUp',
|
|
200
|
+
run: () => {
|
|
201
|
+
if (!single() || !atTop())
|
|
202
|
+
return null;
|
|
203
|
+
const prev = all.sibling(this.key, -1);
|
|
204
|
+
if (prev)
|
|
205
|
+
prev.enter('last', column());
|
|
206
|
+
else
|
|
207
|
+
this.#leave(-1, column());
|
|
208
|
+
return claimed;
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
key: 'ArrowDown',
|
|
213
|
+
run: () => {
|
|
214
|
+
if (!single() || !atBottom())
|
|
215
|
+
return null;
|
|
216
|
+
const next = all.sibling(this.key, 1);
|
|
217
|
+
if (next)
|
|
218
|
+
next.enter('first', column());
|
|
219
|
+
else
|
|
220
|
+
this.#leave(1, column());
|
|
221
|
+
return claimed;
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
];
|
|
225
|
+
const delegate = (which) => () => {
|
|
226
|
+
const h = source.history;
|
|
227
|
+
const spec = h?.[which]({ state: source.core.state });
|
|
228
|
+
if (!spec)
|
|
229
|
+
return null;
|
|
230
|
+
// The source's own selection and scroll stay put: the edit is shown where it happened, here.
|
|
231
|
+
source.core.apply({ changes: spec.changes, origin: which === 'undo' ? 'history.undo' : 'history.redo' });
|
|
232
|
+
return claimed;
|
|
233
|
+
};
|
|
234
|
+
const config = {
|
|
235
|
+
core: inner,
|
|
236
|
+
tokens: (signal) => {
|
|
237
|
+
const t = source.tokens();
|
|
238
|
+
return t ? new SliceTokens(t, p, signal) : noTokens;
|
|
239
|
+
},
|
|
240
|
+
undo: delegate('undo'),
|
|
241
|
+
redo: delegate('redo'),
|
|
242
|
+
keymap,
|
|
243
|
+
gutter: {
|
|
244
|
+
offset: () => p.firstLine - 1,
|
|
245
|
+
digits: () => host.digits(),
|
|
246
|
+
// Only a new first line changes the numbers; most edits inside move just the end.
|
|
247
|
+
subscribe: (invalidate) => {
|
|
248
|
+
let first = p.firstLine;
|
|
249
|
+
return p.subscribe(() => {
|
|
250
|
+
if (p.detached || p.firstLine === first)
|
|
251
|
+
return;
|
|
252
|
+
first = p.firstLine;
|
|
253
|
+
invalidate();
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
maxLines: MAX_LINES,
|
|
258
|
+
resolver: (_state, pos) => resolve(source.core.state, p.range.from + pos),
|
|
259
|
+
};
|
|
260
|
+
this.editor = host.createEditor(config);
|
|
261
|
+
this.editor.classList.add('nib-x-editor');
|
|
262
|
+
frame.append(head, this.editor);
|
|
263
|
+
this.#updateWhere();
|
|
264
|
+
p.subscribe(() => this.#updateWhere());
|
|
265
|
+
}
|
|
266
|
+
get line() {
|
|
267
|
+
return this.#expansion.line;
|
|
268
|
+
}
|
|
269
|
+
estimate(lineHeight) {
|
|
270
|
+
const lines = Math.min(MAX_LINES, this.#expansion.projection.inner.state.text.lines);
|
|
271
|
+
return lineHeight * (lines + 1) + 4;
|
|
272
|
+
}
|
|
273
|
+
mount(host, signal) {
|
|
274
|
+
host.appendChild(this.#frame);
|
|
275
|
+
// Play the reveal once, when the expansion opens, not each time it scrolls back in.
|
|
276
|
+
if (this.#entered)
|
|
277
|
+
this.#frame.classList.remove('nib-x-enter');
|
|
278
|
+
this.#entered = true;
|
|
279
|
+
signal.addEventListener('abort', () => this.#frame.remove(), { once: true });
|
|
280
|
+
}
|
|
281
|
+
/** Focus the nested editor at the first or last line, at `column` or its end. */
|
|
282
|
+
enter(edge, column) {
|
|
283
|
+
const inner = this.#expansion.projection.inner;
|
|
284
|
+
const text = inner.state.text;
|
|
285
|
+
const line = text.line(edge === 'first' ? 1 : text.lines);
|
|
286
|
+
const pos = line.from + Math.min(column, line.to - line.from);
|
|
287
|
+
inner.apply({ selection: Selection.single(pos), origin: 'select', scrollIntoView: true });
|
|
288
|
+
this.editor.focus({ preventScroll: true });
|
|
289
|
+
}
|
|
290
|
+
/** Close the expansion. With `refocus`, the host editor takes focus at the call site. */
|
|
291
|
+
collapse(refocus) {
|
|
292
|
+
const anchor = this.#expansion.anchor;
|
|
293
|
+
this.#host.expansions.close(this.key);
|
|
294
|
+
if (refocus)
|
|
295
|
+
this.#host.focusAt(anchor);
|
|
296
|
+
}
|
|
297
|
+
dispose() {
|
|
298
|
+
this.#lifetime.abort();
|
|
299
|
+
this.#frame.remove();
|
|
300
|
+
}
|
|
301
|
+
#leave(dir, column) {
|
|
302
|
+
const core = this.#host.core;
|
|
303
|
+
const text = core.state.text;
|
|
304
|
+
let n = this.#expansion.line;
|
|
305
|
+
if (dir > 0)
|
|
306
|
+
n = nextVisibleLine(this.#host.folds.hidden, n, 1, text.lines);
|
|
307
|
+
if (n > text.lines)
|
|
308
|
+
n = text.lines;
|
|
309
|
+
const line = text.line(n);
|
|
310
|
+
this.#host.focusAt(line.from + Math.min(column, line.to - line.from));
|
|
311
|
+
}
|
|
312
|
+
#updateWhere() {
|
|
313
|
+
const p = this.#expansion.projection;
|
|
314
|
+
if (p.detached)
|
|
315
|
+
return;
|
|
316
|
+
const first = p.firstLine;
|
|
317
|
+
const last = first + p.inner.state.text.lines - 1;
|
|
318
|
+
const label = this.#expansion.doc.label;
|
|
319
|
+
const text = `${label ? `${label} ` : ''}${first === last ? `line ${first}` : `lines ${first}–${last}`}`;
|
|
320
|
+
if (this.#where.textContent !== text)
|
|
321
|
+
this.#where.textContent = text;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const noTokens = { lineTokens: () => undefined, setViewport() { }, onChange: () => () => { } };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Snapshot } from '../state/core.ts';
|
|
2
|
+
import { type InputSurface, type SurfaceGeometry, type SurfaceSink } from './surface.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Chromium path. The host element gets `editContext`; it receives focus and keydown directly.
|
|
5
|
+
*
|
|
6
|
+
* - textupdate -> 'replace' with window offsets translated by `window.from`.
|
|
7
|
+
* - compositionstart/end -> track `composing`; compositionend -> 'composition-end'.
|
|
8
|
+
* - textformatupdate -> ignored in the first slice (the view draws one composition underline).
|
|
9
|
+
* - characterboundsupdate -> answered from the view's measurer via geometry.
|
|
10
|
+
*
|
|
11
|
+
* Chromium bug guard (D2, crbug 332913417): never call updateSelection while composing. Nothing
|
|
12
|
+
* else moves the selection mid-composition in the first slice, so sync skips the platform
|
|
13
|
+
* entirely until compositionend.
|
|
14
|
+
*/
|
|
15
|
+
export declare class EditContextSurface implements InputSurface {
|
|
16
|
+
#private;
|
|
17
|
+
readonly focusTarget: HTMLElement;
|
|
18
|
+
constructor(host: HTMLElement, sink: SurfaceSink);
|
|
19
|
+
get composing(): boolean;
|
|
20
|
+
sync(state: Snapshot, geometry: SurfaceGeometry): void;
|
|
21
|
+
setClipboardSource(read: () => string): void;
|
|
22
|
+
dispose(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { rangeFrom, rangeTo } from "../state/selection.js";
|
|
2
|
+
import { WINDOW_MARGIN, diffStrings, keyEvent, listenClipboard, windowAround, } from "./surface.js";
|
|
3
|
+
/**
|
|
4
|
+
* Chromium path. The host element gets `editContext`; it receives focus and keydown directly.
|
|
5
|
+
*
|
|
6
|
+
* - textupdate -> 'replace' with window offsets translated by `window.from`.
|
|
7
|
+
* - compositionstart/end -> track `composing`; compositionend -> 'composition-end'.
|
|
8
|
+
* - textformatupdate -> ignored in the first slice (the view draws one composition underline).
|
|
9
|
+
* - characterboundsupdate -> answered from the view's measurer via geometry.
|
|
10
|
+
*
|
|
11
|
+
* Chromium bug guard (D2, crbug 332913417): never call updateSelection while composing. Nothing
|
|
12
|
+
* else moves the selection mid-composition in the first slice, so sync skips the platform
|
|
13
|
+
* entirely until compositionend.
|
|
14
|
+
*/
|
|
15
|
+
export class EditContextSurface {
|
|
16
|
+
focusTarget;
|
|
17
|
+
#ctx;
|
|
18
|
+
/** Mirrors the EditContext's text exactly, in document coordinates. */
|
|
19
|
+
#window = { from: 0, text: '' };
|
|
20
|
+
#composing = false;
|
|
21
|
+
#geometry = null;
|
|
22
|
+
#read = () => '';
|
|
23
|
+
#lifetime = new AbortController();
|
|
24
|
+
constructor(host, sink) {
|
|
25
|
+
this.focusTarget = host;
|
|
26
|
+
const Ctor = globalThis.EditContext;
|
|
27
|
+
const ctx = new Ctor({ text: '', selectionStart: 0, selectionEnd: 0 });
|
|
28
|
+
this.#ctx = ctx;
|
|
29
|
+
host.editContext = ctx;
|
|
30
|
+
const signal = this.#lifetime.signal;
|
|
31
|
+
ctx.addEventListener('textupdate', (ev) => {
|
|
32
|
+
const e = ev;
|
|
33
|
+
const w = this.#window;
|
|
34
|
+
this.#window = { from: w.from, text: w.text.slice(0, e.updateRangeStart) + e.text + w.text.slice(e.updateRangeEnd) };
|
|
35
|
+
sink({ kind: 'replace', from: w.from + e.updateRangeStart, to: w.from + e.updateRangeEnd, text: e.text, composing: this.#composing });
|
|
36
|
+
}, { signal });
|
|
37
|
+
ctx.addEventListener('compositionstart', () => (this.#composing = true), { signal });
|
|
38
|
+
ctx.addEventListener('compositionend', () => {
|
|
39
|
+
this.#composing = false;
|
|
40
|
+
sink({ kind: 'composition-end' });
|
|
41
|
+
}, { signal });
|
|
42
|
+
ctx.addEventListener('characterboundsupdate', (ev) => {
|
|
43
|
+
const e = ev;
|
|
44
|
+
const g = this.#geometry;
|
|
45
|
+
if (!g)
|
|
46
|
+
return;
|
|
47
|
+
const bounds = [];
|
|
48
|
+
for (let i = e.rangeStart; i < e.rangeEnd; i++) {
|
|
49
|
+
const a = g.coordsAt(this.#window.from + i);
|
|
50
|
+
const b = g.coordsAt(this.#window.from + i + 1);
|
|
51
|
+
if (!a)
|
|
52
|
+
return;
|
|
53
|
+
bounds.push(new DOMRect(a.left, a.top, b && b.top === a.top ? b.left - a.left : 0, a.height));
|
|
54
|
+
}
|
|
55
|
+
ctx.updateCharacterBounds(e.rangeStart, bounds);
|
|
56
|
+
}, { signal });
|
|
57
|
+
const own = (e) => e.composedPath()[0] === host;
|
|
58
|
+
host.addEventListener('keydown', (e) => own(e) && sink(keyEvent(e)), { signal });
|
|
59
|
+
host.addEventListener('focus', (e) => own(e) && sink({ kind: 'focus', focused: true }), { signal });
|
|
60
|
+
host.addEventListener('blur', (e) => own(e) && sink({ kind: 'focus', focused: false }), { signal });
|
|
61
|
+
// With no DOM selection, Chromium targets clipboard events at the body, not the EditContext
|
|
62
|
+
// host. Taking them only while the host itself is focused keeps other inputs' copy and paste.
|
|
63
|
+
const doc = host.ownerDocument;
|
|
64
|
+
const focused = () => doc.activeElement === host && !host.shadowRoot?.activeElement;
|
|
65
|
+
listenClipboard(doc, focused, sink, () => this.#read(), signal);
|
|
66
|
+
}
|
|
67
|
+
get composing() {
|
|
68
|
+
return this.#composing;
|
|
69
|
+
}
|
|
70
|
+
sync(state, geometry) {
|
|
71
|
+
this.#geometry = geometry;
|
|
72
|
+
const ctx = this.#ctx;
|
|
73
|
+
ctx.updateControlBounds(geometry.control);
|
|
74
|
+
if (geometry.caret)
|
|
75
|
+
ctx.updateSelectionBounds(geometry.caret);
|
|
76
|
+
if (this.#composing)
|
|
77
|
+
return;
|
|
78
|
+
const w = windowAround(state, WINDOW_MARGIN);
|
|
79
|
+
const old = this.#window;
|
|
80
|
+
if (w.from !== old.from)
|
|
81
|
+
ctx.updateText(0, ctx.text.length, w.text);
|
|
82
|
+
else if (w.text !== old.text) {
|
|
83
|
+
const d = diffStrings(old.text, w.text);
|
|
84
|
+
ctx.updateText(d.start, d.endA, w.text.slice(d.start, d.endB));
|
|
85
|
+
}
|
|
86
|
+
this.#window = w;
|
|
87
|
+
const p = state.selection.primary;
|
|
88
|
+
const start = rangeFrom(p) - w.from;
|
|
89
|
+
const end = rangeTo(p) - w.from;
|
|
90
|
+
if (ctx.selectionStart !== start || ctx.selectionEnd !== end)
|
|
91
|
+
ctx.updateSelection(start, end);
|
|
92
|
+
}
|
|
93
|
+
setClipboardSource(read) {
|
|
94
|
+
this.#read = read;
|
|
95
|
+
}
|
|
96
|
+
dispose() {
|
|
97
|
+
this.#lifetime.abort();
|
|
98
|
+
const host = this.focusTarget;
|
|
99
|
+
if (host.editContext === this.#ctx)
|
|
100
|
+
host.editContext = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Snapshot } from '../state/core.ts';
|
|
2
|
+
import type { KeyLike } from '../commands/keymap.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The one kind of event the editor receives from either input path. Every position is a
|
|
5
|
+
* document offset; the platform's own coordinates (EditContext text, textarea value) never leave
|
|
6
|
+
* the surface.
|
|
7
|
+
*
|
|
8
|
+
* Text-producing input (typing, autocorrect, dictation, IME) arrives as `replace`. Keys the
|
|
9
|
+
* keymap owns (arrows, Backspace, Delete, Enter, shortcuts) arrive as `key` from keydown, before
|
|
10
|
+
* the platform acts, in both paths. The element decides from the result of the keymap whether
|
|
11
|
+
* to preventDefault.
|
|
12
|
+
*/
|
|
13
|
+
export type SurfaceEvent =
|
|
14
|
+
/** Replace [from, to) with text. `composing` marks preedit text, which is still real document
|
|
15
|
+
* text (the view draws it with a 'composition' overlay). */
|
|
16
|
+
{
|
|
17
|
+
readonly kind: 'replace';
|
|
18
|
+
readonly from: number;
|
|
19
|
+
readonly to: number;
|
|
20
|
+
readonly text: string;
|
|
21
|
+
readonly composing: boolean;
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: 'composition-end';
|
|
24
|
+
}
|
|
25
|
+
/** The platform moved its own selection (native word motion, assistive tech). */
|
|
26
|
+
| {
|
|
27
|
+
readonly kind: 'select';
|
|
28
|
+
readonly anchor: number;
|
|
29
|
+
readonly head: number;
|
|
30
|
+
} | {
|
|
31
|
+
readonly kind: 'key';
|
|
32
|
+
readonly key: KeyLike;
|
|
33
|
+
readonly preventDefault: () => void;
|
|
34
|
+
} | {
|
|
35
|
+
readonly kind: 'paste';
|
|
36
|
+
readonly text: string;
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: 'cut';
|
|
39
|
+
} | {
|
|
40
|
+
readonly kind: 'focus';
|
|
41
|
+
readonly focused: boolean;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The slice of the document the platform sees. EditContext and the textarea both get a window
|
|
45
|
+
* around the primary selection, not the whole document: a 100k-line string per keystroke would
|
|
46
|
+
* blow the budget. Invariant: [from, from + text.length) is a range of the current document and
|
|
47
|
+
* contains the primary selection.
|
|
48
|
+
*/
|
|
49
|
+
export interface TextWindow {
|
|
50
|
+
readonly from: number;
|
|
51
|
+
readonly text: string;
|
|
52
|
+
}
|
|
53
|
+
/** Pure. Chooses the window: the primary selection's lines plus up to `margin` chars each side,
|
|
54
|
+
* snapped to line starts/ends. */
|
|
55
|
+
export declare function windowAround(state: Snapshot, margin: number): TextWindow;
|
|
56
|
+
/** Characters of context the platform sees on each side of the primary selection's lines. */
|
|
57
|
+
export declare const WINDOW_MARGIN = 2000;
|
|
58
|
+
/** Common prefix and suffix of two strings. The prefix never extends past `prefixCap`, so an
|
|
59
|
+
* insertion of a repeated character lands where the caret was, not at the first equal run. */
|
|
60
|
+
export declare function diffStrings(a: string, b: string, prefixCap?: number): {
|
|
61
|
+
start: number;
|
|
62
|
+
endA: number;
|
|
63
|
+
endB: number;
|
|
64
|
+
};
|
|
65
|
+
export interface SurfaceGeometry {
|
|
66
|
+
/** Bounds of the editor content box, viewport coordinates. */
|
|
67
|
+
readonly control: DOMRect;
|
|
68
|
+
/** Caret box of the primary head, for IME candidate windows. null when off screen. */
|
|
69
|
+
readonly caret: DOMRect | null;
|
|
70
|
+
/** Caret box at any document offset, for EditContext character bounds. null when unmounted. */
|
|
71
|
+
readonly coordsAt: (pos: number) => DOMRect | null;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* One implementation per platform path. The element owns exactly one.
|
|
75
|
+
*
|
|
76
|
+
* Lifecycle: created on connect, `sync` after every Update and after layout changes, `dispose`
|
|
77
|
+
* on disconnect. `sync` is idempotent: syncing the same snapshot twice touches the platform once.
|
|
78
|
+
*/
|
|
79
|
+
export interface InputSurface {
|
|
80
|
+
/** The element that holds focus: the host itself for EditContext, the textarea otherwise. */
|
|
81
|
+
readonly focusTarget: HTMLElement;
|
|
82
|
+
readonly composing: boolean;
|
|
83
|
+
sync(state: Snapshot, geometry: SurfaceGeometry): void;
|
|
84
|
+
/** Text for copy/cut, provided by the element when the platform asks. */
|
|
85
|
+
setClipboardSource(read: () => string): void;
|
|
86
|
+
dispose(): void;
|
|
87
|
+
}
|
|
88
|
+
export type SurfaceSink = (e: SurfaceEvent) => void;
|
|
89
|
+
export type SurfaceKind = 'edit-context' | 'textarea';
|
|
90
|
+
/** 'auto' and 'editcontext' both pick EditContext where the platform has it ('editcontext' forces
|
|
91
|
+
* it where available; there is nothing to force it against where it is not). 'textarea' always
|
|
92
|
+
* wins, since it is the only path with no feature check. */
|
|
93
|
+
export declare function surfaceKind(preference: 'textarea' | 'editcontext' | 'auto'): SurfaceKind;
|
|
94
|
+
/** Both modules are in the core entry; they are small. Async only to leave room for lazy loading
|
|
95
|
+
* the fallback if payload numbers say so. `host` must already have its shadow root. */
|
|
96
|
+
export declare function createInputSurface(host: HTMLElement, sink: SurfaceSink, kind: SurfaceKind): Promise<InputSurface>;
|
|
97
|
+
/** The KeyboardEvent as a 'key' SurfaceEvent. */
|
|
98
|
+
export declare function keyEvent(e: KeyboardEvent): SurfaceEvent;
|
|
99
|
+
/** Wires copy/cut/paste on `target`, for events `owns` accepts. Copy and cut take over only when
|
|
100
|
+
* the editor has text to give; otherwise the platform's default stands. */
|
|
101
|
+
export declare function listenClipboard(target: EventTarget, owns: () => boolean, sink: SurfaceSink, read: () => string, signal: AbortSignal): void;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { rangeFrom, rangeTo } from "../state/selection.js";
|
|
2
|
+
import { EditContextSurface } from "./edit-context.js";
|
|
3
|
+
import { TextareaSurface } from "./textarea.js";
|
|
4
|
+
/** Pure. Chooses the window: the primary selection's lines plus up to `margin` chars each side,
|
|
5
|
+
* snapped to line starts/ends. */
|
|
6
|
+
export function windowAround(state, margin) {
|
|
7
|
+
const { text, selection } = state;
|
|
8
|
+
const lo = text.lineAt(rangeFrom(selection.primary)).from;
|
|
9
|
+
const hi = text.lineAt(rangeTo(selection.primary)).to;
|
|
10
|
+
// Margins snap inward, so one huge neighbouring line never pulls in more than `margin`.
|
|
11
|
+
let from = lo;
|
|
12
|
+
if (lo > 0) {
|
|
13
|
+
const target = Math.max(0, lo - margin);
|
|
14
|
+
const l = text.lineAt(target);
|
|
15
|
+
from = l.from === target ? target : Math.min(l.to + 1, lo);
|
|
16
|
+
}
|
|
17
|
+
let to = hi;
|
|
18
|
+
if (hi < text.length) {
|
|
19
|
+
const target = Math.min(text.length, hi + margin);
|
|
20
|
+
const l = text.lineAt(target);
|
|
21
|
+
to = l.to === target ? target : Math.max(l.from - 1, hi);
|
|
22
|
+
}
|
|
23
|
+
return { from, text: text.slice(from, to) };
|
|
24
|
+
}
|
|
25
|
+
/** Characters of context the platform sees on each side of the primary selection's lines. */
|
|
26
|
+
export const WINDOW_MARGIN = 2000;
|
|
27
|
+
/** Common prefix and suffix of two strings. The prefix never extends past `prefixCap`, so an
|
|
28
|
+
* insertion of a repeated character lands where the caret was, not at the first equal run. */
|
|
29
|
+
export function diffStrings(a, b, prefixCap = Infinity) {
|
|
30
|
+
const max = Math.min(a.length, b.length, prefixCap);
|
|
31
|
+
let start = 0;
|
|
32
|
+
while (start < max && a.charCodeAt(start) === b.charCodeAt(start))
|
|
33
|
+
start++;
|
|
34
|
+
let endA = a.length;
|
|
35
|
+
let endB = b.length;
|
|
36
|
+
while (endA > start && endB > start && a.charCodeAt(endA - 1) === b.charCodeAt(endB - 1)) {
|
|
37
|
+
endA--;
|
|
38
|
+
endB--;
|
|
39
|
+
}
|
|
40
|
+
return { start, endA, endB };
|
|
41
|
+
}
|
|
42
|
+
/** 'auto' and 'editcontext' both pick EditContext where the platform has it ('editcontext' forces
|
|
43
|
+
* it where available; there is nothing to force it against where it is not). 'textarea' always
|
|
44
|
+
* wins, since it is the only path with no feature check. */
|
|
45
|
+
export function surfaceKind(preference) {
|
|
46
|
+
return preference !== 'textarea' && 'EditContext' in globalThis ? 'edit-context' : 'textarea';
|
|
47
|
+
}
|
|
48
|
+
/** Both modules are in the core entry; they are small. Async only to leave room for lazy loading
|
|
49
|
+
* the fallback if payload numbers say so. `host` must already have its shadow root. */
|
|
50
|
+
export async function createInputSurface(host, sink, kind) {
|
|
51
|
+
return kind === 'edit-context' ? new EditContextSurface(host, sink) : new TextareaSurface(host, sink);
|
|
52
|
+
}
|
|
53
|
+
/** The KeyboardEvent as a 'key' SurfaceEvent. */
|
|
54
|
+
export function keyEvent(e) {
|
|
55
|
+
return { kind: 'key', key: e, preventDefault: () => e.preventDefault() };
|
|
56
|
+
}
|
|
57
|
+
/** Wires copy/cut/paste on `target`, for events `owns` accepts. Copy and cut take over only when
|
|
58
|
+
* the editor has text to give; otherwise the platform's default stands. */
|
|
59
|
+
export function listenClipboard(target, owns, sink, read, signal) {
|
|
60
|
+
const give = (e) => {
|
|
61
|
+
if (!owns())
|
|
62
|
+
return false;
|
|
63
|
+
const text = read();
|
|
64
|
+
if (!text || !e.clipboardData)
|
|
65
|
+
return false;
|
|
66
|
+
e.clipboardData.setData('text/plain', text);
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
return true;
|
|
69
|
+
};
|
|
70
|
+
const on = (type, l) => target.addEventListener(type, l, { signal });
|
|
71
|
+
on('copy', (e) => void give(e));
|
|
72
|
+
on('cut', (e) => {
|
|
73
|
+
if (give(e))
|
|
74
|
+
sink({ kind: 'cut' });
|
|
75
|
+
});
|
|
76
|
+
on('paste', (e) => {
|
|
77
|
+
if (!owns())
|
|
78
|
+
return;
|
|
79
|
+
const text = e.clipboardData?.getData('text/plain');
|
|
80
|
+
e.preventDefault();
|
|
81
|
+
if (text)
|
|
82
|
+
sink({ kind: 'paste', text });
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Snapshot } from '../state/core.ts';
|
|
2
|
+
import { type InputSurface, type SurfaceGeometry, type SurfaceSink } from './surface.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Fallback path. A transparent textarea in the shadow root, positioned at the primary caret so
|
|
5
|
+
* IME candidate windows appear there. Its value is the TextWindow; its selection mirrors the
|
|
6
|
+
* primary range.
|
|
7
|
+
*
|
|
8
|
+
* - input: diff the textarea's value against the last synced window (common prefix and suffix,
|
|
9
|
+
* which is O(edit) for typing), emit 'replace' in document coordinates.
|
|
10
|
+
* - compositionstart/end: track composing; the commit is diffed at compositionend, before engines
|
|
11
|
+
* that fire the last input event after it.
|
|
12
|
+
* - paste/cut/copy: ClipboardEvent, handled before the textarea changes.
|
|
13
|
+
* - keydown: forwarded as 'key'.
|
|
14
|
+
*
|
|
15
|
+
* The textarea never holds more than the window, so select-all and very long selections are the
|
|
16
|
+
* editor's own state, not the textarea's.
|
|
17
|
+
*/
|
|
18
|
+
export declare class TextareaSurface implements InputSurface {
|
|
19
|
+
#private;
|
|
20
|
+
readonly focusTarget: HTMLTextAreaElement;
|
|
21
|
+
constructor(host: HTMLElement, sink: SurfaceSink);
|
|
22
|
+
get composing(): boolean;
|
|
23
|
+
sync(state: Snapshot, geometry: SurfaceGeometry): void;
|
|
24
|
+
setClipboardSource(read: () => string): void;
|
|
25
|
+
dispose(): void;
|
|
26
|
+
}
|