@willcgage/module-schematic 0.38.0 → 0.40.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
@@ -561,41 +561,38 @@ function emptyEditorState(lengthInches) {
561
561
  sections: [],
562
562
  controlPoints: [],
563
563
  industries: [],
564
- mainPath: []
564
+ mainPath: [],
565
+ main2Path: []
565
566
  };
566
567
  }
567
568
  function isTransitionTurnout(t) {
568
569
  return t.onTrack === MAIN_TRACK_ID && t.divergeTrack === MAIN2_TRACK_ID || t.onTrack === MAIN2_TRACK_ID && t.divergeTrack === MAIN_TRACK_ID;
569
570
  }
570
571
  function main1Track(state) {
571
- const bothDouble = state.configA === "double" && state.configB === "double";
572
- const isDouble = state.configA === "double" || state.configB === "double";
573
572
  const sw = state.turnouts.find(isTransitionTurnout);
574
- const lane = state.mainsSwapped && isDouble ? 1 : 0;
575
- if (!isDouble || bothDouble || !sw || sw.onTrack !== MAIN2_TRACK_ID) {
576
- return { id: MAIN_TRACK_ID, role: "main", lane, from: "A", to: "B" };
573
+ const legacyThroughMain2 = !!sw && sw.onTrack === MAIN2_TRACK_ID && sw.divergeTrack === MAIN_TRACK_ID;
574
+ const isDouble = state.configA === "double" || state.configB === "double";
575
+ const bothDouble = state.configA === "double" && state.configB === "double";
576
+ if (!isDouble || bothDouble || !legacyThroughMain2) {
577
+ return { id: MAIN_TRACK_ID, role: "main", lane: 0, from: "A", to: "B" };
577
578
  }
578
- return state.configA === "double" ? (
579
- // Double at A: Main 1 runs from A and ends at the turnout.
580
- { id: MAIN_TRACK_ID, role: "main", lane, fromPos: 0, toPos: sw.pos }
581
- ) : (
582
- // Double at B: Main 1 begins at the turnout and runs to B.
583
- { id: MAIN_TRACK_ID, role: "main", lane, fromPos: sw.pos, toPos: state.lengthInches }
584
- );
579
+ return state.configA === "double" ? { id: MAIN_TRACK_ID, role: "main", lane: 0, fromPos: 0, toPos: sw.pos } : { id: MAIN_TRACK_ID, role: "main", lane: 0, fromPos: sw.pos, toPos: state.lengthInches };
585
580
  }
586
581
  function main2Track(state) {
587
582
  const bothDouble = state.configA === "double" && state.configB === "double";
588
583
  const sws = state.turnouts.filter((t) => isTransitionTurnout(t) && t.divergeTrack === MAIN2_TRACK_ID).sort((a, b) => a.pos - b.pos);
589
- const lane = state.mainsSwapped ? 0 : 1;
584
+ const lane = state.mainsSwapped ? -1 : 1;
585
+ const authored = state.main2Path.length >= 2 ? { path: state.main2Path } : {};
590
586
  if (bothDouble || !sws.length) {
591
- return { id: MAIN2_TRACK_ID, role: "main", lane, from: "A", to: "B" };
587
+ return { id: MAIN2_TRACK_ID, role: "main", lane, from: "A", to: "B", ...authored };
592
588
  }
593
589
  const track = (fromPos, toPos) => ({
594
590
  id: MAIN2_TRACK_ID,
595
591
  role: "main",
596
592
  lane,
597
593
  fromPos,
598
- toPos
594
+ toPos,
595
+ ...authored
599
596
  });
600
597
  if (state.configA === "double") return track(0, sws[0].pos);
601
598
  if (state.configB === "double") return track(sws[0].pos, state.lengthInches);
@@ -614,12 +611,12 @@ function buildTransition(state) {
614
611
  id: swId,
615
612
  name: "End of Double Track",
616
613
  pos,
617
- // The turnout sits ON the ending main (Main 2, the upper track) and diverges
618
- // down to the continuous Main 1 — the modeller's view of the junction. Left
619
- // hand when the double end is west (Main 2 comes down going east), right
620
- // when it's east (mirror).
621
- onTrack: MAIN2_TRACK_ID,
622
- divergeTrack: MAIN_TRACK_ID,
614
+ // The turnout sits ON the through mainline (Main 1) and diverges TO Main 2,
615
+ // the second main being added to start the double track — the modeller's
616
+ // view of the junction, and the direction an owner authors it (Steve
617
+ // Branton, #131). Main 1 runs the full module; Main 2 is the branch.
618
+ onTrack: MAIN_TRACK_ID,
619
+ divergeTrack: MAIN2_TRACK_ID,
623
620
  kind: aDouble ? "left" : "right"
624
621
  };
625
622
  const cpId = nextId("cp", state.controlPoints.map((c) => c.id));
@@ -796,7 +793,8 @@ function stateToDoc(state, recordNumber) {
796
793
  // that never used them keeps exactly the doc it had before (#96 phase 2).
797
794
  ...state.sections.length ? { sections: moduleSections({ sections: state.sections }) } : {},
798
795
  // Authored mainline path (module-local inches); only when it's a real path.
799
- ...state.mainPath.length >= 2 ? { mainPath: state.mainPath } : {}
796
+ ...state.mainPath.length >= 2 ? { mainPath: state.mainPath } : {},
797
+ ...state.main2Path.length >= 2 ? { main2Path: state.main2Path } : {}
800
798
  };
801
799
  }
802
800
  function docToState(doc, fallbackLength, moduleTracks = []) {
@@ -881,6 +879,8 @@ function docToState(doc, fallbackLength, moduleTracks = []) {
881
879
  ...Number.isFinite(p.bulge) && p.bulge ? { bulge: p.bulge } : {}
882
880
  }));
883
881
  const mainPath = trackPath(d.mainPath) ?? [];
882
+ const main2Track_ = (d.tracks ?? []).find((t) => t.id === MAIN2_TRACK_ID);
883
+ const main2Path = trackPath(d.main2Path ?? main2Track_?.path) ?? [];
884
884
  return {
885
885
  lengthInches: len,
886
886
  loop,
@@ -902,6 +902,7 @@ function docToState(doc, fallbackLength, moduleTracks = []) {
902
902
  sectionBreaks: (d.sectionBreaks ?? []).filter((n) => Number.isFinite(n)).map((n) => sc(n)),
903
903
  sections: moduleSections(d),
904
904
  mainPath,
905
+ main2Path,
905
906
  crossings: (d.crossings ?? []).map((x) => ({
906
907
  id: x.id,
907
908
  name: x.name ?? "",