@johnoscott/isometric-engine 0.12.6 → 0.14.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 +9 -2
- package/README.md +1 -1
- package/assets/app.css +6 -0
- package/assets/marks.css +2 -0
- package/iso-editor.js +188 -187
- package/js/iso-editor/make-way.js +62 -0
- package/js/iso-editor/ui/canvas.js +477 -216
- package/js/iso-editor/ui/palette-look.js +53 -0
- package/js/iso-editor/ui/setup.js +3 -0
- package/package.json +1 -1
- package/src/iso-editor/make-way.ts +89 -0
- package/src/iso-editor/ui/app.css +4 -0
- package/src/iso-editor/ui/canvas.ts +390 -170
- package/src/iso-editor/ui/marks.css +2 -0
- package/src/iso-editor/ui/palette-look.ts +51 -0
- package/src/iso-editor/ui/setup.ts +8 -0
- package/types/iso-editor/make-way.d.ts +40 -0
- package/types/iso-editor/ui/canvas.d.ts +33 -12
- package/types/iso-editor/ui/palette-look.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +5 -0
|
@@ -7,6 +7,7 @@ import { regionKind } from "../../iso-objects/kinds.js";
|
|
|
7
7
|
import { iso, C, S } from "../../iso-core/kernel.js";
|
|
8
8
|
import { applyStep } from "../splice.js";
|
|
9
9
|
import { seqCmd } from "../commands.js";
|
|
10
|
+
import { makeWay, hits, DWELL_HINT, DWELL_GIVE, BACK_AFTER, GLIDE, JIGGLE } from "../make-way.js";
|
|
10
11
|
import { fromPointer, toPointer } from "../path.js";
|
|
11
12
|
import { buildSvg } from "../../iso-core/svg-dom.js";
|
|
12
13
|
import { viewport } from "../../iso-viewer/viewport.js";
|
|
@@ -148,7 +149,8 @@ function canvasView(host, doc, opts) {
|
|
|
148
149
|
lastKey = key2;
|
|
149
150
|
lastS3 = s3;
|
|
150
151
|
lastData = data;
|
|
151
|
-
|
|
152
|
+
drawLooks();
|
|
153
|
+
if (pinDrag() || dropping) {
|
|
152
154
|
pendingAfterDrag = true;
|
|
153
155
|
return;
|
|
154
156
|
}
|
|
@@ -489,14 +491,21 @@ function canvasView(host, doc, opts) {
|
|
|
489
491
|
stage.addEventListener("pointerdown", down);
|
|
490
492
|
stage.addEventListener("pointermove", move);
|
|
491
493
|
stage.addEventListener("pointerup", up);
|
|
492
|
-
stage.addEventListener("pointercancel",
|
|
494
|
+
stage.addEventListener("pointercancel", (e) => {
|
|
495
|
+
if (pinDrag()) void endPin(false);
|
|
496
|
+
else up(e);
|
|
497
|
+
});
|
|
493
498
|
stage.addEventListener("wheel", wheel, { passive: false });
|
|
494
499
|
stage.addEventListener("contextmenu", context);
|
|
495
500
|
stage.addEventListener("dblclick", (e) => {
|
|
496
|
-
const
|
|
497
|
-
if (
|
|
498
|
-
|
|
499
|
-
|
|
501
|
+
const t = e.target === stage ? fdoc().elementFromPoint(e.clientX, e.clientY) : e.target;
|
|
502
|
+
if (ui.doubleClick === "rename") {
|
|
503
|
+
const p2 = pathAtEvent(t), r = p2 === void 0 ? p2 : resourceOf(p2);
|
|
504
|
+
if (r && doc.doc.ok) rename(fromPointer(r.path));
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const p = pathAt(t, e.clientX, e.clientY);
|
|
508
|
+
if (p !== void 0) opts.showInText(fromPointer(p));
|
|
500
509
|
});
|
|
501
510
|
};
|
|
502
511
|
const pageXY = (e) => ({ x: e.clientX, y: e.clientY });
|
|
@@ -566,7 +575,6 @@ function canvasView(host, doc, opts) {
|
|
|
566
575
|
const start = localAt(cd.scene, id);
|
|
567
576
|
if (item && start) {
|
|
568
577
|
const b = item.box, par = item.parent;
|
|
569
|
-
const siblings = (par ? par.children : cd.scene.layout.items.filter((i) => !i.parent)).filter((i) => i !== item).map((i) => ({ x0: i.box.x0, y0: i.box.y0, x1: i.box.x1, y1: i.box.y1 }));
|
|
570
578
|
drag = {
|
|
571
579
|
kind: "pin",
|
|
572
580
|
x0: x,
|
|
@@ -579,8 +587,8 @@ function canvasView(host, doc, opts) {
|
|
|
579
587
|
shown: start,
|
|
580
588
|
z0: b.z0,
|
|
581
589
|
size: [b.x1 - b.x0, b.y1 - b.y0],
|
|
582
|
-
|
|
583
|
-
parent: par ? { w: par.box.x1 - par.box.x0, d: par.box.y1 - par.box.y0, pad: paddingOf(K.catalog, par.region), box: par.box, label: par.region?.label ?? par.id } : void 0,
|
|
590
|
+
beside: besideOf(cd, id),
|
|
591
|
+
parent: par ? { id: par.id, w: par.box.x1 - par.box.x0, d: par.box.y1 - par.box.y0, pad: paddingOf(K.catalog, par.region), box: par.box, label: par.region?.label ?? par.id } : void 0,
|
|
584
592
|
origCd: cd
|
|
585
593
|
};
|
|
586
594
|
}
|
|
@@ -623,15 +631,15 @@ function canvasView(host, doc, opts) {
|
|
|
623
631
|
}
|
|
624
632
|
const [ax, ay] = screenToGrid(a.x, a.y), [bx, by] = screenToGrid(b.x, b.y);
|
|
625
633
|
const g = { x0: Math.min(ax, bx), y0: Math.min(ay, by), x1: Math.max(ax, bx), y1: Math.max(ay, by) };
|
|
626
|
-
const
|
|
634
|
+
const hits2 = /* @__PURE__ */ new Map();
|
|
627
635
|
for (const [ptr, foot] of footprints()) {
|
|
628
636
|
const f = foot;
|
|
629
637
|
const inside = f.x0 >= g.x0 && f.y0 >= g.y0 && f.x1 <= g.x1 && f.y1 <= g.y1;
|
|
630
638
|
const touches = f.x0 < g.x1 && f.x1 > g.x0 && f.y0 < g.y1 && f.y1 > g.y0;
|
|
631
|
-
if (drag.touch ? touches : inside)
|
|
639
|
+
if (drag.touch ? touches : inside) hits2.set(ptr, fromPointer(ptr));
|
|
632
640
|
}
|
|
633
641
|
const base2 = new Map((drag.sel0 ?? []).map((p) => [toPointer(p), p]));
|
|
634
|
-
for (const [k2, p] of
|
|
642
|
+
for (const [k2, p] of hits2) base2.set(k2, p);
|
|
635
643
|
doc.select([...base2.values()], "canvas");
|
|
636
644
|
paintOverlay();
|
|
637
645
|
const poly = fdoc().createElementNS(SVGNS, "polygon");
|
|
@@ -659,13 +667,13 @@ function canvasView(host, doc, opts) {
|
|
|
659
667
|
function marquee2d(a, b) {
|
|
660
668
|
if (!drag || !flat || !cd) return;
|
|
661
669
|
const g = { l: Math.min(a.x, b.x), t: Math.min(a.y, b.y), r: Math.max(a.x, b.x), b: Math.max(a.y, b.y) };
|
|
662
|
-
const
|
|
670
|
+
const hits2 = /* @__PURE__ */ new Map();
|
|
663
671
|
for (const id of flat.hits(g, !!drag.touch)) {
|
|
664
672
|
const ptr = cd.pathOf.get(id);
|
|
665
|
-
if (ptr !== void 0 && resourceOf(ptr)?.path === ptr)
|
|
673
|
+
if (ptr !== void 0 && resourceOf(ptr)?.path === ptr) hits2.set(ptr, fromPointer(ptr));
|
|
666
674
|
}
|
|
667
675
|
const base2 = new Map((drag.sel0 ?? []).map((p) => [toPointer(p), p]));
|
|
668
|
-
for (const [k2, p] of
|
|
676
|
+
for (const [k2, p] of hits2) base2.set(k2, p);
|
|
669
677
|
doc.select([...base2.values()], "canvas");
|
|
670
678
|
paintOverlay();
|
|
671
679
|
const r = fdoc().createElementNS(SVGNS, "rect");
|
|
@@ -678,74 +686,246 @@ function canvasView(host, doc, opts) {
|
|
|
678
686
|
}
|
|
679
687
|
let pinFrame = false, lastPin, relayout = 0;
|
|
680
688
|
const pinDrag = () => drag?.kind === "pin" && drag.moved ? drag : void 0;
|
|
689
|
+
function besideOf(d, id, inside = d.scene.layout.byId.get(id)?.parent) {
|
|
690
|
+
const pinned = new Set(d.pinId.values());
|
|
691
|
+
return (inside ? inside.children : d.scene.layout.items.filter((i) => !i.parent)).filter((i) => i.id !== id).map((i) => ({ id: i.id, x0: i.box.x0, y0: i.box.y0, x1: i.box.x1, y1: i.box.y1, movable: pinned.has(i.id) && !!localAt(d.scene, i.id) }));
|
|
692
|
+
}
|
|
693
|
+
const besideNow = (g) => g.beside.map((b) => {
|
|
694
|
+
const [dx, dy] = g.given?.get(b.id) ?? [0, 0];
|
|
695
|
+
return { ...b, x0: b.x0 + dx, x1: b.x1 + dx, y0: b.y0 + dy, y1: b.y1 + dy };
|
|
696
|
+
});
|
|
697
|
+
const footAt = (g, [x, y]) => {
|
|
698
|
+
const ox = g.parent ? g.parent.box.x0 : 0, oy = g.parent ? g.parent.box.y0 : 0;
|
|
699
|
+
return { x0: ox + x, y0: oy + y, x1: ox + x + g.size[0], y1: oy + y + g.size[1] };
|
|
700
|
+
};
|
|
701
|
+
const motionOn = () => ui.dragMotion !== "respect" || !matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
681
702
|
function pinStep() {
|
|
682
703
|
const g = pinDrag();
|
|
683
704
|
if (!g || !lastPin || !cd) return;
|
|
684
705
|
const k = scale();
|
|
685
706
|
const step = lastPin.alt ? 0.5 : 1;
|
|
686
707
|
g.step = step;
|
|
687
|
-
const
|
|
688
|
-
const
|
|
689
|
-
const
|
|
708
|
+
const p = g.fresh ? toSvg(lastPin.x, lastPin.y) : void 0;
|
|
709
|
+
const f0 = footAt(g, g.start), grip = iso((f0.x0 + f0.x1) / 2, (f0.y0 + f0.y1) / 2, g.z0);
|
|
710
|
+
const d = p ? screenToGrid(p.x - grip[0], p.y - grip[1]) : screenToGrid((lastPin.x - g.x0) / k, (lastPin.y - g.y0) / k);
|
|
711
|
+
const m = snapMove(g.start, d, step, g.size, g.parent);
|
|
712
|
+
const foot = footAt(g, m.at);
|
|
690
713
|
const changed = !g.move || g.move.at[0] !== m.at[0] || g.move.at[1] !== m.at[1];
|
|
691
714
|
g.move = m;
|
|
692
|
-
g.invalid = overlaps(foot, g
|
|
715
|
+
g.invalid = overlaps(foot, besideNow(g));
|
|
716
|
+
if (changed) dwell(g);
|
|
717
|
+
if (!g.invalid) g.clearAt = m.at;
|
|
693
718
|
svg?.classList.add("dragging");
|
|
694
719
|
svg?.classList.toggle("drag-bad", g.invalid);
|
|
695
720
|
shiftDragged(g.id, [m.at[0] - g.shown[0], m.at[1] - g.shown[1]]);
|
|
696
721
|
paintDrag(foot);
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
now.
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
}
|
|
722
|
+
sayDrag(g);
|
|
723
|
+
if (changed && !g.invalid) relayoutSoon(g);
|
|
724
|
+
}
|
|
725
|
+
function sayDrag(g) {
|
|
726
|
+
const within = g.parent ? `${g.fresh ? "in" : "within"} ${g.parent.label}` : "on the canvas";
|
|
727
|
+
const n = g.given?.size ?? 0;
|
|
728
|
+
opts.onHint?.(`${g.fresh ? "Adding" : "Moving"} ${g.name} ${within} \xB7 ${g.step === 1 ? "whole cells (\u2325: half cells)" : "half cells"} \xB7 ${g.invalid ? g.way ? "hold still: it makes way" : g.fresh ? "taken: the layout places it" : "overlaps: it won't drop here" : n ? `${n} made way \xB7 Esc cancels` : "Esc cancels"}${T.pinHint}`);
|
|
729
|
+
}
|
|
730
|
+
function relayoutSoon(g, glide = false) {
|
|
731
|
+
if (!g.fresh && !lastS3) return;
|
|
732
|
+
clearTimeout(relayout);
|
|
733
|
+
const go = () => {
|
|
734
|
+
const now = pinDrag();
|
|
735
|
+
if (!now || now !== g || now.invalid || !now.move) return;
|
|
736
|
+
let next;
|
|
737
|
+
try {
|
|
738
|
+
({ next, keep: now.keep } = trialLayout(now));
|
|
739
|
+
} catch {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
showTrial(now, next, now.move.at, glide);
|
|
743
|
+
};
|
|
744
|
+
if (glide) go();
|
|
745
|
+
else relayout = window.setTimeout(go, 0);
|
|
746
|
+
}
|
|
747
|
+
function showTrial(g, next, at, glide) {
|
|
748
|
+
const was = cd;
|
|
749
|
+
stillOn(g, next);
|
|
750
|
+
void draw(next);
|
|
751
|
+
if (pinDrag() !== g) return;
|
|
752
|
+
g.shown = at;
|
|
753
|
+
shiftDragged(g.id, [g.move.at[0] - at[0], g.move.at[1] - at[1]]);
|
|
754
|
+
paintDrag(footAt(g, g.move.at));
|
|
755
|
+
if (!glide || !motionOn()) return;
|
|
756
|
+
const from = /* @__PURE__ */ new Map();
|
|
757
|
+
for (const b of g.beside) {
|
|
758
|
+
const a = was.scene.layout.byId.get(b.id)?.box, z = next.scene.layout.byId.get(b.id)?.box;
|
|
759
|
+
if (a && z && (Math.abs(a.x0 - z.x0) > 1e-6 || Math.abs(a.y0 - z.y0) > 1e-6)) from.set(b.id, [a.x0 - z.x0, a.y0 - z.y0]);
|
|
760
|
+
}
|
|
761
|
+
glideFrom(from);
|
|
762
|
+
}
|
|
763
|
+
function stillOn(g, next) {
|
|
764
|
+
const now = g.parent && next.scene.layout.byId.get(g.parent.id)?.box;
|
|
765
|
+
if (!g.parent || !now || !V.view) return;
|
|
766
|
+
const was = g.parent.box, dx = now.x0 - was.x0, dy = now.y0 - was.y0;
|
|
767
|
+
g.parent = { ...g.parent, box: now, w: Math.max(g.parent.w, now.x1 - now.x0), d: Math.max(g.parent.d, now.y1 - now.y0) };
|
|
768
|
+
if (Math.abs(dx) < 1e-9 && Math.abs(dy) < 1e-9) return;
|
|
769
|
+
g.beside = g.beside.map((b) => ({ ...b, x0: b.x0 + dx, x1: b.x1 + dx, y0: b.y0 + dy, y1: b.y1 + dy }));
|
|
770
|
+
shiftView(g, (dx - dy) * C * S, (dx + dy) * 0.5 * S);
|
|
771
|
+
}
|
|
772
|
+
function shiftView(g, x, y) {
|
|
773
|
+
if (!V.view) return;
|
|
774
|
+
V.view = { ...V.view, x: V.view.x + x, y: V.view.y + y };
|
|
775
|
+
g.shifted = [(g.shifted?.[0] ?? 0) + x, (g.shifted?.[1] ?? 0) + y];
|
|
776
|
+
}
|
|
777
|
+
function unshift(g) {
|
|
778
|
+
if (g.shifted) {
|
|
779
|
+
shiftView(g, -g.shifted[0], -g.shifted[1]);
|
|
780
|
+
g.shifted = void 0;
|
|
717
781
|
}
|
|
718
782
|
}
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
783
|
+
let hintTimer = 0, giveTimer = 0, backTimer = 0, wayFrame = 0, jiggling = [];
|
|
784
|
+
function stillWay() {
|
|
785
|
+
cancelAnimationFrame(wayFrame);
|
|
786
|
+
wayFrame = 0;
|
|
787
|
+
for (const id of jiggling) shiftDragged(id, [0, 0]);
|
|
788
|
+
jiggling = [];
|
|
789
|
+
}
|
|
790
|
+
function restart() {
|
|
791
|
+
clearTimeout(hintTimer);
|
|
792
|
+
clearTimeout(giveTimer);
|
|
793
|
+
stillWay();
|
|
794
|
+
}
|
|
795
|
+
function endWay() {
|
|
796
|
+
clearTimeout(backTimer);
|
|
797
|
+
backTimer = 0;
|
|
798
|
+
restart();
|
|
799
|
+
}
|
|
800
|
+
const needed = (g, foot) => g.beside.some((b) => g.given?.has(b.id) && hits(b, foot));
|
|
801
|
+
function dwell(g) {
|
|
802
|
+
restart();
|
|
803
|
+
g.way = void 0;
|
|
804
|
+
const foot = footAt(g, g.move.at);
|
|
805
|
+
if (!g.given?.size || needed(g, foot)) {
|
|
806
|
+
clearTimeout(backTimer);
|
|
807
|
+
backTimer = 0;
|
|
808
|
+
} else if (!backTimer) backTimer = window.setTimeout(() => {
|
|
809
|
+
backTimer = 0;
|
|
810
|
+
if (pinDrag() === g) putBack(g);
|
|
811
|
+
}, BACK_AFTER);
|
|
812
|
+
if (!g.invalid || !g.beside.some((b) => hits(b, foot))) return;
|
|
813
|
+
hintTimer = window.setTimeout(() => {
|
|
814
|
+
if (pinDrag() !== g) return;
|
|
815
|
+
const from = g.clearAt ?? g.start, par = g.parent, pad = par?.pad ?? 0;
|
|
816
|
+
const way = makeWay({
|
|
817
|
+
foot,
|
|
818
|
+
others: g.beside,
|
|
819
|
+
going: [g.move.at[0] - from[0], g.move.at[1] - from[1]],
|
|
820
|
+
bounds: par ? { x0: par.box.x0 + pad, y0: par.box.y0 + pad, x1: par.box.x1 - pad, y1: par.box.y1 - pad } : void 0
|
|
821
|
+
});
|
|
822
|
+
if (!way?.size) return;
|
|
823
|
+
g.way = way;
|
|
824
|
+
paintDrag(foot);
|
|
825
|
+
sayDrag(g);
|
|
826
|
+
jiggle(g);
|
|
827
|
+
giveTimer = window.setTimeout(() => {
|
|
828
|
+
if (pinDrag() === g && g.way === way) give(g, way);
|
|
829
|
+
}, DWELL_GIVE - DWELL_HINT);
|
|
830
|
+
}, DWELL_HINT);
|
|
831
|
+
}
|
|
832
|
+
function give(g, way) {
|
|
833
|
+
stillWay();
|
|
834
|
+
g.given = way;
|
|
835
|
+
g.way = void 0;
|
|
836
|
+
g.invalid = false;
|
|
837
|
+
g.clearAt = g.move.at;
|
|
838
|
+
relayoutSoon(g, true);
|
|
839
|
+
sayDrag(g);
|
|
840
|
+
}
|
|
841
|
+
function putBack(g) {
|
|
842
|
+
stillWay();
|
|
843
|
+
g.given = void 0;
|
|
844
|
+
g.invalid = overlaps(footAt(g, g.move.at), besideNow(g));
|
|
845
|
+
sayDrag(g);
|
|
846
|
+
if (!g.invalid) {
|
|
847
|
+
relayoutSoon(g, true);
|
|
848
|
+
return;
|
|
724
849
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
850
|
+
showTrial(g, g.fresh?.base ?? g.origCd, g.start, true);
|
|
851
|
+
dwell(g);
|
|
852
|
+
}
|
|
853
|
+
function jiggle(g) {
|
|
854
|
+
if (!motionOn()) return;
|
|
855
|
+
const t0 = performance.now(), ids = [...g.way.keys()];
|
|
856
|
+
jiggling = ids;
|
|
857
|
+
const frame = (now) => {
|
|
858
|
+
if (pinDrag() !== g || !g.way) return;
|
|
859
|
+
const t = now - t0, a = JIGGLE.px * Math.min(1, 0.25 + 0.75 * t / (DWELL_GIVE - DWELL_HINT)) / scale();
|
|
860
|
+
ids.forEach((id, i) => shiftScreen(id, a * Math.sin(t / 1e3 * JIGGLE.hz * 2 * Math.PI + i * 1.7)));
|
|
861
|
+
wayFrame = requestAnimationFrame(frame);
|
|
862
|
+
};
|
|
863
|
+
wayFrame = requestAnimationFrame(frame);
|
|
864
|
+
}
|
|
865
|
+
function glideFrom(from) {
|
|
866
|
+
if (!from.size) return;
|
|
867
|
+
const t0 = performance.now(), on = svg;
|
|
868
|
+
jiggling = [...from.keys()];
|
|
869
|
+
const frame = (now) => {
|
|
870
|
+
if (svg !== on) {
|
|
871
|
+
jiggling = [];
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const f = Math.min(1, Math.max(0, (now - t0) / GLIDE)), left = Math.pow(1 - f, 3);
|
|
875
|
+
for (const [id, [dx, dy]] of from) shiftDragged(id, [dx * left, dy * left]);
|
|
876
|
+
if (f < 1) wayFrame = requestAnimationFrame(frame);
|
|
877
|
+
else jiggling = [];
|
|
878
|
+
};
|
|
879
|
+
frame(t0);
|
|
880
|
+
}
|
|
881
|
+
function trialLayout(g, at = g.move.at) {
|
|
730
882
|
const orig = g.origCd, nameOf = new Map([...orig.pinId].map(([n, id]) => [id, n]));
|
|
731
|
-
const
|
|
732
|
-
const
|
|
883
|
+
const gave = [];
|
|
884
|
+
for (const [id, [dx, dy]] of g.given ?? []) {
|
|
885
|
+
const was = localAt(orig.scene, id);
|
|
886
|
+
gave.push([nameOf.get(id), [was[0] + dx, was[1] + dy]]);
|
|
887
|
+
}
|
|
888
|
+
const set = [[g.name, at], ...gave];
|
|
889
|
+
const lay = layWith(g);
|
|
890
|
+
let next = lay(set);
|
|
891
|
+
const have = /* @__PURE__ */ new Set([...Object.keys(K.pins.of(lastData) ?? {}), ...set.map(([n]) => n)]);
|
|
733
892
|
const keep = [];
|
|
734
|
-
for (const
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
if (
|
|
738
|
-
if (Math.abs(now.x0 - sb.box.x0) > 1e-6 || Math.abs(now.y0 - sb.box.y0) > 1e-6) keep.push([n, was]);
|
|
893
|
+
for (const b of g.beside) {
|
|
894
|
+
const n = nameOf.get(b.id), was = localAt(orig.scene, b.id), now = localAt(next.scene, b.id);
|
|
895
|
+
if (!n || !was || !now || have.has(n)) continue;
|
|
896
|
+
if (Math.abs(now[0] - was[0]) > 1e-6 || Math.abs(now[1] - was[1]) > 1e-6) keep.push([n, was]);
|
|
739
897
|
}
|
|
740
|
-
if (keep.length)
|
|
741
|
-
|
|
742
|
-
|
|
898
|
+
if (keep.length) next = lay([...set, ...keep]);
|
|
899
|
+
return { next, keep: [...gave, ...keep] };
|
|
900
|
+
}
|
|
901
|
+
function layWith(g) {
|
|
902
|
+
if (g.fresh) {
|
|
903
|
+
const text = g.fresh.text;
|
|
904
|
+
return (list) => {
|
|
905
|
+
const t = K.trial(pinsCmd(list, "").build(text).steps.reduce((u, st) => applyStep(u, st), text), opts.theme);
|
|
906
|
+
if (!t) throw new Error("not drawn");
|
|
907
|
+
return t;
|
|
908
|
+
};
|
|
743
909
|
}
|
|
744
|
-
return
|
|
910
|
+
return (list) => {
|
|
911
|
+
const base2 = JSON.parse(JSON.stringify(lastData ?? {}));
|
|
912
|
+
if (K.pins.legacyOnly(base2)) {
|
|
913
|
+
base2[PINS_KEY] = base2[K.pins.legacyKey];
|
|
914
|
+
delete base2[K.pins.legacyKey];
|
|
915
|
+
}
|
|
916
|
+
base2[PINS_KEY] ??= {};
|
|
917
|
+
base2[PINS_KEY].layout ??= {};
|
|
918
|
+
for (const [n, at] of list) base2[PINS_KEY].layout[n] = { at };
|
|
919
|
+
return K.draw(lastS3, base2, opts.theme);
|
|
920
|
+
};
|
|
745
921
|
}
|
|
746
922
|
function shiftDragged(id, [gx, gy]) {
|
|
747
923
|
if (!svg || !cd) return;
|
|
748
|
-
|
|
924
|
+
shiftBy(id, (gx - gy) * C * S, (gx + gy) * 0.5 * S);
|
|
925
|
+
}
|
|
926
|
+
const shiftScreen = (id, dx) => shiftBy(id, dx, 0);
|
|
927
|
+
function shiftBy(id, sx, sy) {
|
|
928
|
+
if (!svg || !cd) return;
|
|
749
929
|
const ids = /* @__PURE__ */ new Set([id]);
|
|
750
930
|
for (const it of cd.scene.layout.items) {
|
|
751
931
|
let p = it.parent;
|
|
@@ -760,7 +940,7 @@ function canvasView(host, doc, opts) {
|
|
|
760
940
|
for (const el of svg.querySelectorAll("[data-id]")) {
|
|
761
941
|
if (!ids.has(el.getAttribute("data-id") ?? "")) continue;
|
|
762
942
|
if (el.parentElement?.closest(`[data-id="${CSS.escape(el.getAttribute("data-id"))}"]`)) continue;
|
|
763
|
-
el.setAttribute("transform",
|
|
943
|
+
el.setAttribute("transform", sx || sy ? `translate(${sx.toFixed(2)} ${sy.toFixed(2)})` : "");
|
|
764
944
|
}
|
|
765
945
|
}
|
|
766
946
|
function paintDrag(foot) {
|
|
@@ -785,10 +965,20 @@ function canvasView(host, doc, opts) {
|
|
|
785
965
|
for (let v = Math.ceil((area.x0 - ox) / st) * st; ox + v <= area.x1 + 1e-9; v += st) line(ox + v, area.y0, ox + v, area.y1, Number.isInteger(v) ? "dgrid" : "dgrid sub");
|
|
786
966
|
for (let v = Math.ceil((area.y0 - oy) / st) * st; oy + v <= area.y1 + 1e-9; v += st) line(area.x0, oy + v, area.x1, oy + v, Number.isInteger(v) ? "dgrid" : "dgrid sub");
|
|
787
967
|
overlay.append(wrap);
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
968
|
+
const poly = (b, cls) => {
|
|
969
|
+
const t = d.createElementNS(SVGNS, "polygon");
|
|
970
|
+
t.setAttribute("class", cls);
|
|
971
|
+
t.setAttribute("points", [P(b.x0, b.y0), P(b.x1, b.y0), P(b.x1, b.y1), P(b.x0, b.y1)].join(" "));
|
|
972
|
+
overlay.append(t);
|
|
973
|
+
return t;
|
|
974
|
+
};
|
|
975
|
+
poly(foot, `drop${g.invalid ? " bad" : ""}`);
|
|
976
|
+
for (const b of g.way ? besideNow({ ...g, given: g.way }) : []) {
|
|
977
|
+
if (!g.way.has(b.id)) continue;
|
|
978
|
+
const w = poly(b, "drop way");
|
|
979
|
+
w.setAttribute("stroke-dasharray", "5 4");
|
|
980
|
+
w.dataset.way = b.id;
|
|
981
|
+
}
|
|
792
982
|
if (g.parent && g.move) {
|
|
793
983
|
const pb = g.parent.box, c = g.move.clamped, pad = g.parent.pad;
|
|
794
984
|
const e = (x1, y1, x2, y2) => {
|
|
@@ -818,6 +1008,16 @@ function canvasView(host, doc, opts) {
|
|
|
818
1008
|
if (!g) return;
|
|
819
1009
|
drag = void 0;
|
|
820
1010
|
clearTimeout(relayout);
|
|
1011
|
+
endWay();
|
|
1012
|
+
let stale = false;
|
|
1013
|
+
if (commit && g.given?.size && g.move) {
|
|
1014
|
+
const foot = footAt(g, g.move.at);
|
|
1015
|
+
if (!needed(g, foot)) {
|
|
1016
|
+
g.given = void 0;
|
|
1017
|
+
g.invalid = overlaps(foot, besideNow(g));
|
|
1018
|
+
stale = true;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
821
1021
|
const shown = overlay ? [...overlay.querySelectorAll(".gridwrap, .drop, .edge")] : [];
|
|
822
1022
|
if (shown.length) {
|
|
823
1023
|
for (const e of shown) e.classList.add("out");
|
|
@@ -827,29 +1027,182 @@ function canvasView(host, doc, opts) {
|
|
|
827
1027
|
svg?.classList.remove("dragging", "drag-bad");
|
|
828
1028
|
paintNav();
|
|
829
1029
|
const m = g.move;
|
|
1030
|
+
if (g.fresh) {
|
|
1031
|
+
await endFresh(g, commit, stale);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
830
1034
|
const moved = m && (m.at[0] !== g.start[0] || m.at[1] !== g.start[1]);
|
|
831
1035
|
if (commit && m && moved && !g.invalid) {
|
|
832
1036
|
const label = `Pin ${describe(fromPointer(g.ptr))} at [${m.at.join(", ")}]`;
|
|
833
1037
|
let keep = g.keep ?? [];
|
|
834
|
-
if (!g.shown || g.shown[0] !== m.at[0] || g.shown[1] !== m.at[1]) try {
|
|
1038
|
+
if (stale || !g.shown || g.shown[0] !== m.at[0] || g.shown[1] !== m.at[1]) try {
|
|
835
1039
|
keep = trialLayout(g).keep;
|
|
836
1040
|
} catch {
|
|
837
1041
|
}
|
|
838
|
-
run(doc, keep.length ? pinsCmd([[g.name, m.at], ...keep], `${label}
|
|
839
|
-
|
|
840
|
-
pendingAfterDrag = false;
|
|
841
|
-
lastKey = "";
|
|
842
|
-
render(lastS3, lastData);
|
|
843
|
-
}
|
|
1042
|
+
run(doc, keep.length ? pinsCmd([[g.name, m.at], ...keep], `${label}${wayWords(g, keep)}`) : withPinsMoved(setCmd([PINS_KEY, "layout", g.name], { at: m.at }, label)), "canvas", opts.note);
|
|
1043
|
+
pendingAfterDrag = false;
|
|
844
1044
|
return;
|
|
845
1045
|
}
|
|
846
1046
|
if (commit && g.invalid) opts.note(`${g.name} can't go there: it would overlap`, "warn");
|
|
1047
|
+
unshift(g);
|
|
847
1048
|
await draw(g.origCd);
|
|
848
|
-
|
|
1049
|
+
redrawPending();
|
|
1050
|
+
}
|
|
1051
|
+
function redrawPending() {
|
|
1052
|
+
if (pendingAfterDrag) {
|
|
849
1053
|
pendingAfterDrag = false;
|
|
850
1054
|
lastKey = "";
|
|
851
|
-
|
|
1055
|
+
setTimeout(() => {
|
|
1056
|
+
if (lastS3 && !pinDrag()) void render(lastS3, lastData);
|
|
1057
|
+
}, 0);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
function wayWords(g, keep) {
|
|
1061
|
+
const nameOf = new Map([...g.origCd.pinId].map(([n, id]) => [id, n])), gave = new Set([...g.given?.keys() ?? []].map((id) => nameOf.get(id)));
|
|
1062
|
+
const a = keep.filter(([n]) => gave.has(n)).map(([n]) => n), b = keep.filter(([n]) => !gave.has(n)).map(([n]) => n);
|
|
1063
|
+
const words = [...a.length ? [`${a.join(", ")} made way`] : [], ...b.length ? [`${b.join(", ")} kept in place`] : []].join("; ");
|
|
1064
|
+
return words && ` (${words})`;
|
|
1065
|
+
}
|
|
1066
|
+
function freshPlan(sec, item, name, p, at, home) {
|
|
1067
|
+
const k = kindOf(sec), preset = item?.value?.();
|
|
1068
|
+
const what = item && preset !== void 0 ? item.label.toLowerCase() : k.singular;
|
|
1069
|
+
const under = at && home ? outlinesAt(home, at) : [], drop = K.dropInto?.(sec);
|
|
1070
|
+
let value = preset ?? k.template(), into, words = "";
|
|
1071
|
+
const c = drop ? under.find((u) => u.kind === drop.container) : void 0;
|
|
1072
|
+
if (drop && c) {
|
|
1073
|
+
into = c.name;
|
|
1074
|
+
value = { ...drop.value(c.name), ...preset ?? {} };
|
|
1075
|
+
words = drop.label(c.name);
|
|
1076
|
+
}
|
|
1077
|
+
if (!into && K.dropOnto) for (const t of under) {
|
|
1078
|
+
const plan = K.dropOnto(t, value, sec, doc.goodDoc.data);
|
|
1079
|
+
if (plan) {
|
|
1080
|
+
into = t.name;
|
|
1081
|
+
value = plan.value;
|
|
1082
|
+
words = plan.label ? ` ${plan.label}` : ` in ${t.name}`;
|
|
1083
|
+
break;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
const label = `Add ${what} ${name}${words}`;
|
|
1087
|
+
return { into, label, add: insertCmd(p.slice(0, -1), name, value, { label }) };
|
|
1088
|
+
}
|
|
1089
|
+
function outlinesAt(d, q) {
|
|
1090
|
+
if (flat) return flat.panelsAt(q.x, q.y).map((id) => {
|
|
1091
|
+
const ptr = d.pathOf.get(id), ref = ptr === void 0 ? void 0 : resourceOf(ptr);
|
|
1092
|
+
return ref && ref.path === ptr ? ref : void 0;
|
|
1093
|
+
}).filter((x) => !!x);
|
|
1094
|
+
const e = 2 / scale() / S;
|
|
1095
|
+
const inside = (poly) => {
|
|
1096
|
+
let c = false;
|
|
1097
|
+
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
1098
|
+
const [xi, yi] = poly[i], [xj, yj] = poly[j];
|
|
1099
|
+
if (yi > q.y !== yj > q.y && q.x < (xj - xi) * (q.y - yi) / (yj - yi) + xi) c = !c;
|
|
1100
|
+
}
|
|
1101
|
+
return c;
|
|
1102
|
+
};
|
|
1103
|
+
const out = [];
|
|
1104
|
+
for (const [id, ptr] of d.pathOf) {
|
|
1105
|
+
const it = d.scene.layout.byId.get(id), ref = resourceOf(ptr);
|
|
1106
|
+
if (!it || !ref || ref.path !== ptr) continue;
|
|
1107
|
+
const b = it.box;
|
|
1108
|
+
if (!inside(boxSilhouette({ ...b, x0: b.x0 - e, y0: b.y0 - e, x1: b.x1 + e, y1: b.y1 + e }))) continue;
|
|
1109
|
+
let depth = 0;
|
|
1110
|
+
for (let up2 = it.parent; up2; up2 = up2.parent) depth++;
|
|
1111
|
+
out.push({ r: ref, depth, area: (b.x1 - b.x0) * (b.y1 - b.y0) });
|
|
1112
|
+
}
|
|
1113
|
+
return out.sort((a, b) => b.depth - a.depth || a.area - b.area).map((x) => x.r);
|
|
1114
|
+
}
|
|
1115
|
+
function dragFresh(sec, item, at) {
|
|
1116
|
+
if (mode !== "3d" || !cd || !svg || !doc.doc.ok || !lastS3) return false;
|
|
1117
|
+
const was = pinDrag()?.fresh ? pinDrag() : void 0, home = was?.origCd ?? cd;
|
|
1118
|
+
const k = kindOf(sec), name = was?.fresh.name ?? freshName(doc.text, item?.name ?? k.singular), path = [...k.section, name];
|
|
1119
|
+
const sv = toSvg(at.x, at.y), off = was?.shifted ?? [0, 0];
|
|
1120
|
+
const plan = freshPlan(sec, item, name, path, { x: sv.x - off[0], y: sv.y - off[1] }, home);
|
|
1121
|
+
if (!was || was.fresh.plan.into !== plan.into) {
|
|
1122
|
+
const text = plan.add.build(doc.text).steps.reduce((t, st) => applyStep(t, st), doc.text);
|
|
1123
|
+
let trial;
|
|
1124
|
+
try {
|
|
1125
|
+
trial = K.trial(text, opts.theme);
|
|
1126
|
+
} catch {
|
|
1127
|
+
}
|
|
1128
|
+
const id = trial?.pinId.get(name), it = id ? trial.scene.layout.byId.get(id) : void 0, start = id ? localAt(trial.scene, id) : void 0;
|
|
1129
|
+
if (!trial || trial.diags.length || !id || !it || !start) return false;
|
|
1130
|
+
const b = it.box, par = it.parent, inHome = par ? home.scene.layout.byId.get(par.id) : void 0;
|
|
1131
|
+
if (par && !inHome) return false;
|
|
1132
|
+
endWay();
|
|
1133
|
+
clearTimeout(relayout);
|
|
1134
|
+
const g = {
|
|
1135
|
+
kind: "pin",
|
|
1136
|
+
x0: at.x,
|
|
1137
|
+
y0: at.y,
|
|
1138
|
+
moved: true,
|
|
1139
|
+
name,
|
|
1140
|
+
id,
|
|
1141
|
+
start,
|
|
1142
|
+
shown: start,
|
|
1143
|
+
z0: b.z0,
|
|
1144
|
+
size: [b.x1 - b.x0, b.y1 - b.y0],
|
|
1145
|
+
origCd: home,
|
|
1146
|
+
// its container, as it was: what is drawn is moved to it (stillOn)
|
|
1147
|
+
parent: par && inHome ? { id: par.id, w: par.box.x1 - par.box.x0, d: par.box.y1 - par.box.y0, pad: paddingOf(K.catalog, par.region), box: inHome.box, label: par.region?.label ?? par.id } : void 0,
|
|
1148
|
+
beside: besideOf(home, id, inHome),
|
|
1149
|
+
shifted: was?.shifted,
|
|
1150
|
+
fresh: { base: trial, name, path, plan, text },
|
|
1151
|
+
gridShown: was?.gridShown
|
|
1152
|
+
};
|
|
1153
|
+
drag = g;
|
|
1154
|
+
unshift(g);
|
|
1155
|
+
try {
|
|
1156
|
+
g.fresh.base = trialLayout(g, start).next;
|
|
1157
|
+
} catch {
|
|
1158
|
+
}
|
|
1159
|
+
stillOn(g, g.fresh.base);
|
|
1160
|
+
void draw(g.fresh.base);
|
|
1161
|
+
if (pinDrag() !== g) return false;
|
|
1162
|
+
}
|
|
1163
|
+
lastPin = { x: at.x, y: at.y, alt: at.alt };
|
|
1164
|
+
pinStep();
|
|
1165
|
+
return true;
|
|
1166
|
+
}
|
|
1167
|
+
function hideFresh() {
|
|
1168
|
+
const g = pinDrag();
|
|
1169
|
+
if (!g?.fresh) return;
|
|
1170
|
+
endWay();
|
|
1171
|
+
clearTimeout(relayout);
|
|
1172
|
+
drag = void 0;
|
|
1173
|
+
lastKey = "";
|
|
1174
|
+
unshift(g);
|
|
1175
|
+
void draw(g.origCd);
|
|
1176
|
+
paintNav();
|
|
1177
|
+
}
|
|
1178
|
+
async function endFresh(g, commit, stale) {
|
|
1179
|
+
const f = g.fresh, m = g.move;
|
|
1180
|
+
if (!commit || !m) {
|
|
1181
|
+
unshift(g);
|
|
1182
|
+
await draw(g.origCd);
|
|
1183
|
+
redrawPending();
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
pendingAfterDrag = false;
|
|
1187
|
+
let keep = g.keep ?? [];
|
|
1188
|
+
if (!g.invalid && (stale || !g.shown || g.shown[0] !== m.at[0] || g.shown[1] !== m.at[1] || g.keep === void 0)) try {
|
|
1189
|
+
keep = trialLayout(g).keep;
|
|
1190
|
+
} catch {
|
|
1191
|
+
keep = [];
|
|
1192
|
+
}
|
|
1193
|
+
const own = snapMove(g.start, [0, 0], g.step ?? 1, g.size, g.parent).at, its = m.at[0] !== own[0] || m.at[1] !== own[1];
|
|
1194
|
+
const pins = g.invalid ? [] : [...its || keep.length ? [[f.name, m.at]] : [], ...keep];
|
|
1195
|
+
const cmd = pins.length ? seqCmd(`${f.plan.label} at [${m.at.join(", ")}]${wayWords(g, keep)}`, [f.plan.add, pinsCmd(pins, "")]) : f.plan.add;
|
|
1196
|
+
if (g.invalid) unshift(g);
|
|
1197
|
+
if (!run(doc, { ...cmd, select: [f.path] }, "canvas", opts.note)) {
|
|
1198
|
+
lastKey = "";
|
|
1199
|
+
unshift(g);
|
|
1200
|
+
await draw(g.origCd);
|
|
1201
|
+
return;
|
|
852
1202
|
}
|
|
1203
|
+
if (g.invalid) opts.note(`${f.name} added; that spot is taken, so the layout placed it`);
|
|
1204
|
+
doc.select([f.path], "canvas");
|
|
1205
|
+
rename(f.path, { joins: doc.undoId, at: lastPin });
|
|
853
1206
|
}
|
|
854
1207
|
function up(e) {
|
|
855
1208
|
if (linking && !linking.pick) {
|
|
@@ -891,13 +1244,6 @@ function canvasView(host, doc, opts) {
|
|
|
891
1244
|
}
|
|
892
1245
|
return out;
|
|
893
1246
|
}
|
|
894
|
-
const round = (v) => Math.round(v * 2) / 2;
|
|
895
|
-
const snap = ([x, y]) => [round(x), round(y)];
|
|
896
|
-
const gridToScreen = (gx, gy) => {
|
|
897
|
-
const [ux, uy] = screenToGrid(1, 0), [vx, vy] = screenToGrid(0, 1);
|
|
898
|
-
const det = ux * vy - uy * vx;
|
|
899
|
-
return [(gx * vy - gy * vx) / det, (gy * ux - gx * uy) / det];
|
|
900
|
-
};
|
|
901
1247
|
function wheel(e) {
|
|
902
1248
|
e.preventDefault();
|
|
903
1249
|
const { x, y } = pageXY(e);
|
|
@@ -1038,138 +1384,22 @@ function canvasView(host, doc, opts) {
|
|
|
1038
1384
|
rename(p, { joins: doc.undoId });
|
|
1039
1385
|
}
|
|
1040
1386
|
}
|
|
1041
|
-
function placeAt(name, add, at) {
|
|
1042
|
-
if (!cd || !svg) return void 0;
|
|
1043
|
-
const text = add.build(doc.text).steps.reduce((t, st) => applyStep(t, st), doc.text);
|
|
1044
|
-
let trial;
|
|
1045
|
-
try {
|
|
1046
|
-
trial = K.trial(text, opts.theme);
|
|
1047
|
-
} catch {
|
|
1048
|
-
return void 0;
|
|
1049
|
-
}
|
|
1050
|
-
if (!trial) return void 0;
|
|
1051
|
-
const id = trial.pinId.get(name), item = id ? trial.scene.layout.byId.get(id) : void 0, start = id ? localAt(trial.scene, id) : void 0;
|
|
1052
|
-
if (!item || !start) return void 0;
|
|
1053
|
-
const b = item.box, par = item.parent;
|
|
1054
|
-
const cx = (b.x0 + b.x1) / 2, cy = (b.y0 + b.y1) / 2;
|
|
1055
|
-
const centre = [(cx - cy) * C * S, (cx + cy) * 0.5 * S - b.z0 * S], drop = toSvg(at.x, at.y);
|
|
1056
|
-
const m = snapMove(
|
|
1057
|
-
start,
|
|
1058
|
-
screenToGrid(drop.x - centre[0], drop.y - centre[1]),
|
|
1059
|
-
1,
|
|
1060
|
-
[b.x1 - b.x0, b.y1 - b.y0],
|
|
1061
|
-
par ? { w: par.box.x1 - par.box.x0, d: par.box.y1 - par.box.y0, pad: paddingOf(K.catalog, par.region) } : void 0
|
|
1062
|
-
);
|
|
1063
|
-
const dx = m.at[0] - start[0], dy = m.at[1] - start[1];
|
|
1064
|
-
const moved = { x0: b.x0 + dx, y0: b.y0 + dy, x1: b.x1 + dx, y1: b.y1 + dy };
|
|
1065
|
-
const siblings = (par ? par.children : trial.scene.layout.items.filter((i) => !i.parent)).filter((i) => i !== item).map((i) => i.box);
|
|
1066
|
-
return overlaps(moved, siblings) ? void 0 : m.at;
|
|
1067
|
-
}
|
|
1068
|
-
function containerAt(kind, at) {
|
|
1069
|
-
if (!cd) return void 0;
|
|
1070
|
-
const p = toSvg(at.x, at.y);
|
|
1071
|
-
if (flat) {
|
|
1072
|
-
for (const id of flat.panelsAt(p.x, p.y)) {
|
|
1073
|
-
const name = [...cd.pinId].find(([, pid]) => pid === id)?.[0];
|
|
1074
|
-
if (name && resourceOf(cd.pathOf.get(id) ?? "")?.kind === kind) return name;
|
|
1075
|
-
}
|
|
1076
|
-
return void 0;
|
|
1077
|
-
}
|
|
1078
|
-
const inside = (poly) => {
|
|
1079
|
-
let c = false;
|
|
1080
|
-
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
1081
|
-
const [xi, yi] = poly[i], [xj, yj] = poly[j];
|
|
1082
|
-
if (yi > p.y !== yj > p.y && p.x < (xj - xi) * (p.y - yi) / (yj - yi) + xi) c = !c;
|
|
1083
|
-
}
|
|
1084
|
-
return c;
|
|
1085
|
-
};
|
|
1086
|
-
for (const [name, id] of cd.pinId) {
|
|
1087
|
-
if (resourceOf(cd.pathOf.get(id) ?? "")?.kind !== kind) continue;
|
|
1088
|
-
const item = cd.scene.layout.byId.get(id);
|
|
1089
|
-
if (item && inside(floorOf(item.box))) return name;
|
|
1090
|
-
}
|
|
1091
|
-
return void 0;
|
|
1092
|
-
}
|
|
1093
|
-
function objectsUnder(at) {
|
|
1094
|
-
if (!cd) return [];
|
|
1095
|
-
const p = toSvg(at.x, at.y);
|
|
1096
|
-
const refOf = (id) => {
|
|
1097
|
-
const ptr = cd.pathOf.get(id);
|
|
1098
|
-
const r = ptr === void 0 ? void 0 : resourceOf(ptr);
|
|
1099
|
-
return r && r.path === ptr ? r : void 0;
|
|
1100
|
-
};
|
|
1101
|
-
if (flat) return flat.panelsAt(p.x, p.y).map(refOf).filter((r) => !!r);
|
|
1102
|
-
const inside = (poly) => {
|
|
1103
|
-
let c = false;
|
|
1104
|
-
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
1105
|
-
const [xi, yi] = poly[i], [xj, yj] = poly[j];
|
|
1106
|
-
if (yi > p.y !== yj > p.y && p.x < (xj - xi) * (p.y - yi) / (yj - yi) + xi) c = !c;
|
|
1107
|
-
}
|
|
1108
|
-
return c;
|
|
1109
|
-
};
|
|
1110
|
-
const depth = (i) => {
|
|
1111
|
-
let n = 0;
|
|
1112
|
-
for (let q = i.parent; q; q = q.parent) n++;
|
|
1113
|
-
return n;
|
|
1114
|
-
};
|
|
1115
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1116
|
-
for (const e of fdoc().elementsFromPoint(at.x, at.y)) {
|
|
1117
|
-
if (!svg?.contains(e)) continue;
|
|
1118
|
-
for (let el = e.closest("[data-id]"); el; el = el.parentElement?.closest("[data-id]") ?? null) {
|
|
1119
|
-
const r = refOf(el.getAttribute("data-id") ?? "");
|
|
1120
|
-
if (r && !seen.has(r.path)) seen.set(r.path, r);
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
const hits = [];
|
|
1124
|
-
for (const [, id] of cd.pinId) {
|
|
1125
|
-
const item = cd.scene.layout.byId.get(id), r = refOf(id);
|
|
1126
|
-
if (item && r && !seen.has(r.path) && inside(floorOf(item.box))) hits.push({ r, d: depth(item), a: (item.box.x1 - item.box.x0) * (item.box.y1 - item.box.y0) });
|
|
1127
|
-
}
|
|
1128
|
-
return [...seen.values(), ...hits.sort((x, y) => y.d - x.d || x.a - y.a).map((h2) => h2.r)];
|
|
1129
|
-
}
|
|
1130
1387
|
function addFromPalette(sec, at, item) {
|
|
1131
1388
|
if (!doc.doc.ok) {
|
|
1132
1389
|
opts.note("Fix the YAML first", "warn");
|
|
1133
1390
|
return void 0;
|
|
1134
1391
|
}
|
|
1135
|
-
const k = kindOf(sec);
|
|
1136
|
-
const
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
const p = [...k.section, name];
|
|
1140
|
-
let value = preset ?? k.template();
|
|
1141
|
-
let into;
|
|
1142
|
-
const drop = at ? K.dropInto?.(sec) : void 0;
|
|
1143
|
-
if (at && drop) {
|
|
1144
|
-
const c = containerAt(drop.container, at);
|
|
1145
|
-
if (c) {
|
|
1146
|
-
into = c;
|
|
1147
|
-
value = { ...drop.value(c), ...preset ?? {} };
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
let intoLabel = into ? drop.label(into) : "";
|
|
1151
|
-
if (at && !into && K.dropOnto) {
|
|
1152
|
-
for (const t of objectsUnder(at)) {
|
|
1153
|
-
const plan = K.dropOnto(t, value, sec, doc.goodDoc.data);
|
|
1154
|
-
if (plan) {
|
|
1155
|
-
into = t.name;
|
|
1156
|
-
value = plan.value;
|
|
1157
|
-
intoLabel = plan.label ? ` ${plan.label}` : ` in ${t.name}`;
|
|
1158
|
-
break;
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
const label = `Add ${what} ${name}${intoLabel}`;
|
|
1163
|
-
const add = insertCmd(p.slice(0, -1), name, value, { label });
|
|
1164
|
-
const pin = at && !into && mode === "3d" ? placeAt(name, add, at) : void 0;
|
|
1165
|
-
const cmd = pin ? seqCmd(`${label} at [${pin.join(", ")}]`, [add, withPinsMoved(setCmd([PINS_KEY, "layout", name], { at: pin }))]) : add;
|
|
1166
|
-
if (!run(doc, { ...cmd, select: [p] }, "canvas", opts.note)) return void 0;
|
|
1167
|
-
if (at && !into && !pin) opts.note(mode === "2d" ? `${k.singular} ${name} added; the layout placed it (in 3D a drop pins it where it lands)` : `${k.singular} ${name} added; that spot is taken, so the layout placed it`);
|
|
1392
|
+
const k = kindOf(sec), name = freshName(doc.text, item?.name ?? k.singular), p = [...k.section, name];
|
|
1393
|
+
const plan = freshPlan(sec, item, name, p, at && toSvg(at.x, at.y), cd);
|
|
1394
|
+
if (!run(doc, { ...plan.add, select: [p] }, "canvas", opts.note)) return void 0;
|
|
1395
|
+
if (at && !plan.into) opts.note(`${k.singular} ${name} added; the layout placed it${mode === "2d" ? " (in 3D a drop pins it where it lands)" : ""}`);
|
|
1168
1396
|
doc.select([p], "canvas");
|
|
1169
1397
|
rename(p, { joins: doc.undoId, at });
|
|
1170
1398
|
return p;
|
|
1171
1399
|
}
|
|
1172
1400
|
const palette = h("div", { class: "palette", role: "toolbar", "aria-label": T.palette, "aria-orientation": "vertical" });
|
|
1401
|
+
const drawLooks = K.looks?.(palette, () => disposed ? void 0 : doc.goodDoc.data, ui, opts.theme) ?? (() => {
|
|
1402
|
+
});
|
|
1173
1403
|
const sections = /* @__PURE__ */ new Map();
|
|
1174
1404
|
const PAL_KEY = "canvas-palette";
|
|
1175
1405
|
let lastSection;
|
|
@@ -1185,6 +1415,7 @@ function canvasView(host, doc, opts) {
|
|
|
1185
1415
|
if (!g) return;
|
|
1186
1416
|
g.head.setAttribute("aria-expanded", String(open));
|
|
1187
1417
|
g.body.hidden = !open;
|
|
1418
|
+
if (open) drawLooks();
|
|
1188
1419
|
};
|
|
1189
1420
|
function sectionBody(sec) {
|
|
1190
1421
|
const have = sections.get(sec);
|
|
@@ -1240,6 +1471,7 @@ function canvasView(host, doc, opts) {
|
|
|
1240
1471
|
before = void 0;
|
|
1241
1472
|
}
|
|
1242
1473
|
none.hidden = shown > 0;
|
|
1474
|
+
drawLooks();
|
|
1243
1475
|
none.textContent = shown ? "" : `Nothing matches "${search.value.trim()}"`;
|
|
1244
1476
|
}
|
|
1245
1477
|
const setDockW = (w, keep = true) => {
|
|
@@ -1349,29 +1581,58 @@ function canvasView(host, doc, opts) {
|
|
|
1349
1581
|
} catch {
|
|
1350
1582
|
}
|
|
1351
1583
|
const x0 = e.clientX, y0 = e.clientY;
|
|
1352
|
-
let ghost;
|
|
1584
|
+
let ghost, dragged = false, real = false, frame = 0, last;
|
|
1353
1585
|
const over = (ev) => {
|
|
1354
1586
|
const r = stage.getBoundingClientRect();
|
|
1355
1587
|
return ev.clientX >= r.left && ev.clientX <= r.right && ev.clientY >= r.top && ev.clientY <= r.bottom && !palHome.contains(document.elementFromPoint(ev.clientX, ev.clientY));
|
|
1356
1588
|
};
|
|
1357
|
-
const
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1589
|
+
const step = (ev) => {
|
|
1590
|
+
const on = over(ev);
|
|
1591
|
+
real = on && dragFresh(sec, item, { x: ev.clientX, y: ev.clientY, alt: ev.altKey });
|
|
1592
|
+
if (!on || !real) hideFresh();
|
|
1593
|
+
if (!real) {
|
|
1594
|
+
if (!ghost) {
|
|
1595
|
+
ghost = h("div", { class: "pal-ghost", "aria-hidden": "true" }, icon(ic), label);
|
|
1596
|
+
host.append(ghost);
|
|
1597
|
+
}
|
|
1598
|
+
const hr = host.getBoundingClientRect();
|
|
1599
|
+
ghost.style.left = `${ev.clientX - hr.left + 8}px`;
|
|
1600
|
+
ghost.style.top = `${ev.clientY - hr.top + 8}px`;
|
|
1601
|
+
ghost.classList.toggle("bad", !on);
|
|
1602
|
+
} else {
|
|
1603
|
+
ghost?.remove();
|
|
1604
|
+
ghost = void 0;
|
|
1362
1605
|
}
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1606
|
+
};
|
|
1607
|
+
const mv = (ev) => {
|
|
1608
|
+
if (!dragged && Math.hypot(ev.clientX - x0, ev.clientY - y0) < 4) return;
|
|
1609
|
+
dragged = true;
|
|
1610
|
+
last = ev;
|
|
1611
|
+
if (frame) return;
|
|
1612
|
+
const go = () => {
|
|
1613
|
+
if (!frame) return;
|
|
1614
|
+
cancelAnimationFrame(frame);
|
|
1615
|
+
frame = 0;
|
|
1616
|
+
if (last) step(last);
|
|
1617
|
+
};
|
|
1618
|
+
frame = requestAnimationFrame(go);
|
|
1619
|
+
setTimeout(go, 32);
|
|
1367
1620
|
};
|
|
1368
1621
|
const done = (ev, drop) => {
|
|
1369
1622
|
b.removeEventListener("pointermove", mv);
|
|
1370
1623
|
b.removeEventListener("pointerup", upH);
|
|
1371
1624
|
b.removeEventListener("pointercancel", cancelH);
|
|
1372
1625
|
removeEventListener("keydown", esc, true);
|
|
1373
|
-
|
|
1626
|
+
cancelAnimationFrame(frame);
|
|
1627
|
+
frame = 0;
|
|
1628
|
+
if (drop && dragged) step(ev);
|
|
1374
1629
|
ghost?.remove();
|
|
1630
|
+
if (real && pinDrag()?.fresh) {
|
|
1631
|
+
if (drop) usedSection(item);
|
|
1632
|
+
void endPin(drop);
|
|
1633
|
+
return;
|
|
1634
|
+
}
|
|
1635
|
+
hideFresh();
|
|
1375
1636
|
if (!drop) return;
|
|
1376
1637
|
if (!dragged) {
|
|
1377
1638
|
usedSection(item);
|
|
@@ -1586,7 +1847,6 @@ function canvasView(host, doc, opts) {
|
|
|
1586
1847
|
palette,
|
|
1587
1848
|
groupSelection,
|
|
1588
1849
|
startPick,
|
|
1589
|
-
objectsUnder,
|
|
1590
1850
|
get linking() {
|
|
1591
1851
|
return linking;
|
|
1592
1852
|
},
|
|
@@ -1611,6 +1871,7 @@ function canvasView(host, doc, opts) {
|
|
|
1611
1871
|
},
|
|
1612
1872
|
dispose() {
|
|
1613
1873
|
endMorph();
|
|
1874
|
+
endWay();
|
|
1614
1875
|
clearTimeout(relayout);
|
|
1615
1876
|
V.dispose();
|
|
1616
1877
|
disposed = true;
|