@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,441 @@
|
|
|
1
|
+
import { parseDocument, stringify, isMap, isSeq, isScalar, isPair, Scalar, Document } from "yaml";
|
|
2
|
+
class SpliceError extends Error {
|
|
3
|
+
}
|
|
4
|
+
function applyStep(text, step) {
|
|
5
|
+
let out = text;
|
|
6
|
+
for (const e of [...step].sort((a, b) => b.start - a.start)) out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
7
|
+
return out;
|
|
8
|
+
}
|
|
9
|
+
function invertStep(text, step) {
|
|
10
|
+
const sorted = [...step].sort((a, b) => a.start - b.start);
|
|
11
|
+
let delta = 0;
|
|
12
|
+
return sorted.map((e) => {
|
|
13
|
+
const start = e.start + delta;
|
|
14
|
+
delta += e.text.length - (e.end - e.start);
|
|
15
|
+
return { start, end: start + e.text.length, text: text.slice(e.start, e.end) };
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const parse = (text) => ({ text, doc: parseDocument(text, { keepSourceTokens: true, uniqueKeys: false }) });
|
|
19
|
+
function keyOf(p) {
|
|
20
|
+
return isScalar(p.key) ? String(p.key.value) : String(p.key);
|
|
21
|
+
}
|
|
22
|
+
function find(c, path) {
|
|
23
|
+
let node = c.doc.contents;
|
|
24
|
+
let pair;
|
|
25
|
+
let parent;
|
|
26
|
+
const chain = [node];
|
|
27
|
+
for (const seg of path) {
|
|
28
|
+
pair = void 0;
|
|
29
|
+
if (isMap(node)) {
|
|
30
|
+
parent = node;
|
|
31
|
+
const p = node.items.find((x) => isPair(x) && keyOf(x) === String(seg));
|
|
32
|
+
if (!p) return { node: void 0, parent, chain };
|
|
33
|
+
pair = p;
|
|
34
|
+
node = p.value;
|
|
35
|
+
} else if (isSeq(node) && typeof seg === "number") {
|
|
36
|
+
parent = node;
|
|
37
|
+
node = node.items[seg];
|
|
38
|
+
if (node === void 0) return { node: void 0, parent, chain };
|
|
39
|
+
} else return { node: void 0, chain };
|
|
40
|
+
chain.push(node);
|
|
41
|
+
}
|
|
42
|
+
return { node, pair, parent, chain };
|
|
43
|
+
}
|
|
44
|
+
const inFlow = (chain) => chain.some((n) => (isMap(n) || isSeq(n)) && n.flow);
|
|
45
|
+
const lineStart = (t, off) => t.lastIndexOf("\n", off - 1) + 1;
|
|
46
|
+
const lineEnd = (t, off) => {
|
|
47
|
+
const i = t.indexOf("\n", off);
|
|
48
|
+
return i < 0 ? t.length : i;
|
|
49
|
+
};
|
|
50
|
+
const nextLine = (t, off) => {
|
|
51
|
+
const i = t.indexOf("\n", off);
|
|
52
|
+
return i < 0 ? t.length : i + 1;
|
|
53
|
+
};
|
|
54
|
+
const colOf = (t, off) => off - lineStart(t, off);
|
|
55
|
+
function pairEnd(c, p) {
|
|
56
|
+
const k = p.key;
|
|
57
|
+
const v = p.value;
|
|
58
|
+
const end = v?.range ? Math.max(v.range[1], k.range[1]) : k.range[1];
|
|
59
|
+
const last = end > 0 && c.text[end - 1] === "\n" ? end - 1 : end;
|
|
60
|
+
return nextLine(c.text, last);
|
|
61
|
+
}
|
|
62
|
+
function pairStart(c, p) {
|
|
63
|
+
const k = p.key.range[0];
|
|
64
|
+
let s = lineStart(c.text, k);
|
|
65
|
+
const ind = k - s;
|
|
66
|
+
if (onDash(c.text, k)) return k;
|
|
67
|
+
for (; ; ) {
|
|
68
|
+
if (s === 0) break;
|
|
69
|
+
const ps = lineStart(c.text, s - 1);
|
|
70
|
+
const line = c.text.slice(ps, s - 1);
|
|
71
|
+
if (/^\s*#/.test(line) && line.length - line.trimStart().length === ind) s = ps;
|
|
72
|
+
else break;
|
|
73
|
+
}
|
|
74
|
+
return s;
|
|
75
|
+
}
|
|
76
|
+
const firstOnLine = (t, k) => /^\s*(-\s+)*$/.test(t.slice(lineStart(t, k), k));
|
|
77
|
+
const onDash = (t, k) => /-\s+$/.test(t.slice(lineStart(t, k), k));
|
|
78
|
+
const SAFE_FLOW = /[,[\]{}]/;
|
|
79
|
+
function scalarText(v, opts) {
|
|
80
|
+
if (v === null || v === void 0) return "null";
|
|
81
|
+
if (typeof v !== "string") return String(v);
|
|
82
|
+
if (opts.style === Scalar.QUOTE_DOUBLE) return JSON.stringify(v);
|
|
83
|
+
if (opts.style === Scalar.QUOTE_SINGLE && !v.includes("\n")) return `'${v.replace(/'/g, "''")}'`;
|
|
84
|
+
if (v.includes("\n")) {
|
|
85
|
+
if (opts.flow) return JSON.stringify(v);
|
|
86
|
+
const body = v.endsWith("\n") ? v.slice(0, -1) : v;
|
|
87
|
+
if (/(^|\n)\s/.test(body.split("\n")[0] ?? "") || /^\s/.test(body)) return JSON.stringify(v);
|
|
88
|
+
return (v.endsWith("\n") ? "|" : "|-") + "\n" + body.split("\n").map((l) => l ? " ".repeat(opts.indent) + l : "").join("\n");
|
|
89
|
+
}
|
|
90
|
+
const plain = stringify(v, { lineWidth: 0 }).replace(/\n$/, "");
|
|
91
|
+
if (opts.flow && SAFE_FLOW.test(plain) && !/^["']/.test(plain)) return JSON.stringify(v);
|
|
92
|
+
return plain;
|
|
93
|
+
}
|
|
94
|
+
function flowText(v) {
|
|
95
|
+
if (Array.isArray(v)) return v.length ? `[${v.map(flowText).join(", ")}]` : "[]";
|
|
96
|
+
if (v && typeof v === "object") {
|
|
97
|
+
const e = Object.entries(v);
|
|
98
|
+
return e.length ? `{ ${e.map(([k, x]) => `${keyText(k)}: ${flowText(x)}`).join(", ")} }` : "{}";
|
|
99
|
+
}
|
|
100
|
+
return scalarText(v, { flow: true, indent: 0 });
|
|
101
|
+
}
|
|
102
|
+
const keyText = (k) => scalarText(k, { flow: true, indent: 0 });
|
|
103
|
+
const isObj = (v) => !!v && typeof v === "object";
|
|
104
|
+
const flowFits = (v) => {
|
|
105
|
+
if (Array.isArray(v)) return v.every((x) => !isObj(x) && !(typeof x === "string" && x.includes("\n"))) && flowText(v).length <= 60;
|
|
106
|
+
if (isObj(v)) {
|
|
107
|
+
const e = Object.entries(v);
|
|
108
|
+
return e.length > 0 && e.length <= 2 && e.every(([, x]) => !isObj(x) && !(typeof x === "string" && x.includes("\n")) || Array.isArray(x) && flowFits(x)) && flowText(v).length <= 40;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
};
|
|
112
|
+
function yamlText(v, ind = 0) {
|
|
113
|
+
const pad = " ".repeat(ind);
|
|
114
|
+
const inline = (x) => {
|
|
115
|
+
if (!isObj(x)) return scalarText(x, { flow: false, indent: ind + 2 });
|
|
116
|
+
if (Array.isArray(x) ? !x.length : !Object.keys(x).length) return Array.isArray(x) ? "[]" : "{}";
|
|
117
|
+
return flowFits(x) ? flowText(x) : void 0;
|
|
118
|
+
};
|
|
119
|
+
if (Array.isArray(v)) {
|
|
120
|
+
return v.map((x) => {
|
|
121
|
+
const one = inline(x);
|
|
122
|
+
if (one !== void 0) return `${pad}- ${one}`;
|
|
123
|
+
const inner = yamlText(x, ind + 2);
|
|
124
|
+
return Array.isArray(x) ? `${pad}-
|
|
125
|
+
${inner}` : `${pad}- ${inner.slice(ind + 2)}`;
|
|
126
|
+
}).join("\n");
|
|
127
|
+
}
|
|
128
|
+
return Object.entries(v).map(([k, x]) => {
|
|
129
|
+
const one = inline(x);
|
|
130
|
+
return one !== void 0 ? `${pad}${keyText(k)}: ${one}` : `${pad}${keyText(k)}:
|
|
131
|
+
${yamlText(x, ind + 2)}`;
|
|
132
|
+
}).join("\n");
|
|
133
|
+
}
|
|
134
|
+
function blockValue(v, indent) {
|
|
135
|
+
if (!isObj(v)) return " " + scalarText(v, { flow: false, indent: indent + 2 });
|
|
136
|
+
if (Array.isArray(v) ? !v.length : !Object.keys(v).length) return Array.isArray(v) ? " []" : " {}";
|
|
137
|
+
if (flowFits(v)) return " " + flowText(v);
|
|
138
|
+
return "\n" + reindent(yamlText(v), indent + 2);
|
|
139
|
+
}
|
|
140
|
+
const reindent = (t, n) => t.split("\n").map((l) => l ? " ".repeat(n) + l : l).join("\n");
|
|
141
|
+
function setValue(text, path, value) {
|
|
142
|
+
const c = parse(text);
|
|
143
|
+
const f = find(c, path);
|
|
144
|
+
if (f.node === void 0) throw new SpliceError(`no value at ${path.join(".")}`);
|
|
145
|
+
const flow = inFlow(f.chain.slice(0, -1));
|
|
146
|
+
const n = f.node;
|
|
147
|
+
const pairKey = f.pair?.key;
|
|
148
|
+
const indent = pairKey?.range ? colOf(text, pairKey.range[0]) : n?.range ? colOf(text, n.range[0]) : 0;
|
|
149
|
+
if (isScalar(n) && n.range && !isObj(value) && n.range[1] > n.range[0]) {
|
|
150
|
+
const style = n.type === Scalar.BLOCK_LITERAL || n.type === Scalar.BLOCK_FOLDED ? void 0 : n.type;
|
|
151
|
+
let t2 = scalarText(value, { flow, indent: indent + 2, style });
|
|
152
|
+
let end2 = n.range[1];
|
|
153
|
+
if (text[end2 - 1] === "\n" && !t2.endsWith("\n")) {
|
|
154
|
+
end2 -= 1;
|
|
155
|
+
while (end2 > n.range[0] && text[end2 - 1] === "\n") end2 -= 1;
|
|
156
|
+
}
|
|
157
|
+
if (t2.startsWith("|") && !text.slice(n.range[0], end2).includes("\n")) {
|
|
158
|
+
const lineE2 = lineEnd(text, end2);
|
|
159
|
+
const trail = text.slice(end2, lineE2);
|
|
160
|
+
if (/^\s*#/.test(trail)) {
|
|
161
|
+
t2 = t2.replace(/^(\|-?)/, `$1${trail}`);
|
|
162
|
+
end2 = lineE2;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return { steps: [[{ start: n.range[0], end: end2, text: t2 }]] };
|
|
166
|
+
}
|
|
167
|
+
if (flow || isObj(n) && n.flow || !f.pair) {
|
|
168
|
+
if (n?.range && n.range[1] > n.range[0]) {
|
|
169
|
+
const t2 = flow || !isObj(value) || !f.pair ? isObj(value) ? flowText(value) : scalarText(value, { flow, indent: indent + 2 }) : null;
|
|
170
|
+
if (t2 !== null) {
|
|
171
|
+
let end2 = n.range[1];
|
|
172
|
+
while (end2 > n.range[0] && text[end2 - 1] === "\n") end2 -= 1;
|
|
173
|
+
return { steps: [[{ start: n.range[0], end: end2, text: t2 }]] };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!f.pair || !pairKey?.range) return fallback(text, path.slice(0, -1), (d) => d.setIn(path, value));
|
|
178
|
+
const colon = text.indexOf(":", pairKey.range[1]);
|
|
179
|
+
const vEnd = n?.range ? n.range[1] : colon + 1;
|
|
180
|
+
const end = text[vEnd - 1] === "\n" ? vEnd - 1 : vEnd;
|
|
181
|
+
const lineE = lineEnd(text, colon);
|
|
182
|
+
const comment = n?.range && n.range[0] > lineE ? text.slice(colon + 1, lineE) : "";
|
|
183
|
+
const after = /^\s*#/.test(comment) ? comment : "";
|
|
184
|
+
let t = blockValue(value, indent);
|
|
185
|
+
let stop = end;
|
|
186
|
+
if (after && t.startsWith("\n")) t = after + t;
|
|
187
|
+
else if (after) t = t + after;
|
|
188
|
+
else if (t.startsWith("\n") && end <= lineE) {
|
|
189
|
+
const trail = text.slice(end, lineE);
|
|
190
|
+
if (/^\s*#/.test(trail)) {
|
|
191
|
+
t = trail + t;
|
|
192
|
+
stop = lineE;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return { steps: [[{ start: colon + 1, end: stop, text: t }]] };
|
|
196
|
+
}
|
|
197
|
+
function insertKey(text, mapPath, key, value, opts = {}) {
|
|
198
|
+
const TOP_ORDER = opts.order ?? [];
|
|
199
|
+
const c = parse(text);
|
|
200
|
+
const f = find(c, mapPath);
|
|
201
|
+
if (f.node === void 0 || f.node === null || isScalar(f.node) && f.node.value === null) {
|
|
202
|
+
if (!mapPath.length) {
|
|
203
|
+
const t = `${keyText(key)}:${blockValue(value, 0)}
|
|
204
|
+
`;
|
|
205
|
+
return { steps: [[{ start: text.length, end: text.length, text: (text && !text.endsWith("\n") ? "\n" : "") + t }]] };
|
|
206
|
+
}
|
|
207
|
+
if (f.node === void 0) return insertKey(text, mapPath.slice(0, -1), String(mapPath[mapPath.length - 1]), { [key]: value }, { order: opts.order });
|
|
208
|
+
return setValue(text, mapPath, { [key]: value });
|
|
209
|
+
}
|
|
210
|
+
const map = f.node;
|
|
211
|
+
if (!isMap(map)) throw new SpliceError(`${mapPath.join(".") || "the file"} is not a map`);
|
|
212
|
+
if (map.items.some((p) => keyOf(p) === key)) throw new SpliceError(`"${key}" is already in ${mapPath.join(".") || "the file"}`);
|
|
213
|
+
const flow = map.flow || inFlow(f.chain.slice(0, -1));
|
|
214
|
+
const items = map.items;
|
|
215
|
+
let at = items.length;
|
|
216
|
+
if (opts.after !== void 0) at = items.findIndex((p) => keyOf(p) === opts.after) + 1 || items.length;
|
|
217
|
+
else if (opts.before !== void 0) {
|
|
218
|
+
const i = items.findIndex((p) => keyOf(p) === opts.before);
|
|
219
|
+
if (i >= 0) at = i;
|
|
220
|
+
} else if (!mapPath.length && TOP_ORDER.includes(key)) {
|
|
221
|
+
const rank = TOP_ORDER.indexOf(key);
|
|
222
|
+
const i = items.findIndex((p) => {
|
|
223
|
+
const r = TOP_ORDER.indexOf(keyOf(p));
|
|
224
|
+
return r > rank || keyOf(p).startsWith("x-");
|
|
225
|
+
});
|
|
226
|
+
if (i >= 0) at = i;
|
|
227
|
+
}
|
|
228
|
+
if (flow && map.range && text[map.range[0]] !== "{") {
|
|
229
|
+
const now = c.doc.getIn(mapPath, true).toJSON();
|
|
230
|
+
return { steps: [[{ start: map.range[0], end: map.range[1], text: flowText({ ...now, [key]: value }) }]] };
|
|
231
|
+
}
|
|
232
|
+
if (flow) {
|
|
233
|
+
const t = `${keyText(key)}: ${flowText(value)}`;
|
|
234
|
+
if (!items.length) {
|
|
235
|
+
const r = map.range;
|
|
236
|
+
return { steps: [[{ start: r[0], end: text.lastIndexOf("}", r[1]) + 1, text: `{ ${t} }` }]] };
|
|
237
|
+
}
|
|
238
|
+
if (at < items.length) {
|
|
239
|
+
const k = items[at].key.range[0];
|
|
240
|
+
return { steps: [[{ start: k, end: k, text: `${t}, ` }]] };
|
|
241
|
+
}
|
|
242
|
+
const last = items[items.length - 1];
|
|
243
|
+
const e = last.value?.range?.[1] ?? last.key.range[1];
|
|
244
|
+
return { steps: [[{ start: e, end: e, text: `, ${t}` }]] };
|
|
245
|
+
}
|
|
246
|
+
const first = items[0]?.key;
|
|
247
|
+
if (!first?.range) return fallback(text, mapPath, (d) => d.setIn([...mapPath, key], value), true);
|
|
248
|
+
const indent = colOf(text, first.range[0]);
|
|
249
|
+
if (items.some((p) => !firstOnLine(text, p.key.range[0]))) {
|
|
250
|
+
return fallback(text, mapPath, (d) => d.setIn([...mapPath, key], value), true);
|
|
251
|
+
}
|
|
252
|
+
const body = `${" ".repeat(indent)}${keyText(key)}:${blockValue(value, indent)}
|
|
253
|
+
`;
|
|
254
|
+
const blank = !mapPath.length && isObj(value) ? "\n" : "";
|
|
255
|
+
if (at === 0 && items.length && onDash(text, first.range[0])) {
|
|
256
|
+
const k = first.range[0];
|
|
257
|
+
return { steps: [[{ start: k, end: k, text: body.slice(indent) + " ".repeat(indent) }]] };
|
|
258
|
+
}
|
|
259
|
+
if (at < items.length) {
|
|
260
|
+
const s = pairStart(c, items[at]);
|
|
261
|
+
return { steps: [[{ start: s, end: s, text: blank && at > 0 ? body + "\n" : body }]] };
|
|
262
|
+
}
|
|
263
|
+
const end = pairEnd(c, items[items.length - 1]);
|
|
264
|
+
const pre = end === text.length && text.length && !text.endsWith("\n") ? "\n" : "";
|
|
265
|
+
return { steps: [[{ start: end, end, text: pre + blank + body }]] };
|
|
266
|
+
}
|
|
267
|
+
function deleteAt(text, path) {
|
|
268
|
+
if (!path.length) throw new SpliceError("can't delete the whole file");
|
|
269
|
+
const c = parse(text);
|
|
270
|
+
const parentPath = path.slice(0, -1);
|
|
271
|
+
const pf = find(c, parentPath);
|
|
272
|
+
const coll = pf.node;
|
|
273
|
+
const seg = path[path.length - 1];
|
|
274
|
+
if (!isMap(coll) && !isSeq(coll)) throw new SpliceError(`no entry at ${path.join(".")}`);
|
|
275
|
+
const items = coll.items;
|
|
276
|
+
const i = isMap(coll) ? items.findIndex((p) => keyOf(p) === String(seg)) : Number(seg);
|
|
277
|
+
if (i < 0 || i >= items.length) throw new SpliceError(`no entry at ${path.join(".")}`);
|
|
278
|
+
const flow = coll.flow;
|
|
279
|
+
const startOf = (x) => isPair(x) ? x.key.range[0] : x.range[0];
|
|
280
|
+
const endOf = (x) => isPair(x) ? x.value?.range?.[1] ?? x.key.range[1] : x.range[1];
|
|
281
|
+
if (items.length === 1) {
|
|
282
|
+
if (!parentPath.length && isMap(coll) && !coll.flow) return { steps: [[{ start: pairStart(c, items[0]), end: pairEnd(c, items[0]), text: "" }]] };
|
|
283
|
+
if (!parentPath.length) return { steps: [[{ start: 0, end: text.length, text: "" }]] };
|
|
284
|
+
return setValue(text, parentPath, isMap(coll) ? {} : []);
|
|
285
|
+
}
|
|
286
|
+
if (flow) {
|
|
287
|
+
if (i < items.length - 1) return { steps: [[{ start: startOf(items[i]), end: startOf(items[i + 1]), text: "" }]] };
|
|
288
|
+
return { steps: [[{ start: endOf(items[i - 1]), end: endOf(items[i]), text: "" }]] };
|
|
289
|
+
}
|
|
290
|
+
if (isMap(coll)) {
|
|
291
|
+
const p = items[i];
|
|
292
|
+
const k = p.key.range[0];
|
|
293
|
+
if (!firstOnLine(text, k)) return fallback(text, parentPath, (d) => d.deleteIn(path), true);
|
|
294
|
+
if (onDash(text, k)) return { steps: [[{ start: k, end: items[i + 1] ? items[i + 1].key.range[0] : pairEnd(c, p), text: "" }]] };
|
|
295
|
+
return { steps: [[{ start: pairStart(c, p), end: pairEnd(c, p), text: "" }]] };
|
|
296
|
+
}
|
|
297
|
+
const it = items[i];
|
|
298
|
+
const s = lineStart(text, it.range[0]);
|
|
299
|
+
if (!/^\s*-/.test(text.slice(s, it.range[0]))) return fallback(text, parentPath, (d) => d.deleteIn(path), true);
|
|
300
|
+
const e = it.range[1];
|
|
301
|
+
const end = nextLine(text, text[e - 1] === "\n" ? e - 1 : e);
|
|
302
|
+
return { steps: [[{ start: s, end, text: "" }]] };
|
|
303
|
+
}
|
|
304
|
+
function insertItem(text, listPath, value, index, order) {
|
|
305
|
+
const c = parse(text);
|
|
306
|
+
const f = find(c, listPath);
|
|
307
|
+
if (f.node === void 0 || f.node === null || isScalar(f.node) && f.node.value === null) {
|
|
308
|
+
if (f.node === void 0) return insertKey(text, listPath.slice(0, -1), String(listPath[listPath.length - 1]), [value], { order });
|
|
309
|
+
return setValue(text, listPath, [value]);
|
|
310
|
+
}
|
|
311
|
+
const list = f.node;
|
|
312
|
+
if (!isSeq(list)) throw new SpliceError(`${listPath.join(".")} is not a list`);
|
|
313
|
+
const items = list.items;
|
|
314
|
+
const at = index === void 0 ? items.length : Math.max(0, Math.min(index, items.length));
|
|
315
|
+
if (list.flow || inFlow(f.chain.slice(0, -1))) {
|
|
316
|
+
const t = flowText(value);
|
|
317
|
+
if (!items.length) {
|
|
318
|
+
const r = list.range;
|
|
319
|
+
const close = text.lastIndexOf("]", r[1]);
|
|
320
|
+
return { steps: [[{ start: r[0], end: close + 1, text: `[${t}]` }]] };
|
|
321
|
+
}
|
|
322
|
+
if (at < items.length) {
|
|
323
|
+
const s = items[at].range[0];
|
|
324
|
+
return { steps: [[{ start: s, end: s, text: `${t}, ` }]] };
|
|
325
|
+
}
|
|
326
|
+
const e2 = items[items.length - 1].range[1];
|
|
327
|
+
return { steps: [[{ start: e2, end: e2, text: `, ${t}` }]] };
|
|
328
|
+
}
|
|
329
|
+
const first = items[0];
|
|
330
|
+
const dash = lineStart(text, first.range[0]);
|
|
331
|
+
const prefix = text.slice(dash, first.range[0]);
|
|
332
|
+
if (!/^\s*- $/.test(prefix)) return fallback(text, listPath, (d) => {
|
|
333
|
+
const s = d.getIn(listPath);
|
|
334
|
+
s.items.splice(at, 0, d.createNode(value));
|
|
335
|
+
}, true);
|
|
336
|
+
const indent = prefix.indexOf("-");
|
|
337
|
+
const body = " ".repeat(indent) + "- " + (isObj(value) && Object.keys(value).length ? reindent(yamlText(value), indent + 2).slice(indent + 2) : scalarText(value, { flow: false, indent: indent + 2 })) + "\n";
|
|
338
|
+
if (at < items.length) {
|
|
339
|
+
const s = lineStart(text, items[at].range[0]);
|
|
340
|
+
return { steps: [[{ start: s, end: s, text: body }]] };
|
|
341
|
+
}
|
|
342
|
+
const e = items[items.length - 1].range[1];
|
|
343
|
+
const end = nextLine(text, text[e - 1] === "\n" ? e - 1 : e);
|
|
344
|
+
const pre = end === text.length && !text.endsWith("\n") ? "\n" : "";
|
|
345
|
+
return { steps: [[{ start: end, end, text: pre + body }]] };
|
|
346
|
+
}
|
|
347
|
+
function renameKey(text, path, to) {
|
|
348
|
+
const c = parse(text);
|
|
349
|
+
const f = find(c, path);
|
|
350
|
+
if (!f.pair) throw new SpliceError(`no key at ${path.join(".")}`);
|
|
351
|
+
if (f.parent.items.some((p) => keyOf(p) === to)) throw new SpliceError(`"${to}" is already there`);
|
|
352
|
+
const k = f.pair.key;
|
|
353
|
+
return { steps: [[{ start: k.range[0], end: k.range[1], text: keyText(to) }]] };
|
|
354
|
+
}
|
|
355
|
+
function moveKey(text, path, before, order) {
|
|
356
|
+
const c = parse(text);
|
|
357
|
+
const f = find(c, path);
|
|
358
|
+
if (!f.pair || !isMap(f.parent)) throw new SpliceError(`no key at ${path.join(".")}`);
|
|
359
|
+
const map = f.parent;
|
|
360
|
+
const dashy = onDash(text, f.pair.key.range[0]) || map.items.some((p) => onDash(text, p.key.range[0]));
|
|
361
|
+
if (map.flow || dashy) {
|
|
362
|
+
const v = f.pair.value;
|
|
363
|
+
const value = v ? c.doc.getIn(path)?.toJSON?.() ?? c.doc.getIn(path) : null;
|
|
364
|
+
const del = deleteAt(text, path);
|
|
365
|
+
const mid = applyStep(text, del.steps[0]);
|
|
366
|
+
const ins = insertKey(mid, path.slice(0, -1), String(path[path.length - 1]), value, before ? { before, order } : { order });
|
|
367
|
+
return { steps: [...del.steps, ...ins.steps] };
|
|
368
|
+
}
|
|
369
|
+
const s = pairStart(c, f.pair), e = pairEnd(c, f.pair);
|
|
370
|
+
const chunk = text.slice(s, e);
|
|
371
|
+
const target = before ? map.items.find((p) => keyOf(p) === before) : void 0;
|
|
372
|
+
if (target === f.pair) return { steps: [] };
|
|
373
|
+
const at = target ? pairStart(c, target) : pairEnd(c, map.items[map.items.length - 1]);
|
|
374
|
+
const body = chunk.endsWith("\n") ? chunk : chunk + "\n";
|
|
375
|
+
return { steps: [[{ start: s, end: e, text: "" }, { start: at, end: at, text: at === text.length && !text.endsWith("\n") ? "\n" + body : body }]] };
|
|
376
|
+
}
|
|
377
|
+
function fallback(text, collPath, mutate, _structural = false) {
|
|
378
|
+
const c = parse(text);
|
|
379
|
+
const f = find(c, collPath);
|
|
380
|
+
const node = f.node;
|
|
381
|
+
mutate(c.doc);
|
|
382
|
+
if (!collPath.length || !node?.range) return { steps: [[{ start: 0, end: text.length, text: c.doc.toString({ lineWidth: 0 }) }]], fallback: true };
|
|
383
|
+
const after = c.doc.getIn(collPath, true);
|
|
384
|
+
const flow = node.flow;
|
|
385
|
+
const one = new Document();
|
|
386
|
+
one.contents = after;
|
|
387
|
+
let t = one.toString({ lineWidth: 0, indent: 2 }).replace(/\n$/, "");
|
|
388
|
+
const r = node.range;
|
|
389
|
+
if (flow) return { steps: [[{ start: r[0], end: r[1], text: t.replace(/\n\s*/g, " ") }]], fallback: true };
|
|
390
|
+
const s = lineStart(text, r[0]);
|
|
391
|
+
const ind = r[0] - s;
|
|
392
|
+
const e = r[1] > 0 && text[r[1] - 1] === "\n" ? r[1] - 1 : lineEnd(text, r[1]);
|
|
393
|
+
t = reindent(t, ind).slice(text.slice(s, r[0]).trim() ? ind : 0);
|
|
394
|
+
return { steps: [[{ start: text.slice(s, r[0]).trim() ? r[0] : s, end: e, text: t }]], fallback: true };
|
|
395
|
+
}
|
|
396
|
+
function insertRawPair(text, mapPath, key, pairText2, order) {
|
|
397
|
+
const c = parse(text);
|
|
398
|
+
const f = find(c, mapPath);
|
|
399
|
+
const map = f.node;
|
|
400
|
+
const value = parseDocument(pairText2).toJS()?.[key];
|
|
401
|
+
if (!isMap(map) || map.flow || inFlow(f.chain.slice(0, -1)) || !map.items.length) return insertKey(text, mapPath, key, value, { order });
|
|
402
|
+
const items = map.items;
|
|
403
|
+
const first = items[0].key;
|
|
404
|
+
if (!first.range || items.some((p) => !firstOnLine(text, p.key.range[0]) || onDash(text, p.key.range[0]))) return insertKey(text, mapPath, key, value, { order });
|
|
405
|
+
if (items.some((p) => keyOf(p) === key)) throw new SpliceError(`"${key}" is already in ${mapPath.join(".") || "the file"}`);
|
|
406
|
+
const indent = colOf(text, first.range[0]);
|
|
407
|
+
const body = reindent(pairText2.replace(/\n+$/, ""), indent) + "\n";
|
|
408
|
+
const end = pairEnd(c, items[items.length - 1]);
|
|
409
|
+
const pre = end === text.length && text.length && !text.endsWith("\n") ? "\n" : "";
|
|
410
|
+
return { steps: [[{ start: end, end, text: pre + body }]] };
|
|
411
|
+
}
|
|
412
|
+
function pairText(text, path) {
|
|
413
|
+
const c = parse(text);
|
|
414
|
+
const f = find(c, path);
|
|
415
|
+
if (!f.pair || !isMap(f.parent)) return void 0;
|
|
416
|
+
const k = f.pair.key.range[0];
|
|
417
|
+
if (f.parent.flow || inFlow(f.chain.slice(0, -1)) || !firstOnLine(text, k) || onDash(text, k)) {
|
|
418
|
+
const v = c.doc.getIn(path, true);
|
|
419
|
+
const value = v && typeof v === "object" && v.toJSON ? v.toJSON() : c.doc.getIn(path);
|
|
420
|
+
return `${keyText(String(path[path.length - 1]))}:${blockValue(value, 0)}
|
|
421
|
+
`;
|
|
422
|
+
}
|
|
423
|
+
const s = pairStart(c, f.pair), e = pairEnd(c, f.pair);
|
|
424
|
+
const ind = colOf(text, k);
|
|
425
|
+
return text.slice(s, e).split("\n").map((l) => l.slice(0, ind).trim() === "" ? l.slice(ind) : l.trimStart()).join("\n").replace(/\n*$/, "\n");
|
|
426
|
+
}
|
|
427
|
+
export {
|
|
428
|
+
SpliceError,
|
|
429
|
+
applyStep,
|
|
430
|
+
deleteAt,
|
|
431
|
+
flowText,
|
|
432
|
+
insertItem,
|
|
433
|
+
insertKey,
|
|
434
|
+
insertRawPair,
|
|
435
|
+
invertStep,
|
|
436
|
+
moveKey,
|
|
437
|
+
pairText,
|
|
438
|
+
renameKey,
|
|
439
|
+
scalarText,
|
|
440
|
+
setValue
|
|
441
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { h } from "./icons.js";
|
|
2
|
+
import { ask } from "./dialog.js";
|
|
3
|
+
import { applyStep } from "../splice.js";
|
|
4
|
+
import { fromPointer, isPrefix, toPointer } from "../path.js";
|
|
5
|
+
const getIn = (o, p) => p.reduce((x, k) => x && typeof x === "object" ? x[k] : void 0, o);
|
|
6
|
+
function run(doc, c, origin, note) {
|
|
7
|
+
try {
|
|
8
|
+
return doc.apply(c, origin);
|
|
9
|
+
} catch (e) {
|
|
10
|
+
note?.(String(e.message), "bad");
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function composite(label, cmds) {
|
|
15
|
+
return {
|
|
16
|
+
label,
|
|
17
|
+
remap: (p) => cmds.reduce((x, c) => x && c.remap ? c.remap(x) : x, p),
|
|
18
|
+
build: (t) => {
|
|
19
|
+
const steps = [];
|
|
20
|
+
let fallback = false;
|
|
21
|
+
for (const c of cmds) {
|
|
22
|
+
const s = c.build(t);
|
|
23
|
+
for (const st of s.steps) {
|
|
24
|
+
steps.push(st);
|
|
25
|
+
t = applyStep(t, st);
|
|
26
|
+
}
|
|
27
|
+
fallback ||= !!s.fallback;
|
|
28
|
+
}
|
|
29
|
+
return { steps, fallback };
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function actionsFor(kit) {
|
|
34
|
+
const { deleteCmd, renameCmd, renameKeyCmd, referencesTo, describe, duplicateCmd } = kit.commands;
|
|
35
|
+
const isRes = (p) => {
|
|
36
|
+
const r = kit.objectOf(toPointer(p));
|
|
37
|
+
return !!r && r.path === toPointer(p);
|
|
38
|
+
};
|
|
39
|
+
async function deletePaths(doc, paths, origin, note) {
|
|
40
|
+
const top = paths.filter((p) => p.length && !paths.some((o) => o !== p && o.length < p.length && isPrefix(o, p)));
|
|
41
|
+
if (!top.length) return false;
|
|
42
|
+
const refs = top.filter(isRes).flatMap((p) => {
|
|
43
|
+
const r = kit.objectOf(toPointer(p));
|
|
44
|
+
return referencesTo(doc.text, r.kind, r.name).filter((x) => !top.some((o) => isPrefix(o, fromPointer(x.path)))).map((x) => ({ p, x }));
|
|
45
|
+
});
|
|
46
|
+
let withRefs = false;
|
|
47
|
+
if (refs.length) {
|
|
48
|
+
const choice = await ask(`Delete ${top.length === 1 ? describe(top[0]) : `${top.length} items`}?`, [
|
|
49
|
+
h("p", { class: "sub" }, `${refs.length === 1 ? "One place refers" : `${refs.length} places refer`} to ${top.length === 1 ? "it" : "them"}:`),
|
|
50
|
+
h("ul", { class: "ref-list" }, ...refs.map(({ x }) => h("li", null, h("code", null, x.path.slice(1).replace(/\//g, ".")), ` \u2014 ${x.slot.what}`))),
|
|
51
|
+
h("p", { class: "sub" }, "Delete the references too, or leave them (the problems list will show them). Either way, Undo brings everything back.")
|
|
52
|
+
], [{ label: "Cancel", value: "cancel" }, { label: "Delete only", value: "only", danger: true }, { label: "Delete with references", value: "with", danger: true, primary: true }]);
|
|
53
|
+
if (!choice) return false;
|
|
54
|
+
withRefs = choice === "with";
|
|
55
|
+
}
|
|
56
|
+
const order = [...top].sort((a, b) => typeof a.at(-1) === "number" && typeof b.at(-1) === "number" ? b.at(-1) - a.at(-1) : 0);
|
|
57
|
+
const cmds = order.map((p) => deleteCmd(p, { withRefs: withRefs && isRes(p) }));
|
|
58
|
+
const ok = run(doc, cmds.length === 1 ? cmds[0] : composite(`Delete ${top.length} items`, cmds), origin, note);
|
|
59
|
+
if (ok) doc.select([], origin);
|
|
60
|
+
return ok;
|
|
61
|
+
}
|
|
62
|
+
async function renameDialog(doc, path, origin, note) {
|
|
63
|
+
const now = String(path[path.length - 1]);
|
|
64
|
+
const input = h("input", { type: "text", value: now, "aria-label": "New name", spellcheck: "false", autocomplete: "off", class: "mono-input" });
|
|
65
|
+
input.value = now;
|
|
66
|
+
const why = h("p", { class: "dlg-error", role: "alert" });
|
|
67
|
+
const used = () => kit.kinds.flatMap((k) => Object.keys(getIn(doc.goodDoc.data, k.section) ?? {}));
|
|
68
|
+
const check = () => {
|
|
69
|
+
const v2 = input.value.trim();
|
|
70
|
+
const bad = !kit.nameRule.test(v2) && isRes(path) ? kit.nameHint : v2 !== now && used().includes(v2) ? `"${v2}" is already used` : "";
|
|
71
|
+
why.textContent = bad;
|
|
72
|
+
return bad ? void 0 : v2;
|
|
73
|
+
};
|
|
74
|
+
input.addEventListener("input", check);
|
|
75
|
+
const go = ask(`Rename ${describe(path)}`, [h("label", { class: "field" }, h("span", null, "Name"), input), why, isRes(path) ? h("p", { class: "sub" }, "Every reference to it is renamed too.") : ""], [{ label: "Cancel", value: "cancel" }, { label: "Rename", value: "ok", primary: true }]);
|
|
76
|
+
setTimeout(() => {
|
|
77
|
+
input.focus();
|
|
78
|
+
input.select();
|
|
79
|
+
}, 0);
|
|
80
|
+
if (await go !== "ok") return false;
|
|
81
|
+
const v = check();
|
|
82
|
+
if (!v || v === now) return false;
|
|
83
|
+
return run(doc, isRes(path) ? renameCmd(path, v) : renameKeyCmd(path, v), origin, note);
|
|
84
|
+
}
|
|
85
|
+
const duplicate = (doc, path, origin, note) => {
|
|
86
|
+
const c = duplicateCmd(path);
|
|
87
|
+
if (run(doc, c, origin, note) && c.select) doc.select(c.select, origin);
|
|
88
|
+
};
|
|
89
|
+
function connected(doc, paths) {
|
|
90
|
+
const out = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const p of paths) {
|
|
92
|
+
const r = kit.objectOf(toPointer(p));
|
|
93
|
+
if (!r) continue;
|
|
94
|
+
out.set(r.path, fromPointer(r.path));
|
|
95
|
+
for (const x of referencesTo(doc.text, r.kind, r.name)) {
|
|
96
|
+
const q = kit.objectOf(x.path);
|
|
97
|
+
out.set(q ? q.path : x.path, fromPointer(q ? q.path : x.path));
|
|
98
|
+
}
|
|
99
|
+
const v = getIn(doc.goodDoc.data, fromPointer(r.path));
|
|
100
|
+
const names = JSON.stringify(v ?? {}).match(/[a-z][a-z0-9-]*/g) ?? [];
|
|
101
|
+
for (const k of kit.kinds) for (const n of Object.keys(getIn(doc.goodDoc.data, k.section) ?? {})) {
|
|
102
|
+
if (names.includes(n) && n !== r.name) {
|
|
103
|
+
const q = toPointer([...k.section, n]);
|
|
104
|
+
out.set(q, fromPointer(q));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return [...out.values()];
|
|
109
|
+
}
|
|
110
|
+
return { deletePaths, renameDialog, duplicate, connected };
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
actionsFor,
|
|
114
|
+
composite,
|
|
115
|
+
run
|
|
116
|
+
};
|
|
File without changes
|