@markasso/mcp 0.9.0 → 0.9.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.
Files changed (2) hide show
  1. package/dist/markasso-mcp.js +423 -80
  2. package/package.json +1 -1
@@ -3731,6 +3731,116 @@ function getConnectorHeads(el, vectorFor) {
3731
3731
  });
3732
3732
  return heads;
3733
3733
  }
3734
+ /** Element types connectors attach to, and so must not run through. */
3735
+ var NODE_TYPES = /* @__PURE__ */ new Set([
3736
+ "rectangle",
3737
+ "ellipse",
3738
+ "rhombus",
3739
+ "shape",
3740
+ "stencil",
3741
+ "text",
3742
+ "image",
3743
+ "polygon"
3744
+ ]);
3745
+ function isRouteNodeType(type) {
3746
+ return NODE_TYPES.has(type);
3747
+ }
3748
+ /**
3749
+ * Part of a node a connector must not cross: the rectangle inscribed in an
3750
+ * ellipse or rhombus (so passing near a corner of the bounding box is not a
3751
+ * crossing), the connector box otherwise, inset by a couple of pixels so a
3752
+ * stroke running along the border doesn't count.
3753
+ */
3754
+ function nodeCoreRect(type, box) {
3755
+ const scale = type === "ellipse" ? Math.SQRT1_2 : type === "rhombus" ? .5 : 1;
3756
+ const w = box.w * scale - 4;
3757
+ const h = box.h * scale - 4;
3758
+ return {
3759
+ x: box.x + (box.w - w) / 2,
3760
+ y: box.y + (box.h - h) / 2,
3761
+ w: Math.max(0, w),
3762
+ h: Math.max(0, h)
3763
+ };
3764
+ }
3765
+ /** Length of segment a-b inside `r` (Liang-Barsky clipping). */
3766
+ function lengthInside(a, b, r) {
3767
+ const dx = b[0] - a[0];
3768
+ const dy = b[1] - a[1];
3769
+ let t0 = 0;
3770
+ let t1 = 1;
3771
+ const edges = [
3772
+ [-dx, a[0] - r.x],
3773
+ [dx, r.x + r.w - a[0]],
3774
+ [-dy, a[1] - r.y],
3775
+ [dy, r.y + r.h - a[1]]
3776
+ ];
3777
+ for (const [p, q] of edges) {
3778
+ if (Math.abs(p) < 1e-9) {
3779
+ if (q < 0) return 0;
3780
+ continue;
3781
+ }
3782
+ const t = q / p;
3783
+ if (p < 0) t0 = Math.max(t0, t);
3784
+ else t1 = Math.min(t1, t);
3785
+ if (t0 > t1) return 0;
3786
+ }
3787
+ return (t1 - t0) * Math.hypot(dx, dy);
3788
+ }
3789
+ function containsRect(outer, inner) {
3790
+ return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.w <= outer.x + outer.w && inner.y + inner.h <= outer.y + outer.h;
3791
+ }
3792
+ /**
3793
+ * Nodes drawn around others: frames or groups. Connectors inside them are
3794
+ * expected, so they are no obstacles (and no "through node" warning). Only
3795
+ * a node holding another node that is not text counts: a box with a caption
3796
+ * inside is still a box. Nodes are swept in x order, so the check stays
3797
+ * close to linear on boards where few nodes overlap.
3798
+ */
3799
+ function containerIds(nodes) {
3800
+ const ids = /* @__PURE__ */ new Set();
3801
+ const sorted = [...nodes].sort((a, b) => a.box.x - b.box.x);
3802
+ for (let i = 0; i < sorted.length; i++) {
3803
+ const outer = sorted[i];
3804
+ const right = outer.box.x + outer.box.w;
3805
+ let j = i;
3806
+ while (j > 0 && sorted[j - 1].box.x >= outer.box.x) j--;
3807
+ for (; j < sorted.length && sorted[j].box.x <= right; j++) {
3808
+ const inner = sorted[j];
3809
+ if (inner === outer || inner.type === "text") continue;
3810
+ if (containsRect(outer.box, inner.box)) {
3811
+ ids.add(outer.id);
3812
+ break;
3813
+ }
3814
+ }
3815
+ }
3816
+ return ids;
3817
+ }
3818
+ /**
3819
+ * Obstacles a route runs through: at least MIN_INSIDE of it inside their
3820
+ * core, its own end nodes excluded. Ids in obstacle order.
3821
+ */
3822
+ function crossedObstacles(route, obstacles, ends) {
3823
+ const crossed = [];
3824
+ let minX = Infinity;
3825
+ let minY = Infinity;
3826
+ let maxX = -Infinity;
3827
+ let maxY = -Infinity;
3828
+ for (const [x, y] of route) {
3829
+ minX = Math.min(minX, x);
3830
+ minY = Math.min(minY, y);
3831
+ maxX = Math.max(maxX, x);
3832
+ maxY = Math.max(maxY, y);
3833
+ }
3834
+ for (const obstacle of obstacles) {
3835
+ const { core } = obstacle;
3836
+ if (core.x > maxX || core.y > maxY || core.x + core.w < minX || core.y + core.h < minY) continue;
3837
+ if (ends.has(obstacle.id)) continue;
3838
+ let inside = 0;
3839
+ for (let i = 1; i < route.length; i++) inside += lengthInside(route[i - 1], route[i], obstacle.core);
3840
+ if (inside >= 8) crossed.push(obstacle.id);
3841
+ }
3842
+ return crossed;
3843
+ }
3734
3844
  //#endregion
