@willcgage/module-schematic 0.49.5 → 0.49.6

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
@@ -541,12 +541,23 @@ function carCapacity(fromPos, toPos, carLengthInches = N_CAR_LENGTH_INCHES) {
541
541
  if (!(carLengthInches > 0)) return 0;
542
542
  return Math.max(0, Math.floor(Math.abs(toPos - fromPos) / carLengthInches));
543
543
  }
544
+ function healSelfDivergingTurnouts(turnouts) {
545
+ if (!turnouts?.some((t) => t.onTrack === t.divergeTrack)) return turnouts;
546
+ return turnouts.map((t) => {
547
+ if (t.onTrack !== t.divergeTrack) return t;
548
+ if (t.onTrack === MAIN_TRACK_ID) return { ...t, divergeTrack: MAIN2_TRACK_ID };
549
+ if (t.onTrack === MAIN2_TRACK_ID) return { ...t, divergeTrack: MAIN_TRACK_ID };
550
+ return t;
551
+ });
552
+ }
544
553
  function asModuleSchematic(x) {
545
554
  if (!x || typeof x !== "object") return null;
546
555
  const d = x;
547
556
  if (typeof d.version !== "number") return null;
548
557
  if (!Array.isArray(d.endplates) || !Array.isArray(d.tracks)) return null;
549
- return d;
558
+ const doc = d;
559
+ const healed = healSelfDivergingTurnouts(doc.turnouts);
560
+ return healed === doc.turnouts ? doc : { ...doc, turnouts: healed };
550
561
  }
551
562
  function emptyEditorState(lengthInches) {
552
563
  return {
@@ -935,24 +946,19 @@ function docToState(doc, fallbackLength, moduleTracks = []) {
935
946
  trackB: x.tracks?.[1] ?? MAIN_TRACK_ID
936
947
  })),
937
948
  extraTracks,
938
- turnouts: (d.turnouts ?? []).map((t) => {
939
- let divergeTrack = t.divergeTrack;
940
- if (divergeTrack === t.onTrack) {
941
- if (t.onTrack === MAIN_TRACK_ID) divergeTrack = MAIN2_TRACK_ID;
942
- else if (t.onTrack === MAIN2_TRACK_ID) divergeTrack = MAIN_TRACK_ID;
943
- }
944
- return {
945
- id: t.id,
946
- name: t.name ?? "",
947
- pos: sc(t.pos),
948
- onTrack: t.onTrack,
949
- divergeTrack,
950
- kind: t.kind ?? "right",
951
- ...t.size ? { size: t.size } : {},
952
- ...t.curved ? { curved: true } : {},
953
- ...t.flipped ? { flipped: true } : {}
954
- };
955
- }),
949
+ // Heal a turnout that diverges into the track it sits on (#172) with the
950
+ // same helper the read path uses, so the editor and every renderer agree.
951
+ turnouts: (healSelfDivergingTurnouts(d.turnouts) ?? []).map((t) => ({
952
+ id: t.id,
953
+ name: t.name ?? "",
954
+ pos: sc(t.pos),
955
+ onTrack: t.onTrack,
956
+ divergeTrack: t.divergeTrack,
957
+ kind: t.kind ?? "right",
958
+ ...t.size ? { size: t.size } : {},
959
+ ...t.curved ? { curved: true } : {},
960
+ ...t.flipped ? { flipped: true } : {}
961
+ })),
956
962
  controlPoints: readControlPoints(d, sc),
957
963
  industries: (d.industries ?? []).map((ind) => ({
958
964
  id: ind.id,