expressive-charts 0.9.0 → 0.11.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/dist/index.cjs CHANGED
@@ -10082,6 +10082,40 @@ function between$1(outer, inner, root, screen = screenMatrix) {
10082
10082
  if (a && b) return matrix.multiply(invert(a), b);
10083
10083
  return matrix.multiply(invert(fromRoot(outer, root)), fromRoot(inner, root));
10084
10084
  }
10085
+ function mapMark(mark, m) {
10086
+ if (isIdentity$1(m)) return mark;
10087
+ const point = (p) => matrix.apply(m, p);
10088
+ const path = (d) => formatPath(transformPath(parsePath(d), m));
10089
+ const scale = scaleOf$2(m);
10090
+ const { outline } = mark;
10091
+ return {
10092
+ key: mark.key,
10093
+ ...mark.series === void 0 ? {} : { series: mark.series },
10094
+ start: point(mark.start),
10095
+ end: point(mark.end),
10096
+ length: mark.length * scale,
10097
+ path: (t) => point(mark.path(t)),
10098
+ angle: (t) => {
10099
+ const angle = mark.angle(t);
10100
+ const cos = Math.cos(angle);
10101
+ const sin = Math.sin(angle);
10102
+ return Math.atan2(m[1] * cos + m[3] * sin, m[0] * cos + m[2] * sin);
10103
+ },
10104
+ slice: (t0, t1) => path(mark.slice(t0, t1)),
10105
+ ...mark.gaps === void 0 ? {} : { gaps: mark.gaps },
10106
+ thickness: mark.thickness * scale,
10107
+ outline: {
10108
+ d: path(outline.d),
10109
+ closed: outline.closed,
10110
+ sample: (n) => outline.sample(n).map(point)
10111
+ },
10112
+ bounds: boxIn(m, mark.bounds),
10113
+ center: point(mark.center),
10114
+ pivot: point(mark.pivot),
10115
+ ...mark.segments === void 0 ? {} : { segments: mark.segments.map((segment) => mapMark(segment, m)) }
10116
+ };
10117
+ }
10118
+ const isIdentity$1 = (m) => m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
10085
10119
  function ownTransform(element) {
10086
10120
  return parseTransform(element.getAttribute("transform") ?? "");
10087
10121
  }
@@ -11121,7 +11155,7 @@ function expressive$1(target, options, env) {
11121
11155
  if (root.lastElementChild !== overlay) root.appendChild(overlay);
11122
11156
  overlay.removeAttribute("transform");
11123
11157
  const m = between$1(overlay, ancestor, root);
11124
- if (!isIdentity$1(m)) overlay.setAttribute("transform", transformAttribute(m));
11158
+ if (!isIdentity(m)) overlay.setAttribute("transform", transformAttribute(m));
11125
11159
  }
11126
11160
  function moveTo(next) {
11127
11161
  detach();
@@ -11458,6 +11492,7 @@ function expressive$1(target, options, env) {
11458
11492
  return true;
11459
11493
  }
11460
11494
  };
