@willcgage/module-schematic 0.49.4 → 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
@@ -464,13 +464,15 @@ function moduleFootprint(input) {
464
464
  const authored = benchworkOutline(input);
465
465
  const offA = input.endplateTrackOffsets?.["A"] ?? 0;
466
466
  const offB = input.endplateTrackOffsets?.["B"] ?? 0;
467
- const sectionOutlines = sectionFootprints(input, {
467
+ const secs = input.sections ?? [];
468
+ const sectionsOwnShape = secs.length > 1 || secs.some((s) => (s.outline?.length ?? 0) >= 3);
469
+ const sectionOutlines = sectionsOwnShape ? sectionFootprints(input, {
468
470
  centerline,
469
471
  widthA,
470
472
  widthB,
471
473
  offsetA: offA,
472
474
  offsetB: offB
473
- });
475
+ }) : [];
474
476
  return {
475
477
  centerline,
476
478
  band: benchworkBand(centerline, widthA, widthB, offA, offB),
@@ -539,12 +541,23 @@ function carCapacity(fromPos, toPos, carLengthInches = N_CAR_LENGTH_INCHES) {
539
541
  if (!(carLengthInches > 0)) return 0;
540
542
  return Math.max(0, Math.floor(Math.abs(toPos - fromPos) / carLengthInches));
541
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
+ }
542
553
  function asModuleSchematic(x) {
543
554
  if (!x || typeof x !== "object") return null;
544
555
  const d = x;
545
556
  if (typeof d.version !== "number") return null;
546
557
  if (!Array.isArray(d.endplates) || !Array.isArray(d.tracks)) return null;
547
- return d;
558
+ const doc = d;
559
+ const healed = healSelfDivergingTurnouts(doc.turnouts);
560
+ return healed === doc.turnouts ? doc : { ...doc, turnouts: healed };
548
561
  }
549
562
  function emptyEditorState(lengthInches) {
550
563
  return {
@@ -933,24 +946,19 @@ function docToState(doc, fallbackLength, moduleTracks = []) {
933
946
  trackB: x.tracks?.[1] ?? MAIN_TRACK_ID
934
947
  })),
935
948
  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
- }),
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
+ })),
954
962
  controlPoints: readControlPoints(d, sc),
955
963
  industries: (d.industries ?? []).map((ind) => ({
956
964
  id: ind.id,