canvas-recorder-lib 0.8.4 → 0.8.5

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.
@@ -1779,6 +1779,56 @@ function updateSaveSubtitlesNote(startMs: number, endMs: number): void {
1779
1779
  saveSubtitlesNote.style.color = missing ? "#a06000" : "#555";
1780
1780
  }
1781
1781
 
1782
+ /**
1783
+ * The subtitles as a file of their own, for YouTube, which ignores the track inside the MP4.
1784
+ * The times match a video saved from the same start and end: the file starts at its first
1785
+ * frame, as startRecording() does.
1786
+ */
1787
+ getById("saveSubtitlesFileBtn", HTMLButtonElement).addEventListener(
1788
+ "click",
1789
+ async () => {
1790
+ const startMs = (saveStartSecondsInput.valueAsNumber || 0) * 1000;
1791
+ const endMs = (saveEndSecondsInput.valueAsNumber || 0) * 1000;
1792
+ const fileStartMs = (firstFrameAtOrAfter(startMs) * 1000) / FPS;
1793
+ const fileEndMs =
1794
+ fileStartMs + (framesBetween(startMs, endMs) * 1000) / FPS;
1795
+ const text = webVtt(
1796
+ subtitleWords(
1797
+ collectSoundClips(toShow),
1798
+ (source) => readyTranscript(source)?.words,
1799
+ ),
1800
+ fileStartMs,
1801
+ fileEndMs,
1802
+ );
1803
+ if (!text) {
1804
+ saveSubtitlesNote.textContent = "No words to save in that range.";
1805
+ saveSubtitlesNote.style.color = "#a06000";
1806
+ return;
1807
+ }
1808
+ try {
1809
+ const fileHandle = await window.showSaveFilePicker({
1810
+ // The same id as the video, so it opens in the same folder.
1811
+ id: "video-save",
1812
+ suggestedName: "canvas-recording.vtt",
1813
+ types: [
1814
+ { description: "WebVTT subtitles", accept: { "text/vtt": [".vtt"] } },
1815
+ ],
1816
+ });
1817
+ const writable = await fileHandle.createWritable();
1818
+ await writable.write(text);
1819
+ await writable.close();
1820
+ saveSubtitlesNote.textContent = `Saved ${fileHandle.name}.`;
1821
+ saveSubtitlesNote.style.color = "#555";
1822
+ } catch (e) {
1823
+ // Closing the file picker is not an error.
1824
+ if (e instanceof DOMException && e.name === "AbortError") return;
1825
+ console.error("Saving the subtitles failed:", e);
1826
+ saveSubtitlesNote.textContent = `Not saved: ${e instanceof Error ? e.message : String(e)}`;
1827
+ saveSubtitlesNote.style.color = "red";
1828
+ }
1829
+ },
1830
+ );
1831
+
1782
1832
  function setSaveRange(startMs: number, endMs: number): void {
1783
1833
  saveStartSecondsInput.value = (startMs / 1000).toString();
1784
1834
  saveEndSecondsInput.value = (endMs / 1000).toString();
@@ -2164,10 +2214,8 @@ const openHistoryDialogBtn = getById("openHistoryDialogBtn", HTMLButtonElement);
2164
2214
  const historyDialog = getById("historyDialog", HTMLDialogElement);
2165
2215
  const historyList = getById("historyList", HTMLUListElement);
2166
2216
  const historyDeleteBtn = getById("historyDeleteBtn", HTMLButtonElement);
2167
- const historyAbandonBtn = getById("historyAbandonBtn", HTMLButtonElement);
2168
- const historySaveBtn = getById("historySaveBtn", HTMLButtonElement);
2169
- const historyOkBtn = getById("historyOkBtn", HTMLButtonElement);
2170
- const historyCancelBtn = getById("historyCancelBtn", HTMLButtonElement);
2217
+ const historyDoneBtn = getById("historyDoneBtn", HTMLButtonElement);
2218
+ const historyRevertBtn = getById("historyRevertBtn", HTMLButtonElement);
2171
2219
  const historyDialogHint = getById("historyDialogHint", HTMLElement);
2172
2220
 
2173
2221
  // MARK: Rect marker drag state
@@ -3792,8 +3840,16 @@ function drawFilesButton(): void {
3792
3840
  const summaries = syncs.map(([, sync]) => sync.summary);
3793
3841
  const action = summaries.includes("action");
3794
3842
  const pending = summaries.includes("pending");
3795
- filesBtn.textContent = `💾 Files${action ? " ⚠" : pending ? " *" : ""}`;
3796
- filesBtn.style.background = action ? "#e53935" : pending ? "#ffb74d" : "";
3843
+ filesBtn.textContent = `💾 Files${action ? " ⚠" : ""}`;
3844
+ if (pending && !action) {
3845
+ // Only the asterisk is orange: an orange button was too distracting.
3846
+ const star = document.createElement("span");
3847
+ star.textContent = " *";
3848
+ star.style.color = "#ef6c00";
3849
+ star.style.fontWeight = "bold";
3850
+ filesBtn.append(star);
3851
+ }
3852
+ filesBtn.style.background = action ? "#e53935" : "";
3797
3853
  filesBtn.style.color = action ? "white" : "";
3798
3854
  filesBtn.title = [
3799
3855
  action
@@ -4684,17 +4740,48 @@ let _dialogFileRecords: FileRecord[] = [];
4684
4740
  /** Async load state for each filename; populated after the dialog opens. */
4685
4741
  const _dialogFileStates = new Map<string, FileEntryState>();
4686
4742
 
4687
- function _dialogSelectItem(index: number, target: Showable) {
4688
- if (index < 0 || index >= _dialogItems.length) return;
4689
- _selectedDialogIndex = index;
4743
+ /** What the dialog had selected when it opened, as {@link _dialogItemKey} says it. */
4744
+ let _initialItemKey: string | undefined;
4745
+
4746
+ /** Names an entry, so it can be found again after the list is rebuilt. */
4747
+ function _dialogItemKey(item: DialogListItem | undefined): string | undefined {
4748
+ switch (item?.kind) {
4749
+ case undefined:
4750
+ return undefined;
4751
+ case "ts-defaults":
4752
+ return "ts-defaults";
4753
+ case "db-entry":
4754
+ return `db ${item.entry.timestamp}`;
4755
+ case "file-entry":
4756
+ return `file ${item.fileRecord.filename}`;
4757
+ }
4758
+ }
4690
4759
 
4760
+ /** True if the selected entry isn't the one loaded when the dialog opened. */
4761
+ function _dialogChanged(): boolean {
4762
+ return (
4763
+ _selectedDialogIndex >= 0 &&
4764
+ _dialogItemKey(_dialogItems[_selectedDialogIndex]) !== _initialItemKey
4765
+ );
4766
+ }
4767
+
4768
+ /** Mark an entry selected in the list, without loading it. -1 for none. */
4769
+ function _highlightDialogItem(index: number): void {
4770
+ _selectedDialogIndex = index;
4691
4771
  for (let i = 0; i < historyList.children.length; i++) {
4692
4772
  historyList.children[i].classList.toggle("selected", i === index);
4693
4773
  }
4694
-
4695
4774
  const item = _dialogItems[index];
4696
4775
  // Top item (index 0) and ts-defaults are never deletable.
4697
- historyDeleteBtn.disabled = index === 0 || item.kind === "ts-defaults";
4776
+ historyDeleteBtn.disabled =
4777
+ !item || index === 0 || item.kind === "ts-defaults";
4778
+ historyRevertBtn.disabled = !_dialogChanged();
4779
+ }
4780
+
4781
+ function _dialogSelectItem(index: number, target: Showable) {
4782
+ if (index < 0 || index >= _dialogItems.length) return;
4783
+ _highlightDialogItem(index);
4784
+ const item = _dialogItems[index];
4698
4785
 
4699
4786
  // Preview the selected state
4700
4787
  if (item.kind === "ts-defaults") {
@@ -4710,11 +4797,6 @@ function _dialogSelectItem(index: number, target: Showable) {
4710
4797
  updateComponentEditor(target);
4711
4798
  updateScheduleEditor(target);
4712
4799
  afterStateApplied();
4713
-
4714
- // The Abandon/Save buttons are no longer used; only Keep is shown.
4715
- historyOkBtn.hidden = false;
4716
- historyAbandonBtn.hidden = true;
4717
- historySaveBtn.hidden = true;
4718
4800
  }
4719
4801
 
4720
4802
  /**
@@ -4913,7 +4995,10 @@ async function openHistoryDialog(target: Showable) {
4913
4995
  }
4914
4996
  }
4915
4997
 
4998
+ _initialItemKey = undefined;
4916
4999
  _rebuildDialogList(target);
5000
+ _initialItemKey = _dialogItemKey(_dialogItems[_selectedDialogIndex]);
5001
+ historyRevertBtn.disabled = true;
4917
5002
  historyDialog.showModal();
4918
5003
  historyList.focus();
4919
5004
 
@@ -4955,35 +5040,32 @@ async function _applyDialogSelection() {
4955
5040
  afterStateApplied();
4956
5041
  }
4957
5042
 
4958
- historyOkBtn.addEventListener("click", () => {
4959
- void _applyDialogSelection();
4960
- historyDialog.close();
5043
+ // Done and Escape both close, and closing keeps the entry chosen. Every choice is already
5044
+ // on the canvas; this saves it, so a reload brings it back. Nothing changed, nothing saved.
5045
+ historyDoneBtn.addEventListener("click", () => historyDialog.close());
5046
+ historyDialog.addEventListener("close", () => {
5047
+ if (_dialogChanged()) void _applyDialogSelection();
4961
5048
  });
4962
- historyAbandonBtn.addEventListener("click", () => {
4963
- // No longer shown; kept for safety.
4964
- historyDialog.close();
4965
- });
4966
- historySaveBtn.addEventListener("click", () => {
4967
- // No longer shown; kept for safety.
4968
- historyDialog.close();
4969
- });
4970
- historyCancelBtn.addEventListener("click", () => {
4971
- // Revert to the state before the dialog opened. The snapshot covers the whole video, so
5049
+ historyRevertBtn.addEventListener("click", () => {
5050
+ // Put back the state from before the dialog opened. The snapshot covers the whole video, so
4972
5051
  // restore the whole video -- a preview may have been scoped to one chapter, but undoing it
4973
5052
  // against `target` alone would apply the root's state to that chapter.
4974
5053
  const target = _historyTarget;
4975
- if (target) {
4976
- applyTree(
4977
- toShow,
4978
- loadableTree(JSON.parse(_preDialogSnapshotJson) as SerializedFixedChild),
4979
- );
4980
- selectedSlideChild = null;
4981
- activeRootComponentEditor?.resetAll();
4982
- updateComponentEditor(target);
4983
- updateScheduleEditor(target);
4984
- afterStateApplied();
4985
- }
4986
- historyDialog.close();
5054
+ if (!target) return;
5055
+ applyTree(
5056
+ toShow,
5057
+ loadableTree(JSON.parse(_preDialogSnapshotJson) as SerializedFixedChild),
5058
+ );
5059
+ selectedSlideChild = null;
5060
+ activeRootComponentEditor?.resetAll();
5061
+ updateComponentEditor(target);
5062
+ updateScheduleEditor(target);
5063
+ afterStateApplied();
5064
+ // Select the entry it came from, without loading it again: the snapshot is exact.
5065
+ _highlightDialogItem(
5066
+ _dialogItems.findIndex((item) => _dialogItemKey(item) === _initialItemKey),
5067
+ );
5068
+ historyList.focus();
4987
5069
  });
4988
5070
 
4989
5071
  historyDeleteBtn.addEventListener("click", async () => {
@@ -317,14 +317,21 @@ export const playerBodyHtml = `
317
317
  🗑 Delete
318
318
  </button>
319
319
  <div style="flex: 1"></div>
320
- <button id="historyAbandonBtn" type="button" hidden>
321
- Switch &amp; Abandon
320
+ <button
321
+ id="historyRevertBtn"
322
+ type="button"
323
+ disabled
324
+ title="Put back what was loaded when this opened. The dialog stays open."
325
+ >
326
+ Revert
322
327
  </button>
323
- <button id="historySaveBtn" type="button" hidden>
324
- Switch &amp; Save
328
+ <button
329
+ id="historyDoneBtn"
330
+ type="button"
331
+ title="Close, keeping the entry you chose. Escape does the same."
332
+ >
333
+ Done
325
334
  </button>
326
- <button id="historyOkBtn" type="button">Keep</button>
327
- <button id="historyCancelBtn" type="button">Revert</button>
328
335
  </div>
329
336
  </dialog>
330
337
 
@@ -497,6 +504,13 @@ export const playerBodyHtml = `
497
504
  title="Put the transcripts in the file as a subtitle track, one word at a time as each is spoken, with the line before above it. Fix the words first with ✎ on a sound clip."
498
505
  ><input type="checkbox" id="saveSubtitles" checked /> Subtitles</label
499
506
  >
507
+ <button
508
+ type="button"
509
+ id="saveSubtitlesFileBtn"
510
+ title="Save the same subtitles as a separate .vtt file, for the same start and end as the video. YouTube ignores the track inside the MP4. Upload this in YouTube Studio: Subtitles → Add language → Add → Upload file → With timing."
511
+ >
512
+ Save .vtt…
513
+ </button>
500
514
  <span
501
515
  id="saveSubtitlesNote"
502
516
  style="font-size: 0.85em; color: #555; margin-left: 0.5em"
@@ -125,6 +125,10 @@ function formatTime(ms: number): string {
125
125
  return `${minutes}:${seconds.toFixed(1).padStart(4, "0")}`;
126
126
  }
127
127
 
128
+ /** Marks a word that runs from one row onto the next. */
129
+ const SPLIT_ARROW = "→";
130
+ const SPLIT_COLOR = "#ffd400";
131
+
128
132
  /** `text`, cut short with "…" to fit in `width` px, or "" if not even that fits. */
129
133
  function fitText(
130
134
  context: CanvasRenderingContext2D,
@@ -132,8 +136,10 @@ function fitText(
132
136
  width: number,
133
137
  ): string {
134
138
  if (context.measureText(text).width <= width) return text;
135
- for (let length = text.length - 1; length > 0; length--) {
136
- const shorter = text.slice(0, length) + "…";
139
+ // By code point, so a letter like 𝙿 is never cut in half.
140
+ const letters = Array.from(text);
141
+ for (let length = letters.length - 1; length > 0; length--) {
142
+ const shorter = letters.slice(0, length).join("") + "…";
137
143
  if (context.measureText(shorter).width <= width) return shorter;
138
144
  }
139
145
  return "";
@@ -549,14 +555,26 @@ export function openTranscriptEditor(hooks: TranscriptEditorHooks): void {
549
555
  ? EDITED_COLOR
550
556
  : PLAIN_COLOR;
551
557
  ctx.fillRect(x0, top + 1, Math.max(2, x1 - x0), LANE_HEIGHT - 2);
558
+ // A word cut by the row's edge gets a yellow arrow there, at both
559
+ // ends of the cut, so it doesn't look like the same word said twice.
560
+ const middle = top + LANE_HEIGHT / 2;
561
+ const arrowWidth = ctx.measureText(SPLIT_ARROW).width;
562
+ const continues = word.endMs > toMs;
563
+ const continued = word.startMs < fromMs;
564
+ ctx.fillStyle = SPLIT_COLOR;
565
+ if (continues) ctx.fillText(SPLIT_ARROW, width - 3 - arrowWidth, middle);
566
+ if (continued) ctx.fillText(SPLIT_ARROW, 3, middle);
552
567
  // The label can run past its word, up to the next word in the same lane.
553
568
  const nextInLane = words[i + 2];
554
- const limit = Math.min(width, nextInLane ? x(nextInLane.startMs) : width);
555
- const textX = Math.max(x0, 0) + 3;
569
+ const limit = Math.min(
570
+ continues ? width - 6 - arrowWidth : width,
571
+ nextInLane ? x(nextInLane.startMs) : width,
572
+ );
573
+ const textX = Math.max(x0, 0) + 3 + (continued ? arrowWidth + 3 : 0);
556
574
  const text = fitText(ctx, labelOf(word), limit - 3 - textX);
557
575
  if (text) {
558
576
  ctx.fillStyle = "#fff";
559
- ctx.fillText(text, textX, top + LANE_HEIGHT / 2);
577
+ ctx.fillText(text, textX, middle);
560
578
  }
561
579
  }
562
580
 
@@ -1468,6 +1468,46 @@ function updateSaveSubtitlesNote(startMs, endMs) {
1468
1468
  : words;
1469
1469
  saveSubtitlesNote.style.color = missing ? "#a06000" : "#555";
1470
1470
  }
1471
+ /**
1472
+ * The subtitles as a file of their own, for YouTube, which ignores the track inside the MP4.
1473
+ * The times match a video saved from the same start and end: the file starts at its first
1474
+ * frame, as startRecording() does.
1475
+ */
1476
+ getById("saveSubtitlesFileBtn", HTMLButtonElement).addEventListener("click", async () => {
1477
+ const startMs = (saveStartSecondsInput.valueAsNumber || 0) * 1000;
1478
+ const endMs = (saveEndSecondsInput.valueAsNumber || 0) * 1000;
1479
+ const fileStartMs = (firstFrameAtOrAfter(startMs) * 1000) / FPS;
1480
+ const fileEndMs = fileStartMs + (framesBetween(startMs, endMs) * 1000) / FPS;
1481
+ const text = webVtt(subtitleWords(collectSoundClips(toShow), (source) => readyTranscript(source)?.words), fileStartMs, fileEndMs);
1482
+ if (!text) {
1483
+ saveSubtitlesNote.textContent = "No words to save in that range.";
1484
+ saveSubtitlesNote.style.color = "#a06000";
1485
+ return;
1486
+ }
1487
+ try {
1488
+ const fileHandle = await window.showSaveFilePicker({
1489
+ // The same id as the video, so it opens in the same folder.
1490
+ id: "video-save",
1491
+ suggestedName: "canvas-recording.vtt",
1492
+ types: [
1493
+ { description: "WebVTT subtitles", accept: { "text/vtt": [".vtt"] } },
1494
+ ],
1495
+ });
1496
+ const writable = await fileHandle.createWritable();
1497
+ await writable.write(text);
1498
+ await writable.close();
1499
+ saveSubtitlesNote.textContent = `Saved ${fileHandle.name}.`;
1500
+ saveSubtitlesNote.style.color = "#555";
1501
+ }
1502
+ catch (e) {
1503
+ // Closing the file picker is not an error.
1504
+ if (e instanceof DOMException && e.name === "AbortError")
1505
+ return;
1506
+ console.error("Saving the subtitles failed:", e);
1507
+ saveSubtitlesNote.textContent = `Not saved: ${e instanceof Error ? e.message : String(e)}`;
1508
+ saveSubtitlesNote.style.color = "red";
1509
+ }
1510
+ });
1471
1511
  function setSaveRange(startMs, endMs) {
1472
1512
  saveStartSecondsInput.value = (startMs / 1000).toString();
1473
1513
  saveEndSecondsInput.value = (endMs / 1000).toString();
@@ -1777,10 +1817,8 @@ const openHistoryDialogBtn = getById("openHistoryDialogBtn", HTMLButtonElement);
1777
1817
  const historyDialog = getById("historyDialog", HTMLDialogElement);
1778
1818
  const historyList = getById("historyList", HTMLUListElement);
1779
1819
  const historyDeleteBtn = getById("historyDeleteBtn", HTMLButtonElement);
1780
- const historyAbandonBtn = getById("historyAbandonBtn", HTMLButtonElement);
1781
- const historySaveBtn = getById("historySaveBtn", HTMLButtonElement);
1782
- const historyOkBtn = getById("historyOkBtn", HTMLButtonElement);
1783
- const historyCancelBtn = getById("historyCancelBtn", HTMLButtonElement);
1820
+ const historyDoneBtn = getById("historyDoneBtn", HTMLButtonElement);
1821
+ const historyRevertBtn = getById("historyRevertBtn", HTMLButtonElement);
1784
1822
  const historyDialogHint = getById("historyDialogHint", HTMLElement);
1785
1823
  /** The single rect keyframe currently in "edit" mode (drag handles shown on canvas). At most one at a time. */
1786
1824
  let editingRectKf = null;
@@ -3134,8 +3172,16 @@ function drawFilesButton() {
3134
3172
  const summaries = syncs.map(([, sync]) => sync.summary);
3135
3173
  const action = summaries.includes("action");
3136
3174
  const pending = summaries.includes("pending");
3137
- filesBtn.textContent = `💾 Files${action ? " ⚠" : pending ? " *" : ""}`;
3138
- filesBtn.style.background = action ? "#e53935" : pending ? "#ffb74d" : "";
3175
+ filesBtn.textContent = `💾 Files${action ? " ⚠" : ""}`;
3176
+ if (pending && !action) {
3177
+ // Only the asterisk is orange: an orange button was too distracting.
3178
+ const star = document.createElement("span");
3179
+ star.textContent = " *";
3180
+ star.style.color = "#ef6c00";
3181
+ star.style.fontWeight = "bold";
3182
+ filesBtn.append(star);
3183
+ }
3184
+ filesBtn.style.background = action ? "#e53935" : "";
3139
3185
  filesBtn.style.color = action ? "white" : "";
3140
3186
  filesBtn.title = [
3141
3187
  action
@@ -3841,16 +3887,43 @@ let _allHistoryEntries = [];
3841
3887
  let _dialogFileRecords = [];
3842
3888
  /** Async load state for each filename; populated after the dialog opens. */
3843
3889
  const _dialogFileStates = new Map();
3844
- function _dialogSelectItem(index, target) {
3845
- if (index < 0 || index >= _dialogItems.length)
3846
- return;
3890
+ /** What the dialog had selected when it opened, as {@link _dialogItemKey} says it. */
3891
+ let _initialItemKey;
3892
+ /** Names an entry, so it can be found again after the list is rebuilt. */
3893
+ function _dialogItemKey(item) {
3894
+ switch (item?.kind) {
3895
+ case undefined:
3896
+ return undefined;
3897
+ case "ts-defaults":
3898
+ return "ts-defaults";
3899
+ case "db-entry":
3900
+ return `db ${item.entry.timestamp}`;
3901
+ case "file-entry":
3902
+ return `file ${item.fileRecord.filename}`;
3903
+ }
3904
+ }
3905
+ /** True if the selected entry isn't the one loaded when the dialog opened. */
3906
+ function _dialogChanged() {
3907
+ return (_selectedDialogIndex >= 0 &&
3908
+ _dialogItemKey(_dialogItems[_selectedDialogIndex]) !== _initialItemKey);
3909
+ }
3910
+ /** Mark an entry selected in the list, without loading it. -1 for none. */
3911
+ function _highlightDialogItem(index) {
3847
3912
  _selectedDialogIndex = index;
3848
3913
  for (let i = 0; i < historyList.children.length; i++) {
3849
3914
  historyList.children[i].classList.toggle("selected", i === index);
3850
3915
  }
3851
3916
  const item = _dialogItems[index];
3852
3917
  // Top item (index 0) and ts-defaults are never deletable.
3853
- historyDeleteBtn.disabled = index === 0 || item.kind === "ts-defaults";
3918
+ historyDeleteBtn.disabled =
3919
+ !item || index === 0 || item.kind === "ts-defaults";
3920
+ historyRevertBtn.disabled = !_dialogChanged();
3921
+ }
3922
+ function _dialogSelectItem(index, target) {
3923
+ if (index < 0 || index >= _dialogItems.length)
3924
+ return;
3925
+ _highlightDialogItem(index);
3926
+ const item = _dialogItems[index];
3854
3927
  // Preview the selected state
3855
3928
  if (item.kind === "ts-defaults") {
3856
3929
  applyTsDefaults(target);
@@ -3867,10 +3940,6 @@ function _dialogSelectItem(index, target) {
3867
3940
  updateComponentEditor(target);
3868
3941
  updateScheduleEditor(target);
3869
3942
  afterStateApplied();
3870
- // The Abandon/Save buttons are no longer used; only Keep is shown.
3871
- historyOkBtn.hidden = false;
3872
- historyAbandonBtn.hidden = true;
3873
- historySaveBtn.hidden = true;
3874
3943
  }
3875
3944
  /**
3876
3945
  * Rebuilds _dialogItems and the list DOM by merging DB entries, file records,
@@ -4056,7 +4125,10 @@ async function openHistoryDialog(target) {
4056
4125
  _dialogFileStates.set(fr.filename, { status: "loading" });
4057
4126
  }
4058
4127
  }
4128
+ _initialItemKey = undefined;
4059
4129
  _rebuildDialogList(target);
4130
+ _initialItemKey = _dialogItemKey(_dialogItems[_selectedDialogIndex]);
4131
+ historyRevertBtn.disabled = true;
4060
4132
  historyDialog.showModal();
4061
4133
  historyList.focus();
4062
4134
  // Populate file entry contents asynchronously (grayed out until ready).
@@ -4096,32 +4168,29 @@ async function _applyDialogSelection() {
4096
4168
  updateScheduleEditor(target);
4097
4169
  afterStateApplied();
4098
4170
  }
4099
- historyOkBtn.addEventListener("click", () => {
4100
- void _applyDialogSelection();
4101
- historyDialog.close();
4102
- });
4103
- historyAbandonBtn.addEventListener("click", () => {
4104
- // No longer shown; kept for safety.
4105
- historyDialog.close();
4106
- });
4107
- historySaveBtn.addEventListener("click", () => {
4108
- // No longer shown; kept for safety.
4109
- historyDialog.close();
4171
+ // Done and Escape both close, and closing keeps the entry chosen. Every choice is already
4172
+ // on the canvas; this saves it, so a reload brings it back. Nothing changed, nothing saved.
4173
+ historyDoneBtn.addEventListener("click", () => historyDialog.close());
4174
+ historyDialog.addEventListener("close", () => {
4175
+ if (_dialogChanged())
4176
+ void _applyDialogSelection();
4110
4177
  });
4111
- historyCancelBtn.addEventListener("click", () => {
4112
- // Revert to the state before the dialog opened. The snapshot covers the whole video, so
4178
+ historyRevertBtn.addEventListener("click", () => {
4179
+ // Put back the state from before the dialog opened. The snapshot covers the whole video, so
4113
4180
  // restore the whole video -- a preview may have been scoped to one chapter, but undoing it
4114
4181
  // against `target` alone would apply the root's state to that chapter.
4115
4182
  const target = _historyTarget;
4116
- if (target) {
4117
- applyTree(toShow, loadableTree(JSON.parse(_preDialogSnapshotJson)));
4118
- selectedSlideChild = null;
4119
- activeRootComponentEditor?.resetAll();
4120
- updateComponentEditor(target);
4121
- updateScheduleEditor(target);
4122
- afterStateApplied();
4123
- }
4124
- historyDialog.close();
4183
+ if (!target)
4184
+ return;
4185
+ applyTree(toShow, loadableTree(JSON.parse(_preDialogSnapshotJson)));
4186
+ selectedSlideChild = null;
4187
+ activeRootComponentEditor?.resetAll();
4188
+ updateComponentEditor(target);
4189
+ updateScheduleEditor(target);
4190
+ afterStateApplied();
4191
+ // Select the entry it came from, without loading it again: the snapshot is exact.
4192
+ _highlightDialogItem(_dialogItems.findIndex((item) => _dialogItemKey(item) === _initialItemKey));
4193
+ historyList.focus();
4125
4194
  });
4126
4195
  historyDeleteBtn.addEventListener("click", async () => {
4127
4196
  const target = _historyTarget;