@nibling/editor 0.0.0-stage → 0.0.1
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 +64 -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,135 @@
|
|
|
1
|
+
import { OVERLAY_BUDGET, overlayKinds } from "./decorations.js";
|
|
2
|
+
import { domPoint } from "./line-dom.js";
|
|
3
|
+
const underlined = new Set(['diagnostic-error', 'diagnostic-warning', 'diagnostic-info']);
|
|
4
|
+
/**
|
|
5
|
+
* Owns this instance's entries in the document-wide `CSS.highlights` registry: one Highlight per
|
|
6
|
+
* overlay kind, named `nib-<instance>-<kind>`, registered on first use and deleted on dispose.
|
|
7
|
+
*
|
|
8
|
+
* `paint` diffs the new overlay list against what is registered (by kind, from, to) and touches
|
|
9
|
+
* a Highlight only when its membership changed, because any change makes Chrome walk every
|
|
10
|
+
* registered range. Overlays outside mounted lines are clipped away here, so a source may return
|
|
11
|
+
* more than is visible. The budget is enforced here and only here.
|
|
12
|
+
*/
|
|
13
|
+
export class OverlayPainter {
|
|
14
|
+
#instance;
|
|
15
|
+
#highlights = new Map();
|
|
16
|
+
#registered = new Map();
|
|
17
|
+
/** Lines rerendered since the last paint; their StaticRanges point at replaced nodes. */
|
|
18
|
+
#invalid = new Set();
|
|
19
|
+
/** Segments that fit on mounted lines but not in the budget, in the last paint. */
|
|
20
|
+
dropped = 0;
|
|
21
|
+
constructor(instance) {
|
|
22
|
+
this.#instance = instance;
|
|
23
|
+
}
|
|
24
|
+
#name(kind) {
|
|
25
|
+
return `nib-${this.#instance}-${kind}`;
|
|
26
|
+
}
|
|
27
|
+
/** The `::highlight()` rules for this instance, for the adopted stylesheet. */
|
|
28
|
+
css(colors) {
|
|
29
|
+
return Object.keys(overlayKinds)
|
|
30
|
+
.map((kind) => {
|
|
31
|
+
const c = colors[kind];
|
|
32
|
+
const body = underlined.has(kind)
|
|
33
|
+
? `text-decoration:underline wavy ${c};`
|
|
34
|
+
: kind === 'composition'
|
|
35
|
+
? `text-decoration:underline ${c};`
|
|
36
|
+
: `background-color:${c};`;
|
|
37
|
+
return `::highlight(${this.#name(kind)}){${body}}`;
|
|
38
|
+
})
|
|
39
|
+
.join('\n');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Overlays must be sorted by (kind priority desc, from). A range spanning several lines is split
|
|
43
|
+
* at line boundaries into one StaticRange per mounted line, each built from domPoint on its box.
|
|
44
|
+
*/
|
|
45
|
+
paint(overlays, text, boxes) {
|
|
46
|
+
let minLine = Infinity;
|
|
47
|
+
let maxLine = -Infinity;
|
|
48
|
+
for (const n of boxes.keys()) {
|
|
49
|
+
if (n < minLine)
|
|
50
|
+
minLine = n;
|
|
51
|
+
if (n > maxLine)
|
|
52
|
+
maxLine = n;
|
|
53
|
+
}
|
|
54
|
+
const next = new Map();
|
|
55
|
+
let count = 0;
|
|
56
|
+
let dropped = 0;
|
|
57
|
+
for (const o of overlays) {
|
|
58
|
+
if (o.to <= o.from || boxes.size === 0)
|
|
59
|
+
continue;
|
|
60
|
+
const lo = Math.max(text.lineAt(o.from).number, minLine);
|
|
61
|
+
const hi = Math.min(text.lineAt(o.to).number, maxLine);
|
|
62
|
+
for (let n = lo; n <= hi; n++) {
|
|
63
|
+
const box = boxes.get(n);
|
|
64
|
+
if (!box)
|
|
65
|
+
continue;
|
|
66
|
+
const line = text.line(n);
|
|
67
|
+
const from = Math.max(o.from, line.from);
|
|
68
|
+
const to = Math.min(o.to, line.to);
|
|
69
|
+
if (to <= from)
|
|
70
|
+
continue;
|
|
71
|
+
let kindMap = next.get(o.kind);
|
|
72
|
+
if (!kindMap)
|
|
73
|
+
next.set(o.kind, (kindMap = new Map()));
|
|
74
|
+
const key = `${from}:${to}`;
|
|
75
|
+
if (kindMap.has(key))
|
|
76
|
+
continue;
|
|
77
|
+
if (count >= OVERLAY_BUDGET) {
|
|
78
|
+
dropped++;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
count++;
|
|
82
|
+
const prev = this.#invalid.has(n) ? undefined : this.#registered.get(o.kind)?.get(key);
|
|
83
|
+
kindMap.set(key, prev ?? staticRange(box, from - line.from, to - line.from));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const kind of new Set([...this.#registered.keys(), ...next.keys()])) {
|
|
87
|
+
const before = this.#registered.get(kind);
|
|
88
|
+
const after = next.get(kind);
|
|
89
|
+
let h = this.#highlights.get(kind);
|
|
90
|
+
if (before && h)
|
|
91
|
+
for (const [key, r] of before)
|
|
92
|
+
if (after?.get(key) !== r)
|
|
93
|
+
h.delete(r);
|
|
94
|
+
if (after) {
|
|
95
|
+
for (const [key, r] of after) {
|
|
96
|
+
if (before?.get(key) === r)
|
|
97
|
+
continue;
|
|
98
|
+
if (!h)
|
|
99
|
+
h = this.#register(kind);
|
|
100
|
+
h.add(r);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.#registered = next;
|
|
105
|
+
this.#invalid.clear();
|
|
106
|
+
this.dropped = dropped;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Mounted lines rerendered this frame invalidate the StaticRanges that point into them, even if
|
|
110
|
+
* the overlay list is unchanged (StaticRange does not track DOM mutation). The view passes those
|
|
111
|
+
* lines; their ranges are rebuilt.
|
|
112
|
+
*/
|
|
113
|
+
invalidateLines(lines) {
|
|
114
|
+
for (const n of lines)
|
|
115
|
+
this.#invalid.add(n);
|
|
116
|
+
}
|
|
117
|
+
dispose() {
|
|
118
|
+
for (const kind of this.#highlights.keys())
|
|
119
|
+
CSS.highlights.delete(this.#name(kind));
|
|
120
|
+
this.#highlights.clear();
|
|
121
|
+
this.#registered.clear();
|
|
122
|
+
}
|
|
123
|
+
#register(kind) {
|
|
124
|
+
const h = new Highlight();
|
|
125
|
+
h.priority = overlayKinds[kind].priority;
|
|
126
|
+
CSS.highlights.set(this.#name(kind), h);
|
|
127
|
+
this.#highlights.set(kind, h);
|
|
128
|
+
return h;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function staticRange(box, from, to) {
|
|
132
|
+
const a = domPoint(box, from);
|
|
133
|
+
const b = domPoint(box, to);
|
|
134
|
+
return new StaticRange({ startContainer: a.node, startOffset: a.offset, endContainer: b.node, endOffset: b.offset });
|
|
135
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EditorCore } from '../state/core.ts';
|
|
2
|
+
import type { FoldRangeSource } from '../state/fold-ranges.ts';
|
|
3
|
+
import type { FoldState, HiddenSpan } from '../state/folds.ts';
|
|
4
|
+
import type { LineClassSource, ViewFolding } from './decorations.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Line numbers, fold toggles, and the folded placeholder. The view calls its hooks while it places
|
|
7
|
+
* and renders lines; the rail owns no geometry and no elements of its own beyond those it puts in
|
|
8
|
+
* the ones it is handed. Fold state and fold discovery are headless followers it reads.
|
|
9
|
+
*
|
|
10
|
+
* Pointer: a toggle or placeholder takes the mousedown before the element sees it and prevents its
|
|
11
|
+
* default, so focus and the selection stay where they were. Nothing in the rail is focusable, and
|
|
12
|
+
* its elements are aria-hidden: folding is visual, and the textarea's text window (D2) still holds
|
|
13
|
+
* the hidden lines, so a screen reader reads the same text with or without folds.
|
|
14
|
+
*/
|
|
15
|
+
export declare class FoldRail implements ViewFolding {
|
|
16
|
+
#private;
|
|
17
|
+
readonly lineClasses: LineClassSource;
|
|
18
|
+
constructor(core: EditorCore, folds: FoldState, source: FoldRangeSource, signal: AbortSignal);
|
|
19
|
+
/** Adopt the rail's stylesheet after the view's, and take pointer presses on toggles. */
|
|
20
|
+
mount(root: ShadowRoot, signal: AbortSignal): void;
|
|
21
|
+
hidden(): readonly HiddenSpan[];
|
|
22
|
+
subscribe(invalidate: () => void): () => void;
|
|
23
|
+
gutter(el: HTMLElement, line: number): void;
|
|
24
|
+
line(el: HTMLElement, line: number): void;
|
|
25
|
+
/** Open the fold at `line`, or fold the region that starts there. */
|
|
26
|
+
toggle(line: number): boolean;
|
|
27
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { foldRange } from "../commands/fold.js";
|
|
2
|
+
import { sheetFor } from "../sheet.js";
|
|
3
|
+
/**
|
|
4
|
+
* The rail's look. Quiet by default: numbers at low contrast, the active line's a step brighter,
|
|
5
|
+
* and no chevrons until the pointer is over the rail, where foldable lines trade their number for
|
|
6
|
+
* a solid chevron. The innermost fold region around each cursor shows its chevron at rest too. A folded header keeps a placeholder after its text, so hidden code is never
|
|
7
|
+
* invisible. Only opacity, color, and transform animate, and only when the user has not asked for
|
|
8
|
+
* reduced motion.
|
|
9
|
+
*
|
|
10
|
+
* Every color and timing is a `--nib-editor-*` knob (docs/design/theming.md) that defaults to a
|
|
11
|
+
* platform token and then to a mix of the editor's own background and foreground.
|
|
12
|
+
*/
|
|
13
|
+
const railCss = `
|
|
14
|
+
:host{
|
|
15
|
+
--nib-editor-gutter-fg-active:var(--nib-color-text,color-mix(in srgb,var(--nib-fg,#d4d4d4) 78%,var(--nib-bg,#1e1e1e)));
|
|
16
|
+
--nib-editor-fold:var(--nib-color-text-muted,color-mix(in srgb,var(--nib-fg,#d4d4d4) 45%,var(--nib-bg,#1e1e1e)));
|
|
17
|
+
--nib-editor-fold-placeholder-bg:var(--nib-color-hover,color-mix(in srgb,var(--nib-fg,#d4d4d4) 9%,transparent));
|
|
18
|
+
}
|
|
19
|
+
/* Numbers a step smaller than code, 0.75ch of air on the left, and a small gap to the code. No
|
|
20
|
+
chevron lane: while the pointer is over the rail, each foldable line's number cross-fades into
|
|
21
|
+
its chevron, and every other number stays put. The gap doubles as the horizontal-scroll
|
|
22
|
+
affordance: its background fades out, so text scrolled under the sticky gutter shows through its
|
|
23
|
+
last 7px and leaves the numbers clear space. */
|
|
24
|
+
.nib-scroller{--nib-rail-gap:14px;--nib-gutter:calc(var(--nib-digits,1) * .85ch + .75ch + var(--nib-rail-gap))}
|
|
25
|
+
.nib-gutter{background:linear-gradient(to right,var(--nib-bg,#1e1e1e) calc(100% - 7px),
|
|
26
|
+
color-mix(in srgb,var(--nib-bg,#1e1e1e) 55%,transparent) calc(100% - 3px),transparent)}
|
|
27
|
+
.nib-gutter{color:var(--nib-gutter-fg,var(--nib-color-text-muted,color-mix(in srgb,var(--nib-fg,#d4d4d4) 42%,var(--nib-bg,#1e1e1e))))}
|
|
28
|
+
.nib-gutter-line{left:0;right:0;padding-right:var(--nib-rail-gap);box-sizing:border-box;text-align:right;font-size:.85em;font-variant-numeric:tabular-nums}
|
|
29
|
+
.nib-gutter-line.nib-gutter-active{color:var(--nib-editor-gutter-fg-active)}
|
|
30
|
+
.nib-fold-toggle{position:absolute;top:0;left:0;right:var(--nib-rail-gap);height:100%;cursor:pointer;opacity:0;
|
|
31
|
+
color:var(--nib-editor-fold)}
|
|
32
|
+
.nib-fold-toggle::before{content:'';position:absolute;top:0;bottom:0;right:0;width:10px;background:currentColor;
|
|
33
|
+
-webkit-mask:var(--nib-chevron) center/10px 10px no-repeat;mask:var(--nib-chevron) center/10px 10px no-repeat}
|
|
34
|
+
.nib-scroller{
|
|
35
|
+
--nib-chevron:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath d='M2.4 3.6h5.2L5 6.8z' fill='%23000' stroke='%23000' stroke-width='1.3' stroke-linejoin='round'/%3E%3C/svg%3E");
|
|
36
|
+
--nib-dots:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 2'%3E%3Ccircle cx='1' cy='1' r='1'/%3E%3Ccircle cx='5' cy='1' r='1'/%3E%3Ccircle cx='9' cy='1' r='1'/%3E%3C/svg%3E")}
|
|
37
|
+
.nib-gutter:hover .nib-fold-toggle,.nib-fold-current .nib-fold-toggle{opacity:1}
|
|
38
|
+
.nib-gutter:hover :is(.nib-foldable,.nib-folded),.nib-gutter-line.nib-fold-current{color:transparent}
|
|
39
|
+
.nib-gutter-line:hover .nib-fold-toggle{color:var(--nib-editor-gutter-fg-active)}
|
|
40
|
+
.nib-folded .nib-fold-toggle::before{transform:rotate(-90deg)}
|
|
41
|
+
.nib-fold-placeholder{position:relative;display:inline-block;vertical-align:top;width:2.5ch;height:calc(var(--nib-lh,18px) - 6px);
|
|
42
|
+
margin:3px 0 0 1ch;border-radius:4px;cursor:pointer;color:var(--nib-editor-fold);background:var(--nib-editor-fold-placeholder-bg)}
|
|
43
|
+
.nib-fold-placeholder::before{content:'';position:absolute;inset:0;background:currentColor;
|
|
44
|
+
-webkit-mask:var(--nib-dots) center/10px 2px no-repeat;mask:var(--nib-dots) center/10px 2px no-repeat}
|
|
45
|
+
.nib-fold-placeholder:hover{color:var(--nib-editor-gutter-fg-active)}
|
|
46
|
+
@media (prefers-reduced-motion:no-preference){
|
|
47
|
+
.nib-fold-toggle{transition:opacity var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
48
|
+
.nib-gutter-line{transition:color var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
49
|
+
.nib-fold-toggle::before{transition:transform var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
50
|
+
.nib-fold-placeholder.nib-enter{animation:nib-fold-in var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
51
|
+
}
|
|
52
|
+
@keyframes nib-fold-in{from{opacity:0;transform:translateX(-2px)}}
|
|
53
|
+
`;
|
|
54
|
+
/** Lines whose number shows as active. Past this many cursors, only the primary's does. */
|
|
55
|
+
const ACTIVE_LIMIT = 1000;
|
|
56
|
+
/** Cursors whose fold region shows its chevron. Past this many, only the primary's does: each
|
|
57
|
+
* one may walk up to the fold source's scan limit to find its region. */
|
|
58
|
+
const CURRENT_LIMIT = 64;
|
|
59
|
+
/**
|
|
60
|
+
* Line numbers, fold toggles, and the folded placeholder. The view calls its hooks while it places
|
|
61
|
+
* and renders lines; the rail owns no geometry and no elements of its own beyond those it puts in
|
|
62
|
+
* the ones it is handed. Fold state and fold discovery are headless followers it reads.
|
|
63
|
+
*
|
|
64
|
+
* Pointer: a toggle or placeholder takes the mousedown before the element sees it and prevents its
|
|
65
|
+
* default, so focus and the selection stay where they were. Nothing in the rail is focusable, and
|
|
66
|
+
* its elements are aria-hidden: folding is visual, and the textarea's text window (D2) still holds
|
|
67
|
+
* the hidden lines, so a screen reader reads the same text with or without folds.
|
|
68
|
+
*/
|
|
69
|
+
export class FoldRail {
|
|
70
|
+
#core;
|
|
71
|
+
#folds;
|
|
72
|
+
#source;
|
|
73
|
+
#marks = new WeakMap();
|
|
74
|
+
/** Which line each toggle or placeholder currently stands for. */
|
|
75
|
+
#lineOf = new WeakMap();
|
|
76
|
+
#active = new Set();
|
|
77
|
+
/** Headers of the innermost fold region around each cursor: their chevron shows at rest. */
|
|
78
|
+
#current = new Set();
|
|
79
|
+
/** Headers folded since their line last rendered: only their placeholder fades in, not one
|
|
80
|
+
* that is rendered again for new tokens or typing. */
|
|
81
|
+
#entering = new Set();
|
|
82
|
+
#wasFolded = new Set();
|
|
83
|
+
#listeners = [];
|
|
84
|
+
#classListeners = [];
|
|
85
|
+
lineClasses;
|
|
86
|
+
constructor(core, folds, source, signal) {
|
|
87
|
+
this.#core = core;
|
|
88
|
+
this.#folds = folds;
|
|
89
|
+
this.#source = source;
|
|
90
|
+
this.lineClasses = {
|
|
91
|
+
classesFor: (_state, line) => (folds.isFolded(line) ? 'nib-fold-closed' : undefined),
|
|
92
|
+
subscribe: (invalidate) => {
|
|
93
|
+
this.#classListeners.push(invalidate);
|
|
94
|
+
return () => (this.#classListeners = this.#classListeners.filter((l) => l !== invalidate));
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
this.#active = this.#activeLines();
|
|
98
|
+
this.#current = this.#currentHeaders();
|
|
99
|
+
const offFolds = folds.subscribe(() => {
|
|
100
|
+
const now = new Set(folds.ranges.map((r) => r.line));
|
|
101
|
+
this.#entering = new Set([...now].filter((n) => !this.#wasFolded.has(n)));
|
|
102
|
+
this.#wasFolded = now;
|
|
103
|
+
this.#emit();
|
|
104
|
+
for (const l of this.#classListeners)
|
|
105
|
+
l();
|
|
106
|
+
});
|
|
107
|
+
const offSource = source.subscribe(() => {
|
|
108
|
+
this.#current = this.#currentHeaders();
|
|
109
|
+
this.#emit();
|
|
110
|
+
});
|
|
111
|
+
core.listen((u) => this.#onUpdate(u), signal);
|
|
112
|
+
signal.addEventListener('abort', () => {
|
|
113
|
+
offFolds();
|
|
114
|
+
offSource();
|
|
115
|
+
}, { once: true });
|
|
116
|
+
}
|
|
117
|
+
/** Adopt the rail's stylesheet after the view's, and take pointer presses on toggles. */
|
|
118
|
+
mount(root, signal) {
|
|
119
|
+
const sheet = sheetFor(root);
|
|
120
|
+
sheet.replaceSync(railCss);
|
|
121
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
122
|
+
root.addEventListener('mousedown', (e) => this.#onMouseDown(e), { signal });
|
|
123
|
+
signal.addEventListener('abort', () => (root.adoptedStyleSheets = root.adoptedStyleSheets.filter((s) => s !== sheet)), { once: true });
|
|
124
|
+
}
|
|
125
|
+
hidden() {
|
|
126
|
+
return this.#folds.hidden;
|
|
127
|
+
}
|
|
128
|
+
subscribe(invalidate) {
|
|
129
|
+
this.#listeners.push(invalidate);
|
|
130
|
+
return () => {
|
|
131
|
+
this.#listeners = this.#listeners.filter((l) => l !== invalidate);
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
gutter(el, line) {
|
|
135
|
+
const folded = this.#folds.isFolded(line);
|
|
136
|
+
const foldable = folded || this.#source.startsFold(line);
|
|
137
|
+
const current = foldable && this.#current.has(line) ? ' nib-fold-current' : '';
|
|
138
|
+
const cls = `nib-gutter-line${this.#active.has(line) ? ' nib-gutter-active' : ''}${folded ? ' nib-folded' : foldable ? ' nib-foldable' : ''}${current}`;
|
|
139
|
+
let mark = this.#marks.get(el);
|
|
140
|
+
if (!mark) {
|
|
141
|
+
mark = { cls: el.className, toggle: null };
|
|
142
|
+
this.#marks.set(el, mark);
|
|
143
|
+
}
|
|
144
|
+
if (mark.cls !== cls) {
|
|
145
|
+
el.className = cls;
|
|
146
|
+
mark.cls = cls;
|
|
147
|
+
}
|
|
148
|
+
const toggle = mark.toggle;
|
|
149
|
+
if (foldable) {
|
|
150
|
+
const t = toggle ?? (mark.toggle = this.#element(el, 'nib-fold-toggle', 'fold-toggle'));
|
|
151
|
+
// The view's number write replaced the children, toggle included, when the line changed.
|
|
152
|
+
if (t.parentNode !== el)
|
|
153
|
+
el.appendChild(t);
|
|
154
|
+
this.#lineOf.set(t, line);
|
|
155
|
+
}
|
|
156
|
+
else if (toggle?.parentNode === el)
|
|
157
|
+
toggle.remove();
|
|
158
|
+
}
|
|
159
|
+
line(el, line) {
|
|
160
|
+
if (!this.#folds.isFolded(line))
|
|
161
|
+
return;
|
|
162
|
+
const p = this.#element(el, 'nib-fold-placeholder', 'fold-placeholder');
|
|
163
|
+
if (this.#entering.delete(line))
|
|
164
|
+
p.classList.add('nib-enter');
|
|
165
|
+
el.appendChild(p);
|
|
166
|
+
this.#lineOf.set(p, line);
|
|
167
|
+
}
|
|
168
|
+
#element(near, cls, part) {
|
|
169
|
+
const s = near.ownerDocument.createElement('span');
|
|
170
|
+
s.className = cls;
|
|
171
|
+
s.part.add(part);
|
|
172
|
+
s.setAttribute('aria-hidden', 'true');
|
|
173
|
+
return s;
|
|
174
|
+
}
|
|
175
|
+
#onMouseDown(e) {
|
|
176
|
+
if (e.button !== 0)
|
|
177
|
+
return;
|
|
178
|
+
const hit = e.target?.closest?.('.nib-fold-toggle, .nib-fold-placeholder');
|
|
179
|
+
const line = hit ? this.#lineOf.get(hit) : undefined;
|
|
180
|
+
if (line === undefined)
|
|
181
|
+
return;
|
|
182
|
+
e.preventDefault();
|
|
183
|
+
e.stopPropagation();
|
|
184
|
+
this.toggle(line);
|
|
185
|
+
}
|
|
186
|
+
/** Open the fold at `line`, or fold the region that starts there. */
|
|
187
|
+
toggle(line) {
|
|
188
|
+
if (this.#folds.unfold(line))
|
|
189
|
+
return true;
|
|
190
|
+
const range = this.#source.rangeAt(line);
|
|
191
|
+
if (!range)
|
|
192
|
+
return false;
|
|
193
|
+
const spec = foldRange(this.#folds, range)({ state: this.#core.state });
|
|
194
|
+
if (spec)
|
|
195
|
+
this.#core.apply(spec);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
#activeLines() {
|
|
199
|
+
const { text, selection } = this.#core.state;
|
|
200
|
+
const ranges = selection.ranges.length > ACTIVE_LIMIT ? [selection.primary] : selection.ranges;
|
|
201
|
+
return new Set(ranges.map((r) => text.lineAt(r.head).number));
|
|
202
|
+
}
|
|
203
|
+
/** The header of the innermost fold region holding each cursor's line, its own header included. */
|
|
204
|
+
#currentHeaders() {
|
|
205
|
+
const { text, selection } = this.#core.state;
|
|
206
|
+
const ranges = selection.ranges.length > CURRENT_LIMIT ? [selection.primary] : selection.ranges;
|
|
207
|
+
const out = new Set();
|
|
208
|
+
for (const r of ranges) {
|
|
209
|
+
const line = text.lineAt(r.head).number;
|
|
210
|
+
const header = this.#source.startsFold(line) ? line : this.#source.enclosing(line)?.line;
|
|
211
|
+
if (header !== undefined)
|
|
212
|
+
out.add(header);
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
#onUpdate(u) {
|
|
217
|
+
if (u.after.selection === u.before.selection && !u.docChanged)
|
|
218
|
+
return;
|
|
219
|
+
const next = this.#activeLines();
|
|
220
|
+
const current = this.#currentHeaders();
|
|
221
|
+
const same = sameSet(next, this.#active) && sameSet(current, this.#current);
|
|
222
|
+
this.#active = next;
|
|
223
|
+
this.#current = current;
|
|
224
|
+
if (!same)
|
|
225
|
+
this.#emit();
|
|
226
|
+
}
|
|
227
|
+
#emit() {
|
|
228
|
+
for (const l of this.#listeners)
|
|
229
|
+
l();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const sameSet = (a, b) => a.size === b.size && [...a].every((n) => b.has(n));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Snapshot } from '../state/core.ts';
|
|
2
|
+
import type { Overlay, RemoteCursorSource } from './decorations.ts';
|
|
3
|
+
/** How long a collaborator's name label stays up after they last moved or typed. */
|
|
4
|
+
export declare const LABEL_MS = 2500;
|
|
5
|
+
export declare const remoteCss = "\n.nib-remote{position:absolute;top:0;left:var(--nib-gutter);z-index:1;pointer-events:none}\n.nib-rcaret{position:absolute;width:2px;margin-left:-1px;height:var(--nib-lh,18px);background:var(--nib-rc)}\n.nib-rlabel{position:absolute;transform:translateY(-100%);padding:0 4px;border-radius:3px 3px 3px 0;white-space:nowrap;\n font:600 10px/14px system-ui,sans-serif;background:var(--nib-rc);color:var(--nib-editor-on-presence);\n transition:opacity var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}\n.nib-rlabel.nib-below{transform:translateY(var(--nib-lh,18px));border-radius:0 3px 3px 3px}\n.nib-rlabel.nib-idle{opacity:0}\n@media (prefers-reduced-motion:reduce){.nib-rlabel{transition:none}}\n";
|
|
6
|
+
/** Content coordinates of a caret at a position, or null when its line is not mounted. */
|
|
7
|
+
export type Place = (pos: number) => {
|
|
8
|
+
readonly x: number;
|
|
9
|
+
readonly y: number;
|
|
10
|
+
} | null;
|
|
11
|
+
/**
|
|
12
|
+
* Collaborators' carets and name labels, in a layer beside the local carets. Their selections are
|
|
13
|
+
* overlays (`remote-<slot>`), painted with the view's other highlights. Remote carets don't blink
|
|
14
|
+
* and stay visible while the editor is blurred.
|
|
15
|
+
*/
|
|
16
|
+
export declare class RemoteCursors {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(layer: HTMLDivElement, invalidate: () => void);
|
|
19
|
+
add(source: RemoteCursorSource, signal?: AbortSignal): void;
|
|
20
|
+
overlays(state: Snapshot, from: number, to: number): Iterable<Overlay>;
|
|
21
|
+
/** Reads every position first (slow lines may measure), then writes. */
|
|
22
|
+
draw(state: Snapshot, place: Place): void;
|
|
23
|
+
dispose(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/** How long a collaborator's name label stays up after they last moved or typed. */
|
|
2
|
+
export const LABEL_MS = 2500;
|
|
3
|
+
export const remoteCss = `
|
|
4
|
+
.nib-remote{position:absolute;top:0;left:var(--nib-gutter);z-index:1;pointer-events:none}
|
|
5
|
+
.nib-rcaret{position:absolute;width:2px;margin-left:-1px;height:var(--nib-lh,18px);background:var(--nib-rc)}
|
|
6
|
+
.nib-rlabel{position:absolute;transform:translateY(-100%);padding:0 4px;border-radius:3px 3px 3px 0;white-space:nowrap;
|
|
7
|
+
font:600 10px/14px system-ui,sans-serif;background:var(--nib-rc);color:var(--nib-editor-on-presence);
|
|
8
|
+
transition:opacity var(--nib-editor-motion-duration-fast) var(--nib-editor-motion-ease-out)}
|
|
9
|
+
.nib-rlabel.nib-below{transform:translateY(var(--nib-lh,18px));border-radius:0 3px 3px 3px}
|
|
10
|
+
.nib-rlabel.nib-idle{opacity:0}
|
|
11
|
+
@media (prefers-reduced-motion:reduce){.nib-rlabel{transition:none}}
|
|
12
|
+
`;
|
|
13
|
+
/**
|
|
14
|
+
* Collaborators' carets and name labels, in a layer beside the local carets. Their selections are
|
|
15
|
+
* overlays (`remote-<slot>`), painted with the view's other highlights. Remote carets don't blink
|
|
16
|
+
* and stay visible while the editor is blurred.
|
|
17
|
+
*/
|
|
18
|
+
export class RemoteCursors {
|
|
19
|
+
#layer;
|
|
20
|
+
#invalidate;
|
|
21
|
+
#sources = [];
|
|
22
|
+
#carets = [];
|
|
23
|
+
#labels = new Map();
|
|
24
|
+
/** Per collaborator without `activeAt`: their ranges last frame, and when those last changed. */
|
|
25
|
+
#seen = new Map();
|
|
26
|
+
#timer = null;
|
|
27
|
+
constructor(layer, invalidate) {
|
|
28
|
+
this.#layer = layer;
|
|
29
|
+
this.#invalidate = invalidate;
|
|
30
|
+
}
|
|
31
|
+
add(source, signal) {
|
|
32
|
+
if (signal?.aborted)
|
|
33
|
+
return;
|
|
34
|
+
this.#sources.push(source);
|
|
35
|
+
const off = source.subscribe(this.#invalidate);
|
|
36
|
+
signal?.addEventListener('abort', () => {
|
|
37
|
+
off();
|
|
38
|
+
this.#sources = this.#sources.filter((s) => s !== source);
|
|
39
|
+
this.#invalidate();
|
|
40
|
+
}, { once: true });
|
|
41
|
+
this.#invalidate();
|
|
42
|
+
}
|
|
43
|
+
*overlays(state, from, to) {
|
|
44
|
+
for (const c of this.#cursors(state)) {
|
|
45
|
+
for (const r of c.ranges) {
|
|
46
|
+
const a = Math.min(r.anchor, r.head);
|
|
47
|
+
const b = Math.max(r.anchor, r.head);
|
|
48
|
+
if (a < b && b >= from && a <= to)
|
|
49
|
+
yield { kind: `remote-${c.slot}`, from: a, to: b };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Reads every position first (slow lines may measure), then writes. */
|
|
54
|
+
draw(state, place) {
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
const carets = [];
|
|
57
|
+
const labels = [];
|
|
58
|
+
const live = new Set();
|
|
59
|
+
let wake = Infinity;
|
|
60
|
+
for (const c of this.#cursors(state)) {
|
|
61
|
+
live.add(c.id);
|
|
62
|
+
const color = `var(--nib-editor-presence-${c.slot})`;
|
|
63
|
+
const until = this.#activeAt(c, now) + LABEL_MS;
|
|
64
|
+
if (until > now)
|
|
65
|
+
wake = Math.min(wake, until);
|
|
66
|
+
c.ranges.forEach((r, i) => {
|
|
67
|
+
const p = place(r.head);
|
|
68
|
+
if (!p)
|
|
69
|
+
return;
|
|
70
|
+
carets.push({ ...p, color });
|
|
71
|
+
if (i === 0)
|
|
72
|
+
labels.push({ id: c.id, name: c.name, ...p, color, idle: until <= now });
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
for (const id of this.#seen.keys())
|
|
76
|
+
if (!live.has(id))
|
|
77
|
+
this.#seen.delete(id);
|
|
78
|
+
const doc = this.#layer.ownerDocument;
|
|
79
|
+
for (let i = 0; i < carets.length; i++) {
|
|
80
|
+
let el = this.#carets[i];
|
|
81
|
+
if (!el) {
|
|
82
|
+
el = doc.createElement('div');
|
|
83
|
+
el.className = 'nib-rcaret';
|
|
84
|
+
this.#layer.appendChild(el);
|
|
85
|
+
this.#carets.push(el);
|
|
86
|
+
}
|
|
87
|
+
const m = carets[i];
|
|
88
|
+
el.style.left = `${m.x}px`;
|
|
89
|
+
el.style.top = `${m.y}px`;
|
|
90
|
+
el.style.setProperty('--nib-rc', m.color);
|
|
91
|
+
}
|
|
92
|
+
for (let i = carets.length; i < this.#carets.length; i++)
|
|
93
|
+
this.#carets[i].remove();
|
|
94
|
+
this.#carets.length = Math.min(this.#carets.length, carets.length);
|
|
95
|
+
const shown = new Set();
|
|
96
|
+
for (const l of labels) {
|
|
97
|
+
shown.add(l.id);
|
|
98
|
+
let el = this.#labels.get(l.id);
|
|
99
|
+
if (!el) {
|
|
100
|
+
el = doc.createElement('div');
|
|
101
|
+
el.className = 'nib-rlabel';
|
|
102
|
+
this.#layer.appendChild(el);
|
|
103
|
+
this.#labels.set(l.id, el);
|
|
104
|
+
}
|
|
105
|
+
if (el.textContent !== l.name)
|
|
106
|
+
el.textContent = l.name;
|
|
107
|
+
el.style.left = `${l.x}px`;
|
|
108
|
+
el.style.top = `${l.y}px`;
|
|
109
|
+
el.style.setProperty('--nib-rc', l.color);
|
|
110
|
+
// On the first line there is no room above, so the label hangs below the caret.
|
|
111
|
+
el.classList.toggle('nib-below', l.y < 1);
|
|
112
|
+
el.classList.toggle('nib-idle', l.idle);
|
|
113
|
+
}
|
|
114
|
+
for (const [id, el] of this.#labels) {
|
|
115
|
+
if (shown.has(id))
|
|
116
|
+
continue;
|
|
117
|
+
el.remove();
|
|
118
|
+
this.#labels.delete(id);
|
|
119
|
+
}
|
|
120
|
+
if (this.#timer)
|
|
121
|
+
clearTimeout(this.#timer);
|
|
122
|
+
this.#timer = wake === Infinity ? null : setTimeout(() => ((this.#timer = null), this.#invalidate()), wake - now);
|
|
123
|
+
}
|
|
124
|
+
dispose() {
|
|
125
|
+
if (this.#timer)
|
|
126
|
+
clearTimeout(this.#timer);
|
|
127
|
+
this.#timer = null;
|
|
128
|
+
}
|
|
129
|
+
*#cursors(state) {
|
|
130
|
+
for (const s of this.#sources)
|
|
131
|
+
yield* s.cursors(state);
|
|
132
|
+
}
|
|
133
|
+
#activeAt(c, now) {
|
|
134
|
+
if (c.activeAt !== undefined)
|
|
135
|
+
return c.activeAt;
|
|
136
|
+
const key = c.ranges.map((r) => `${r.anchor}:${r.head}`).join(',');
|
|
137
|
+
const seen = this.#seen.get(c.id);
|
|
138
|
+
if (seen?.key === key)
|
|
139
|
+
return seen.at;
|
|
140
|
+
this.#seen.set(c.id, { key, at: now });
|
|
141
|
+
return now;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EditorCore } from '../state/core.ts';
|
|
2
|
+
import type { FoldRangeSource } from '../state/fold-ranges.ts';
|
|
3
|
+
import type { HeightMap } from './heightmap.ts';
|
|
4
|
+
/** What ScopeFocus needs from the view: fresh geometry, and a cue for when it may have changed. */
|
|
5
|
+
export interface ScopeFocusHost {
|
|
6
|
+
readonly heights: HeightMap;
|
|
7
|
+
/** Called after each real view frame, so folds, edits, and scrolling can move the band without
|
|
8
|
+
* a pointer event of their own. */
|
|
9
|
+
onFrame(listener: () => void): () => void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One absolutely positioned element in the content layer, never a line class: a line-class change
|
|
13
|
+
* would make the view rerender every changed line (`#renderIfChanged`'s identity check), so
|
|
14
|
+
* hovering down the rail would rerender lines on every pointer move. Positioned from the
|
|
15
|
+
* HeightMap, so folds inside the region (and elsewhere in the document) are respected, and a
|
|
16
|
+
* region partly or wholly outside the mounted range still lands in the right place.
|
|
17
|
+
*
|
|
18
|
+
* Mounted as the sizer's first child, so it paints behind the gutter, the lines (text, selection
|
|
19
|
+
* highlights, indent guides, which are line backgrounds) and the carets, all of which come after
|
|
20
|
+
* it in document order or sit at a higher z-index.
|
|
21
|
+
*/
|
|
22
|
+
export declare class ScopeFocus {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(core: EditorCore, source: FoldRangeSource, host: ScopeFocusHost);
|
|
25
|
+
/** Adopt the band's stylesheet, insert its element, and start tracking the pointer. */
|
|
26
|
+
mount(root: ShadowRoot, signal: AbortSignal): void;
|
|
27
|
+
}
|