11495
+ const refreshed = /* @__PURE__ */ new Set();
11461
11496
  insides.set(link, {
11462
11497
  root: () => root,
11463
11498
  ancestor: () => ancestor,
@@ -11465,7 +11500,8 @@ function expressive$1(target, options, env) {
11465
11500
  keyOf: (element) => ownerOf(element)?.record.key,
11466
11501
  colorOf: (key) => records.get(key)?.mark.parts[0]?.colors?.color,
11467
11502
  moveTabStop: (key) => keyboard?.moveTabStop(key) ?? false,
11468
- edits
11503
+ edits,
11504
+ refreshed
11469
11505
  });
11470
11506
  return linkable(link, {
11471
11507
  refresh(next) {
@@ -11504,6 +11540,7 @@ function expressive$1(target, options, env) {
11504
11540
  for (const record of following) runner.change(record.mark.geometry, record.mark.data);
11505
11541
  runner.settle();
11506
11542
  keyboard?.sync();
11543
+ for (const listener of refreshed) listener();
11507
11544
  exited.then(() => {
11508
11545
  for (const record of diff.exiting) if (leaving.get(record.key) === record) leaving.delete(record.key);
11509
11546
  });
@@ -11669,7 +11706,7 @@ function tryInvert(m) {
11669
11706
  return;
11670
11707
  }
11671
11708
  }
11672
- function isIdentity$1(m) {
11709
+ function isIdentity(m) {
11673
11710
  return Math.abs(m[0] - 1) < 1e-9 && Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9 && Math.abs(m[3] - 1) < 1e-9 && Math.abs(m[4]) < 1e-6 && Math.abs(m[5]) < 1e-6;
11674
11711
  }
11675
11712
  function valuesChanged(from, to) {
@@ -11722,18 +11759,37 @@ function scaled(value, scale) {
11722
11759
  if (scale === 1) return value;
11723
11760
  return value.replace(/(-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(px)?(?![\w.%])/gi, (_, n, px) => `${Math.round(Number(n) * scale * 1e3) / 1e3}${px ?? ""}`);
11724
11761
  }
11725
- function hostView(attachment, options = {}) {
11726
- const port = typeof attachment === "object" && attachment !== null ? portOf(attachment) : void 0;
11727
- const inside = insidesOf(port);
11728
- if (!port || !inside?.root()) throw new TypeError("hostView needs an attachment that expressive() or fromVegaView() returned, with marks in an <svg>.");
11762
+ function hostView(attachments, options = {}) {
11763
+ const list = Array.isArray(attachments) ? attachments : [attachments];
11764
+ if (list.length === 0) throw new TypeError("hostView needs an attachment, or an array of attachments on one <svg>: hostView([dots, connectors]).");
11765
+ const parts = list.map((attachment, i) => {
11766
+ const port = typeof attachment === "object" && attachment !== null ? portOf(attachment) : void 0;
11767
+ const inside = insidesOf(port);
11768
+ if (!port || !inside?.root()) throw new TypeError(list === attachments ? `hostView needs attachments that expressive() or fromVegaView() returned, each with marks in an <svg>: attachments[${i}] isn't one.` : "hostView needs an attachment that expressive() or fromVegaView() returned, with marks in an <svg>.");
11769
+ if (list.indexOf(attachment) !== i) throw new TypeError(`hostView: attachments[${i}] is attachments[${list.indexOf(attachment)}] again. Each attachment is in a view once.`);
11770
+ return {
11771
+ attachment,
11772
+ port,
11773
+ inside,
11774
+ hidden: []
11775
+ };
11776
+ });
11729
11777
  if (typeof options !== "object" || options === null || options.labels !== void 0 && typeof options.labels !== "function") throw new TypeError("hostView: labels must be a function that returns the host's labels, such as () => names.map((element) => ({ element, key: element.textContent, role: \"category\" })).");
11730
- const { edits } = inside;
11731
- const svg = () => inside.root();
11778
+ if (options.guides !== void 0 && typeof options.guides !== "function") throw new TypeError("hostView: guides must be a function that returns the host's guides, such as () => [{ element: svg.querySelector(\"g.axis\"), key: \"y\" }].");
11779
+ const lead = parts[0];
11780
+ const several = parts.length > 1;
11781
+ const { edits } = lead.inside;
11782
+ const svg = () => lead.inside.root();
11732
11783
  let given;
11733
- let hidden = [];
11784
+ let guided;
11785
+ let classed;
11786
+ const clash = several ? clashOf(parts, svg()) : void 0;
11787
+ if (clash) throw new TypeError(`hostView: ${clash.problem}.`);
11788
+ const drawn = () => parts.filter((part) => part.inside.root() === svg());
11789
+ const ownerOf = (key) => drawn().find((part) => part.attachment.marks().some((mark) => mark.key === key));
11734
11790
  function read() {
11735
11791
  if (!options.labels) return [];
11736
- const keys = new Set(attachment.marks().map((mark) => mark.key));
11792
+ const keys = new Set(drawn().flatMap((part) => part.attachment.marks().map((mark) => mark.key)));
11737
11793
  try {
11738
11794
  return Array.from(options.labels()).filter((label) => label?.element?.localName === "text" && svg().contains(label.element) && keys.has(label.key) && (label.role === "category" || label.role === "value"));
11739
11795
  } catch (error) {
@@ -11741,6 +11797,16 @@ function hostView(attachment, options = {}) {
11741
11797
  return [];
11742
11798
  }
11743
11799
  }
11800
+ function guides() {
11801
+ if (!options.guides) return [];
11802
+ const root = svg();
11803
+ try {
11804
+ return Array.from(options.guides()).filter((guide) => (guide?.element)?.nodeType === 1 && guide.element !== root && root.contains(guide.element) && typeof guide.key === "string");
11805
+ } catch (error) {
11806
+ report(error);
11807
+ return [];
11808
+ }
11809
+ }
11744
11810
  function chartLabel({ element, key, role }) {
11745
11811
  const root = svg();
11746
11812
  const m = between$1(view.space, element, root);
@@ -11770,44 +11836,115 @@ function hostView(attachment, options = {}) {
11770
11836
  element: text
11771
11837
  };
11772
11838
  }
11839
+ const hide = (on) => {
11840
+ if (on && given === void 0 && several) {
11841
+ const found = clashOf(parts, svg());
11842
+ if (found) report(/* @__PURE__ */ new TypeError(`hostView: ${found.problem}. The switch plays on, and ${found.left}.`));
11843
+ }
11844
+ const root = on ? svg() : void 0;
11845
+ given = on ? read() : void 0;
11846
+ guided = on ? guides() : void 0;
11847
+ const named = [...(given ?? []).map((label) => label.element), ...(guided ?? []).map((guide) => guide.element)];
11848
+ const now = parts.map(({ inside, hidden }, i) => new Set(given ? [
11849
+ ...inside.elements(),
11850
+ ...i === 0 ? named : [],
11851
+ ...hidden.filter((element) => root?.contains(element))
11852
+ ] : []));
11853
+ for (let i = 1; i < now.length; i++) for (const element of now[i]) if (now.some((earlier, j) => j < i && earlier.has(element))) now[i].delete(element);
11854
+ parts.forEach((part, i) => {
11855
+ for (const element of part.hidden) if (!now[i].has(element)) part.inside.edits.style(element, "visibility", void 0);
11856
+ });
11857
+ parts.forEach((part, i) => {
11858
+ part.hidden = [...now[i]];
11859
+ for (const element of part.hidden) part.inside.edits.style(element, "visibility", "hidden");
11860
+ });
11861
+ if (classed !== root) {
11862
+ if (classed) edits.toggleClass(classed, "ec-morphing", false);
11863
+ if (root) edits.toggleClass(root, "ec-morphing", true);
11864
+ classed = root;
11865
+ }
11866
+ };
11773
11867
  const view = {
11774
11868
  get element() {
11775
11869
  return svg();
11776
11870
  },
11777
11871
  get space() {
11778
- return inside.ancestor() ?? svg();
11779
- },
11780
- refresh: (source) => attachment.refresh(source),
11781
- update: (next) => attachment.update(next),
11782
- on: (type, listener) => attachment.on(type, listener),
11783
- marks: () => attachment.marks(),
11784
- focus: (key) => attachment.focus(key),
11785
- leave: () => attachment.leave(),
11872
+ const root = svg();
11873
+ const [space, ...others] = drawn().map((part) => part.inside.ancestor() ?? root);
11874
+ let common = space;
11875
+ for (const other of others) while (common !== root && !common.contains(other)) common = common.parentElement ?? root;
11876
+ return common;
11877
+ },
11878
+ refresh(source) {
11879
+ if (several && source !== void 0) throw new TypeError("hostView: a view of several attachments refreshes each from what it last read, with refresh(). To read new sources, refresh each attachment with its own.");
11880
+ for (const { attachment } of parts) attachment.refresh(source);
11881
+ },
11882
+ update: (next) => several ? Promise.all(parts.map(({ attachment }) => attachment.update(next))).then(() => {}) : lead.attachment.update(next),
11883
+ on(type, listener) {
11884
+ const stops = parts.map(({ attachment }) => attachment.on(type, listener));
11885
+ return several ? () => stops.forEach((stop) => stop()) : stops[0];
11886
+ },
11887
+ marks() {
11888
+ const root = svg();
11889
+ const space = view.space;
11890
+ const keys = /* @__PURE__ */ new Set();
11891
+ return drawn().flatMap(({ attachment, inside }) => {
11892
+ const ancestor = inside.ancestor() ?? root;
11893
+ const m = ancestor === space ? void 0 : between$1(space, ancestor, root);
11894
+ return attachment.marks().flatMap((mark) => keys.has(mark.key) ? [] : (keys.add(mark.key), [m ? mapMark(mark, m) : mark]));
11895
+ });
11896
+ },
11897
+ focus: (key) => ownerOf(key)?.attachment.focus(key),
11898
+ leave() {
11899
+ for (const { attachment } of parts) attachment.leave();
11900
+ },
11786
11901
  destroy() {
11787
- given = void 0;
11788
- hidden = [];
11789
- attachment.destroy();
11902
+ given = guided = classed = void 0;
11903
+ for (const part of parts) part.hidden = [];
11904
+ for (const { attachment } of [...parts].reverse()) attachment.destroy();
11790
11905
  },
11791
11906
  labels: () => (given ?? read()).map(chartLabel),
11907
+ guides: () => guided ?? guides(),
11792
11908
  paintOf(key) {
11793
- const color = inside.colorOf(key);
11794
- return color === void 0 ? void 0 : {
11795
- color: key,
11796
- drawn: true,
11797
- cssColor: color
11798
- };
11909
+ for (const { inside } of drawn()) {
11910
+ const color = inside.colorOf(key);
11911
+ if (color !== void 0) return {
11912
+ color: key,
11913
+ drawn: true,
11914
+ cssColor: color
11915
+ };
11916
+ }
11799
11917
  },
11800
- keyOf: (element) => inside.keyOf(element),
11801
- moveTabStop: (key) => inside.moveTabStop(key),
11802
- morphing(on) {
11803
- for (const element of hidden) edits.style(element, "visibility", void 0);
11804
- given = on ? read() : void 0;
11805
- hidden = given ? [...inside.elements(), ...given.map((label) => label.element)] : [];
11806
- for (const element of hidden) edits.style(element, "visibility", "hidden");
11807
- edits.toggleClass(svg(), "ec-morphing", on);
11808
- }
11918
+ keyOf(element) {
11919
+ for (const { inside } of parts) {
11920
+ const key = inside.keyOf(element);
11921
+ if (key !== void 0) return key;
11922
+ }
11923
+ },
11924
+ moveTabStop: (key) => drawn().some(({ inside }) => inside.moveTabStop(key)),
11925
+ morphing: hide
11809
11926
  };
11810
- return linkable(port, view);
11927
+ for (const { inside } of parts) inside.refreshed.add(() => {
11928
+ if (given) hide(true);
11929
+ });
11930
+ return several ? view : linkable(lead.port, view);
11931
+ }
11932
+ function clashOf(parts, root) {
11933
+ const owners = /* @__PURE__ */ new Map();
11934
+ for (const [i, { attachment, inside }] of parts.entries()) {
11935
+ if (inside.root() !== root) return {
11936
+ problem: `attachments[${i}]'s marks are in another <svg> than attachments[0]'s, and a view's attachments share one`,
11937
+ left: `attachments[${i}]'s marks are left out`
11938
+ };
11939
+ for (const { key } of attachment.marks()) {
11940
+ const owner = owners.get(key);
11941
+ if (owner !== void 0 && owner !== i) return {
11942
+ problem: `attachments[${owner}] and attachments[${i}] both have a mark with the key ${JSON.stringify(key)}, and each key must be one mark's. Key each one's marks apart, such as (d) => \`first@\${d.name}\``,
11943
+ left: `the key stays attachments[${owner}]'s: attachments[${i}]'s mark is left out`
11944
+ };
11945
+ owners.set(key, i);
11946
+ }
11947
+ }
11811
11948
  }
11812
11949
  // src/charts/adapter/vega.ts
11813
11950
  const MARK_TYPES = [
@@ -12506,6 +12643,7 @@ function chartSwitch$1(target, options, env) {
12506
12643
  }) : void 0;
12507
12644
  observer?.observe(wrapper);
12508
12645
  const view = (name) => views.get(name);
12646
+ const missing = (name, what = "") => /* @__PURE__ */ new TypeError(`chartSwitch: there's no view ${JSON.stringify(name)}${what}. The views are ${names.map((n) => JSON.stringify(n)).join(", ")}.`);
12509
12647
  function focused() {
12510
12648
  return wrapper.getRootNode().activeElement ?? null;
12511
12649
  }
@@ -12541,7 +12679,10 @@ function chartSwitch$1(target, options, env) {
12541
12679
  function reached() {
12542
12680
  if (held) linked?.shown();
12543
12681
  }
12544
- function start(from, to, carried) {
12682
+ function jump(from, to, via, carried) {
12683
+ return via === void 0 || !motion.staged || via === from.name || via === to.name ? start(from, to, carried) : start(from, view(via), carried, to);
12684
+ }
12685
+ function start(from, to, carried, next) {
12545
12686
  const memory = carried ? carried.focus : readFocus(from);
12546
12687
  const waiting = [];
12547
12688
  opening = waiting;
@@ -12550,28 +12691,31 @@ function chartSwitch$1(target, options, env) {
12550
12691
  } finally {
12551
12692
  opening = void 0;
12552
12693
  }
12553
- const done = destroyed ? Promise.resolve() : begin(from, to, memory);
12694
+ const done = destroyed ? Promise.resolve() : begin(from, to, memory, next);
12554
12695
  for (const run of waiting) run();
12555
12696
  return done;
12556
12697
  }
12557
- function begin(from, to, memory) {
12558
- current = to.name;
12698
+ function begin(from, to, memory, next, carried) {
12699
+ current = (next ?? to).name;
12559
12700
  if (motion.mode === "off") {
12560
- for (const each of views.values()) setActive(each.container, each === to);
12561
- restoreFocus(to, memory);
12562
- say(to);
12701
+ const end = view(current);
12702
+ for (const each of views.values()) setActive(each.container, each === end);
12703
+ restoreFocus(end, memory);
12704
+ say(end);
12563
12705
  reached();
12564
12706
  return Promise.resolve();
12565
12707
  }
12708
+ const guides = [];
12566
12709
  for (const each of [from, to]) {
12567
12710
  each.container.classList.remove("ec-switch-inactive");
12711
+ guides.push(guidesOf(each));
12568
12712
  each.container.classList.add("ec-switching");
12569
12713
  each.container.setAttribute("inert", "");
12570
12714
  each.chart.morphing?.(true);
12571
12715
  each.chart.element.classList.add("ec-morphing");
12572
12716
  }
12573
- const { pairs, surface, scene } = prepare(from, to);
12574
- const { done, settle } = deferred();
12717
+ const { pairs, surface, scene } = prepare(from, to, guides);
12718
+ const { done, settle } = carried ?? deferred();
12575
12719
  const session = {
12576
12720
  a: from,
12577
12721
  b: to,
@@ -12584,6 +12728,7 @@ function chartSwitch$1(target, options, env) {
12584
12728
  surface,
12585
12729
  scene,
12586
12730
  focus: memory,
12731
+ next,
12587
12732
  progress: 0,
12588
12733
  transition: void 0,
12589
12734
  driver: void 0,
@@ -12598,7 +12743,7 @@ function chartSwitch$1(target, options, env) {
12598
12743
  function play(session, begin) {
12599
12744
  const run = ++session.run;
12600
12745
  if (begin >= 1) {
12601
- land(session, true);
12746
+ arrive(session);
12602
12747
  return;
12603
12748
  }
12604
12749
  const at = (progress) => {
@@ -12620,7 +12765,19 @@ function chartSwitch$1(target, options, env) {
12620
12765
  });
12621
12766
  started.release();
12622
12767
  Promise.all([session.driver.finished, session.transition.finished]).then(([result]) => {
12623
- if (active === session && session.run === run && result === "done") land(session, true);
12768
+ if (active === session && session.run === run && result === "done") arrive(session);
12769
+ });
12770
+ }
12771
+ function arrive(session) {
12772
+ const next = session.forward ? session.next : void 0;
12773
+ if (!next || motion.mode === "off") return land(session, true);
12774
+ const { done, settle } = session;
12775
+ session.next = void 0;
12776
+ session.settle = () => {};
12777
+ land(session, false);
12778
+ begin(session.b, next, session.focus, void 0, {
12779
+ done,
12780
+ settle
12624
12781
  });
12625
12782
  }
12626
12783
  function reverse(session) {
@@ -12640,7 +12797,7 @@ function chartSwitch$1(target, options, env) {
12640
12797
  session.driver?.cancel();
12641
12798
  session.transition?.clear();
12642
12799
  session.scene.clear();
12643
- const to = session.forward ? session.b : session.a;
12800
+ const to = session.forward ? session.next ?? session.b : session.a;
12644
12801
  for (const each of [session.a, session.b]) {
12645
12802
  each.chart.element.classList.remove("ec-morphing");
12646
12803
  each.chart.morphing?.(false);
@@ -12655,7 +12812,7 @@ function chartSwitch$1(target, options, env) {
12655
12812
  }
12656
12813
  session.settle();
12657
12814
  }
12658
- function prepare(a, b) {
12815
+ function prepare(a, b, guides) {
12659
12816
  const toA = between$1(overlay, a.chart.space ?? a.chart.element, wrapper);
12660
12817
  const toB = between$1(overlay, b.chart.space ?? b.chart.element, wrapper);
12661
12818
  const scaleA = scaleOf(toA);
@@ -12705,9 +12862,39 @@ function chartSwitch$1(target, options, env) {
12705
12862
  }
12706
12863
  for (const label of labelsA) if (!matched.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toA, sizeA), fadeOut));
12707
12864
  for (const label of labelsB) if (!byId.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toB, sizeB), fadeIn));
12865
+ const [guidesA = [], guidesB = []] = guides;
12866
+ const partners = /* @__PURE__ */ new Map();
12867
+ for (const guide of guidesA) if (!partners.has(guide.key)) partners.set(guide.key, guide);
12868
+ const kept = /* @__PURE__ */ new Set();
12869
+ for (const guide of guidesB) {
12870
+ const partner = partners.get(guide.key);
12871
+ partners.delete(guide.key);
12872
+ if (partner && sameGuide(partner, guide)) kept.add(partner).add(guide);
12873
+ }
12874
+ let guidesLayer;
12875
+ if (guidesA.length + guidesB.length > 0) {
12876
+ const layer = guidesLayer = doc.createElementNS(SVG_NS$2, "g");
12877
+ layer.setAttribute("class", "ec-switch-guides");
12878
+ overlay.insertBefore(layer, fadesLayer);
12879
+ [[
12880
+ guidesA,
12881
+ (s) => s < HALF ? 1 : 0,
12882
+ othersOut
12883
+ ], [
12884
+ guidesB,
12885
+ (s) => s < HALF ? 0 : 1,
12886
+ othersIn
12887
+ ]].forEach(([list, steady, fading]) => {
12888
+ for (const guide of list) {
12889
+ layer.appendChild(guide.group);
12890
+ const opacity = kept.has(guide) ? steady : fading;
12891
+ drawings.push((s) => guide.group.setAttribute("opacity", opacityText(opacity(s))));
12892
+ }
12893
+ });
12894
+ }
12708
12895
  drawings.push((s) => {
12709
- setOpacity(a.container, 1 - clamp$1(s / OUT_BY));
12710
- setOpacity(b.container, clamp$1((s - HALF) / .5));
12896
+ setOpacity(a.container, othersOut(s));
12897
+ setOpacity(b.container, othersIn(s));
12711
12898
  });
12712
12899
  return {
12713
12900
  pairs,
@@ -12717,6 +12904,7 @@ function chartSwitch$1(target, options, env) {
12717
12904
  for (const draw of drawings) draw(s);
12718
12905
  },
12719
12906
  clear: () => {
12907
+ guidesLayer?.remove();
12720
12908
  fadesLayer.replaceChildren();
12721
12909
  runsLayer.replaceChildren();
12722
12910
  labelsLayer.replaceChildren();
@@ -12724,6 +12912,31 @@ function chartSwitch$1(target, options, env) {
12724
12912
  }
12725
12913
  };
12726
12914
  }
12915
+ function guidesOf(view) {
12916
+ const root = view.chart.element;
12917
+ return (view.chart.guides?.() ?? []).flatMap((guide) => {
12918
+ const element = guide?.element;
12919
+ const parent = element?.parentElement;
12920
+ if (typeof guide?.key !== "string" || !parent || !root.contains(parent)) return [];
12921
+ const m = between$1(overlay, parent, wrapper);
12922
+ const copy = paintedCopy(element, root);
12923
+ const group = doc.createElementNS(SVG_NS$2, "g");
12924
+ setAttributes(group, {
12925
+ class: "ec-switch-guide",
12926
+ "data-key": guide.key,
12927
+ transform: isIdentity$1(m) ? void 0 : transformAttribute(m)
12928
+ });
12929
+ group.appendChild(copy);
12930
+ const own = ownTransform(element);
12931
+ return [{
12932
+ key: guide.key,
12933
+ group,
12934
+ markup: copy.outerHTML,
12935
+ corners: cornersOf(element).map((p) => matrix.apply(m, matrix.apply(own, p))),
12936
+ place: m
12937
+ }];
12938
+ });
12939
+ }
12727
12940
  function fadeMark(mark, paint, scale, opacity) {
12728
12941
  const group = svgElement("g", {
12729
12942
  class: "ec-switch-fade",
@@ -12781,23 +12994,35 @@ function chartSwitch$1(target, options, env) {
12781
12994
  get current() {
12782
12995
  return current;
12783
12996
  },
12784
- show(name) {
12785
- if (!views.has(name)) return Promise.reject(/* @__PURE__ */ new TypeError(`chartSwitch: there's no view ${JSON.stringify(name)}. The views are ${names.map((n) => JSON.stringify(n)).join(", ")}.`));
12997
+ show(name, options) {
12998
+ if (!views.has(name)) return Promise.reject(missing(name));
12999
+ const via = options?.via;
13000
+ if (via !== void 0 && !views.has(via)) return Promise.reject(missing(via, " to go via"));
12786
13001
  if (destroyed) return Promise.resolve();
12787
13002
  const waiting = opening;
12788
- if (waiting) return new Promise((resolve) => waiting.push(() => resolve(api.show(name))));
13003
+ if (waiting) return new Promise((resolve) => waiting.push(() => resolve(api.show(name, options))));
12789
13004
  const session = active;
12790
- if (!session) return name === current ? Promise.resolve() : start(view(current), view(name));
13005
+ if (!session) return name === current ? Promise.resolve() : jump(view(current), view(name), via);
12791
13006
  const heading = session.forward ? session.b : session.a;
12792
13007
  const leaving = session.forward ? session.a : session.b;
12793
- if (name === heading.name) return session.done;
13008
+ const next = session.next;
13009
+ if (name === (next ?? heading).name) return session.done;
13010
+ if (next) {
13011
+ session.next = void 0;
13012
+ if (name === heading.name) {
13013
+ current = name;
13014
+ session.settle();
13015
+ ({done: session.done, settle: session.settle} = deferred());
13016
+ return session.done;
13017
+ }
13018
+ }
12794
13019
  if (name === leaving.name) return reverse(session);
12795
13020
  land(session, false);
12796
- return start(heading, view(name), { focus: session.focus });
13021
+ return jump(heading, view(name), via, { focus: session.focus });
12797
13022
  },
12798
13023
  chart(name) {
12799
13024
  const found = views.get(name);
12800
- if (!found) throw new TypeError(`chartSwitch: there's no view ${JSON.stringify(name)}. The views are ${names.map((n) => JSON.stringify(n)).join(", ")}.`);
13025
+ if (!found) throw missing(name);
12801
13026
  return found.chart;
12802
13027
  },
12803
13028
  on(type, listener) {
@@ -12917,8 +13142,105 @@ function setActive(container, active) {
12917
13142
  else container.setAttribute("aria-hidden", "true");
12918
13143
  }
12919
13144
  function setOpacity(container, opacity) {
12920
- container.style.setProperty("opacity", String(Math.round(opacity * 1e3) / 1e3));
13145
+ container.style.setProperty("opacity", opacityText(opacity));
13146
+ }
13147
+ const opacityText = (opacity) => String(Math.round(opacity * 1e3) / 1e3);
13148
+ const INHERITED = [
13149
+ "fill",
13150
+ "fill-opacity",
13151
+ "fill-rule",
13152
+ "stroke",
13153
+ "stroke-opacity",
13154
+ "stroke-width",
13155
+ "stroke-dasharray",
13156
+ "stroke-dashoffset",
13157
+ "stroke-linecap",
13158
+ "stroke-linejoin",
13159
+ "stroke-miterlimit",
13160
+ "paint-order",
13161
+ "marker-start",
13162
+ "marker-mid",
13163
+ "marker-end",
13164
+ "color",
13165
+ "visibility",
13166
+ "font-family",
13167
+ "font-size",
13168
+ "font-style",
13169
+ "font-weight",
13170
+ "font-stretch",
13171
+ "font-variant-caps",
13172
+ "font-variant-ligatures",
13173
+ "font-variant-numeric",
13174
+ "font-feature-settings",
13175
+ "font-kerning",
13176
+ "letter-spacing",
13177
+ "word-spacing",
13178
+ "text-anchor",
13179
+ "dominant-baseline",
13180
+ "text-rendering",
13181
+ "shape-rendering",
13182
+ "writing-mode",
13183
+ "direction"
13184
+ ];
13185
+ const OWN = {
13186
+ opacity: "1",
13187
+ display: "inline",
13188
+ "clip-path": "none",
13189
+ mask: "none",
13190
+ filter: "none",
13191
+ "vector-effect": "none"
13192
+ };
13193
+ function paintedCopy(source, root) {
13194
+ const copy = source.cloneNode(true);
13195
+ const sources = [source, ...source.querySelectorAll("*")];
13196
+ const copies = [copy, ...copy.querySelectorAll("*")];
13197
+ sources.forEach((element, i) => {
13198
+ const target = copies[i];
13199
+ target.removeAttribute("id");
13200
+ const computed = element.ownerDocument.defaultView?.getComputedStyle?.(element);
13201
+ const styles = {};
13202
+ for (const property of INHERITED) styles[property] = drawnStyle(element, root, property) || void 0;
13203
+ for (const [property, initial] of Object.entries(OWN)) {
13204
+ const value = computed?.getPropertyValue(property).trim();
13205
+ if (value && value !== initial) styles[property] = value;
13206
+ }
13207
+ setStyles(target, styles);
13208
+ });
13209
+ return copy;
13210
+ }
13211
+ function cornersOf(element) {
13212
+ try {
13213
+ const box = element.getBBox?.();
13214
+ if (box) return [
13215
+ {
13216
+ x: box.x,
13217
+ y: box.y
13218
+ },
13219
+ {
13220
+ x: box.x + box.width,
13221
+ y: box.y
13222
+ },
13223
+ {
13224
+ x: box.x,
13225
+ y: box.y + box.height
13226
+ },
13227
+ {
13228
+ x: box.x + box.width,
13229
+ y: box.y + box.height
13230
+ }
13231
+ ];
13232
+ } catch {}
13233
+ return [{
13234
+ x: 0,
13235
+ y: 0
13236
+ }];
12921
13237
  }
13238
+ const sameGuide = (a, b) => a.markup === b.markup && a.corners.length === b.corners.length && a.corners.every((p, i) => Math.hypot(p.x - b.corners[i].x, p.y - b.corners[i].y) <= .5) && [
13239
+ 0,
13240
+ 1,
13241
+ 2,
13242
+ 3
13243
+ ].every((i) => Math.abs(a.place[i] - b.place[i]) <= 1e-4);
12922
13244
  function moveLabel(a, b) {
12923
13245
  return (s) => {
12924
13246
  const x = mix$1(a.at.x, b.at.x, s);
@@ -12951,6 +13273,8 @@ function fadeLabel(label, opacity) {
12951
13273
  }
12952
13274
  const fadeOut = (s) => 1 - clamp$1(s / HALF);
12953
13275
  const fadeIn = (s) => clamp$1((s - HALF) / .5);
13276
+ const othersOut = (s) => 1 - clamp$1(s / OUT_BY);
13277
+ const othersIn = fadeIn;
12954
13278
  const clamp$1 = (value) => Math.min(1, Math.max(0, value));
12955
13279
  const mix$1 = (x, y, s) => x * (1 - s) + y * s;
12956
13280
  function paintOf(view, mark) {
@@ -12984,41 +13308,7 @@ function placeDot(element, end) {
12984
13308
  "stroke-width": end.ring
12985
13309
  });
12986
13310
  }
12987
- const isIdentity = (m) => m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
12988
13311
  const scaleOf = (m) => Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
12989
- function mapMark(mark, m) {
12990
- if (isIdentity(m)) return mark;
12991
- const point = (p) => matrix.apply(m, p);
12992
- const path = (d) => formatPath(transformPath(parsePath(d), m));
12993
- const scale = scaleOf(m);
12994
- const { outline } = mark;
12995
- return {
12996
- key: mark.key,
12997
- ...mark.series === void 0 ? {} : { series: mark.series },
12998
- start: point(mark.start),
12999
- end: point(mark.end),
13000
- length: mark.length * scale,
13001
- path: (t) => point(mark.path(t)),
13002
- angle: (t) => {
13003
- const angle = mark.angle(t);
13004
- const cos = Math.cos(angle);
13005
- const sin = Math.sin(angle);
13006
- return Math.atan2(m[1] * cos + m[3] * sin, m[0] * cos + m[2] * sin);
13007
- },
13008
- slice: (t0, t1) => path(mark.slice(t0, t1)),
13009
- ...mark.gaps === void 0 ? {} : { gaps: mark.gaps },
13010
- thickness: mark.thickness * scale,
13011
- outline: {
13012
- d: path(outline.d),
13013
- closed: outline.closed,
13014
- sample: (n) => outline.sample(n).map(point)
13015
- },
13016
- bounds: boxIn(m, mark.bounds),
13017
- center: point(mark.center),
13018
- pivot: point(mark.pivot),
13019
- ...mark.segments === void 0 ? {} : { segments: mark.segments.map((segment) => mapMark(segment, m)) }
13020
- };
13021
- }
13022
13312
  function deferred() {
13023
13313
  let settle;
13024
13314
  return {
@@ -14223,10 +14513,11 @@ function drawing(from, to, layer, points, align) {
14223
14513
  if (closed !== to.outline.closed) return crossfade(from, to, layer);
14224
14514
  const across = closed ? void 0 : acrossX(from, to, points);
14225
14515
  if (across) return piecewise(from, to, layer, across.stops, across.kinds);
14226
- const a = from.outline.sample(points);
14227
- const b = to.outline.sample(points);
14516
+ const bands = closed ? bandsAcross(from, to, points) : void 0;
14517
+ const a = bands?.from ?? from.outline.sample(points);
14518
+ const b = bands?.to ?? to.outline.sample(points);
14228
14519
  if (!closed) return from.gaps?.length || to.gaps?.length ? gapped(from, to, layer, a, b) : stroke(from, to, layer, a, b);
14229
- const [shiftA, shiftB] = align === "drop" ? turn(from, to, points) : [0, 0];
14520
+ const [shiftA, shiftB] = !bands && align === "drop" ? turn(from, to, points) : [0, 0];
14230
14521
  const holes = holePairs(from, to, a, b, [shiftA, shiftB], points / 4);
14231
14522
  let path;
14232
14523
  return (p) => {
@@ -14316,6 +14607,9 @@ function acrossX(from, to, points) {
14316
14607
  const a = courseOf(from);
14317
14608
  const b = a && courseOf(to);
14318
14609
  if (!a || !b || a.way !== b.way) return void 0;
14610
+ return byX(a, b, points);
14611
+ }
14612
+ function byX(a, b, points) {
14319
14613
  const all = [
14320
14614
  ...Array.from({ length: points }, (_, i) => [i / (points - 1), 0]),
14321
14615
  ...corners(a).map((s) => [s, 1]),
@@ -14371,7 +14665,9 @@ function courseOf(mark) {
14371
14665
  before.push(points.length - 1);
14372
14666
  from = end;
14373
14667
  }
14374
- if (!stretch(1) || next < subpaths.length || points.length < 2) return void 0;
14668
+ return !stretch(1) || next < subpaths.length || points.length < 2 ? void 0 : courseThrough(points, before);
14669
+ }
14670
+ function courseThrough(points, before = []) {
14375
14671
  const x0 = points[0].x;
14376
14672
  const span = points[points.length - 1].x - x0;
14377
14673
  const way = Math.sign(span);
@@ -14412,6 +14708,48 @@ function placed(course, shares) {
14412
14708
  return mixed(points[i], points[i + 1], width > 0 ? Math.min(1, Math.max(0, (s - at[i]) / width)) : 0);
14413
14709
  });
14414
14710
  }
14711
+ function bandsAcross(from, to, points) {
14712
+ const a = bandOf(from.outline);
14713
+ const b = a && bandOf(to.outline);
14714
+ if (!a || !b || Math.abs(a.left - b.left) > SAME_X || Math.abs(a.right - b.right) > SAME_X) return void 0;
14715
+ const upper = byX(a.upper, b.upper, points);
14716
+ const lower = byX(a.lower, b.lower, points);
14717
+ if (!upper || !lower) return void 0;
14718
+ const stops = [...upper.stops, ...lower.stops];
14719
+ return {
14720
+ from: stops.map((stop) => stop.from),
14721
+ to: stops.map((stop) => stop.to)
14722
+ };
14723
+ }
14724
+ function bandOf(outline) {
14725
+ const loops = loopsOf(outline);
14726
+ const ring = [];
14727
+ for (const segment of loops[outsideOf(loops)] ?? []) {
14728
+ if (segment.kind !== "line") return void 0;
14729
+ ring.push(segment.to);
14730
+ }
14731
+ if (areaOf(ring) < 0) ring.reverse();
14732
+ const left = ring.reduce((x, point) => Math.min(x, point.x), Infinity);
14733
+ const right = ring.reduce((x, point) => Math.max(x, point.x), -Infinity);
14734
+ if (!(right - left > 2 * SAME_X)) return void 0;
14735
+ const atLeft = (point) => point.x - left <= SAME_X;
14736
+ const atRight = (point) => right - point.x <= SAME_X;
14737
+ const s = ring.findIndex((point, i) => atLeft(point) && !atLeft(ring[(i + 1) % ring.length]));
14738
+ const walk = [...ring.slice(s), ...ring.slice(0, s + 1)];
14739
+ const topRight = walk.findIndex(atRight);
14740
+ let baseRight = topRight;
14741
+ while (atRight(walk[baseRight + 1])) baseRight++;
14742
+ const baseLeft = walk.findIndex((point, i) => i > baseRight && atLeft(point));
14743
+ if (walk.slice(baseLeft).some((point) => !atLeft(point))) return void 0;
14744
+ const upper = courseThrough(walk.slice(0, topRight + 1));
14745
+ const lower = courseThrough(walk.slice(baseRight, baseLeft + 1));
14746
+ return upper && lower && upper.points.length > 2 && lower.points.length > 2 ? {
14747
+ upper,
14748
+ lower,
14749
+ left,
14750
+ right
14751
+ } : void 0;
14752
+ }
14415
14753
  function inside(gaps) {
14416
14754
  let k = 0;
14417
14755
  return (s) => {
@@ -14537,13 +14875,9 @@ function placeIn(point, own, other) {
14537
14875
  };
14538
14876
  }
14539
14877
  function holesOf(outline, count) {
14540
- const loops = flattenPath(parsePath(outline.d)).map((subpath) => subpath.segments).filter((segments) => segments.length > 0);
14878
+ const loops = loopsOf(outline);
14541
14879
  if (loops.length < 2) return [];
14542
- const lengths = loops.map((loop) => loop.reduce((sum, segment) => sum + segment.length, 0));
14543
- let longest = 0;
14544
- lengths.forEach((length, i) => {
14545
- if (length > lengths[longest] + Math.max(.01, lengths[longest] / 1e3)) longest = i;
14546
- });
14880
+ const longest = outsideOf(loops);
14547
14881
  return loops.filter((_, i) => i !== longest).map((loop) => {
14548
14882
  const round = loopOutline([loop]).sample(4 * count);
14549
14883
  const points = round.filter((_, i) => i % 4 === 0);
@@ -14559,6 +14893,15 @@ function holesOf(outline, count) {
14559
14893
  };
14560
14894
  }).sort((x, y) => y.area - x.area);
14561
14895
  }
14896
+ const loopsOf = (outline) => flattenPath(parsePath(outline.d)).map((subpath) => subpath.segments).filter((segments) => segments.length > 0);
14897
+ function outsideOf(loops) {
14898
+ const lengths = loops.map((loop) => loop.reduce((sum, segment) => sum + segment.length, 0));
14899
+ let longest = 0;
14900
+ lengths.forEach((length, i) => {
14901
+ if (length > lengths[longest] + Math.max(.01, lengths[longest] / 1e3)) longest = i;
14902
+ });
14903
+ return longest;
14904
+ }
14562
14905
  function areaOf(points) {
14563
14906
  let sum = 0;
14564
14907
  points.forEach((p, i) => {
@@ -15213,7 +15556,7 @@ function configure(options) {
15213
15556
  }
15214
15557
  }
15215
15558
  // src/index.ts
15216
- const VERSION = "0.9.0";
15559
+ const VERSION = "0.11.0";
15217
15560
  const env = {
15218
15561
  presets: presetRegistry,
15219
15562
  icons: iconRegistry,