3735
3845
  //#region ../src/rendering/shape_geometry.ts
3736
3846
  /** Samples per cubic segment: enough for smooth snapping at any zoom. */
@@ -4215,8 +4325,9 @@ function getElementPorts(el) {
4215
4325
  */
4216
4326
  function resolveArrowEndpoints(el, allElements) {
4217
4327
  let { x, y, x2, y2 } = el;
4218
- const startEl = el.startElementId ? allElements.find((e) => e.id === el.startElementId) ?? null : null;
4219
- const endEl = el.endElementId ? allElements.find((e) => e.id === el.endElementId) ?? null : null;
4328
+ const byId = elementsById(allElements);
4329
+ const startEl = el.startElementId ? byId.get(el.startElementId) ?? null : null;
4330
+ const endEl = el.endElementId ? byId.get(el.endElementId) ?? null : null;
4220
4331
  const startPinned = startEl && el.startAnchor ? anchorToWorld(startEl, el.startAnchor) : null;
4221
4332
  const endPinned = endEl && el.endAnchor ? anchorToWorld(endEl, el.endAnchor) : null;
4222
4333
  const startB = startEl ? getElementBounds(startEl) : null;
@@ -4286,7 +4397,7 @@ function sideDirection(el, side) {
4286
4397
  * floating end moves to the port facing `target`, so flowcharts get clean
4287
4398
  * mid-side attachments instead of corner hits.
4288
4399
  */
4289
- function elbowEnd(el, anchor, pinned, target, targetBox) {
4400
+ function elbowEnd(el, anchor, pinned, target, targetBox, forcedSide) {
4290
4401
  const b = getElementBounds(el);
4291
4402
  const box = getConnectorBox(el);
4292
4403
  const cx = b.x + b.w / 2;
@@ -4301,7 +4412,7 @@ function elbowEnd(el, anchor, pinned, target, targetBox) {
4301
4412
  };
4302
4413
  }
4303
4414
  const [lx, ly] = el.rotation ? rotatePoint(target[0], target[1], cx, cy, -el.rotation) : target;
4304
- const side = targetBox && !el.rotation ? sideTowardBox(box, targetBox) : sideFacing(lx - cx, ly - cy, b.w / 2, b.h / 2);
4415
+ const side = forcedSide ?? (targetBox && !el.rotation ? sideTowardBox(box, targetBox) : sideFacing(lx - cx, ly - cy, b.w / 2, b.h / 2));
4305
4416
  const port = getElementPorts(el)[side];
4306
4417
  const dir = sideDirection(el, side);
4307
4418
  return {
@@ -4315,20 +4426,21 @@ function elbowEnd(el, anchor, pinned, target, targetBox) {
4315
4426
  * elements it is attached to: the side each end leaves by, the port it
4316
4427
  * starts from, and a shared coordinate when two flat sides face each other.
4317
4428
  */
4318
- function planElbowEnds(el, allElements) {
4429
+ function planElbowEnds(el, allElements, sides) {
4319
4430
  const pts = resolveArrowEndpoints(el, allElements);
4320
4431
  const start = [pts.x, pts.y];
4321
4432
  const end = [pts.x2, pts.y2];
4322
- const startEl = el.startElementId ? allElements.find((e) => e.id === el.startElementId) : void 0;
4323
- const endEl = el.endElementId ? allElements.find((e) => e.id === el.endElementId) : void 0;
4433
+ const byId = elementsById(allElements);
4434
+ const startEl = el.startElementId ? byId.get(el.startElementId) : void 0;
4435
+ const endEl = el.endElementId ? byId.get(el.endElementId) : void 0;
4324
4436
  const targetOf = (other, otherAnchor, otherPoint) => {
4325
4437
  if (!other || otherAnchor) return otherPoint;
4326
4438
  const [ocx, ocy] = getElementCenter(other);
4327
4439
  return [ocx, ocy];
4328
4440
  };
4329
4441
  const boxOf = (other, otherAnchor) => other && !otherAnchor && !other.rotation ? getConnectorBox(other) : void 0;
4330
- const s = startEl ? elbowEnd(startEl, el.startAnchor, start, targetOf(endEl, el.endAnchor, end), boxOf(endEl, el.endAnchor)) : null;
4331
- const e = endEl ? elbowEnd(endEl, el.endAnchor, end, targetOf(startEl, el.startAnchor, start), boxOf(startEl, el.startAnchor)) : null;
4442
+ const s = startEl ? elbowEnd(startEl, el.startAnchor, start, targetOf(endEl, el.endAnchor, end), boxOf(endEl, el.endAnchor), sides?.start) : null;
4443
+ const e = endEl ? elbowEnd(endEl, el.endAnchor, end, targetOf(startEl, el.startAnchor, start), boxOf(startEl, el.startAnchor), sides?.end) : null;
4332
4444
  let aligned = false;
4333
4445
  if (s && e && startEl && endEl && !el.startAnchor && !el.endAnchor) {
4334
4446
  const shared = sharedOffset(startEl, s.dir, endEl, e.dir);
@@ -4348,9 +4460,24 @@ function planElbowEnds(el, allElements) {
4348
4460
  aligned
4349
4461
  };
4350
4462
  }
4463
+ var byIdCache = /* @__PURE__ */ new WeakMap();
4464
+ /**
4465
+ * Elements of an (immutable) array by id, built once per array: routing a
4466
+ * scene looks up both ends of every connector. The first element with an
4467
+ * id wins, as `Array.find` did.
4468
+ */
4469
+ function elementsById(allElements) {
4470
+ let map = byIdCache.get(allElements);
4471
+ if (!map) {
4472
+ const built = /* @__PURE__ */ new Map();
4473
+ for (const el of allElements) if (!built.has(el.id)) built.set(el.id, el);
4474
+ map = built;
4475
+ byIdCache.set(allElements, map);
4476
+ }
4477
+ return map;
4478
+ }
4351
4479
  /** Key of one connector end in the port slot map. */
4352
4480
  var slotKey = (id, which) => `${id}:${which}`;
4353
- var portSlotCache = /* @__PURE__ */ new WeakMap();
4354
4481
  /** Space between two connector ends that share a side. */
4355
4482
  var PORT_GAP = 20;
4356
4483
  /** Clearance between a spread end and the end of its side (less on short sides). */
@@ -4405,22 +4532,15 @@ function spreadAlongSide(preferred, lo, hi) {
4405
4532
  *
4406
4533
  * Pinned ends, hidden connectors, self-loops and sides with a single port
4407
4534
  * (see sideSpan) keep their planned point, and a side with a single
4408
- * end is left alone. The result depends only on the scene, so every live
4409
- * peer computes the same routes; it is cached per elements array, which is
4410
- * immutable, so a frame resolving N connectors plans each of them once
4411
- * more, not N times.
4535
+ * end is left alone. The result depends only on the plans, so every live
4536
+ * peer computes the same routes (see routeElbows, which caches them).
4412
4537
  */
4413
- function elbowPortSlots(allElements) {
4414
- const cached = portSlotCache.get(allElements);
4415
- if (cached) return cached;
4538
+ function elbowPortSlots(planned, steered = /* @__PURE__ */ new Set()) {
4416
4539
  const groups = /* @__PURE__ */ new Map();
4417
4540
  /** Connectors whose ends sharedOffset put on one coordinate. */
4418
4541
  const aligned = [];
4419
- for (const el of allElements) {
4420
- if (el.type !== "line" && el.type !== "arrow") continue;
4421
- if (el.routing !== "elbow" || el.visible === false) continue;
4542
+ for (const { el, plan } of planned) {
4422
4543
  if (el.startElementId && el.startElementId === el.endElementId) continue;
4423
- const plan = planElbowEnds(el, allElements);
4424
4544
  const ends = [[
4425
4545
  plan.s,
4426
4546
  plan.startEl,
@@ -4445,6 +4565,7 @@ function elbowPortSlots(allElements) {
4445
4565
  node,
4446
4566
  dir: end.dir,
4447
4567
  at: end.point[axis],
4568
+ toward: other[axis],
4448
4569
  angle: Math.atan2(other[axis] - end.point[axis], depth),
4449
4570
  id: el.id
4450
4571
  };
@@ -4470,7 +4591,8 @@ function elbowPortSlots(allElements) {
4470
4591
  for (const m of members) {
4471
4592
  const partner = partnerOf.get(m.key);
4472
4593
  const there = partner === void 0 ? void 0 : placedAt.get(partner);
4473
- preferred.set(m, there !== void 0 && there >= lo && there <= hi ? there : m.at);
4594
+ const own = steered.has(m.id) && members.length > 1 ? Math.min(Math.max(m.toward, lo), hi) : m.at;
4595
+ preferred.set(m, there !== void 0 && there >= lo && there <= hi ? there : own);
4474
4596
  }
4475
4597
  const pref = (m) => preferred.get(m);
4476
4598
  const grid = (m) => Math.round(pref(m) * 100);
@@ -4481,7 +4603,6 @@ function elbowPortSlots(allElements) {
4481
4603
  if (Math.abs(placed[i] - m.at) > .01) slots.set(m.key, placed[i]);
4482
4604
  });
4483
4605
  }
4484
- portSlotCache.set(allElements, slots);
4485
4606
  return slots;
4486
4607
  }
4487
4608
  /**
@@ -4515,21 +4636,286 @@ function sideOf(dir) {
4515
4636
  * Polyline a connector is drawn along: its two resolved ends, or the full
4516
4637
  * orthogonal route for `routing: 'elbow'`. Curved lines (cx/cy) are not
4517
4638
  * covered here; callers keep their quadratic path for those.
4639
+ *
4640
+ * Elbows of the scene come from routeElbows (computed once per elements
4641
+ * array). A connector that is not in `allElements` as this very object (a
4642
+ * preview being drawn, an edited copy) is routed on its own; an edited copy
4643
+ * still attached exactly like the scene's connector of that id reuses its
4644
+ * port slots and side choice.
4518
4645
  */
4519
4646
  function resolveConnectorRoute(el, allElements) {
4520
4647
  if (el.routing !== "elbow") {
4521
4648
  const pts = resolveArrowEndpoints(el, allElements);
4522
4649
  return [[pts.x, pts.y], [pts.x2, pts.y2]];
4523
4650
  }
4524
- const plan = planElbowEnds(el, allElements);
4525
- const { s, e } = plan;
4526
- const slots = elbowPortSlots(allElements);
4651
+ const scene = routeElbows(allElements);
4652
+ const routed = scene.routes.get(el);
4653
+ if (routed) return routed.map((p) => [p[0], p[1]]);
4654
+ const original = elementsById(allElements).get(el.id);
4655
+ const same = original !== void 0 && (original.type === "line" || original.type === "arrow") && sameAttachment(original, el);
4656
+ return finishRoute({
4657
+ el,
4658
+ plan: planElbowEnds(el, allElements, same ? scene.sides.get(original) : void 0)
4659
+ }, same ? scene.slots : NO_SLOTS);
4660
+ }
4661
+ /** Whether two connectors attach to the same nodes in the same way. */
4662
+ function sameAttachment(a, b) {
4663
+ const anchor = (x) => x ? `${x.x},${x.y}` : "";
4664
+ return a.startElementId === b.startElementId && a.endElementId === b.endElementId && anchor(a.startAnchor) === anchor(b.startAnchor) && anchor(a.endAnchor) === anchor(b.endAnchor);
4665
+ }
4666
+ var routeCache = /* @__PURE__ */ new WeakMap();
4667
+ var NO_SIDES = /* @__PURE__ */ new Map();
4668
+ var NO_SLOTS = /* @__PURE__ */ new Map();
4669
+ /** The route of a planned elbow once its ends sit on their slots. */
4670
+ function finishRoute({ el, plan }, slots) {
4671
+ const s = plan.s && { ...plan.s };
4672
+ const e = plan.e && { ...plan.e };
4527
4673
  const startSlot = slots.get(slotKey(el.id, "start"));
4528
4674
  const endSlot = slots.get(slotKey(el.id, "end"));
4529
4675
  if (s && plan.startEl && startSlot !== void 0) placeAlong(s, plan.startEl, startSlot);
4530
4676
  if (e && plan.endEl && endSlot !== void 0) placeAlong(e, plan.endEl, endSlot);
4531
4677
  return computeElbowRoute(s?.point ?? plan.start, s?.dir ?? null, e?.point ?? plan.end, e?.dir ?? null, s?.stub, e?.stub);
4532
4678
  }
4679
+ /** Plans, port slots and routes of every visible elbow of a scene. */
4680
+ function routeScene(allElements, sides) {
4681
+ const planned = [];
4682
+ for (const el of allElements) {
4683
+ if (el.type !== "line" && el.type !== "arrow") continue;
4684
+ if (el.routing !== "elbow" || el.visible === false) continue;
4685
+ planned.push({
4686
+ el,
4687
+ plan: planElbowEnds(el, allElements, sides.get(el))
4688
+ });
4689
+ }
4690
+ const slots = elbowPortSlots(planned, new Set([...sides.keys()].map((el) => el.id)));
4691
+ const routes = /* @__PURE__ */ new Map();
4692
+ for (const p of planned) routes.set(p.el, finishRoute(p, slots));
4693
+ return {
4694
+ planned,
4695
+ slots,
4696
+ routes
4697
+ };
4698
+ }
4699
+ /**
4700
+ * The nodes routes should not run through (frames and groups excluded). A
4701
+ * rotated node counts with the box around its rotated shape: wider than the
4702
+ * shape itself, so a route that clears it never cuts the real outline.
4703
+ */
4704
+ function sceneObstacles(allElements) {
4705
+ const nodes = allElements.filter((el) => el.visible !== false && isRouteNodeType(el.type)).map((el) => {
4706
+ const box = getConnectorBox(el);
4707
+ return {
4708
+ el,
4709
+ id: el.id,
4710
+ type: el.type,
4711
+ box: el.rotation ? rotateBoxOf(el, box) : box
4712
+ };
4713
+ });
4714
+ const containers = containerIds(nodes);
4715
+ return nodes.filter((n) => !containers.has(n.id)).map((n) => ({
4716
+ id: n.id,
4717
+ core: nodeCoreRect(n.el.rotation ? "rectangle" : n.type, n.box)
4718
+ }));
4719
+ }
4720
+ /** Nodes each elbow runs through, by connector. */
4721
+ function sceneCrossings(planned, routes, obstacles) {
4722
+ const crossings = /* @__PURE__ */ new Map();
4723
+ for (const { el } of planned) {
4724
+ const ends = new Set([el.startElementId, el.endElementId].filter((id) => id !== void 0));
4725
+ const crossed = crossedObstacles(routes.get(el), obstacles, ends);
4726
+ if (crossed.length > 0) crossings.set(el, crossed);
4727
+ }
4728
+ return crossings;
4729
+ }
4730
+ /**
4731
+ * The facing side pair on the other axis, for a connector the baseline
4732
+ * routes through a node: top and bottom when it left sideways, left and
4733
+ * right when it left vertically. Only for connectors between two distinct,
4734
+ * unrotated nodes with floating ends, and only when the boxes leave room on
4735
+ * that axis for both stubs. Null otherwise.
4736
+ */
4737
+ function otherAxisSides({ el, plan }) {
4738
+ const { startEl, endEl, s } = plan;
4739
+ if (!startEl || !endEl || !s || startEl === endEl) return null;
4740
+ if (el.startAnchor || el.endAnchor || startEl.rotation || endEl.rotation) return null;
4741
+ const a = getConnectorBox(startEl);
4742
+ const b = getConnectorBox(endEl);
4743
+ const dx = b.x + b.w / 2 - (a.x + a.w / 2);
4744
+ const dy = b.y + b.h / 2 - (a.y + a.h / 2);
4745
+ if (s.dir.dx !== 0) {
4746
+ if (Math.abs(dy) - (a.h + b.h) / 2 < 32) return null;
4747
+ return dy > 0 ? {
4748
+ start: 2,
4749
+ end: 0
4750
+ } : {
4751
+ start: 0,
4752
+ end: 2
4753
+ };
4754
+ }
4755
+ if (Math.abs(dx) - (a.w + b.w) / 2 < 32) return null;
4756
+ return dx > 0 ? {
4757
+ start: 1,
4758
+ end: 3
4759
+ } : {
4760
+ start: 3,
4761
+ end: 1
4762
+ };
4763
+ }
4764
+ /** Number of `(connector, node)` crossings. */
4765
+ function crossingCount(crossings) {
4766
+ let count = 0;
4767
+ for (const nodes of crossings.values()) count += nodes.length;
4768
+ return count;
4769
+ }
4770
+ /** Whether `next` drops some node crossings of `base` and adds none. */
4771
+ function isStrictSubset(next, base) {
4772
+ for (const [el, nodes] of next) {
4773
+ const before = base.get(el);
4774
+ if (!before || nodes.some((node) => !before.includes(node))) return false;
4775
+ }
4776
+ return crossingCount(next) < crossingCount(base);
4777
+ }
4778
+ /**
4779
+ * Most side switches tried in one scene. Beyond it the scene keeps today's
4780
+ * routes: checking a batch costs time with every switch, and a board that
4781
+ * tangled is not fixed by one alternative side pair per connector.
4782
+ */
4783
+ var MAX_SIDE_SWITCHES = 32;
4784
+ /** Samples of a curved line drawn by its control point. */
4785
+ var CURVE_SAMPLES$2 = 12;
4786
+ /** Every connector's drawn polyline, curves sampled, elbows from `routes`. */
4787
+ function connectorPolylines(allElements, routes) {
4788
+ const lines = /* @__PURE__ */ new Map();
4789
+ for (const el of allElements) {
4790
+ if (el.type !== "line" && el.type !== "arrow" || el.visible === false) continue;
4791
+ const routed = routes.get(el);
4792
+ if (routed) {
4793
+ lines.set(el, routed);
4794
+ continue;
4795
+ }
4796
+ const pts = resolveArrowEndpoints(el, allElements);
4797
+ if (el.type === "line" && el.cx !== void 0 && el.cy !== void 0) {
4798
+ const { cx, cy } = el;
4799
+ lines.set(el, Array.from({ length: 13 }, (_, i) => {
4800
+ const p = getQuadraticPoint(pts.x, pts.y, cx, cy, pts.x2, pts.y2, i / CURVE_SAMPLES$2);
4801
+ return [p.x, p.y];
4802
+ }));
4803
+ } else lines.set(el, [[pts.x, pts.y], [pts.x2, pts.y2]]);
4804
+ }
4805
+ return lines;
4806
+ }
4807
+ /**
4808
+ * Whether segments a-b and c-d cross. A segment's start counts when it is
4809
+ * an inner vertex of its polyline (`aInner`, `cInner`), so a crossing right
4810
+ * on a vertex is seen once, while connectors meeting at their own ends
4811
+ * (on a node's border) are not crossings.
4812
+ */
4813
+ function segmentsCross$1(a, b, c, d, aInner, cInner) {
4814
+ const rx = b[0] - a[0];
4815
+ const ry = b[1] - a[1];
4816
+ const sx = d[0] - c[0];
4817
+ const sy = d[1] - c[1];
4818
+ const den = rx * sy - ry * sx;
4819
+ if (Math.abs(den) < 1e-9) return false;
4820
+ const u = ((c[0] - a[0]) * sy - (c[1] - a[1]) * sx) / den;
4821
+ const v = ((c[0] - a[0]) * ry - (c[1] - a[1]) * rx) / den;
4822
+ const slack = 1e-6;
4823
+ const within = (t, inner) => (inner ? t > -1e-6 : t > slack) && t < 1 - slack;
4824
+ return within(u, aInner) && within(v, cInner);
4825
+ }
4826
+ /** Shortest shared stretch of two collinear segments that reads as one line. */
4827
+ var MIN_OVERLAP$1 = 2;
4828
+ /**
4829
+ * Whether two axis-aligned segments run along the same line for a while:
4830
+ * drawn on top of each other they read as one connector.
4831
+ */
4832
+ function segmentsOverlap(a, b, c, d) {
4833
+ const vertical = (p, q) => Math.abs(p[0] - q[0]) < .5;
4834
+ const horizontal = (p, q) => Math.abs(p[1] - q[1]) < .5;
4835
+ let along;
4836
+ if (vertical(a, b) && vertical(c, d) && Math.abs(a[0] - c[0]) < .5) along = 1;
4837
+ else if (horizontal(a, b) && horizontal(c, d) && Math.abs(a[1] - c[1]) < .5) along = 0;
4838
+ else return false;
4839
+ const lo = Math.max(Math.min(a[along], b[along]), Math.min(c[along], d[along]));
4840
+ return Math.min(Math.max(a[along], b[along]), Math.max(c[along], d[along])) - lo > MIN_OVERLAP$1;
4841
+ }
4842
+ /**
4843
+ * Crossings and shared stretches between the `changed` connectors and every
4844
+ * other connector.
4845
+ */
4846
+ function connectorCrossings(lines, changed, limit = Infinity) {
4847
+ let count = 0;
4848
+ const all = [...lines.entries()];
4849
+ for (let ia = 0; ia < all.length; ia++) {
4850
+ const [a, pa] = all[ia];
4851
+ if (!changed.has(a)) continue;
4852
+ for (let ib = 0; ib < all.length; ib++) {
4853
+ const [b, pb] = all[ib];
4854
+ if (ib === ia || changed.has(b) && ib < ia) continue;
4855
+ for (let i = 1; i < pa.length; i++) for (let j = 1; j < pb.length; j++) if (segmentsCross$1(pa[i - 1], pa[i], pb[j - 1], pb[j], i > 1, j > 1) || segmentsOverlap(pa[i - 1], pa[i], pb[j - 1], pb[j])) {
4856
+ count++;
4857
+ if (count > limit) return count;
4858
+ }
4859
+ }
4860
+ }
4861
+ return count;
4862
+ }
4863
+ /**
4864
+ * Routes every visible elbow of a scene, once per (immutable) elements
4865
+ * array. The baseline is today's routing: side choice by free gap, ports
4866
+ * spread on shared sides, routes from their ends. A connector that runs
4867
+ * through a node may switch to the facing side pair on the other axis (a
4868
+ * fan-out leaving its hub by the bottom instead of sideways), when that
4869
+ * route alone crosses nothing. All switches are then applied together, the
4870
+ * ports spread again, and the result kept only if its node crossings are a
4871
+ * strict subset of the baseline's and the connectors whose routes changed
4872
+ * cross or run along no more connectors than before; otherwise the
4873
+ * baseline stands.
4874
+ * Scenes whose routes cross nothing are untouched, and the outcome depends
4875
+ * only on the scene, so live peers agree.
4876
+ */
4877
+ function routeElbows(allElements) {
4878
+ const cached = routeCache.get(allElements);
4879
+ if (cached) return cached;
4880
+ const base = routeScene(allElements, NO_SIDES);
4881
+ let result = {
4882
+ ...base,
4883
+ sides: NO_SIDES
4884
+ };
4885
+ const candidates = base.planned.filter((p) => otherAxisSides(p) !== null);
4886
+ if (candidates.length > 0) {
4887
+ const obstacles = sceneObstacles(allElements);
4888
+ const baseCrossings = sceneCrossings(base.planned, base.routes, obstacles);
4889
+ const proposals = /* @__PURE__ */ new Map();
4890
+ for (const p of candidates) {
4891
+ if (!baseCrossings.has(p.el)) continue;
4892
+ const pair = otherAxisSides(p);
4893
+ const alone = {
4894
+ el: p.el,
4895
+ plan: planElbowEnds(p.el, allElements, pair)
4896
+ };
4897
+ const route = finishRoute(alone, NO_SLOTS);
4898
+ if (sceneCrossings([alone], /* @__PURE__ */ new Map([[p.el, route]]), obstacles).size === 0) proposals.set(p.el, pair);
4899
+ }
4900
+ if (proposals.size > 0 && proposals.size <= MAX_SIDE_SWITCHES) {
4901
+ const next = routeScene(allElements, proposals);
4902
+ if (isStrictSubset(sceneCrossings(next.planned, next.routes, obstacles), baseCrossings)) {
4903
+ const changed = new Set(next.planned.filter((p) => {
4904
+ const a = base.routes.get(p.el);
4905
+ const b = next.routes.get(p.el);
4906
+ return a.length !== b.length || a.some((pt, i) => pt[0] !== b[i][0] || pt[1] !== b[i][1]);
4907
+ }).map((p) => p.el));
4908
+ const before = connectorCrossings(connectorPolylines(allElements, base.routes), changed);
4909
+ if (connectorCrossings(connectorPolylines(allElements, next.routes), changed, before) <= before) result = {
4910
+ ...next,
4911
+ sides: proposals
4912
+ };
4913
+ }
4914
+ }
4915
+ }
4916
+ routeCache.set(allElements, result);
4917
+ return result;
4918
+ }
4533
4919
  /**
4534
4920
  * Stretch of the side a connector leaves `el` by where several ends can
4535
4921
  * spread, along that side (x for top/bottom, y for left/right): a flat side
@@ -4744,7 +5130,7 @@ function segmentsOf$1(points) {
4744
5130
  return points.slice(1).map((p, i) => [points[i], p]);
4745
5131
  }
4746
5132
  /** Length of the part of segment a→b inside `r` (Liang-Barsky clipping). */
4747
- function clippedLength$1(a, b, r) {
5133
+ function clippedLength(a, b, r) {
4748
5134
  const dx = b[0] - a[0];
4749
5135
  const dy = b[1] - a[1];
4750
5136
  let t0 = 0;
@@ -4849,7 +5235,7 @@ function bestBox(ownerId, points, w, h, strokes, placed, nodes) {
4849
5235
  h: r.h - 2
4850
5236
  };
4851
5237
  let cost = 0;
4852
- for (const s of strokes) if (s.owner !== ownerId) cost += clippedLength$1(s.seg[0], s.seg[1], inner) * 20;
5238
+ for (const s of strokes) if (s.owner !== ownerId) cost += clippedLength(s.seg[0], s.seg[1], inner) * 20;
4853
5239
  for (const other of placed) cost += overlapArea(r, rectOf(other)) * 2;
4854
5240
  for (const node of nodes) cost += overlapArea(inner, node) * 2;
4855
5241
  cost += Math.max(0, -room) * 2;
@@ -5347,7 +5733,7 @@ function escapeXml(str) {
5347
5733
  }
5348
5734
  //#endregion
5349
5735
  //#region package.json
5350
- var version = "0.9.0";
5736
+ var version = "0.9.1";
5351
5737
  //#endregion
5352
5738
  //#region ../src/core/layout.ts
5353
5739
  /** Space kept on both sides of a label between ranks. */
@@ -9247,8 +9633,6 @@ var LiveLink = class LiveLink {
9247
9633
  };
9248
9634
  //#endregion
9249
9635
  //#region src/warnings.ts
9250
- /** Shortest run of a connector inside a node that counts as crossing it. */
9251
- var MIN_INSIDE = 8;
9252
9636
  /** Smallest overlap, in px on each axis, worth reporting. */
9253
9637
  var MIN_OVERLAP = 2;
9254
9638
  /** Crossings this close to a segment end are joins, not crossings. */
@@ -9274,56 +9658,11 @@ function polylineOf(el, elements) {
9274
9658
  function segmentsOf(points) {
9275
9659
  return points.slice(1).map((p, i) => [points[i], p]);
9276
9660
  }
9277
- /**
9278
- * Part of a node a connector must not cross: the rectangle inscribed in an
9279
- * ellipse or rhombus (so passing near a corner of the bounding box is not
9280
- * reported), the connector box otherwise, inset by a couple of pixels so
9281
- * a stroke running along the border doesn't count.
9282
- */
9283
- function coreRect(el) {
9284
- const b = getConnectorBox(el);
9285
- const scale = el.type === "ellipse" ? Math.SQRT1_2 : el.type === "rhombus" ? .5 : 1;
9286
- const w = b.w * scale - 4;
9287
- const h = b.h * scale - 4;
9288
- return {
9289
- x: b.x + (b.w - w) / 2,
9290
- y: b.y + (b.h - h) / 2,
9291
- w: Math.max(0, w),
9292
- h: Math.max(0, h)
9293
- };
9294
- }
9295
- /** Length of segment a-b inside `r` (Liang-Barsky clipping). */
9296
- function clippedLength(a, b, r) {
9297
- const dx = b[0] - a[0];
9298
- const dy = b[1] - a[1];
9299
- let t0 = 0;
9300
- let t1 = 1;
9301
- const edges = [
9302
- [-dx, a[0] - r.x],
9303
- [dx, r.x + r.w - a[0]],
9304
- [-dy, a[1] - r.y],
9305
- [dy, r.y + r.h - a[1]]
9306
- ];
9307
- for (const [p, q] of edges) {
9308
- if (Math.abs(p) < 1e-9) {
9309
- if (q < 0) return 0;
9310
- continue;
9311
- }
9312
- const t = q / p;
9313
- if (p < 0) t0 = Math.max(t0, t);
9314
- else t1 = Math.min(t1, t);
9315
- if (t0 > t1) return 0;
9316
- }
9317
- return (t1 - t0) * Math.hypot(dx, dy);
9318
- }
9319
9661
  function overlaps(a, b) {
9320
9662
  const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x);
9321
9663
  const h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
9322
9664
  return w >= MIN_OVERLAP && h >= MIN_OVERLAP;
9323
9665
  }
9324
- function contains(outer, inner) {
9325
- return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.w <= outer.x + outer.w && inner.y + inner.h <= outer.y + outer.h;
9326
- }
9327
9666
  /**
9328
9667
  * Whether two segments cross away from their ends. Connectors that share a
9329
9668
  * node meet at its border, and an elbow's bends meet the next segment: both
@@ -9349,9 +9688,13 @@ function findWarnings(diagram) {
9349
9688
  const keys = diagram.nodeKeys();
9350
9689
  const nodes = elements.filter((el) => keys.has(el.id) && el.visible !== false);
9351
9690
  const boxes = new Map(nodes.map((el) => [el.id, getConnectorBox(el)]));
9352
- const containers = new Set(nodes.filter((outer) => nodes.some((inner) => inner !== outer && contains(boxes.get(outer.id), boxes.get(inner.id)))).map((el) => el.id));
9691
+ const containers = containerIds(nodes.map((el) => ({
9692
+ id: el.id,
9693
+ box: boxes.get(el.id),
9694
+ type: el.type
9695
+ })));
9353
9696
  const solid = nodes.filter((el) => !containers.has(el.id));
9354
- const cores = new Map(solid.map((el) => [el.id, coreRect(el)]));
9697
+ const cores = new Map(solid.map((el) => [el.id, nodeCoreRect(el.type, boxes.get(el.id))]));
9355
9698
  const edges = [];
9356
9699
  for (const el of elements) {
9357
9700
  if (!isConnector(el) || el.visible === false) continue;
@@ -9373,7 +9716,7 @@ function findWarnings(diagram) {
9373
9716
  for (const edge of edges) for (const node of solid) {
9374
9717
  if (node.id === edge.el.startElementId || node.id === edge.el.endElementId) continue;
9375
9718
  const core = cores.get(node.id);
9376
- if (edge.segments.reduce((sum, [a, b]) => sum + clippedLength(a, b, core), 0) >= MIN_INSIDE) throughNodes.push({
9719
+ if (edge.segments.reduce((sum, [a, b]) => sum + lengthInside(a, b, core), 0) >= 8) throughNodes.push({
9377
9720
  edge: edge.name,
9378
9721
  node: keys.get(node.id)
9379
9722
  });
@@ -9419,7 +9762,7 @@ function findWarnings(diagram) {
9419
9762
  const ba = boxes.get(a.id);
9420
9763
  for (const b of nodes.slice(i + 1)) {
9421
9764
  const bb = boxes.get(b.id);
9422
- if (overlaps(ba, bb) && !contains(ba, bb) && !contains(bb, ba)) nodeOverlaps.push([keys.get(a.id), keys.get(b.id)]);
9765
+ if (overlaps(ba, bb) && !containsRect(ba, bb) && !containsRect(bb, ba)) nodeOverlaps.push([keys.get(a.id), keys.get(b.id)]);
9423
9766
  }
9424
9767
  });
9425
9768
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markasso/mcp",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "mcpName": "io.github.AlbertoBarrago/markasso",
5
5
  "description": "MCP server that lets AI agents create, read and edit Markasso diagrams, offline or live on a shared board",
6
6
  "type": "module",