-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheditor.ts
More file actions
774 lines (732 loc) · 23.1 KB
/
Copy patheditor.ts
File metadata and controls
774 lines (732 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/**
* The Posecode code editor: a CodeMirror 6 setup that turns the plain textarea
* into a real editor: syntax highlighting, inline ROM/error squiggles,
* context-aware autocomplete, and hover docs. All language smarts come from
* `posecode-language` (shared with the LSP), so the editor never reimplements them.
*/
import {
EditorState,
StateEffect,
StateField,
Transaction,
} from "@codemirror/state";
import {
EditorView,
keymap,
lineNumbers,
highlightActiveLine,
highlightActiveLineGutter,
drawSelection,
hoverTooltip,
placeholder,
Decoration,
WidgetType,
type DecorationSet,
} from "@codemirror/view";
import {
defaultKeymap,
history,
historyKeymap,
indentWithTab,
} from "@codemirror/commands";
import {
StreamLanguage,
LanguageSupport,
syntaxHighlighting,
HighlightStyle,
bracketMatching,
} from "@codemirror/language";
import {
autocompletion,
completionKeymap,
closeBrackets,
closeBracketsKeymap,
type CompletionContext,
type CompletionResult,
} from "@codemirror/autocomplete";
import { linter, lintGutter, type Diagnostic as CmDiagnostic } from "@codemirror/lint";
import { tags as t } from "@lezer/highlight";
import {
getDiagnostics,
getCompletions,
getHover,
type CompletionKind,
} from "posecode-language";
import {
ACTION_NAMES,
EFFECTOR_NAMES,
GROUND_LOCK_EFFECTOR_NAMES,
JOINT_NAMES,
MODES,
MOVEMENT_KINDS,
PROP_TYPES,
START_POSE_NAMES,
expandJoint,
} from "posecode-parser";
import {
angleRangeFor,
angleTargetAt,
findAngleTargets,
normalizeAngle,
type AngleTarget,
} from "./direct-manipulation.js";
// --- Syntax highlighting ----------------------------------------------------
const KEYWORDS = new Set([
"posecode",
"rig",
"prop",
"pose",
"start",
"clip",
"step",
"repeat",
"ground-lock",
"reach",
"pin",
"grip",
"turn",
"travel",
"cue",
"hold",
]);
const KINDS = new Set<string>(MOVEMENT_KINDS);
const ACTIONS = new Set<string>(ACTION_NAMES);
const ATOMS = new Set([
...MODES,
"ease-in",
"ease-out",
"ease-in-out",
...START_POSE_NAMES,
...PROP_TYPES,
...EFFECTOR_NAMES,
...GROUND_LOCK_EFFECTOR_NAMES,
"humanoid",
]);
const JOINTS = new Set<string>(JOINT_NAMES);
const posecodeStream = StreamLanguage.define<{ inStep: boolean }>({
name: "posecode",
startState: () => ({ inStep: false }),
token(stream) {
if (stream.eatSpace()) return null;
if (stream.match(/^(#|\/\/).*$/)) return "comment";
if (stream.match(/^"(?:[^"\\]|\\.)*"?/)) return "string";
if (stream.match(/^[0-9]*\.?[0-9]+s\b/)) return "number"; // duration (2s)
if (stream.match(/^-?[0-9]*\.?[0-9]+/)) return "number";
if (stream.match(/^[:,=]/)) return "punct";
const m = stream.match(/^[A-Za-z][\w-]*/) as RegExpMatchArray | null;
if (m) {
const w = m[0];
if (KEYWORDS.has(w)) return "kw";
if (KINDS.has(w)) return "kind";
if (ACTIONS.has(w)) return "action";
if (JOINTS.has(w) || /_(left|right)$/.test(w)) return "joint";
if (ATOMS.has(w)) return "atom";
return null;
}
stream.next();
return null;
},
tokenTable: {
kw: t.keyword,
kind: t.typeName,
action: t.operatorKeyword,
joint: t.variableName,
atom: t.atom,
number: t.number,
string: t.string,
comment: t.lineComment,
punct: t.punctuation,
},
languageData: { commentTokens: { line: "#" } },
});
const posecodeHighlight = HighlightStyle.define([
{ tag: t.keyword, color: "var(--accent)", fontWeight: "600" },
{ tag: t.typeName, color: "#79c0ff" },
{ tag: t.operatorKeyword, color: "#ffcc66" },
{ tag: t.variableName, color: "#c0a7ff" },
{ tag: t.atom, color: "#5cd0c0" },
{ tag: t.number, color: "#ff9d6b" },
{ tag: t.string, color: "#d8b48a" },
{ tag: t.lineComment, color: "#5b6675", fontStyle: "italic" },
{ tag: t.punctuation, color: "#6b7785" },
]);
// --- Language service bridges ----------------------------------------------
const posecodeLinter = linter(
(view) => {
const doc = view.state.doc;
return getDiagnostics(doc.toString()).map((d): CmDiagnostic => {
const lineNo = Math.min(Math.max(d.line, 1), doc.lines);
const line = doc.line(lineNo);
return { from: line.from, to: line.to, severity: d.severity, message: d.message };
});
},
{ delay: 300 },
);
const CM_TYPE: Record<CompletionKind, string> = {
keyword: "keyword",
kind: "type",
pose: "constant",
easing: "constant",
joint: "variable",
action: "function",
effector: "constant",
};
function posecodeCompletions(context: CompletionContext): CompletionResult | null {
const line = context.state.doc.lineAt(context.pos);
const items = getCompletions(
context.state.doc.toString(),
line.number - 1,
context.pos - line.from,
);
if (items.length === 0) return null;
const token = context.matchBefore(/[\w-]*/);
if (!context.explicit && token && token.from === token.to) return null;
return {
from: token ? token.from : context.pos,
options: items.map((i) => ({
label: i.label,
type: CM_TYPE[i.kind],
...(i.detail ? { detail: i.detail } : {}),
})),
validFor: /^[\w-]*$/,
};
}
function escapeHtml(s: string): string {
return s.replace(/[&<>]/g, (c) =>
c === "&" ? "&" : c === "<" ? "<" : ">",
);
}
const posecodeHoverTip = hoverTooltip((view, pos) => {
const line = view.state.doc.lineAt(pos);
const info = getHover(view.state.doc.toString(), line.number - 1, pos - line.from);
if (!info) return null;
return {
pos,
create() {
const dom = document.createElement("div");
dom.className = "cm-posecode-hover";
// Contents are markdown from our own vocab (no user free-text): render bold.
dom.innerHTML = escapeHtml(info.contents).replace(
/\*\*(.+?)\*\*/g,
"<strong>$1</strong>",
);
return { dom };
},
};
});
// --- Theme (Kinetic Lab dark) ----------------------------------------------
const posecodeTheme = EditorView.theme(
{
"&": { height: "100%", color: "var(--text)", backgroundColor: "transparent" },
".cm-scroller": {
fontFamily: "var(--mono)",
fontSize: "13.5px",
lineHeight: "1.65",
},
".cm-content": { padding: "16px 0", caretColor: "var(--accent)" },
".cm-gutters": {
backgroundColor: "transparent",
color: "var(--muted)",
border: "none",
paddingLeft: "6px",
},
".cm-activeLine": { backgroundColor: "rgba(255,255,255,0.025)" },
".cm-activeLineGutter": { backgroundColor: "transparent", color: "var(--text-2)" },
"&.cm-focused .cm-cursor": { borderLeftColor: "var(--accent)" },
"&.cm-focused .cm-selectionBackground, .cm-selectionBackground, ::selection": {
backgroundColor: "var(--accent-veil)",
},
".cm-tooltip": {
backgroundColor: "var(--panel-2)",
border: "1px solid var(--border-2)",
borderRadius: "8px",
boxShadow: "var(--shadow-2)",
},
".cm-posecode-hover": {
padding: "8px 11px",
fontFamily: "var(--sans)",
fontSize: "12.5px",
maxWidth: "320px",
color: "var(--text-2)",
},
".cm-posecode-hover strong": { color: "var(--text)" },
".cm-joint-link": {
cursor: "pointer",
borderBottom: "1px dotted rgba(192, 167, 255, 0.72)",
borderRadius: "2px",
transition: "color 120ms ease, background-color 120ms ease",
},
".cm-joint-link:hover": {
color: "#dfd2ff",
backgroundColor: "rgba(192, 167, 255, 0.12)",
},
".cm-joint-selected": {
color: "var(--accent)",
backgroundColor: "rgba(212, 255, 63, 0.11)",
borderBottomColor: "var(--accent)",
},
".cm-angle-control": {
cursor: "pointer",
color: "#ffb184",
borderBottom: "1px dotted rgba(255, 157, 107, 0.78)",
borderRadius: "2px",
},
".cm-angle-control:hover": {
color: "#ffd1b7",
backgroundColor: "rgba(255, 157, 107, 0.12)",
},
".cm-angle-spinner": {
display: "inline-flex",
alignItems: "center",
verticalAlign: "middle",
margin: "0 2px",
height: "25px",
color: "var(--text)",
backgroundColor: "var(--panel-3)",
border: "1px solid var(--accent)",
borderRadius: "3px",
boxShadow: "0 0 0 2px rgba(212, 255, 63, 0.08)",
overflow: "hidden",
},
".cm-angle-input": {
width: "5.2ch",
height: "100%",
padding: "0 2px 0 5px",
border: "0",
outline: "0",
color: "var(--text)",
backgroundColor: "transparent",
fontFamily: "var(--mono)",
fontSize: "12.5px",
textAlign: "right",
},
".cm-angle-degree": {
paddingRight: "3px",
color: "var(--text-2)",
fontSize: "11px",
},
".cm-angle-step": {
width: "22px",
height: "100%",
padding: "0",
border: "0",
borderRadius: "0",
color: "var(--text-2)",
backgroundColor: "transparent",
fontFamily: "var(--mono)",
fontSize: "14px",
cursor: "pointer",
},
".cm-angle-step:hover": {
color: "var(--bg)",
backgroundColor: "var(--accent)",
},
".cm-angle-step:focus-visible, .cm-angle-input:focus-visible": {
outline: "1px solid var(--text)",
outlineOffset: "-2px",
},
".cm-tooltip-autocomplete > ul > li[aria-selected]": {
backgroundColor: "var(--accent-veil)",
color: "var(--text)",
},
".cm-tooltip-autocomplete > ul > li": { fontFamily: "var(--mono)" },
".cm-completionDetail": { color: "var(--muted)", fontStyle: "normal" },
},
{ dark: true },
);
// --- Active-phase highlight -------------------------------------------------
// As the figure animates, the playground highlights the step block driving the
// current moment, making the text↔motion mapping visible. Lines are 1-based.
const setPhaseHighlight = StateEffect.define<{ from: number; to: number } | null>();
const phaseLineDeco = Decoration.line({ class: "cm-phase-active" });
const phaseHighlightField = StateField.define<DecorationSet>({
create: () => Decoration.none,
update(deco, tr) {
deco = deco.map(tr.changes);
for (const effect of tr.effects) {
if (!effect.is(setPhaseHighlight)) continue;
const range = effect.value;
if (!range) {
deco = Decoration.none;
continue;
}
const lastLine = tr.state.doc.lines;
const marks = [];
for (let n = Math.max(1, range.from); n <= Math.min(range.to, lastLine); n++) {
marks.push(phaseLineDeco.range(tr.state.doc.line(n).from));
}
deco = Decoration.set(marks, true);
}
return deco;
},
provide: (f) => EditorView.decorations.from(f),
});
// --- Direct manipulation ---------------------------------------------------
// Joint names and authored degree values are live controls rather than inert
// syntax. Clicking a joint selects its concrete bones in the viewer; clicking
// the angle replaces only that number with a compact, ROM-aware spinner.
const setSelectedJoint = StateEffect.define<string | null>();
const setActiveAngle = StateEffect.define<AngleTarget | null>();
const selectedJointField = StateField.define<string | null>({
create: () => null,
update(selected, tr) {
for (const effect of tr.effects) {
if (effect.is(setSelectedJoint)) selected = effect.value;
}
if (
selected &&
tr.docChanged &&
!findAngleTargets(tr.state.doc.toString()).some(
(target) => target.joint === selected,
)
) {
return null;
}
return selected;
},
});
const activeAngleField = StateField.define<AngleTarget | null>({
create: () => null,
update(active, tr) {
if (active && tr.docChanged) {
const mapped = tr.changes.mapPos(active.angleFrom, 1);
active = angleTargetAt(tr.state.doc.toString(), mapped, "angle");
}
for (const effect of tr.effects) {
if (effect.is(setActiveAngle)) active = effect.value;
}
return active;
},
});
function replaceActiveAngle(view: EditorView, requested: number): void {
const active = view.state.field(activeAngleField);
if (!active || !Number.isFinite(requested)) return;
const range = angleRangeFor(active.joint, active.action);
if (!range) return;
const insert = normalizeAngle(requested, range);
const current = view.state.doc.sliceString(active.angleFrom, active.angleTo);
if (insert === current) return;
view.dispatch({
changes: {
from: active.angleFrom,
to: active.angleTo,
insert,
},
effects: setActiveAngle.of({
...active,
degrees: Number(insert),
angleTo: active.angleFrom + insert.length,
}),
annotations: Transaction.userEvent.of("input"),
});
}
class AngleSpinnerWidget extends WidgetType {
constructor(
readonly target: AngleTarget,
readonly min: number,
readonly max: number,
) {
super();
}
eq(other: AngleSpinnerWidget): boolean {
return (
other.target.joint === this.target.joint &&
other.target.action === this.target.action &&
other.target.degrees === this.target.degrees &&
other.min === this.min &&
other.max === this.max
);
}
toDOM(view: EditorView): HTMLElement {
const control = document.createElement("span");
control.className = "cm-angle-spinner";
control.title = `Safe range: ${this.min}–${this.max}°`;
const minus = document.createElement("button");
minus.type = "button";
minus.className = "cm-angle-step";
minus.textContent = "−";
minus.setAttribute("aria-label", `Decrease ${this.target.joint} angle`);
const input = document.createElement("input");
input.type = "number";
input.className = "cm-angle-input";
input.min = String(this.min);
input.max = String(this.max);
input.step = "1";
input.value = String(this.target.degrees);
input.setAttribute(
"aria-label",
`${this.target.joint} ${this.target.action} angle in degrees; safe range ${this.min} to ${this.max}`,
);
const degree = document.createElement("span");
degree.className = "cm-angle-degree";
degree.textContent = "°";
degree.setAttribute("aria-hidden", "true");
const plus = document.createElement("button");
plus.type = "button";
plus.className = "cm-angle-step";
plus.textContent = "+";
plus.setAttribute("aria-label", `Increase ${this.target.joint} angle`);
const step = (delta: number): void => {
const current = Number(input.value);
replaceActiveAngle(
view,
(Number.isFinite(current) ? current : this.target.degrees) + delta,
);
};
minus.addEventListener("click", () => step(-1));
plus.addEventListener("click", () => step(1));
input.addEventListener("input", () => {
if (input.value !== "") replaceActiveAngle(view, Number(input.value));
});
input.addEventListener("change", () => {
if (input.value === "") input.value = String(this.target.degrees);
});
input.addEventListener("keydown", (event) => {
if (event.key !== "Escape") return;
event.preventDefault();
view.dispatch({ effects: setActiveAngle.of(null) });
view.focus();
});
control.append(minus, input, degree, plus);
window.setTimeout(() => {
input.focus();
input.select();
}, 0);
return control;
}
updateDOM(dom: HTMLElement): boolean {
const input = dom.querySelector<HTMLInputElement>(".cm-angle-input");
if (!input) return false;
input.min = String(this.min);
input.max = String(this.max);
if (document.activeElement !== input) {
input.value = String(this.target.degrees);
}
dom.title = `Safe range: ${this.min}–${this.max}°`;
return true;
}
ignoreEvent(): boolean {
return true;
}
}
const directManipulationDecorations = EditorView.decorations.compute(
["doc", selectedJointField, activeAngleField],
(state) => {
const selected = state.field(selectedJointField);
const active = state.field(activeAngleField);
const marks = [];
for (const target of findAngleTargets(state.doc.toString())) {
marks.push(
Decoration.mark({
class: `cm-joint-link${
selected === target.joint ? " cm-joint-selected" : ""
}`,
attributes: {
"data-posecode-joint": target.joint,
title: `Select ${target.joint} in the 3D viewer`,
},
}).range(target.jointFrom, target.jointTo),
);
if (
!active ||
active.angleFrom !== target.angleFrom ||
active.angleTo !== target.angleTo
) {
marks.push(
Decoration.mark({
class: "cm-angle-control",
attributes: {
"data-posecode-angle": "true",
title: `Adjust ${target.joint} ${target.action}`,
},
}).range(target.angleFrom, target.angleTo),
);
}
}
return Decoration.set(marks, true);
},
);
const angleSpinnerDecoration = EditorView.decorations.compute(
[activeAngleField],
(state) => {
const active = state.field(activeAngleField);
if (!active) return Decoration.none;
const range = angleRangeFor(active.joint, active.action);
if (!range) return Decoration.none;
return Decoration.set([
Decoration.replace({
widget: new AngleSpinnerWidget(active, range.min, range.max),
}).range(active.angleFrom, active.angleTo),
]);
},
);
// --- Public API -------------------------------------------------------------
export interface PosecodeEditor {
getValue(): string;
setValue(doc: string): void;
focus(): void;
/** Reveal and focus a direct angle spinner for the requested joint action. */
focusAngleControl(joint: string, action: string): boolean;
/** Highlight an inclusive 1-based line range as the active phase; null clears. */
highlightPhase(from: number | null, to?: number): void;
}
export interface PosecodeEditorOptions {
doc: string;
onChange: (
value: string,
userInitiated: boolean,
context?: { previewLine: number },
) => void;
onJointSelect?: (joint: string | null, boneIds: readonly string[]) => void;
}
export function createPosecodeEditor(
parent: HTMLElement,
opts: PosecodeEditorOptions,
): PosecodeEditor {
const view = new EditorView({
parent,
state: EditorState.create({
doc: opts.doc,
extensions: [
lineNumbers(),
highlightActiveLine(),
highlightActiveLineGutter(),
drawSelection(),
history(),
bracketMatching(),
closeBrackets(),
new LanguageSupport(posecodeStream),
syntaxHighlighting(posecodeHighlight),
phaseHighlightField,
selectedJointField,
activeAngleField,
directManipulationDecorations,
angleSpinnerDecoration,
autocompletion({ override: [posecodeCompletions], icons: false }),
posecodeLinter,
lintGutter(),
posecodeHoverTip,
posecodeTheme,
// Shown only while the document is empty: guides the paste-from-LLM flow.
placeholder(
'Paste a movement from your AI chat here, or start typing:\nposecode exercise "My movement"',
),
EditorView.lineWrapping,
EditorView.domEventHandlers({
click(event, targetView) {
const element = (event.target as Element | null)?.closest<HTMLElement>(
"[data-posecode-joint], [data-posecode-angle]",
);
if (!element || !targetView.dom.contains(element)) {
targetView.dispatch({
effects: [
setSelectedJoint.of(null),
setActiveAngle.of(null),
],
});
opts.onJointSelect?.(null, []);
return false;
}
const part = element.dataset.posecodeAngle ? "angle" : "joint";
const position = targetView.posAtDOM(element, 0);
const target = angleTargetAt(
targetView.state.doc.toString(),
position,
part,
);
if (!target) return false;
const selectionFrom =
part === "angle" ? target.angleFrom : target.jointFrom;
const selectionTo =
part === "angle" ? target.angleTo : target.jointTo;
targetView.dispatch({
selection: { anchor: selectionFrom, head: selectionTo },
effects: [
setSelectedJoint.of(target.joint),
setActiveAngle.of(part === "angle" ? target : null),
],
});
opts.onJointSelect?.(target.joint, expandJoint(target.joint));
return true;
},
}),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap,
...historyKeymap,
...completionKeymap,
indentWithTab,
]),
EditorView.updateListener.of((u) => {
if (u.docChanged) {
const userInitiated = u.transactions.some(
(transaction) =>
transaction.annotation(Transaction.userEvent) !== undefined,
);
// Spinner edits are different from ordinary source typing: the
// author is manipulating one key pose and expects to see that pose
// immediately. Pass its resulting source line to the playground;
// main.ts will seek there after rebuilding the timeline.
const directAngleEdit = u.transactions.some((transaction) =>
transaction.effects.some(
(effect) => effect.is(setActiveAngle) && effect.value !== null,
),
);
const activeAngle = directAngleEdit
? u.state.field(activeAngleField)
: null;
opts.onChange(
u.state.doc.toString(),
userInitiated,
activeAngle
? { previewLine: u.state.doc.lineAt(activeAngle.angleFrom).number }
: undefined,
);
}
}),
],
}),
});
// Dev-only handle for preview/E2E testing; stripped from production builds.
if (import.meta.env.DEV) {
(globalThis as unknown as { posecodeView?: EditorView }).posecodeView = view;
}
return {
getValue: () => view.state.doc.toString(),
setValue: (doc: string) => {
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: doc },
effects: [
setSelectedJoint.of(null),
setActiveAngle.of(null),
],
});
opts.onJointSelect?.(null, []);
},
focus: () => view.focus(),
focusAngleControl: (joint: string, action: string) => {
const target = findAngleTargets(view.state.doc.toString()).find(
(candidate) =>
candidate.joint === joint && candidate.action === action,
);
if (!target) return false;
view.dispatch({
selection: { anchor: target.angleFrom, head: target.angleTo },
effects: [
setSelectedJoint.of(target.joint),
setActiveAngle.of(target),
EditorView.scrollIntoView(target.angleFrom, { y: "center" }),
],
});
opts.onJointSelect?.(target.joint, expandJoint(target.joint));
return true;
},
highlightPhase: (from: number | null, to?: number) => {
view.dispatch({
effects: setPhaseHighlight.of(
from === null ? null : { from, to: to ?? from },
),
});
},
};
}