@johnoscott/isometric-engine 0.0.0-stage → 0.9.0
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/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
// The tree: the file's outline (src/iso-editor/outline.ts) with CRUD on every node, in the Files-tree look (.frow,
|
|
2
|
+
// --tree-indent). What the domain's objects are called, their icons, what "New …" offers and the tags' pills are the
|
|
3
|
+
// kit's (TreeKit: the domain's outline, commands and words). Every change is a command on the one model, so the text, the tree and (E4) the canvas
|
|
4
|
+
// stay one document with one undo stack.
|
|
5
|
+
// Keyboard: ↑ ↓ (⇧ extends), → ← (open, close, child, parent), Home End, Enter (edit a value, or open), F2 rename,
|
|
6
|
+
// Delete, ⌘D duplicate, ⌘↑ select parent, + add, Shift-F10 or the menu key for the menu, letters to jump.
|
|
7
|
+
// Mouse: click, ⇧-click a range, ⌘-click to add or remove, double-click to edit, drag to reorder, right-click.
|
|
8
|
+
|
|
9
|
+
import { later, isTyping } from './later.ts';
|
|
10
|
+
import { h, icon, type IconName } from './icons.ts';
|
|
11
|
+
import { openMenu, type MenuItem } from './menu.ts';
|
|
12
|
+
import { ask } from './dialog.ts';
|
|
13
|
+
import { flatten, type OutlineNode } from '../outline.ts';
|
|
14
|
+
import type { EditorModel, Command } from '../model.ts';
|
|
15
|
+
import type { EditorDoc } from '../doc.ts';
|
|
16
|
+
import type { SchemaNav } from '../schema-nav.ts';
|
|
17
|
+
import type { Problem } from '../validate.ts';
|
|
18
|
+
import type { Commands } from '../commands.ts';
|
|
19
|
+
import type { FileRole, ObjectKindInfo, RefSlot } from '../adapter.ts';
|
|
20
|
+
import { applyStep } from '../splice.ts';
|
|
21
|
+
import { toPointer, fromPointer, isPrefix, type Path } from '../path.ts';
|
|
22
|
+
|
|
23
|
+
/** a "New …" entry for a kind's group: its words, its icon, and what it starts as (default: the kind's) */
|
|
24
|
+
export interface NewItem { label: string; icon: IconName; desc?: string; template?: () => unknown; stem?: string }
|
|
25
|
+
/** an entry an entry group offers: its words, its icon, its key (made unique) and its value */
|
|
26
|
+
export interface EntryItem { label: string; icon: IconName; entry: string; value: unknown; cmdLabel?: string; when?: (data: Record<string, unknown>) => boolean }
|
|
27
|
+
|
|
28
|
+
/** what the tree shows of the domain, in its words */
|
|
29
|
+
export interface TreeUi {
|
|
30
|
+
/** the tree's aria-label */
|
|
31
|
+
label: string;
|
|
32
|
+
rootIcon: IconName;
|
|
33
|
+
kindIcon(kind: string): IconName;
|
|
34
|
+
/** a top-level key's icon, if it has one */
|
|
35
|
+
keyIcon(key: string): IconName | undefined;
|
|
36
|
+
/** an entry group's entries' icon */
|
|
37
|
+
entryIcon(group: string): IconName;
|
|
38
|
+
/** a kind's group's "New …" entries, in order */
|
|
39
|
+
newItems(kind: string): NewItem[];
|
|
40
|
+
/** an entry group's own add items (its entries made from the file: owned, not yet entered) */
|
|
41
|
+
entryItems?(group: Path, data: Record<string, unknown>, owned: (kind: string) => string[]): EntryItem[];
|
|
42
|
+
/** a tag's pill (none: no pill) */
|
|
43
|
+
tagPill(tag: string): { title: string } | undefined;
|
|
44
|
+
/** the name rule, in words */
|
|
45
|
+
nameHint: string;
|
|
46
|
+
text: { root: string; rootAdd: string; renameWhy: string; deleteWhy: string; duplicateNote: string; duplicateWhy: string };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** what the tree is given of the domain */
|
|
50
|
+
export interface TreeKit {
|
|
51
|
+
outline(d: EditorDoc, role: FileRole, tags: Map<string, string>): OutlineNode;
|
|
52
|
+
commands: Pick<Commands, 'setCmd' | 'insertCmd' | 'insertItemCmd' | 'deleteCmd' | 'renameCmd' | 'renameKeyCmd' | 'duplicateCmd' | 'moveCmd' | 'moveItemCmd' | 'referencesTo' | 'describe' | 'freshName'>;
|
|
53
|
+
slotAt(ptr: string): RefSlot | undefined;
|
|
54
|
+
/** the kinds, in the outline's order */
|
|
55
|
+
kinds: ObjectKindInfo[];
|
|
56
|
+
/** named maps that aren't kinds (their names a pool of their own) */
|
|
57
|
+
entryGroups: Path[];
|
|
58
|
+
/** what a name must be */
|
|
59
|
+
nameRule: RegExp;
|
|
60
|
+
ui: TreeUi;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface TreeOpts {
|
|
64
|
+
kit: TreeKit;
|
|
65
|
+
nav(): SchemaNav;
|
|
66
|
+
problems(): Problem[];
|
|
67
|
+
role(): FileRole;
|
|
68
|
+
/** each object's tag as the reader resolved it, with the base files merged */
|
|
69
|
+
lifetimes(): Map<string, string>;
|
|
70
|
+
/** show a path in the text pane and give it the focus */
|
|
71
|
+
showInText(path: Path): void;
|
|
72
|
+
/** Copy, Cut and Paste for the menu */
|
|
73
|
+
clipItems?(): import('./menu.ts').MenuItem[];
|
|
74
|
+
/** a message for the person (an edit that can't be made) */
|
|
75
|
+
note(text: string, tone?: 'info' | 'warn' | 'bad'): void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Editing = { id: string; mode: 'rename' | 'value' } | { id: string; mode: 'new'; section?: string; mapPath: Path; template: () => unknown; value?: unknown };
|
|
79
|
+
|
|
80
|
+
export function treeView(el: HTMLElement, doc: EditorModel, opts: TreeOpts) {
|
|
81
|
+
const K = opts.kit, ui = K.ui;
|
|
82
|
+
const { setCmd, insertCmd, insertItemCmd, deleteCmd, renameCmd, renameKeyCmd, duplicateCmd, moveCmd, moveItemCmd, referencesTo, describe, freshName } = K.commands;
|
|
83
|
+
const kindOf = (k: string) => K.kinds.find(x => x.kind === k)!;
|
|
84
|
+
const isRoot = (n: OutlineNode) => n.id === '';
|
|
85
|
+
const inGroup = (p: Path) => K.entryGroups.some(g => p.length === g.length + 1 && isPrefix(g, p));
|
|
86
|
+
const isGroupPath = (p: Path) => K.entryGroups.some(g => g.length === p.length && isPrefix(g, p));
|
|
87
|
+
const expanded = new Set<string>(['', ...K.kinds.map(k => toPointer(k.section)), ...K.entryGroups.map(g => toPointer(g))]);
|
|
88
|
+
let focused = '';
|
|
89
|
+
let anchor = '';
|
|
90
|
+
let editing: Editing | undefined;
|
|
91
|
+
let rows: { n: OutlineNode; depth: number; parent?: OutlineNode }[] = [];
|
|
92
|
+
let byId = new Map<string, { n: OutlineNode; depth: number; parent?: OutlineNode }>();
|
|
93
|
+
let typeahead = '', typeaheadAt = 0;
|
|
94
|
+
const selected = () => new Set(doc.selection.map(toPointer));
|
|
95
|
+
const readOnly = () => !doc.doc.ok;
|
|
96
|
+
|
|
97
|
+
el.setAttribute('role', 'tree');
|
|
98
|
+
el.setAttribute('aria-label', ui.label);
|
|
99
|
+
el.setAttribute('aria-multiselectable', 'true');
|
|
100
|
+
el.classList.add('tree');
|
|
101
|
+
|
|
102
|
+
// ---------- painting ----------
|
|
103
|
+
function paint() {
|
|
104
|
+
const root = K.outline(doc.goodDoc, opts.role(), opts.lifetimes());
|
|
105
|
+
rows = [];
|
|
106
|
+
const walk = (n: OutlineNode, depth: number, parent?: OutlineNode) => {
|
|
107
|
+
rows.push({ n, depth, parent });
|
|
108
|
+
if (expanded.has(n.id)) for (const c of n.children) walk(c, depth + 1, n);
|
|
109
|
+
if (editing?.mode === 'new' && toPointer(editing.mapPath) === n.id && expanded.has(n.id)) rows.push({ n: { id: editing.id, path: [...editing.mapPath, ''], kind: 'resource', label: '', shape: 'scalar', children: [] }, depth: depth + 1, parent: n });
|
|
110
|
+
};
|
|
111
|
+
walk(root, 0);
|
|
112
|
+
byId = new Map(rows.map(r => [r.n.id, r]));
|
|
113
|
+
if (!byId.has(focused)) focused = rows[0]?.n.id ?? '';
|
|
114
|
+
const sel = selected();
|
|
115
|
+
const counts = problemCounts();
|
|
116
|
+
const frag = document.createDocumentFragment();
|
|
117
|
+
if (readOnly()) frag.append(h('div', { class: 'callout compact warn tree-note', role: 'status' }, h('span', { class: 'c-body' }, `Fix the YAML at line ${doc.doc.doc.errors[0] ? doc.doc.pos(doc.doc.doc.errors[0].pos[0]).line : '?'} to edit here; the tree shows the last version that read cleanly.`)));
|
|
118
|
+
for (const { n, depth } of rows) frag.append(row(n, depth, sel.has(n.id), counts.get(n.id)));
|
|
119
|
+
// (0.8.2) the rows are made anew: focus that was in the tree stays in it, on the focused row (a repaint after an
|
|
120
|
+
// edit, e.g. when the reader's problems arrive, would otherwise drop it to the page, and ⌘Z would need a click)
|
|
121
|
+
const hadFocus = el.contains(document.activeElement);
|
|
122
|
+
el.replaceChildren(frag);
|
|
123
|
+
const f = el.querySelector<HTMLElement>(`[data-id="${CSS.escape(focused)}"]`);
|
|
124
|
+
if (f) f.tabIndex = 0;
|
|
125
|
+
const inp = el.querySelector<HTMLInputElement | HTMLSelectElement>('.tedit');
|
|
126
|
+
if (inp) { inp.focus(); if (inp instanceof HTMLInputElement) inp.select(); }
|
|
127
|
+
else if (hadFocus && f && !el.contains(document.activeElement)) f.focus({ preventScroll: true });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function problemCounts(): Map<string, { e: number; w: number }> {
|
|
131
|
+
const m = new Map<string, { e: number; w: number }>();
|
|
132
|
+
for (const p of opts.problems()) {
|
|
133
|
+
if (p.file) continue;
|
|
134
|
+
// every ancestor shown counts it
|
|
135
|
+
const parts = fromPointer(p.path);
|
|
136
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
137
|
+
const id = toPointer(parts.slice(0, i));
|
|
138
|
+
const c = m.get(id) ?? { e: 0, w: 0 };
|
|
139
|
+
if (p.severity === 'error') c.e++; else c.w++;
|
|
140
|
+
m.set(id, c);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return m;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function iconFor(n: OutlineNode): IconName {
|
|
147
|
+
if (isRoot(n)) return ui.rootIcon;
|
|
148
|
+
if (n.section) return ui.kindIcon(n.section);
|
|
149
|
+
const k = String(n.path[n.path.length - 1] ?? '');
|
|
150
|
+
if (n.path.length === 1 && ui.keyIcon(k)) return ui.keyIcon(k)!;
|
|
151
|
+
if (inGroup(n.path)) return ui.entryIcon(String(n.path[0]));
|
|
152
|
+
return n.shape === 'list' ? 'list' : n.shape === 'map' ? 'list-tree' : 'circle-dot';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function row(n: OutlineNode, depth: number, isSel: boolean, count?: { e: number; w: number }): HTMLElement {
|
|
156
|
+
const open = expanded.has(n.id);
|
|
157
|
+
const hasKids = n.children.length > 0 || n.shape === 'missing' && false;
|
|
158
|
+
const r = h('div', {
|
|
159
|
+
class: `trow k-${n.kind}${isSel ? ' selected' : ''}${n.shape === 'missing' ? ' missing' : ''}${n.ext ? ' ext' : ''}`,
|
|
160
|
+
role: 'treeitem', 'aria-level': depth + 1, 'aria-selected': String(isSel), 'aria-expanded': n.children.length ? String(open) : undefined,
|
|
161
|
+
tabindex: '-1', 'data-id': n.id, draggable: canDrag(n) ? 'true' : undefined, 'aria-label': ariaLabel(n),
|
|
162
|
+
});
|
|
163
|
+
r.style.setProperty('--depth', String(depth));
|
|
164
|
+
const tw = h('span', { class: 'twisty', 'aria-hidden': 'true' }, hasKids ? icon(open ? 'chevron-down' : 'chevron-right') : '');
|
|
165
|
+
tw.addEventListener('click', e => { e.stopPropagation(); toggle(n.id); });
|
|
166
|
+
r.append(tw, icon(iconFor(n), 'ticon'));
|
|
167
|
+
const ed = editing && editing.id === n.id ? editing : undefined;
|
|
168
|
+
if (ed && (ed.mode === 'rename' || ed.mode === 'new')) r.append(nameInput(n, ed));
|
|
169
|
+
else r.append(h('span', { class: 'tlabel' }, n.label || (n.kind === 'item' ? '""' : '')));
|
|
170
|
+
if (ed?.mode === 'value') r.append(valueInput(n));
|
|
171
|
+
else if (n.value !== undefined && n.kind !== 'item') r.append(h('span', { class: 'tval' }, n.value));
|
|
172
|
+
else if (n.shape === 'missing') r.append(h('span', { class: 'tval none' }, 'none'));
|
|
173
|
+
else if (n.kind === 'group' && n.children.length) r.append(h('span', { class: 'tcount' }, String(n.children.length)));
|
|
174
|
+
const pill = n.lifetime ? ui.tagPill(n.lifetime) : undefined;
|
|
175
|
+
if (pill) r.append(h('span', { class: `tpill l-${n.lifetime}`, title: pill.title }, n.lifetime!));
|
|
176
|
+
if (count?.e) r.append(h('span', { class: 'count bad', title: `${count.e} error${count.e > 1 ? 's' : ''}` }, String(count.e)));
|
|
177
|
+
else if (count?.w) r.append(h('span', { class: 'count warn', title: `${count.w} warning${count.w > 1 ? 's' : ''}` }, String(count.w)));
|
|
178
|
+
if (!readOnly() && addItems(n).length) {
|
|
179
|
+
const add = h('button', { type: 'button', class: 'tadd btn sm quiet icon-only', tabindex: '-1', 'aria-label': `Add to ${n.label || ui.text.rootAdd}`, title: 'Add…' }, icon('plus'));
|
|
180
|
+
add.addEventListener('click', e => { e.stopPropagation(); setFocus(n.id); openMenu(addItems(n), add, `Add to ${n.label}`); });
|
|
181
|
+
r.append(add);
|
|
182
|
+
}
|
|
183
|
+
return r;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const ariaLabel = (n: OutlineNode) => [n.kind === 'group' ? `${n.label}, ${n.shape === 'missing' ? 'none' : n.children.length}` : n.label, n.value !== undefined && n.kind !== 'item' ? `: ${n.value}` : '', n.lifetime && n.lifetime !== 'run' ? `, ${n.lifetime}` : ''].join('');
|
|
187
|
+
|
|
188
|
+
// ---------- expanding, focus, selection ----------
|
|
189
|
+
function toggle(id: string, to?: boolean) {
|
|
190
|
+
const want = to ?? !expanded.has(id);
|
|
191
|
+
if (want) expanded.add(id); else expanded.delete(id);
|
|
192
|
+
paint();
|
|
193
|
+
}
|
|
194
|
+
function setFocus(id: string) {
|
|
195
|
+
focused = id;
|
|
196
|
+
for (const x of el.querySelectorAll<HTMLElement>('.trow')) x.tabIndex = x.dataset.id === id ? 0 : -1;
|
|
197
|
+
const r = el.querySelector<HTMLElement>(`[data-id="${CSS.escape(id)}"]`);
|
|
198
|
+
r?.focus({ preventScroll: true });
|
|
199
|
+
r?.scrollIntoView({ block: 'nearest' });
|
|
200
|
+
}
|
|
201
|
+
function selectIds(ids: string[], focusId = ids[ids.length - 1]) {
|
|
202
|
+
// the focus moves to the tree first, so the text pane (no longer focused) moves its caret to the selection
|
|
203
|
+
if (focusId !== undefined) { focused = focusId; setFocus(focused); }
|
|
204
|
+
doc.select(ids.map(fromPointer), 'tree');
|
|
205
|
+
paint();
|
|
206
|
+
setFocus(focused);
|
|
207
|
+
}
|
|
208
|
+
function clickSelect(id: string, e: { shiftKey: boolean; metaKey: boolean; ctrlKey: boolean }) {
|
|
209
|
+
if (e.shiftKey && anchor) {
|
|
210
|
+
const ids = rows.map(r => r.n.id);
|
|
211
|
+
const a = ids.indexOf(anchor), b = ids.indexOf(id);
|
|
212
|
+
if (a >= 0 && b >= 0) { selectIds(ids.slice(Math.min(a, b), Math.max(a, b) + 1), id); return; }
|
|
213
|
+
}
|
|
214
|
+
if (e.metaKey || e.ctrlKey) {
|
|
215
|
+
const s = selected();
|
|
216
|
+
if (s.has(id)) s.delete(id); else s.add(id);
|
|
217
|
+
anchor = id;
|
|
218
|
+
selectIds([...s], id);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
anchor = id;
|
|
222
|
+
selectIds([id]);
|
|
223
|
+
}
|
|
224
|
+
/** reveal a path chosen elsewhere: open its ancestors, mark it, scroll to it (without taking the focus) */
|
|
225
|
+
function reveal(paths: Path[]) {
|
|
226
|
+
for (const p of paths) for (let i = 0; i < p.length; i++) expanded.add(toPointer(p.slice(0, i)));
|
|
227
|
+
if (paths[0]) {
|
|
228
|
+
// the nearest node the outline has (a path inside a scalar shows its key)
|
|
229
|
+
let id = toPointer(paths[0]);
|
|
230
|
+
paint();
|
|
231
|
+
while (!byId.has(id) && id) id = id.replace(/\/[^/]*$/, '');
|
|
232
|
+
focused = id;
|
|
233
|
+
}
|
|
234
|
+
paint();
|
|
235
|
+
el.querySelector<HTMLElement>(`[data-id="${CSS.escape(focused)}"]`)?.scrollIntoView({ block: 'nearest' });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ---------- what can be done to a node ----------
|
|
239
|
+
const canDrag = (n: OutlineNode) => !readOnly() && !isRoot(n) && n.kind !== 'group' && n.path.length > 1 || (n.kind === 'item');
|
|
240
|
+
const isResource = (n: OutlineNode) => n.kind === 'resource' && !!n.section;
|
|
241
|
+
const isNameMap = (n: OutlineNode) => n.kind === 'group' && (!!n.section || isGroupPath(n.path));
|
|
242
|
+
|
|
243
|
+
function addItems(n: OutlineNode): MenuItem[] {
|
|
244
|
+
if (readOnly()) return [];
|
|
245
|
+
const nav = opts.nav();
|
|
246
|
+
const items: MenuItem[] = [];
|
|
247
|
+
const data = doc.goodDoc.data as Record<string, unknown> | undefined;
|
|
248
|
+
if (isRoot(n)) {
|
|
249
|
+
for (const k of K.kinds) {
|
|
250
|
+
if (opts.role() !== 'main') break;
|
|
251
|
+
items.push({ label: `New ${k.singular}`, icon: ui.kindIcon(k.kind), onSelect: () => newResource(k.kind) });
|
|
252
|
+
}
|
|
253
|
+
const present = new Set(n.children.filter(c => c.shape !== 'missing').map(c => String(c.path[0])));
|
|
254
|
+
const sectionTops = new Set(K.kinds.map(k => String(k.section[0])));
|
|
255
|
+
const keys = nav.keys([], (data ?? {}) as Record<string, unknown>).filter(k => !present.has(k.name) && !sectionTops.has(k.name) && !(data && k.name in data));
|
|
256
|
+
if (keys.length) items.push({ sep: true }, { heading: 'Settings' }, ...keys.map(k => ({ label: k.name, desc: firstLine(k.doc), onSelect: () => addKey([], k.name) })));
|
|
257
|
+
return items;
|
|
258
|
+
}
|
|
259
|
+
if (n.kind === 'group' && n.section) {
|
|
260
|
+
const s = n.section;
|
|
261
|
+
for (const x of ui.newItems(s)) items.push({ label: x.label, icon: x.icon, desc: x.desc, onSelect: () => newResource(s, x.template, x.stem) });
|
|
262
|
+
return items;
|
|
263
|
+
}
|
|
264
|
+
if (isGroupPath(n.path) && ui.entryItems) return entryItems(n.path);
|
|
265
|
+
if (n.shape === 'missing') return [{ label: `Add ${n.label}`, icon: 'plus', onSelect: () => addKey([], String(n.path[0]), true) }];
|
|
266
|
+
if (n.shape === 'list') {
|
|
267
|
+
const slot = K.slotAt(toPointer([...n.path, 0]));
|
|
268
|
+
const have = new Set(n.children.map(c => c.label));
|
|
269
|
+
if (slot) {
|
|
270
|
+
for (const s of slot.targets) for (const name of namesIn(s)) if (!have.has(slot.write ? slot.write(name) : name)) items.push({ label: slot.write ? slot.write(name) : name, icon: ui.kindIcon(s), desc: `${kindOf(s).singular}`, onSelect: () => run(insertItemCmd(n.path, slot.write ? slot.write(name) : name, undefined, `Add ${name} to ${describe(n.path)}`)) });
|
|
271
|
+
for (const e of slot.extra ?? []) if (!have.has(e)) items.push({ label: e, onSelect: () => run(insertItemCmd(n.path, e, undefined, `Add ${e} to ${describe(n.path)}`)) });
|
|
272
|
+
}
|
|
273
|
+
for (const v of nav.values([...n.path, 0])) if (!have.has(v)) items.push({ label: v, onSelect: () => run(insertItemCmd(n.path, v)) });
|
|
274
|
+
items.push({ label: 'New item', icon: 'plus', onSelect: () => { run(insertItemCmd(n.path, '')); const p = [...n.path, n.children.length]; startEdit(toPointer(p), 'value'); } });
|
|
275
|
+
return items;
|
|
276
|
+
}
|
|
277
|
+
if (n.shape === 'map' || n.kind === 'resource') {
|
|
278
|
+
const here = getIn(data, n.path);
|
|
279
|
+
const present = here && typeof here === 'object' && !Array.isArray(here) ? here as Record<string, unknown> : {};
|
|
280
|
+
for (const k of nav.keys(n.path, present)) if (!(k.name in present)) items.push({ label: k.name, desc: firstLine(k.doc), onSelect: () => addKey(n.path, k.name) });
|
|
281
|
+
return items;
|
|
282
|
+
}
|
|
283
|
+
return items;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** an entry group's add items, as the kit says (each entry's key made unique in the group) */
|
|
287
|
+
function entryItems(group: Path): MenuItem[] {
|
|
288
|
+
const data = (doc.goodDoc.data ?? {}) as Record<string, unknown>;
|
|
289
|
+
const here = (getIn(data, group) ?? {}) as Record<string, unknown>;
|
|
290
|
+
return ui.entryItems!(group, data, ownedIn).filter(x => !x.when || x.when(data))
|
|
291
|
+
.map(x => ({ label: x.label, icon: x.icon, onSelect: () => { run(insertCmd(group, uniqueKey(Object.keys(here), x.entry), x.value, x.cmdLabel ? { label: x.cmdLabel } : {})); } }));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const firstLine = (s?: string) => s?.split(/(?<=\.)\s/)[0]?.replace(/`/g, '');
|
|
295
|
+
const getIn = (o: unknown, p: Path) => p.reduce<unknown>((x, k) => (x && typeof x === 'object' ? (x as Record<string, unknown>)[k as string] : undefined), o);
|
|
296
|
+
const namesIn = (s: string) => { const m = getIn(doc.goodDoc.data, kindOf(s).section); return m && typeof m === 'object' ? Object.keys(m).filter(k => !k.startsWith('x-')) : []; };
|
|
297
|
+
/** a kind's objects this file owns (not external) */
|
|
298
|
+
const ownedIn = (s: string) => { const m = getIn(doc.goodDoc.data, kindOf(s).section) as Record<string, Record<string, unknown>> | undefined; return m ? Object.keys(m).filter(k => !k.startsWith('x-') && !(m[k] && typeof m[k] === 'object' && 'external' in m[k])) : []; };
|
|
299
|
+
const uniqueKey = (have: string[], stem: string) => { if (!have.includes(stem)) return stem; let i = 2; while (have.includes(`${stem}-${i}`)) i++; return `${stem}-${i}`; };
|
|
300
|
+
|
|
301
|
+
function run(c: Command): boolean {
|
|
302
|
+
try { return doc.apply(c, 'tree'); } catch (e) { opts.note(String((e as Error).message), 'bad'); return false; }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function addKey(mapPath: Path, key: string, empty = false) {
|
|
306
|
+
const nav = opts.nav();
|
|
307
|
+
const value = empty ? {} : nav.defaultFor([...mapPath, key]);
|
|
308
|
+
if (run(insertCmd(mapPath, key, value))) {
|
|
309
|
+
expanded.add(toPointer(mapPath));
|
|
310
|
+
const id = toPointer([...mapPath, key]);
|
|
311
|
+
focused = id;
|
|
312
|
+
if (value === '' || typeof value === 'number') startEdit(id, 'value'); else { paint(); setFocus(id); }
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function newResource(s: string, template = kindOf(s).template, stem = kindOf(s).singular) {
|
|
317
|
+
const mapPath = kindOf(s).section;
|
|
318
|
+
expanded.add(toPointer(mapPath));
|
|
319
|
+
editing = { id: '#new', mode: 'new', section: s, mapPath, template, value: freshName(doc.text, stem) };
|
|
320
|
+
paint();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ---------- inline editing ----------
|
|
324
|
+
function startEdit(id: string, mode: 'rename' | 'value') {
|
|
325
|
+
if (readOnly()) return;
|
|
326
|
+
const r = byId.get(id);
|
|
327
|
+
if (!r) { paint(); return; }
|
|
328
|
+
if (mode === 'rename' && (r.n.kind === 'group' || isRoot(r.n) || r.n.kind === 'item')) return;
|
|
329
|
+
if (mode === 'value' && r.n.shape !== 'scalar') return;
|
|
330
|
+
editing = { id, mode };
|
|
331
|
+
focused = id;
|
|
332
|
+
paint();
|
|
333
|
+
}
|
|
334
|
+
function endEdit(refocus = true) { editing = undefined; paint(); if (refocus) setFocus(focused); }
|
|
335
|
+
|
|
336
|
+
function nameInput(n: OutlineNode, ed: Editing): HTMLElement {
|
|
337
|
+
const now = ed.mode === 'new' ? String(ed.value ?? '') : String(n.path[n.path.length - 1]);
|
|
338
|
+
const inp = h('input', { class: 'tedit tname', value: now, 'aria-label': ed.mode === 'new' ? `Name of the new ${ed.section ? kindOf(ed.section).singular : 'entry'}` : `Rename ${n.label}`, spellcheck: 'false', autocomplete: 'off' });
|
|
339
|
+
inp.value = now;
|
|
340
|
+
const check = () => {
|
|
341
|
+
const v = inp.value.trim();
|
|
342
|
+
const needsName = ed.mode === 'new' || isResource(n) || inGroup(n.path);
|
|
343
|
+
const bad = !v ? 'a name is needed' : needsName && !K.nameRule.test(v) ? ui.nameHint : v !== now && taken(ed.mode === 'new' ? ed.mapPath : n.path.slice(0, -1), v, needsName) ? `"${v}" is already used` : '';
|
|
344
|
+
inp.setAttribute('aria-invalid', String(!!bad));
|
|
345
|
+
inp.title = bad;
|
|
346
|
+
return bad ? undefined : v;
|
|
347
|
+
};
|
|
348
|
+
inp.addEventListener('input', check);
|
|
349
|
+
inp.addEventListener('keydown', e => {
|
|
350
|
+
e.stopPropagation();
|
|
351
|
+
if (e.key === 'Escape') { e.preventDefault(); endEdit(); }
|
|
352
|
+
if (e.key === 'Enter') { e.preventDefault(); commit(); }
|
|
353
|
+
});
|
|
354
|
+
let done = false;
|
|
355
|
+
const commit = () => {
|
|
356
|
+
if (done) return;
|
|
357
|
+
const v = check();
|
|
358
|
+
if (v === undefined) { if (!inp.value.trim()) { done = true; endEdit(); } return; }
|
|
359
|
+
done = true;
|
|
360
|
+
editing = undefined;
|
|
361
|
+
if (ed.mode === 'new') {
|
|
362
|
+
const p = [...ed.mapPath, v];
|
|
363
|
+
if (run(insertCmd(ed.mapPath, v, ed.template(), { label: `Add ${ed.section ? kindOf(ed.section).singular : 'entry'} ${v}` }))) { focused = toPointer(p); doc.select([p], 'tree'); expanded.add(focused); }
|
|
364
|
+
} else if (v !== now) {
|
|
365
|
+
const p = n.path;
|
|
366
|
+
if (run(isResource(n) ? renameCmd(p, v) : renameKeyCmd(p, v))) {
|
|
367
|
+
const np = [...p.slice(0, -1), v];
|
|
368
|
+
if (expanded.has(n.id)) { expanded.delete(n.id); expanded.add(toPointer(np)); }
|
|
369
|
+
focused = toPointer(np);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
paint(); setFocus(focused);
|
|
373
|
+
};
|
|
374
|
+
inp.addEventListener('blur', () => { if (!done) { if (inp.value.trim() && check() !== undefined) commit(); else { done = true; editing = undefined; paint(); } } });
|
|
375
|
+
return inp;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function taken(mapPath: Path, v: string, oneNamespace: boolean): boolean {
|
|
379
|
+
if (oneNamespace && !isGroupPath(mapPath)) return K.kinds.some(k => namesIn(k.kind).includes(v));
|
|
380
|
+
const m = getIn(doc.goodDoc.data, mapPath);
|
|
381
|
+
return !!m && typeof m === 'object' && v in (m as object);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function valueInput(n: OutlineNode): HTMLElement {
|
|
385
|
+
const nav = opts.nav();
|
|
386
|
+
const now = getIn(doc.goodDoc.data, n.path);
|
|
387
|
+
const vals = nav.values(n.path);
|
|
388
|
+
const slot = K.slotAt(toPointer(n.path));
|
|
389
|
+
const refNames = slot ? slot.targets.flatMap(s => namesIn(s).map(x => (slot.write ? slot.write(x) : x))).concat(slot.extra ?? []) : [];
|
|
390
|
+
const choices = [...new Set([...vals, ...refNames])];
|
|
391
|
+
let el2: HTMLInputElement | HTMLSelectElement;
|
|
392
|
+
if (choices.length && (vals.length || slot)) {
|
|
393
|
+
el2 = h('select', { class: 'tedit tvalue', 'aria-label': `Value of ${n.label}` }, ...choices.map(v => h('option', { value: v }, v)));
|
|
394
|
+
if (!choices.includes(String(now))) el2.prepend(h('option', { value: String(now ?? '') }, String(now ?? '')));
|
|
395
|
+
el2.value = String(now ?? '');
|
|
396
|
+
el2.addEventListener('change', () => commitValue(n, el2.value));
|
|
397
|
+
} else {
|
|
398
|
+
el2 = h('input', { class: 'tedit tvalue', 'aria-label': `Value of ${n.label}`, spellcheck: 'false', autocomplete: 'off' });
|
|
399
|
+
el2.value = now === null || now === undefined ? '' : String(now);
|
|
400
|
+
}
|
|
401
|
+
el2.addEventListener('keydown', e => {
|
|
402
|
+
e.stopPropagation();
|
|
403
|
+
if ((e as KeyboardEvent).key === 'Escape') { e.preventDefault(); endEdit(); }
|
|
404
|
+
if ((e as KeyboardEvent).key === 'Enter') { e.preventDefault(); commitValue(n, el2.value); }
|
|
405
|
+
});
|
|
406
|
+
el2.addEventListener('blur', () => { if (editing?.id === n.id) commitValue(n, el2.value, false); });
|
|
407
|
+
return el2;
|
|
408
|
+
}
|
|
409
|
+
function commitValue(n: OutlineNode, raw: string, refocus = true) {
|
|
410
|
+
if (editing?.id !== n.id) return;
|
|
411
|
+
editing = undefined;
|
|
412
|
+
const types = opts.nav().types(n.path);
|
|
413
|
+
const now = getIn(doc.goodDoc.data, n.path);
|
|
414
|
+
let v: unknown = raw;
|
|
415
|
+
if ((types.has('integer') || types.has('number') || typeof now === 'number') && /^-?\d+(\.\d+)?$/.test(raw.trim())) v = Number(raw);
|
|
416
|
+
else if ((types.has('boolean') || typeof now === 'boolean') && /^(true|false)$/.test(raw.trim())) v = raw.trim() === 'true';
|
|
417
|
+
if (v !== now) run(setCmd(n.path, v));
|
|
418
|
+
paint();
|
|
419
|
+
if (refocus) setFocus(focused);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ---------- delete and duplicate ----------
|
|
423
|
+
async function remove(ids: string[]) {
|
|
424
|
+
if (readOnly() || !ids.length) return;
|
|
425
|
+
const nodes = ids.map(id => byId.get(id)?.n).filter((n): n is OutlineNode => !!n && !isRoot(n) && n.shape !== 'missing');
|
|
426
|
+
// a node inside another being deleted goes with it
|
|
427
|
+
const top = nodes.filter(n => !nodes.some(o => o !== n && o.path.length < n.path.length && isPrefix(o.path, n.path)));
|
|
428
|
+
if (!top.length) return;
|
|
429
|
+
let withRefs = false;
|
|
430
|
+
const refs = top.filter(isResource).flatMap(n => referencesTo(doc.text, n.section!, String(n.path[n.path.length - 1])).filter(r => !top.some(o => isPrefix(o.path, fromPointer(r.path)))).map(r => ({ n, r })));
|
|
431
|
+
if (refs.length) {
|
|
432
|
+
const list = h('ul', { class: 'ref-list' }, ...refs.map(({ n, r }) => h('li', null, h('code', null, r.path.slice(1).replace(/\//g, '.')), ` — ${r.slot.what} (${n.label})`)));
|
|
433
|
+
const what = top.length === 1 ? describe(top[0].path) : `${top.length} items`;
|
|
434
|
+
const choice = await ask(`Delete ${what}?`, [h('p', { class: 'sub' }, `${refs.length === 1 ? 'One place refers' : `${refs.length} places refer`} to ${top.length === 1 ? 'it' : 'them'}:`), list,
|
|
435
|
+
h('p', { class: 'sub' }, 'Delete the references too, or leave them (the problems list will show them). Either way, Undo brings everything back.')],
|
|
436
|
+
[{ label: 'Cancel', value: 'cancel' }, { label: 'Delete only', value: 'only', danger: true }, { label: 'Delete with references', value: 'with', danger: true, primary: true }]);
|
|
437
|
+
if (!choice) { setFocus(focused); return; }
|
|
438
|
+
withRefs = choice === 'with';
|
|
439
|
+
}
|
|
440
|
+
// list items last index first, so the paths stay right; then the rest
|
|
441
|
+
const order = [...top].sort((a, b) => (typeof a.path.at(-1) === 'number' && typeof b.path.at(-1) === 'number' ? (b.path.at(-1) as number) - (a.path.at(-1) as number) : 0));
|
|
442
|
+
const cmds = order.map(n => deleteCmd(n.path, { withRefs: withRefs && isResource(n) }));
|
|
443
|
+
const ix = rows.findIndex(r => r.n.id === top[0].id);
|
|
444
|
+
const next = rows[ix + 1]?.n.id && !top.some(t => rows[ix + 1].n.id.startsWith(t.id)) ? rows[ix + 1].n.id : rows[ix - 1]?.n.id ?? '';
|
|
445
|
+
const ok = run(cmds.length === 1 ? cmds[0] : composite(`Delete ${top.length} items`, cmds));
|
|
446
|
+
if (ok) { focused = next; doc.select(next ? [fromPointer(next)] : [], 'tree'); paint(); setFocus(focused); }
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function composite(label: string, cmds: Command[]): Command {
|
|
450
|
+
return {
|
|
451
|
+
label,
|
|
452
|
+
remap: p => cmds.reduce<Path | null>((x, c) => (x && c.remap ? c.remap(x) : x), p),
|
|
453
|
+
build: t => {
|
|
454
|
+
const steps = [];
|
|
455
|
+
let fallback = false;
|
|
456
|
+
for (const c of cmds) { const s = c.build(t); for (const st of s.steps) { steps.push(st); t = applyStep(t, st); } fallback ||= !!s.fallback; }
|
|
457
|
+
return { steps, fallback };
|
|
458
|
+
},
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function duplicate(id: string) {
|
|
463
|
+
const n = byId.get(id)?.n;
|
|
464
|
+
if (!n || readOnly()) return;
|
|
465
|
+
if (!isResource(n)) { opts.note(ui.text.duplicateNote, 'info'); return; }
|
|
466
|
+
const c = duplicateCmd(n.path);
|
|
467
|
+
if (run(c)) { const sel = c.select?.[0]; if (sel) { focused = toPointer(sel); doc.select([sel], 'tree'); } paint(); setFocus(focused); }
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// ---------- the menu ----------
|
|
471
|
+
function menuFor(n: OutlineNode, at: { x: number; y: number } | HTMLElement) {
|
|
472
|
+
const ids = selected().has(n.id) ? [...selected()] : [n.id];
|
|
473
|
+
const many = ids.length > 1;
|
|
474
|
+
const adds = addItems(n);
|
|
475
|
+
const ro = readOnly();
|
|
476
|
+
const items: MenuItem[] = [];
|
|
477
|
+
if (adds.length && !many) items.push({ heading: 'Add' }, ...adds.slice(0, 12), ...(adds.length > 12 ? [{ label: 'More…', icon: 'plus' as IconName, onSelect: () => openMenu(adds, at, 'Add') }] : []), { sep: true });
|
|
478
|
+
if (!many) {
|
|
479
|
+
items.push({ label: 'Rename', icon: 'pencil', keys: 'F2', disabled: ro || n.kind === 'group' || isRoot(n) || n.kind === 'item', why: ui.text.renameWhy, onSelect: () => startEdit(n.id, 'rename') });
|
|
480
|
+
if (n.shape === 'scalar') items.push({ label: 'Edit value', icon: 'pencil', keys: 'Enter', disabled: ro, onSelect: () => startEdit(n.id, 'value') });
|
|
481
|
+
items.push({ label: 'Duplicate', icon: 'copy', keys: '⌘D', disabled: ro || !isResource(n), why: ui.text.duplicateWhy, onSelect: () => duplicate(n.id) });
|
|
482
|
+
}
|
|
483
|
+
if (n.path.length) items.push({ label: 'Select parent', icon: 'arrow-up-right', keys: '⌘↑', onSelect: () => selectParent() });
|
|
484
|
+
items.push({ label: 'Show in text', icon: 'code-xml', onSelect: () => opts.showInText(n.path) });
|
|
485
|
+
if (opts.clipItems) items.push({ sep: true }, ...opts.clipItems());
|
|
486
|
+
items.push({ sep: true }, { label: many ? `Delete ${ids.length} items` : 'Delete', icon: 'trash-2', keys: '⌫', danger: true, disabled: ro || isRoot(n) || n.shape === 'missing', why: isRoot(n) ? ui.text.deleteWhy : 'Nothing to delete', onSelect: () => remove(ids) });
|
|
487
|
+
openMenu(items, at, `${n.label || ui.text.root} actions`);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function selectParent() {
|
|
491
|
+
const ps = doc.selection.length ? doc.selection : [fromPointer(focused)];
|
|
492
|
+
const up = [...new Set(ps.map(p => {
|
|
493
|
+
// a resource's parent is its group; anything else its nearest row
|
|
494
|
+
let q = p.slice(0, -1);
|
|
495
|
+
while (q.length && !byId.has(toPointer(q))) q = q.slice(0, -1);
|
|
496
|
+
return toPointer(q);
|
|
497
|
+
}))];
|
|
498
|
+
selectIds(up);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// ---------- events ----------
|
|
502
|
+
el.addEventListener('click', e => {
|
|
503
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
504
|
+
if (!r || (e.target as HTMLElement).closest('.tedit')) return;
|
|
505
|
+
clickSelect(r.dataset.id!, e);
|
|
506
|
+
});
|
|
507
|
+
el.addEventListener('dblclick', e => {
|
|
508
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
509
|
+
if (!r || (e.target as HTMLElement).closest('.tedit, .twisty, .tadd')) return;
|
|
510
|
+
const n = byId.get(r.dataset.id!)?.n;
|
|
511
|
+
if (!n) return;
|
|
512
|
+
if (n.shape === 'scalar' && (e.target as HTMLElement).closest('.tval')) startEdit(n.id, 'value');
|
|
513
|
+
else if (n.kind === 'resource' || n.kind === 'key') startEdit(n.id, 'rename');
|
|
514
|
+
else toggle(n.id);
|
|
515
|
+
});
|
|
516
|
+
el.addEventListener('contextmenu', e => {
|
|
517
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
518
|
+
if (!r) return;
|
|
519
|
+
e.preventDefault();
|
|
520
|
+
const id = r.dataset.id!;
|
|
521
|
+
if (!selected().has(id)) clickSelect(id, { shiftKey: false, metaKey: false, ctrlKey: false });
|
|
522
|
+
const n = byId.get(id)?.n;
|
|
523
|
+
if (n) menuFor(n, { x: e.clientX, y: e.clientY });
|
|
524
|
+
});
|
|
525
|
+
el.addEventListener('focusin', e => {
|
|
526
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
527
|
+
if (r && !(e.target as HTMLElement).closest('.tedit')) focused = r.dataset.id!;
|
|
528
|
+
});
|
|
529
|
+
el.addEventListener('keydown', e => {
|
|
530
|
+
if ((e.target as HTMLElement).closest('.tedit')) return;
|
|
531
|
+
const ids = rows.map(r => r.n.id);
|
|
532
|
+
const i = Math.max(0, ids.indexOf(focused));
|
|
533
|
+
const cur = byId.get(focused);
|
|
534
|
+
const n = cur?.n;
|
|
535
|
+
const mod = e.metaKey || e.ctrlKey;
|
|
536
|
+
const go = (j: number) => {
|
|
537
|
+
const id = ids[Math.max(0, Math.min(ids.length - 1, j))];
|
|
538
|
+
if (e.shiftKey) { if (!anchor) anchor = focused; clickSelect(id, { shiftKey: true, metaKey: false, ctrlKey: false }); }
|
|
539
|
+
else { anchor = id; selectIds([id]); }
|
|
540
|
+
};
|
|
541
|
+
switch (e.key) {
|
|
542
|
+
case 'ArrowDown': if (mod) return; go(i + 1); break;
|
|
543
|
+
case 'ArrowUp': if (mod) { selectParent(); break; } go(i - 1); break;
|
|
544
|
+
case 'Home': go(0); break;
|
|
545
|
+
case 'End': go(ids.length - 1); break;
|
|
546
|
+
case 'ArrowRight':
|
|
547
|
+
if (n && n.children.length && !expanded.has(n.id)) toggle(n.id, true);
|
|
548
|
+
else if (n && n.children.length) go(i + 1);
|
|
549
|
+
break;
|
|
550
|
+
case 'ArrowLeft':
|
|
551
|
+
if (n && expanded.has(n.id) && n.children.length) toggle(n.id, false);
|
|
552
|
+
else if (cur?.parent) { anchor = cur.parent.id; selectIds([cur.parent.id]); }
|
|
553
|
+
break;
|
|
554
|
+
case 'Enter':
|
|
555
|
+
if (!n) return;
|
|
556
|
+
if (n.shape === 'scalar' && n.kind !== 'group') {
|
|
557
|
+
const now = getIn(doc.goodDoc.data, n.path);
|
|
558
|
+
if (typeof now === 'boolean' && !readOnly()) run(setCmd(n.path, !now));
|
|
559
|
+
else startEdit(n.id, 'value');
|
|
560
|
+
} else if (n.children.length) toggle(n.id);
|
|
561
|
+
break;
|
|
562
|
+
case 'F2': if (n) startEdit(n.id, 'rename'); break;
|
|
563
|
+
case 'Delete': case 'Backspace': remove(doc.selection.length ? doc.selection.map(toPointer) : [focused]); break;
|
|
564
|
+
case 'ContextMenu': if (n) menuFor(n, el.querySelector<HTMLElement>(`[data-id="${CSS.escape(focused)}"]`)!); break;
|
|
565
|
+
case 'F10': if (e.shiftKey && n) menuFor(n, el.querySelector<HTMLElement>(`[data-id="${CSS.escape(focused)}"]`)!); else return; break;
|
|
566
|
+
case '+': case 'Insert': if (n && addItems(n).length) openMenu(addItems(n), el.querySelector<HTMLElement>(`[data-id="${CSS.escape(focused)}"]`)!, 'Add'); break;
|
|
567
|
+
default:
|
|
568
|
+
if (mod && e.key.toLowerCase() === 'd') { duplicate(focused); break; }
|
|
569
|
+
if (mod && e.key.toLowerCase() === 'a') { selectIds(ids.filter(x => x), focused); break; }
|
|
570
|
+
if (e.key.length === 1 && !mod && /\S/.test(e.key)) {
|
|
571
|
+
// type-ahead: jump to the next row whose label starts with what was typed
|
|
572
|
+
const t = Date.now();
|
|
573
|
+
typeahead = t - typeaheadAt > 700 ? e.key.toLowerCase() : typeahead + e.key.toLowerCase();
|
|
574
|
+
typeaheadAt = t;
|
|
575
|
+
const order = [...ids.slice(i + (typeahead.length === 1 ? 1 : 0)), ...ids.slice(0, i + 1)];
|
|
576
|
+
const hit = order.find(id => (byId.get(id)?.n.label ?? '').toLowerCase().startsWith(typeahead));
|
|
577
|
+
if (hit !== undefined) { anchor = hit; selectIds([hit]); }
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
e.preventDefault();
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
// ---------- drag to reorder (within a map, or a list) ----------
|
|
586
|
+
let dragId: string | undefined;
|
|
587
|
+
el.addEventListener('dragstart', e => {
|
|
588
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
589
|
+
if (!r) return;
|
|
590
|
+
dragId = r.dataset.id;
|
|
591
|
+
e.dataTransfer?.setData('text/plain', fromPointer(dragId!).join('.'));
|
|
592
|
+
if (e.dataTransfer) e.dataTransfer.effectAllowed = 'move';
|
|
593
|
+
});
|
|
594
|
+
const target = (e: DragEvent) => {
|
|
595
|
+
const r = (e.target as HTMLElement).closest<HTMLElement>('.trow');
|
|
596
|
+
if (!r || !dragId || r.dataset.id === dragId) return undefined;
|
|
597
|
+
const a = byId.get(dragId)?.n, b = byId.get(r.dataset.id!)?.n;
|
|
598
|
+
if (!a || !b || toPointer(a.path.slice(0, -1)) !== toPointer(b.path.slice(0, -1))) return undefined;
|
|
599
|
+
const box = r.getBoundingClientRect();
|
|
600
|
+
return { r, a, b, after: e.clientY > box.top + box.height / 2 };
|
|
601
|
+
};
|
|
602
|
+
el.addEventListener('dragover', e => {
|
|
603
|
+
for (const x of el.querySelectorAll('.drop-before, .drop-after')) x.classList.remove('drop-before', 'drop-after');
|
|
604
|
+
const t = target(e);
|
|
605
|
+
if (!t) return;
|
|
606
|
+
e.preventDefault();
|
|
607
|
+
t.r.classList.add(t.after ? 'drop-after' : 'drop-before');
|
|
608
|
+
});
|
|
609
|
+
el.addEventListener('dragleave', e => { if (e.target === el) for (const x of el.querySelectorAll('.drop-before, .drop-after')) x.classList.remove('drop-before', 'drop-after'); });
|
|
610
|
+
el.addEventListener('drop', e => {
|
|
611
|
+
const t = target(e);
|
|
612
|
+
for (const x of el.querySelectorAll('.drop-before, .drop-after')) x.classList.remove('drop-before', 'drop-after');
|
|
613
|
+
dragId = undefined;
|
|
614
|
+
if (!t) return;
|
|
615
|
+
e.preventDefault();
|
|
616
|
+
const { a, b, after } = t;
|
|
617
|
+
const parent = a.path.slice(0, -1);
|
|
618
|
+
const last = (p: Path) => p[p.length - 1];
|
|
619
|
+
if (typeof last(a.path) === 'number') {
|
|
620
|
+
const from = last(a.path) as number;
|
|
621
|
+
let to = (last(b.path) as number) + (after ? 1 : 0);
|
|
622
|
+
if (to > from) to -= 1;
|
|
623
|
+
if (run(moveItemCmd(parent, from, to))) { focused = toPointer([...parent, to]); doc.select([[...parent, to]], 'tree'); }
|
|
624
|
+
} else {
|
|
625
|
+
const sibs = (byId.get(toPointer(parent))?.n.children ?? []).map(c => String(last(c.path)));
|
|
626
|
+
const bi = sibs.indexOf(String(last(b.path)));
|
|
627
|
+
const before = after ? sibs[bi + 1] : sibs[bi];
|
|
628
|
+
if (before !== String(last(a.path))) run(moveCmd(a.path, before === undefined ? undefined : before));
|
|
629
|
+
focused = a.id;
|
|
630
|
+
}
|
|
631
|
+
paint(); setFocus(focused);
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
// ---------- the model ----------
|
|
635
|
+
doc.on(c => {
|
|
636
|
+
if (c.remap) {
|
|
637
|
+
for (const id of [...expanded]) { const p = c.remap(fromPointer(id)); if (p && toPointer(p) !== id) { expanded.delete(id); expanded.add(toPointer(p)); } }
|
|
638
|
+
const f = c.remap(fromPointer(focused)); if (f) focused = toPointer(f);
|
|
639
|
+
}
|
|
640
|
+
if (c.kind === 'load') { editing = undefined; focused = ''; }
|
|
641
|
+
// typing: once it pauses (a keystroke never waits on the tree); everything else at once
|
|
642
|
+
if (isTyping(c)) paintLater(); else paintLater.now();
|
|
643
|
+
});
|
|
644
|
+
const paintLater = later(() => paint());
|
|
645
|
+
doc.onSelect(c => { if (c.origin !== 'tree') reveal(c.paths); else paint(); });
|
|
646
|
+
|
|
647
|
+
paint();
|
|
648
|
+
return {
|
|
649
|
+
paint,
|
|
650
|
+
dispose: () => paintLater.cancel(),
|
|
651
|
+
focus: () => setFocus(focused),
|
|
652
|
+
/** for the tests */
|
|
653
|
+
rows: () => rows.map(r => ({ id: r.n.id, label: r.n.label, depth: r.depth, kind: r.n.kind, value: r.n.value, lifetime: r.n.lifetime })),
|
|
654
|
+
startEdit, remove, duplicate, expand: (id: string) => toggle(id, true),
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
|