@johnoscott/isometric-engine 0.0.0-stage → 0.10.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/EDITOR-GUIDE.md +355 -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 +269 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +39 -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 +443 -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 +1403 -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 +149 -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 +511 -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 +147 -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 +477 -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 +1227 -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 +39 -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 +153 -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 +178 -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 +307 -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 +49 -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,67 @@
|
|
|
1
|
+
// iso-editor (L3): the text-is-truth editor's core (docs/editor/LAYERS.md §2, §4): the document and its paths, one
|
|
2
|
+
// undo stack of text splices, the commands, the clipboard, completion, the outline, problems and the text panes'
|
|
3
|
+
// providers, all driven by a domain adapter (adapter.ts); and the views' pieces (ui/): the layer menus and dialogs
|
|
4
|
+
// open in, menus, dialogs, icons, the keyboard sheet. (ui/monaco-entry.ts is the text pane's Monaco bundle: a build
|
|
5
|
+
// entry of its own, not part of this API.)
|
|
6
|
+
//
|
|
7
|
+
// The layer's public API. Importers name the module a name comes from (with code splitting, a barrel puts every file
|
|
8
|
+
// it reaches into each bundle that imports it); the Node tests may import this index.
|
|
9
|
+
|
|
10
|
+
export type { FileRole, ObjectKindInfo, RefSlot, Ref, ObjectRef, ReadOutcome, DomainAdapter, ConnectPlan, GroupPlan } from './adapter.ts';
|
|
11
|
+
export { toPointer, fromPointer, parentOf, isPrefix, samePath } from './path.ts';
|
|
12
|
+
export type { Seg, Path } from './path.ts';
|
|
13
|
+
export { EditorDoc, completionContext } from './doc.ts';
|
|
14
|
+
export type { Where, At, CompletionContext } from './doc.ts';
|
|
15
|
+
export { HISTORY_DEPTH, EditorModel } from './model.ts';
|
|
16
|
+
export type { Origin, Command, Change, SelectionChange } from './model.ts';
|
|
17
|
+
export { SpliceError, applyStep, invertStep, scalarText, flowText, setValue, insertKey, deleteAt, insertItem, renameKey, moveKey, insertRawPair, pairText } from './splice.ts';
|
|
18
|
+
export type { TextEdit, Step, Splice } from './splice.ts';
|
|
19
|
+
export { SchemaNav } from './schema-nav.ts';
|
|
20
|
+
export type { KeyInfo } from './schema-nav.ts';
|
|
21
|
+
export { PREF, VIEWS } from './storage-keys.ts';
|
|
22
|
+
export { chain, seqCmd, commandsFor } from './commands.ts';
|
|
23
|
+
export type { Commands } from './commands.ts';
|
|
24
|
+
export { clipboardFor } from './clipboard.ts';
|
|
25
|
+
export type { PastePlan, Clipboard } from './clipboard.ts';
|
|
26
|
+
export { namesIn, complete } from './complete.ts';
|
|
27
|
+
export type { Item, Completion } from './complete.ts';
|
|
28
|
+
export { outline, flatten } from './outline.ts';
|
|
29
|
+
export type { NodeKind, OutlineNode } from './outline.ts';
|
|
30
|
+
export { analyse, place } from './validate.ts';
|
|
31
|
+
export type { Problem, ValidateOpts } from './validate.ts';
|
|
32
|
+
export { docFor, providersFor, rangeOfPath, problemRange, cmSnippet, mdRuns } from './providers.ts';
|
|
33
|
+
export type { Providers } from './providers.ts';
|
|
34
|
+
export { setLayer, layer } from './ui/layer.ts';
|
|
35
|
+
export { icon } from './ui/icons.ts';
|
|
36
|
+
export type { IconName } from './ui/icons.ts';
|
|
37
|
+
export { ask } from './ui/dialog.ts';
|
|
38
|
+
export type { DialogButton } from './ui/dialog.ts';
|
|
39
|
+
export { menuOpen, openMenu, closeMenu } from './ui/menu.ts';
|
|
40
|
+
export type { MenuItem } from './ui/menu.ts';
|
|
41
|
+
export { TYPING_PAUSE_MS, isTyping, later } from './ui/later.ts';
|
|
42
|
+
export { keymapWith, keySheet } from './ui/keys.ts';
|
|
43
|
+
export type { KeyGroup } from './ui/keys.ts';
|
|
44
|
+
export type { TextSel, TextPane, TextServices, PaneOpts } from './ui/pane.ts';
|
|
45
|
+
export { clipboard } from './ui/clip.ts';
|
|
46
|
+
export type { Clip } from './ui/clip.ts';
|
|
47
|
+
export { treeView } from './ui/tree.ts';
|
|
48
|
+
export type { NewItem, EntryItem, TreeUi, TreeKit, TreeOpts } from './ui/tree.ts';
|
|
49
|
+
export { run, composite, actionsFor } from './ui/actions.ts';
|
|
50
|
+
export type { ActionsKit, Actions } from './ui/actions.ts';
|
|
51
|
+
export { propsPanel } from './ui/props.ts';
|
|
52
|
+
export type { PropsUi, PropsKit, PropsOpts } from './ui/props.ts';
|
|
53
|
+
export type { Box as CanvasBox, Outline, CanvasCtx, CanvasLayer } from './ui/canvas-layer.ts';
|
|
54
|
+
export { canvasView } from './ui/canvas.ts';
|
|
55
|
+
export type { CanvasDrawing, DropInto, CanvasUi, CanvasKit, CanvasOpts, FlatCanvas, FlatDrawn, PaletteItem } from './ui/canvas.ts';
|
|
56
|
+
// the canvas's 2D view (0.6.4): a host hands it to the canvas (DomainView.flat.load); the canvas never imports it
|
|
57
|
+
export { flatCanvas } from './ui/flat-canvas.ts';
|
|
58
|
+
export type { PartCtx, Attention, EditorPart } from './ui/part.ts';
|
|
59
|
+
export type { Checked, MountKit } from './ui/mount-kit.ts';
|
|
60
|
+
export { mountEditor } from './ui/mount.ts';
|
|
61
|
+
export type { Theme, SaveResult, HostHooks, EditorOptions, ShellWords, ShellFeatures, ShellCanvas, EditorSetup, EditorState, Editor } from './ui/mount.ts';
|
|
62
|
+
export { PROTOCOL, isMessage } from './window/protocol.ts';
|
|
63
|
+
export type { Refused, Capabilities, BaseToHost, BaseToWindow } from './window/protocol.ts';
|
|
64
|
+
export { setupFor, PLAIN_WORDS, PLAIN_KEYMAP } from './ui/setup.ts';
|
|
65
|
+
export type { DomainView } from './ui/setup.ts';
|
|
66
|
+
export { scopeCss, scopeOne } from './ui/scope-css.ts';
|
|
67
|
+
export { PLAIN_ICON_SVG, PLAIN_ICONS, PLAIN_SPRITE, iconSymbols, spriteOf } from './ui/plain-icons.ts';
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// The editor's model: the text (the truth), the Document parsed from it, the selection, and one undo stack for every
|
|
2
|
+
// view. Nothing changes the text except through here: a command (a structural edit from the tree, canvas or
|
|
3
|
+
// properties, built in commands.ts) or typing (text edits from Monaco, coalesced). Every change is a list of steps of
|
|
4
|
+
// text edits; undo applies their exact inverses. Pure (no DOM): the views listen for changes.
|
|
5
|
+
|
|
6
|
+
import { EditorDoc } from './doc.ts';
|
|
7
|
+
import { applyStep, invertStep, type Splice, type Step } from './splice.ts';
|
|
8
|
+
import type { Path } from './path.ts';
|
|
9
|
+
|
|
10
|
+
export type Origin = 'text' | 'tree' | 'canvas' | 'props' | 'menu' | 'api' | 'history' | 'file';
|
|
11
|
+
|
|
12
|
+
export interface Command {
|
|
13
|
+
/** for the menu and the undo button's tooltip: "Delete node web" */
|
|
14
|
+
label: string;
|
|
15
|
+
/** the change, worked out against the text it will apply to */
|
|
16
|
+
build(text: string): Splice;
|
|
17
|
+
/** the paths it touched (for the views to refresh and reveal) */
|
|
18
|
+
touched?: Path[];
|
|
19
|
+
/** where a path is after the change (a rename moves paths); null when it no longer exists */
|
|
20
|
+
remap?(p: Path): Path | null;
|
|
21
|
+
/** the selection afterwards (default: the old one, remapped) */
|
|
22
|
+
select?: Path[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What a view needs to know about a change. */
|
|
26
|
+
export interface Change {
|
|
27
|
+
kind: 'do' | 'undo' | 'redo' | 'load';
|
|
28
|
+
origin: Origin;
|
|
29
|
+
label: string;
|
|
30
|
+
/** the steps, in order, each against the text the one before produced */
|
|
31
|
+
steps: Step[];
|
|
32
|
+
version: number;
|
|
33
|
+
/** the text selection to put back (undo/redo of typing), opaque to the model */
|
|
34
|
+
textSel?: unknown;
|
|
35
|
+
/** typing: which text pane typed it (each pane skips only its own; another pane takes it like any change) */
|
|
36
|
+
source?: string;
|
|
37
|
+
/** where paths moved (a rename), so views can carry their own state (expanded rows) along */
|
|
38
|
+
remap?(p: Path): Path | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The selection changed, and which view changed it (a view doesn't react to its own). */
|
|
42
|
+
export interface SelectionChange { paths: Path[]; origin: Origin }
|
|
43
|
+
|
|
44
|
+
interface Entry {
|
|
45
|
+
id: number;
|
|
46
|
+
label: string;
|
|
47
|
+
origin: Origin;
|
|
48
|
+
steps: Step[];
|
|
49
|
+
/** the inverse of each step, in the order they undo (last step first) */
|
|
50
|
+
inverse: Step[];
|
|
51
|
+
before: { paths: Path[]; text?: unknown };
|
|
52
|
+
after: { paths: Path[]; text?: unknown };
|
|
53
|
+
time: number;
|
|
54
|
+
fallback: boolean;
|
|
55
|
+
/** typing still being added to */
|
|
56
|
+
open: boolean;
|
|
57
|
+
/** for coalescing: where the last typed edit ended, and whether it inserted or deleted */
|
|
58
|
+
caret?: number;
|
|
59
|
+
typing?: 'insert' | 'delete';
|
|
60
|
+
/** typing: the pane it came from (typing joins only typing from the same pane) */
|
|
61
|
+
source?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const HISTORY_DEPTH = 500;
|
|
65
|
+
const COALESCE_MS = 1000;
|
|
66
|
+
|
|
67
|
+
export class EditorModel {
|
|
68
|
+
private _text = '';
|
|
69
|
+
private _doc: EditorDoc | undefined;
|
|
70
|
+
private _version = 0;
|
|
71
|
+
private undoStack: Entry[] = [];
|
|
72
|
+
private redoStack: Entry[] = [];
|
|
73
|
+
private nextId = 1;
|
|
74
|
+
private cleanId = 0;
|
|
75
|
+
private listeners = new Set<(c: Change) => void>();
|
|
76
|
+
private selListeners = new Set<(c: SelectionChange) => void>();
|
|
77
|
+
private _good: EditorDoc | undefined;
|
|
78
|
+
/** the selected paths (tree, canvas, properties) */
|
|
79
|
+
selection: Path[] = [];
|
|
80
|
+
/** the text pane's selection, as it last reported it (opaque) */
|
|
81
|
+
textSel: unknown;
|
|
82
|
+
/** how many commands needed a fallback (re-serialising a collection): for the tests */
|
|
83
|
+
fallbacks = 0;
|
|
84
|
+
depth = HISTORY_DEPTH;
|
|
85
|
+
now: () => number = () => Date.now();
|
|
86
|
+
|
|
87
|
+
constructor(text = '') { this._text = text; }
|
|
88
|
+
get text() { return this._text; }
|
|
89
|
+
get version() { return this._version; }
|
|
90
|
+
get doc(): EditorDoc { return (this._doc ??= new EditorDoc(this._text)); }
|
|
91
|
+
/** the document as last parsed without a YAML error: what the tree and canvas show while the text is broken */
|
|
92
|
+
get goodDoc(): EditorDoc {
|
|
93
|
+
if (this.doc.ok) this._good = this.doc;
|
|
94
|
+
return this._good ?? this.doc;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Select paths (from a view). Every other view shows it. */
|
|
98
|
+
select(paths: Path[], origin: Origin) {
|
|
99
|
+
this.selection = paths;
|
|
100
|
+
for (const fn of this.selListeners) fn({ paths, origin });
|
|
101
|
+
}
|
|
102
|
+
onSelect(fn: (c: SelectionChange) => void): () => void { this.selListeners.add(fn); return () => this.selListeners.delete(fn); }
|
|
103
|
+
|
|
104
|
+
on(fn: (c: Change) => void): () => void { this.listeners.add(fn); return () => this.listeners.delete(fn); }
|
|
105
|
+
private emit(c: Change) { for (const fn of this.listeners) fn(c); }
|
|
106
|
+
|
|
107
|
+
/** A new file: the text, an empty history, clean. */
|
|
108
|
+
load(text: string, origin: Origin = 'file') {
|
|
109
|
+
const old = this._text;
|
|
110
|
+
this.undoStack = []; this.redoStack = [];
|
|
111
|
+
this.selection = [];
|
|
112
|
+
this._good = undefined;
|
|
113
|
+
this.set(text);
|
|
114
|
+
this.cleanId = 0;
|
|
115
|
+
this.emit({ kind: 'load', origin, label: 'Open', steps: [[{ start: 0, end: old.length, text }]], version: this._version });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private set(text: string) { this._text = text; this._doc = undefined; this._version++; }
|
|
119
|
+
|
|
120
|
+
// ---------- doing ----------
|
|
121
|
+
|
|
122
|
+
/** Run a command: build it against the text, apply its steps, push it. Returns false if it changed nothing. */
|
|
123
|
+
apply(cmd: Command, origin: Origin = 'api'): boolean {
|
|
124
|
+
this.seal();
|
|
125
|
+
const splice = cmd.build(this._text);
|
|
126
|
+
if (!splice.steps.some(s => s.length)) return false;
|
|
127
|
+
const before = { paths: this.selection, text: this.textSel };
|
|
128
|
+
const { inverse } = this.run(splice.steps);
|
|
129
|
+
const remap = cmd.remap ?? ((p: Path) => p);
|
|
130
|
+
this.selection = cmd.select ?? this.selection.map(remap).filter((p): p is Path => p !== null);
|
|
131
|
+
if (splice.fallback) this.fallbacks++;
|
|
132
|
+
this.push({ id: this.nextId++, label: cmd.label, origin, steps: splice.steps, inverse, before, after: { paths: this.selection, text: this.textSel }, time: this.now(), fallback: !!splice.fallback, open: false });
|
|
133
|
+
this.emit({ kind: 'do', origin, label: cmd.label, steps: splice.steps, version: this._version, remap: cmd.remap });
|
|
134
|
+
for (const fn of this.selListeners) fn({ paths: this.selection, origin });
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Text the user typed (one change from the text pane: edits against the text before it). The view has already
|
|
140
|
+
* made the change; the model takes it, and coalesces typing into one command per word or pause.
|
|
141
|
+
*/
|
|
142
|
+
typed(step: Step, sel: { before?: unknown; after?: unknown } = {}, source?: string): void {
|
|
143
|
+
if (!step.length) return;
|
|
144
|
+
const time = this.now();
|
|
145
|
+
const top = this.undoStack[this.undoStack.length - 1];
|
|
146
|
+
const one = step.length === 1 ? step[0] : undefined;
|
|
147
|
+
const kind: 'insert' | 'delete' | undefined = one ? (one.text && one.start === one.end ? 'insert' : !one.text && one.end > one.start ? 'delete' : undefined) : undefined;
|
|
148
|
+
let joins = !!(top && top.open && top.origin === 'text' && top.source === source && this.redoStack.length === 0 && time - top.time < COALESCE_MS && one && kind && kind === top.typing);
|
|
149
|
+
if (joins && kind === 'insert') {
|
|
150
|
+
// a word ends where whitespace or punctuation follows a word character: that starts the next command
|
|
151
|
+
const prevChar = this._text[one!.start - 1] ?? '';
|
|
152
|
+
joins = one!.start === top!.caret && !one!.text.includes('\n') && !(/[\w-]/.test(prevChar) && /^[^\w-]/.test(one!.text));
|
|
153
|
+
}
|
|
154
|
+
if (joins && kind === 'delete') joins = one!.end === top!.caret;
|
|
155
|
+
const { inverse } = this.run([step]);
|
|
156
|
+
if (joins) {
|
|
157
|
+
top!.steps.push(step);
|
|
158
|
+
top!.inverse.unshift(...inverse);
|
|
159
|
+
top!.time = time;
|
|
160
|
+
top!.after = { paths: this.selection, text: sel.after };
|
|
161
|
+
top!.caret = kind === 'insert' ? one!.start + one!.text.length : one!.start;
|
|
162
|
+
} else {
|
|
163
|
+
this.seal();
|
|
164
|
+
this.push({
|
|
165
|
+
id: this.nextId++, label: 'Typing', origin: 'text', steps: [step], inverse, before: { paths: this.selection, text: sel.before ?? this.textSel },
|
|
166
|
+
after: { paths: this.selection, text: sel.after }, time, fallback: false, open: !!kind,
|
|
167
|
+
caret: one ? (kind === 'insert' ? one.start + one.text.length : one.start) : undefined, typing: kind, source,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (sel.after !== undefined) this.textSel = sel.after;
|
|
171
|
+
this.emit({ kind: 'do', origin: 'text', label: 'Typing', steps: [step], version: this._version, source });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** End the typing group being added to (a caret jump, a pause, leaving the text pane, a save). */
|
|
175
|
+
seal() { const top = this.undoStack[this.undoStack.length - 1]; if (top) top.open = false; }
|
|
176
|
+
|
|
177
|
+
private run(steps: Step[]): { inverse: Step[] } {
|
|
178
|
+
const inverse: Step[] = [];
|
|
179
|
+
let t = this._text;
|
|
180
|
+
for (const s of steps) { const next = applyStep(t, s); inverse.unshift(invertStep(t, s)); t = next; }
|
|
181
|
+
this.set(t);
|
|
182
|
+
return { inverse };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private push(e: Entry) {
|
|
186
|
+
this.undoStack.push(e);
|
|
187
|
+
this.redoStack = [];
|
|
188
|
+
if (this.undoStack.length > this.depth) {
|
|
189
|
+
const dropped = this.undoStack.shift()!;
|
|
190
|
+
// the clean point fell off the bottom: the file can no longer be reached by undo, so it stays dirty
|
|
191
|
+
if (dropped.id === this.cleanId) this.cleanId = -1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ---------- undoing ----------
|
|
196
|
+
|
|
197
|
+
get canUndo() { return this.undoStack.length > 0; }
|
|
198
|
+
get canRedo() { return this.redoStack.length > 0; }
|
|
199
|
+
get undoLabel() { return this.undoStack[this.undoStack.length - 1]?.label; }
|
|
200
|
+
get redoLabel() { return this.redoStack[this.redoStack.length - 1]?.label; }
|
|
201
|
+
|
|
202
|
+
undo(): boolean {
|
|
203
|
+
this.seal();
|
|
204
|
+
const e = this.undoStack.pop();
|
|
205
|
+
if (!e) return false;
|
|
206
|
+
this.run(e.inverse);
|
|
207
|
+
this.redoStack.push(e);
|
|
208
|
+
this.selection = e.before.paths;
|
|
209
|
+
this.textSel = e.before.text;
|
|
210
|
+
this.emit({ kind: 'undo', origin: 'history', label: e.label, steps: e.inverse, version: this._version, textSel: e.before.text });
|
|
211
|
+
for (const fn of this.selListeners) fn({ paths: this.selection, origin: 'history' });
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
redo(): boolean {
|
|
216
|
+
const e = this.redoStack.pop();
|
|
217
|
+
if (!e) return false;
|
|
218
|
+
this.run(e.steps);
|
|
219
|
+
this.undoStack.push(e);
|
|
220
|
+
this.selection = e.after.paths;
|
|
221
|
+
this.textSel = e.after.text;
|
|
222
|
+
this.emit({ kind: 'redo', origin: 'history', label: e.label, steps: e.steps, version: this._version, textSel: e.after.text });
|
|
223
|
+
for (const fn of this.selListeners) fn({ paths: this.selection, origin: 'history' });
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// ---------- saving ----------
|
|
228
|
+
|
|
229
|
+
/** The text was saved: this is the clean point. */
|
|
230
|
+
markClean() { this.seal(); this.cleanId = this.undoStack[this.undoStack.length - 1]?.id ?? 0; }
|
|
231
|
+
get dirty() { return (this.undoStack[this.undoStack.length - 1]?.id ?? 0) !== this.cleanId; }
|
|
232
|
+
/** the labels on the stack, oldest first (for tests and a history list) */
|
|
233
|
+
get history() { return this.undoStack.map(e => e.label); }
|
|
234
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// The outline of a file, for the tree: its own settings, then a group per kind in the adapter's order (a missing
|
|
2
|
+
// section is still shown, so something can be added to it), each object with its keys, the setting groups, and every
|
|
3
|
+
// map, list and value under them. Pure: the tree view renders it.
|
|
4
|
+
|
|
5
|
+
import { isMap, isSeq, isScalar, isPair, type Node as YNode, type Pair } from 'yaml';
|
|
6
|
+
import type { EditorDoc } from './doc.ts';
|
|
7
|
+
import { toPointer, type Path } from './path.ts';
|
|
8
|
+
import type { OutlineAdapter, FileRole } from './adapter.ts';
|
|
9
|
+
|
|
10
|
+
export type NodeKind = 'root' | 'group' | 'resource' | 'key' | 'item';
|
|
11
|
+
export interface OutlineNode {
|
|
12
|
+
/** the JSON Pointer of the node's path (unique) */
|
|
13
|
+
id: string;
|
|
14
|
+
path: Path;
|
|
15
|
+
kind: NodeKind;
|
|
16
|
+
label: string;
|
|
17
|
+
/** a scalar's text, as written */
|
|
18
|
+
value?: string;
|
|
19
|
+
shape: 'map' | 'list' | 'scalar' | 'missing';
|
|
20
|
+
/** for an object and its group: its kind */
|
|
21
|
+
section?: string;
|
|
22
|
+
/** for an object: its tag (the reader's, else the adapter's from this file) */
|
|
23
|
+
lifetime?: string;
|
|
24
|
+
/** the key is an x-* extension */
|
|
25
|
+
ext?: boolean;
|
|
26
|
+
children: OutlineNode[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const keyOf = (p: Pair) => (isScalar(p.key) ? String(p.key.value) : String(p.key));
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param tags the reader's tags by object name (with the base files merged), when known; without them an object's
|
|
33
|
+
* tag is the adapter's, from this file alone
|
|
34
|
+
*/
|
|
35
|
+
export function outline(d: EditorDoc, a: OutlineAdapter, role: FileRole = 'main', tags?: Map<string, string>): OutlineNode {
|
|
36
|
+
const text = d.text;
|
|
37
|
+
const data = (d.data ?? {}) as Record<string, unknown>;
|
|
38
|
+
|
|
39
|
+
const scalarText = (n: YNode | null | undefined): string => {
|
|
40
|
+
if (!n) return '';
|
|
41
|
+
const r = n.range;
|
|
42
|
+
if (r && r[1] > r[0]) {
|
|
43
|
+
const t = text.slice(r[0], r[1]).replace(/\s+$/, '');
|
|
44
|
+
// a block scalar: its first line of content
|
|
45
|
+
if (/^[|>]/.test(t)) return (String((n as { value?: unknown }).value ?? '').split('\n')[0] ?? '') + ' …';
|
|
46
|
+
return t;
|
|
47
|
+
}
|
|
48
|
+
return isScalar(n) ? String(n.value ?? '') : '';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** a node for any YAML value, at a path */
|
|
52
|
+
const node = (n: YNode | null | undefined, path: Path, label: string, kind: NodeKind, section?: string): OutlineNode => {
|
|
53
|
+
const id = toPointer(path);
|
|
54
|
+
const ext = typeof path[path.length - 1] === 'string' && String(path[path.length - 1]).startsWith('x-');
|
|
55
|
+
if (isMap(n)) {
|
|
56
|
+
return { id, path, kind, label, shape: 'map', section, ext, children: (n.items as Pair[]).map(p => node(p.value as YNode | null, [...path, keyOf(p)], keyOf(p), 'key')) };
|
|
57
|
+
}
|
|
58
|
+
if (isSeq(n)) {
|
|
59
|
+
return { id, path, kind, label, shape: 'list', section, ext, value: n.items.length ? undefined : '[]',
|
|
60
|
+
children: n.items.map((x, i) => node(x as YNode, [...path, i], isScalar(x) ? scalarText(x as YNode) : `${i + 1}`, 'item')) };
|
|
61
|
+
}
|
|
62
|
+
const out: OutlineNode = { id, path, kind, label, shape: 'scalar', value: scalarText(n), section, ext, children: [] };
|
|
63
|
+
if (kind === 'item') { out.label = out.value ?? ''; delete out.value; }
|
|
64
|
+
return out;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const root = d.doc.contents;
|
|
68
|
+
const pairs = isMap(root) ? (root.items as Pair[]) : [];
|
|
69
|
+
const get = (k: string) => pairs.find(p => keyOf(p) === k);
|
|
70
|
+
const children: OutlineNode[] = [];
|
|
71
|
+
const used = new Set<string>();
|
|
72
|
+
|
|
73
|
+
// the file's own settings first
|
|
74
|
+
for (const k of a.outline.first) { const p = get(k); if (p) { children.push(node(p.value as YNode, [k], k, 'key')); used.add(k); } }
|
|
75
|
+
const nested = new Set<string>();
|
|
76
|
+
if (role === 'main') {
|
|
77
|
+
for (const name of a.outline.kinds) {
|
|
78
|
+
const g = a.kinds.find(k => k.kind === name)!;
|
|
79
|
+
const path = g.section.map(String);
|
|
80
|
+
const top = get(path[0]);
|
|
81
|
+
if (path.length === 1) used.add(path[0]); else nested.add(path[0]);
|
|
82
|
+
let mapNode: YNode | null | undefined = top?.value as YNode | null | undefined;
|
|
83
|
+
if (path.length > 1) mapNode = isMap(mapNode) ? ((mapNode.items as Pair[]).find(p => keyOf(p) === path[1])?.value as YNode | undefined) : undefined;
|
|
84
|
+
const group: OutlineNode = { id: toPointer(path), path, kind: 'group', label: g.group, section: g.kind, shape: isMap(mapNode) ? 'map' : 'missing', children: [] };
|
|
85
|
+
if (isMap(mapNode)) {
|
|
86
|
+
for (const p of mapNode.items as Pair[]) {
|
|
87
|
+
const name = keyOf(p);
|
|
88
|
+
if (name.startsWith('x-')) { group.children.push(node(p.value as YNode, [...path, name], name, 'key')); continue; }
|
|
89
|
+
const r = node(p.value as YNode, [...path, name], name, 'resource', g.kind);
|
|
90
|
+
const tag = tags?.get(name) ?? a.outline.tag?.(g.kind, name, getIn(data, [...path, name]), data);
|
|
91
|
+
if (tag !== undefined) r.lifetime = tag;
|
|
92
|
+
group.children.push(r);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
children.push(group);
|
|
96
|
+
}
|
|
97
|
+
// a map that holds a section (a section nested in it): its other keys, if any
|
|
98
|
+
for (const top of nested) {
|
|
99
|
+
const holder = get(top);
|
|
100
|
+
const inner = new Set(a.kinds.filter(k => String(k.section[0]) === top).map(k => String(k.section[1])));
|
|
101
|
+
if (holder && isMap(holder.value)) {
|
|
102
|
+
used.add(top);
|
|
103
|
+
for (const p of (holder.value as { items: Pair[] }).items) if (!inner.has(keyOf(p))) children.push(node(p.value as YNode, [top, keyOf(p)], `${top}.${keyOf(p)}`, 'key'));
|
|
104
|
+
} else if (holder) used.add(top);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const { key: k, label, when } of a.outline.groups) {
|
|
108
|
+
const p = get(k);
|
|
109
|
+
used.add(k);
|
|
110
|
+
if (p) children.push({ ...node(p.value as YNode, [k], label, 'group'), kind: 'group' });
|
|
111
|
+
else if (role !== 'other' && (!when || when(data))) children.push({ id: '/' + k, path: [k], kind: 'group', label, shape: 'missing', children: [] });
|
|
112
|
+
}
|
|
113
|
+
// anything else, as written (x-* keys, unknown keys)
|
|
114
|
+
for (const p of pairs) { const k = keyOf(p); if (!used.has(k) && isPair(p)) children.push(node(p.value as YNode, [k], k, 'key')); }
|
|
115
|
+
|
|
116
|
+
return { id: '', path: [], kind: 'root', label: a.outline.rootLabel(data, role), shape: 'map', children };
|
|
117
|
+
}
|
|
118
|
+
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);
|
|
119
|
+
|
|
120
|
+
/** Every node, depth first (for search and keyboard). */
|
|
121
|
+
export function flatten(n: OutlineNode, out: OutlineNode[] = []): OutlineNode[] {
|
|
122
|
+
out.push(n);
|
|
123
|
+
for (const c of n.children) flatten(c, out);
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"//": "iso-editor (L3): only the Monaco pane and the Monaco bundle's entry have side effects (they set Monaco up when they load); bundles drop the modules they do not use. LAYERS.md S6, S7.",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"private": true,
|
|
5
|
+
"sideEffects": ["./ui/text.ts", "./ui/monaco-entry.ts"]
|
|
6
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Paths: a node's place in a document, as segments (["nodes","web","size"]) or as a JSON Pointer
|
|
2
|
+
// ("/nodes/web/size"), the form ajv, the validators and the canvas's data-path use. Pure.
|
|
3
|
+
|
|
4
|
+
export type Seg = string | number;
|
|
5
|
+
export type Path = Seg[];
|
|
6
|
+
|
|
7
|
+
const esc = (s: Seg) => String(s).replace(/~/g, '~0').replace(/\//g, '~1');
|
|
8
|
+
export const toPointer = (p: Path) => p.map(s => '/' + esc(s)).join('');
|
|
9
|
+
export function fromPointer(ptr: string): Path {
|
|
10
|
+
if (!ptr) return [];
|
|
11
|
+
return ptr.split('/').slice(1).map(s => {
|
|
12
|
+
const u = s.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
13
|
+
return /^\d+$/.test(u) ? Number(u) : u;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export const parentOf = (p: Path): Path | undefined => (p.length ? p.slice(0, -1) : undefined);
|
|
17
|
+
export const isPrefix = (a: Path, b: Path) => a.length <= b.length && a.every((s, i) => String(s) === String(b[i]));
|
|
18
|
+
export const samePath = (a: Path, b: Path) => a.length === b.length && isPrefix(a, b);
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// What the text panes ask the editor core, by offset into the text, for any editor: hover, go to definition, where a
|
|
2
|
+
// path is, where a problem is, and completion's snippets in each editor's syntax. Pure (no DOM, no editor), shared by
|
|
3
|
+
// the Monaco pane, a thin adapter. Kept editor-neutral (and Node-tested) so another editor can use them again:
|
|
4
|
+
// CodeMirror 6 did in E8 (tag editor-codemirror-e8), with cmSnippet below. What the file's references and schemas are
|
|
5
|
+
// is the domain adapter's (providersFor).
|
|
6
|
+
|
|
7
|
+
import { EditorDoc } from './doc.ts';
|
|
8
|
+
import { SchemaNav } from './schema-nav.ts';
|
|
9
|
+
import { complete, type Item } from './complete.ts';
|
|
10
|
+
import type { Problem } from './validate.ts';
|
|
11
|
+
import { toPointer, type Path } from './path.ts';
|
|
12
|
+
import type { ProvidersAdapter } from './adapter.ts';
|
|
13
|
+
|
|
14
|
+
/** The parsed document for a text: the last one, reused while the text is the same (each keystroke parses once). */
|
|
15
|
+
let last: EditorDoc | undefined;
|
|
16
|
+
export function docFor(text: string): EditorDoc {
|
|
17
|
+
if (last?.text !== text) last = new EditorDoc(text);
|
|
18
|
+
return last;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What the text panes ask, for one domain: completion, hover and go to definition, and the schema navigator for a
|
|
22
|
+
* text (the main one, or a base file's). */
|
|
23
|
+
export function providersFor(a: ProvidersAdapter) {
|
|
24
|
+
// one navigator per schema: a domain may pick its schema by the file's text (its version line)
|
|
25
|
+
const navs = new Map<object, SchemaNav>();
|
|
26
|
+
const navOf = (schema: object) => { let n = navs.get(schema); if (!n) navs.set(schema, (n = new SchemaNav(schema))); return n; };
|
|
27
|
+
const navFor = (text: string) => { const role = a.role(text) === 'base' ? 'base' : 'main'; return navOf(a.schema(role, text)); };
|
|
28
|
+
const sectionPath = (kind: string) => a.kinds.find(k => k.kind === kind)?.section ?? [];
|
|
29
|
+
|
|
30
|
+
/** Completion at an offset: where the replaced text starts, and the items (best first). */
|
|
31
|
+
function completionAt(d: EditorDoc, offset: number): { from: number; items: Item[] } {
|
|
32
|
+
const c = complete(d, offset, navFor(d.text), a);
|
|
33
|
+
const items = c.items.map((it, i) => ({ it, i })).sort((a, b) => Number(!!b.it.preferred) - Number(!!a.it.preferred) || a.i - b.i).map(x => x.it);
|
|
34
|
+
return { from: c.start, items };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A hover at an offset: the range it is about, and its parts (Markdown: **bold** and `code` only). */
|
|
38
|
+
function hoverAt(d: EditorDoc, offset: number): { from: number; to: number; parts: string[] } | null {
|
|
39
|
+
const a = d.at(offset);
|
|
40
|
+
if (!a.path.length || a.where === 'inside') return null;
|
|
41
|
+
const parts: string[] = [];
|
|
42
|
+
const doc = navFor(d.text).hover(a.path);
|
|
43
|
+
if (doc) parts.push(`**${a.path.join('.')}**\n\n${doc}`);
|
|
44
|
+
// a reference: what it points at
|
|
45
|
+
const ref = A.refs(d.data).find(r => r.path === toPointer(a.path));
|
|
46
|
+
if (ref?.name) {
|
|
47
|
+
const target = ref.slot.targets.map(s => [...sectionPath(s), ref.name!]).find(p => d.rangeOf(p));
|
|
48
|
+
parts.push(target ? `→ \`${target.join('.')}\` (line ${d.pos(d.rangeOf(target, 'key')![0]).line}; ⌘-click to go there)` : `→ no ${ref.slot.targets.join('/')} named \`${ref.name}\``);
|
|
49
|
+
}
|
|
50
|
+
if (!parts.length) return null;
|
|
51
|
+
const r = d.rangeOf(a.path, a.where === 'key' ? 'key' : 'value') ?? [offset, offset];
|
|
52
|
+
return { from: r[0], to: r[1], parts };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Go to definition at an offset: the declaration a reference names, or an extends: file to open. */
|
|
56
|
+
function definitionAt(d: EditorDoc, offset: number): { from: number; to: number } | { open: string } | null {
|
|
57
|
+
const a = d.at(offset);
|
|
58
|
+
if (a.path[0] === 'extends' && a.where !== 'key') {
|
|
59
|
+
const v = d.nodeAt(a.path) as { value?: unknown } | undefined;
|
|
60
|
+
if (typeof v?.value === 'string') return { open: v.value };
|
|
61
|
+
}
|
|
62
|
+
const ref = A.refs(d.data).find(r => r.path === toPointer(a.path));
|
|
63
|
+
if (!ref?.name) return null;
|
|
64
|
+
for (const s of ref.slot.targets) {
|
|
65
|
+
const r = d.rangeOf([...sectionPath(s), ref.name], 'key');
|
|
66
|
+
if (r) return { from: r[0], to: r[1] };
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const A = a;
|
|
71
|
+
return { navFor, completionAt, hoverAt, definitionAt };
|
|
72
|
+
}
|
|
73
|
+
export type Providers = ReturnType<typeof providersFor>;
|
|
74
|
+
|
|
75
|
+
/** Where a path is drawn in the text, to mark it: its whole pair, else its value. */
|
|
76
|
+
export function rangeOfPath(d: EditorDoc, p: Path): [number, number] | undefined {
|
|
77
|
+
return d.rangeOf(p, 'pair') ?? d.rangeOf(p);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A problem's range as offsets into the text (its lines and columns are 1-based), clamped to the text. */
|
|
81
|
+
export function problemRange(text: string, p: Pick<Problem, 'startLine' | 'startCol' | 'endLine' | 'endCol'>): [number, number] {
|
|
82
|
+
const starts = [0];
|
|
83
|
+
for (let i = text.indexOf('\n'); i >= 0; i = text.indexOf('\n', i + 1)) starts.push(i + 1);
|
|
84
|
+
const at = (line: number, col: number) => {
|
|
85
|
+
const l = Math.min(Math.max(line, 1), starts.length);
|
|
86
|
+
const end = l < starts.length ? starts[l] - 1 : text.length;
|
|
87
|
+
return Math.min(starts[l - 1] + Math.max(col, 1) - 1, end);
|
|
88
|
+
};
|
|
89
|
+
const a = at(p.startLine, p.startCol);
|
|
90
|
+
return [a, Math.max(a, at(p.endLine, p.endCol))];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Completion's snippets are written in Monaco's syntax (${1:name}, ${2|a,b|}, ${0}). CodeMirror's has fields (${1:name},
|
|
95
|
+
* ${}) but no choices: a choice becomes a field holding its first option (the others are offered as values once the
|
|
96
|
+
* caret is there), and the final ${0} an empty field.
|
|
97
|
+
*/
|
|
98
|
+
export function cmSnippet(monaco: string): string {
|
|
99
|
+
return monaco.replace(/\$\{(\d+)\|([^|}]*)\|\}/g, (_m, n: string, opts: string) => `\${${n}:${opts.split(',')[0]}}`).replace(/\$\{0\}/g, '${}');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The Markdown a hover or completion carries, as runs of text: plain, bold or code, in paragraphs (never markup). */
|
|
103
|
+
export function mdRuns(md: string): { kind: 'text' | 'bold' | 'code'; text: string }[][] {
|
|
104
|
+
return md.split(/\n{2,}/).map(par => {
|
|
105
|
+
const runs: { kind: 'text' | 'bold' | 'code'; text: string }[] = [];
|
|
106
|
+
for (const m of par.matchAll(/\*\*([^*]+)\*\*|`([^`]+)`|([^*`]+|[*`])/g)) {
|
|
107
|
+
if (m[1] !== undefined) runs.push({ kind: 'bold', text: m[1] });
|
|
108
|
+
else if (m[2] !== undefined) runs.push({ kind: 'code', text: m[2] });
|
|
109
|
+
else runs.push({ kind: 'text', text: m[3] });
|
|
110
|
+
}
|
|
111
|
+
return runs;
|
|
112
|
+
});
|
|
113
|
+
}
|