@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.d.cts CHANGED
@@ -668,7 +668,9 @@ declare const N_CAR_LENGTH_INCHES = 3.3;
668
668
  /** How many cars fit in a span, from its drawn length — the derived capacity a
669
669
  * siding or an industry spot holds (never typed). */
670
670
  declare function carCapacity(fromPos: number, toPos: number, carLengthInches?: number): number;
671
- /** Parse a jsonb value into a schematic doc, or null if it isn't one. */
671
+ /** Parse a jsonb value into a schematic doc, or null if it isn't one.
672
+ * EVERY read path goes through here (catalog, module page, my-modules, FD), so
673
+ * it's also where a stored doc gets healed — see healSelfDivergingTurnouts. */
672
674
  declare function asModuleSchematic(x: unknown): ModuleSchematicDoc | null;
673
675
  interface EditorTrack {
674
676
  id: string;
package/dist/index.d.ts CHANGED
@@ -668,7 +668,9 @@ declare const N_CAR_LENGTH_INCHES = 3.3;
668
668
  /** How many cars fit in a span, from its drawn length — the derived capacity a
669
669
  * siding or an industry spot holds (never typed). */
670
670
  declare function carCapacity(fromPos: number, toPos: number, carLengthInches?: number): number;
671
- /** Parse a jsonb value into a schematic doc, or null if it isn't one. */
671
+ /** Parse a jsonb value into a schematic doc, or null if it isn't one.
672
+ * EVERY read path goes through here (catalog, module page, my-modules, FD), so
673
+ * it's also where a stored doc gets healed — see healSelfDivergingTurnouts. */
672
674
  declare function asModuleSchematic(x: unknown): ModuleSchematicDoc | null;
673
675
  interface EditorTrack {
674
676
  id: string;
package/dist/index.js CHANGED
@@ -539,12 +539,23 @@ function carCapacity(fromPos, toPos, carLengthInches = N_CAR_LENGTH_INCHES) {
539
539
  if (!(carLengthInches > 0)) return 0;
540
540
  return Math.max(0, Math.floor(Math.abs(toPos - fromPos) / carLengthInches));
541
541
  }
542
+ function healSelfDivergingTurnouts(turnouts) {
543
+ if (!turnouts?.some((t) => t.onTrack === t.divergeTrack)) return turnouts;
544
+ return turnouts.map((t) => {
545
+ if (t.onTrack !== t.divergeTrack) return t;
546
+ if (t.onTrack === MAIN_TRACK_ID) return { ...t, divergeTrack: MAIN2_TRACK_ID };
547
+ if (t.onTrack === MAIN2_TRACK_ID) return { ...t, divergeTrack: MAIN_TRACK_ID };
548
+ return t;
549
+ });
550
+ }
542
551
  function asModuleSchematic(x) {
543
552
  if (!x || typeof x !== "object") return null;
544
553
  const d = x;
545
554
  if (typeof d.version !== "number") return null;
546
555
  if (!Array.isArray(d.endplates) || !Array.isArray(d.tracks)) return null;
547
- return d;
556
+ const doc = d;
557
+ const healed = healSelfDivergingTurnouts(doc.turnouts);
558
+ return healed === doc.turnouts ? doc : { ...doc, turnouts: healed };
548
559
  }
549
560
  function emptyEditorState(lengthInches) {
550
561
  return {
@@ -933,24 +944,19 @@ function docToState(doc, fallbackLength, moduleTracks = []) {
933
944
  trackB: x.tracks?.[1] ?? MAIN_TRACK_ID
934
945
  })),
935
946
  extraTracks,
936
- turnouts: (d.turnouts ?? []).map((t) => {
937
- let divergeTrack = t.divergeTrack;
938
- if (divergeTrack === t.onTrack) {
939
- if (t.onTrack === MAIN_TRACK_ID) divergeTrack = MAIN2_TRACK_ID;
940
- else if (t.onTrack === MAIN2_TRACK_ID) divergeTrack = MAIN_TRACK_ID;
941
- }
942
- return {
943
- id: t.id,
944
- name: t.name ?? "",
945
- pos: sc(t.pos),
946
- onTrack: t.onTrack,
947
- divergeTrack,
948
- kind: t.kind ?? "right",
949
- ...t.size ? { size: t.size } : {},
950
- ...t.curved ? { curved: true } : {},
951
- ...t.flipped ? { flipped: true } : {}
952
- };
953
- }),
947
+ // Heal a turnout that diverges into the track it sits on (#172) with the
948
+ // same helper the read path uses, so the editor and every renderer agree.
949
+ turnouts: (healSelfDivergingTurnouts(d.turnouts) ?? []).map((t) => ({
950
+ id: t.id,
951
+ name: t.name ?? "",
952
+ pos: sc(t.pos),
953
+ onTrack: t.onTrack,
954
+ divergeTrack: t.divergeTrack,
955
+ kind: t.kind ?? "right",
956
+ ...t.size ? { size: t.size } : {},
957
+ ...t.curved ? { curved: true } : {},
958
+ ...t.flipped ? { flipped: true } : {}
959
+ })),
954
960
  controlPoints: readControlPoints(d, sc),
955
961
  industries: (d.industries ?? []).map((ind) => ({
956
962
  id: ind.id,