canvas-recorder-lib 0.7.3 → 0.7.4

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.
@@ -149,7 +149,7 @@ import {
149
149
  splitSoundClip,
150
150
  splitVideoClip,
151
151
  } from "../src/slide-components/split-video-clip.ts";
152
- import { TranscriptView } from "./transcript-view.ts";
152
+ import { TranscriptView, type TranscriptViewClip } from "./transcript-view.ts";
153
153
  import { TraditionalTextComponent } from "../src/slide-components/traditional-text.ts";
154
154
  import { PaddingComponent } from "../src/slide-components/in-parallel.ts";
155
155
 
@@ -522,13 +522,10 @@ function checkVideosLocalFiles() {
522
522
  * input (pointermove, etc.).
523
523
  */
524
524
  function showFrame(timeInMs: number, live: boolean) {
525
- // While playing, the timeline keeps the play head on screen.
526
- timelineDisplay.setPlayMs(
527
- timeInMs - rootStartMs,
528
- live && playCheckBox.checked,
529
- );
530
- if (live)
531
- transcriptView?.update(timeInMs - rootStartMs, playCheckBox.checked);
525
+ // While playing or saving, the timeline and 📖 Text keep the play head on screen.
526
+ const following = !live || playCheckBox.checked;
527
+ timelineDisplay.setPlayMs(timeInMs - rootStartMs, following);
528
+ transcriptView?.update(timeInMs - rootStartMs, following);
532
529
  for (const update of goToButtonUpdaters) update();
533
530
  editorPanelsTimeChanged();
534
531
  context.reset();
@@ -1411,6 +1408,7 @@ async function startRecording(
1411
1408
 
1412
1409
  await output.start();
1413
1410
  infoDiv.innerHTML = "Recording in progress...";
1411
+ showTranscriptViewForSave();
1414
1412
 
1415
1413
  const frameDuration = 1000 / FPS;
1416
1414
 
@@ -1534,6 +1532,7 @@ async function startRecording(
1534
1532
  applyZoom(currentZoomFactor);
1535
1533
  }
1536
1534
  suspendLiveAnimationLoop = false;
1535
+ endTranscriptViewForSave();
1537
1536
  }
1538
1537
  }
1539
1538
 
@@ -2552,20 +2551,7 @@ onTranscriptsChange(() => {
2552
2551
  // The transcript as text. It rebuilds itself when the clips or transcripts
2553
2552
  // change, so it needs no calls besides the one in showFrame().
2554
2553
  transcriptView = new TranscriptView({
2555
- clips() {
2556
- const root = currentRoot();
2557
- if (!root) return [];
2558
- return soundOwners(root)
2559
- .flatMap(({ owner, startMs }) =>
2560
- (owner.soundClips ?? []).map((clip) => ({
2561
- clip,
2562
- owner,
2563
- startMs: startMs + clip.startMsIntoScene,
2564
- lengthMs: soundClipLengthMs(clip),
2565
- })),
2566
- )
2567
- .sort((a, b) => a.startMs - b.startMs);
2568
- },
2554
+ clips: transcriptViewClips,
2569
2555
  transcript(source) {
2570
2556
  const status = transcriptStatus(source);
2571
2557
  if (!status) return undefined;
@@ -2596,31 +2582,86 @@ transcriptView = new TranscriptView({
2596
2582
  },
2597
2583
  });
2598
2584
  getById("timelineCanvas", HTMLCanvasElement).after(transcriptView.element);
2599
- {
2600
- const READ_KEY = "transcriptView.on";
2601
- const checkbox = getById("transcriptViewCheckbox", HTMLInputElement);
2602
- const timelineCanvas = getById("timelineCanvas", HTMLCanvasElement);
2603
- const apply = () => {
2604
- transcriptView!.hidden = !checkbox.checked;
2605
- // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2606
- timelineCanvas.style.display = checkbox.checked ? "none" : "";
2607
- if (!checkbox.checked) timelineDisplay.redraw();
2608
- };
2585
+
2586
+ /** The current chapter's sound clips, in order of time, for 📖 Text. */
2587
+ function transcriptViewClips(): TranscriptViewClip[] {
2588
+ const root = currentRoot();
2589
+ if (!root) return [];
2590
+ return soundOwners(root)
2591
+ .flatMap(({ owner, startMs }) =>
2592
+ (owner.soundClips ?? []).map((clip) => ({
2593
+ clip,
2594
+ owner,
2595
+ startMs: startMs + clip.startMsIntoScene,
2596
+ lengthMs: soundClipLengthMs(clip),
2597
+ })),
2598
+ )
2599
+ .sort((a, b) => a.startMs - b.startMs);
2600
+ }
2601
+
2602
+ // MARK: 📖 Text checkbox
2603
+
2604
+ const transcriptViewCheckbox = getById(
2605
+ "transcriptViewCheckbox",
2606
+ HTMLInputElement,
2607
+ );
2608
+ const TRANSCRIPT_VIEW_KEY = "transcriptView.on";
2609
+
2610
+ /** Show 📖 Text or the timeline, to match the checkbox. */
2611
+ function applyTranscriptViewCheckbox() {
2612
+ const on = transcriptViewCheckbox.checked;
2613
+ transcriptView!.hidden = !on;
2614
+ // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2615
+ getById("timelineCanvas", HTMLCanvasElement).style.display = on ? "none" : "";
2616
+ if (!on) timelineDisplay.redraw();
2617
+ }
2618
+
2619
+ /**
2620
+ * True while a save has switched to 📖 Text, and the user hasn't touched the checkbox since.
2621
+ * Then the save switches back when it's done.
2622
+ */
2623
+ let transcriptViewJustForSave = false;
2624
+
2625
+ /**
2626
+ * A save shows 📖 Text by default, so you can read along while it records.
2627
+ * Only when there's something to read: transcripts are on, and the chapter has sound.
2628
+ */
2629
+ function showTranscriptViewForSave() {
2630
+ if (transcriptViewCheckbox.checked) return;
2631
+ if (!transcriptsEnabled() || transcriptViewClips().length === 0) return;
2632
+ transcriptViewCheckbox.checked = true;
2633
+ transcriptViewJustForSave = true;
2634
+ applyTranscriptViewCheckbox();
2635
+ }
2636
+
2637
+ /** The save is over. Go back to the timeline, unless the user chose since. */
2638
+ function endTranscriptViewForSave() {
2639
+ if (!transcriptViewJustForSave) return;
2640
+ transcriptViewJustForSave = false;
2641
+ transcriptViewCheckbox.checked = false;
2642
+ applyTranscriptViewCheckbox();
2643
+ }
2644
+
2645
+ try {
2646
+ transcriptViewCheckbox.checked =
2647
+ localStorage.getItem(TRANSCRIPT_VIEW_KEY) === "true";
2648
+ } catch {
2649
+ // Remembering is only a convenience.
2650
+ }
2651
+ transcriptViewCheckbox.addEventListener("input", () => {
2652
+ // The user's choice, so a save won't change it back, and it's remembered.
2653
+ transcriptViewJustForSave = false;
2609
2654
  try {
2610
- checkbox.checked = localStorage.getItem(READ_KEY) === "true";
2655
+ localStorage.setItem(
2656
+ TRANSCRIPT_VIEW_KEY,
2657
+ String(transcriptViewCheckbox.checked),
2658
+ );
2611
2659
  } catch {
2612
- // Remembering is only a convenience.
2660
+ // See above.
2613
2661
  }
2614
- checkbox.addEventListener("input", () => {
2615
- try {
2616
- localStorage.setItem(READ_KEY, String(checkbox.checked));
2617
- } catch {
2618
- // See above.
2619
- }
2620
- apply();
2621
- });
2622
- apply();
2623
- }
2662
+ applyTranscriptViewCheckbox();
2663
+ });
2664
+ applyTranscriptViewCheckbox();
2624
2665
 
2625
2666
  timelineDisplay.onSeek = (localMs) => {
2626
2667
  stopAudio();
@@ -4140,6 +4181,8 @@ async function initFromDB(unloadBackup?: string | null): Promise<void> {
4140
4181
  }
4141
4182
  _loadSource = source;
4142
4183
  _baselineTreeJson = currentTreeJson();
4184
+ // The unload backup is in the database now, or older than it. See the end of "Load previous state".
4185
+ sessionStorage.removeItem("pendingScheduleSave");
4143
4186
 
4144
4187
  canvas.style.visibility = "";
4145
4188
  canvasLoading.style.display = "none";
@@ -4257,6 +4300,11 @@ function currentSaveTarget(): Showable | null {
4257
4300
  }
4258
4301
 
4259
4302
  function saveOnUnload() {
4303
+ // Still starting up: the live tree is the TypeScript defaults, not the user's work.
4304
+ // A backup of it would be newer than the real state, so the next startup would load it,
4305
+ // and everything made in the Visual Editor would disappear. The last session's backup,
4306
+ // if there was one, is still in sessionStorage until initFromDB() has saved it.
4307
+ if (_loadSource === undefined) return;
4260
4308
  // When the history dialog is open the live tree may hold a transient preview from
4261
4309
  // _dialogSelectItem. Back up the pre-dialog state instead, so a preview is never
4262
4310
  // persisted as if the user had accepted it.
@@ -7883,6 +7931,15 @@ function applyMarkerDrag(localX: number, localY: number, shiftKey = false) {
7883
7931
  if (fL) getById("left-col", HTMLDivElement).style.flex = fL;
7884
7932
  } finally {
7885
7933
  sessionStorage.clear();
7934
+ // Keep the unload backup until initFromDB() has saved it. If the page reloads before
7935
+ // then, saveOnUnload() saves nothing, so this is the only copy.
7936
+ if (unloadBackup !== null) {
7937
+ try {
7938
+ sessionStorage.setItem("pendingScheduleSave", unloadBackup);
7939
+ } catch {
7940
+ // It fit a moment ago.
7941
+ }
7942
+ }
7886
7943
  }
7887
7944
  }
7888
7945
 
@@ -374,10 +374,10 @@ function checkVideosLocalFiles() {
374
374
  * input (pointermove, etc.).
375
375
  */
376
376
  function showFrame(timeInMs, live) {
377
- // While playing, the timeline keeps the play head on screen.
378
- timelineDisplay.setPlayMs(timeInMs - rootStartMs, live && playCheckBox.checked);
379
- if (live)
380
- transcriptView?.update(timeInMs - rootStartMs, playCheckBox.checked);
377
+ // While playing or saving, the timeline and 📖 Text keep the play head on screen.
378
+ const following = !live || playCheckBox.checked;
379
+ timelineDisplay.setPlayMs(timeInMs - rootStartMs, following);
380
+ transcriptView?.update(timeInMs - rootStartMs, following);
381
381
  for (const update of goToButtonUpdaters)
382
382
  update();
383
383
  editorPanelsTimeChanged();
@@ -1172,6 +1172,7 @@ async function startRecording(saveStartMs = 0, saveEndMs = toShow.duration, pixe
1172
1172
  const startTime = performance.now();
1173
1173
  await output.start();
1174
1174
  infoDiv.innerHTML = "Recording in progress...";
1175
+ showTranscriptViewForSave();
1175
1176
  const frameDuration = 1000 / FPS;
1176
1177
  // Offline loop: draw + push frames (not limited to realtime)
1177
1178
  let frameNumber = 0;
@@ -1284,6 +1285,7 @@ async function startRecording(saveStartMs = 0, saveEndMs = toShow.duration, pixe
1284
1285
  applyZoom(currentZoomFactor);
1285
1286
  }
1286
1287
  suspendLiveAnimationLoop = false;
1288
+ endTranscriptViewForSave();
1287
1289
  }
1288
1290
  }
1289
1291
  cancelRecordingButton.addEventListener("click", () => {
@@ -2117,19 +2119,7 @@ onTranscriptsChange(() => {
2117
2119
  // The transcript as text. It rebuilds itself when the clips or transcripts
2118
2120
  // change, so it needs no calls besides the one in showFrame().
2119
2121
  transcriptView = new TranscriptView({
2120
- clips() {
2121
- const root = currentRoot();
2122
- if (!root)
2123
- return [];
2124
- return soundOwners(root)
2125
- .flatMap(({ owner, startMs }) => (owner.soundClips ?? []).map((clip) => ({
2126
- clip,
2127
- owner,
2128
- startMs: startMs + clip.startMsIntoScene,
2129
- lengthMs: soundClipLengthMs(clip),
2130
- })))
2131
- .sort((a, b) => a.startMs - b.startMs);
2132
- },
2122
+ clips: transcriptViewClips,
2133
2123
  transcript(source) {
2134
2124
  const status = transcriptStatus(source);
2135
2125
  if (!status)
@@ -2163,34 +2153,77 @@ transcriptView = new TranscriptView({
2163
2153
  },
2164
2154
  });
2165
2155
  getById("timelineCanvas", HTMLCanvasElement).after(transcriptView.element);
2166
- {
2167
- const READ_KEY = "transcriptView.on";
2168
- const checkbox = getById("transcriptViewCheckbox", HTMLInputElement);
2169
- const timelineCanvas = getById("timelineCanvas", HTMLCanvasElement);
2170
- const apply = () => {
2171
- transcriptView.hidden = !checkbox.checked;
2172
- // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2173
- timelineCanvas.style.display = checkbox.checked ? "none" : "";
2174
- if (!checkbox.checked)
2175
- timelineDisplay.redraw();
2176
- };
2156
+ /** The current chapter's sound clips, in order of time, for 📖 Text. */
2157
+ function transcriptViewClips() {
2158
+ const root = currentRoot();
2159
+ if (!root)
2160
+ return [];
2161
+ return soundOwners(root)
2162
+ .flatMap(({ owner, startMs }) => (owner.soundClips ?? []).map((clip) => ({
2163
+ clip,
2164
+ owner,
2165
+ startMs: startMs + clip.startMsIntoScene,
2166
+ lengthMs: soundClipLengthMs(clip),
2167
+ })))
2168
+ .sort((a, b) => a.startMs - b.startMs);
2169
+ }
2170
+ // MARK: 📖 Text checkbox
2171
+ const transcriptViewCheckbox = getById("transcriptViewCheckbox", HTMLInputElement);
2172
+ const TRANSCRIPT_VIEW_KEY = "transcriptView.on";
2173
+ /** Show 📖 Text or the timeline, to match the checkbox. */
2174
+ function applyTranscriptViewCheckbox() {
2175
+ const on = transcriptViewCheckbox.checked;
2176
+ transcriptView.hidden = !on;
2177
+ // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2178
+ getById("timelineCanvas", HTMLCanvasElement).style.display = on ? "none" : "";
2179
+ if (!on)
2180
+ timelineDisplay.redraw();
2181
+ }
2182
+ /**
2183
+ * True while a save has switched to 📖 Text, and the user hasn't touched the checkbox since.
2184
+ * Then the save switches back when it's done.
2185
+ */
2186
+ let transcriptViewJustForSave = false;
2187
+ /**
2188
+ * A save shows 📖 Text by default, so you can read along while it records.
2189
+ * Only when there's something to read: transcripts are on, and the chapter has sound.
2190
+ */
2191
+ function showTranscriptViewForSave() {
2192
+ if (transcriptViewCheckbox.checked)
2193
+ return;
2194
+ if (!transcriptsEnabled() || transcriptViewClips().length === 0)
2195
+ return;
2196
+ transcriptViewCheckbox.checked = true;
2197
+ transcriptViewJustForSave = true;
2198
+ applyTranscriptViewCheckbox();
2199
+ }
2200
+ /** The save is over. Go back to the timeline, unless the user chose since. */
2201
+ function endTranscriptViewForSave() {
2202
+ if (!transcriptViewJustForSave)
2203
+ return;
2204
+ transcriptViewJustForSave = false;
2205
+ transcriptViewCheckbox.checked = false;
2206
+ applyTranscriptViewCheckbox();
2207
+ }
2208
+ try {
2209
+ transcriptViewCheckbox.checked =
2210
+ localStorage.getItem(TRANSCRIPT_VIEW_KEY) === "true";
2211
+ }
2212
+ catch {
2213
+ // Remembering is only a convenience.
2214
+ }
2215
+ transcriptViewCheckbox.addEventListener("input", () => {
2216
+ // The user's choice, so a save won't change it back, and it's remembered.
2217
+ transcriptViewJustForSave = false;
2177
2218
  try {
2178
- checkbox.checked = localStorage.getItem(READ_KEY) === "true";
2219
+ localStorage.setItem(TRANSCRIPT_VIEW_KEY, String(transcriptViewCheckbox.checked));
2179
2220
  }
2180
2221
  catch {
2181
- // Remembering is only a convenience.
2222
+ // See above.
2182
2223
  }
2183
- checkbox.addEventListener("input", () => {
2184
- try {
2185
- localStorage.setItem(READ_KEY, String(checkbox.checked));
2186
- }
2187
- catch {
2188
- // See above.
2189
- }
2190
- apply();
2191
- });
2192
- apply();
2193
- }
2224
+ applyTranscriptViewCheckbox();
2225
+ });
2226
+ applyTranscriptViewCheckbox();
2194
2227
  timelineDisplay.onSeek = (localMs) => {
2195
2228
  stopAudio();
2196
2229
  loadPlayPositionSeconds(localMs + rootStartMs);
@@ -3411,6 +3444,8 @@ async function initFromDB(unloadBackup) {
3411
3444
  }
3412
3445
  _loadSource = source;
3413
3446
  _baselineTreeJson = currentTreeJson();
3447
+ // The unload backup is in the database now, or older than it. See the end of "Load previous state".
3448
+ sessionStorage.removeItem("pendingScheduleSave");
3414
3449
  canvas.style.visibility = "";
3415
3450
  canvasLoading.style.display = "none";
3416
3451
  const currentSel = currentRoot();
@@ -3517,6 +3552,12 @@ function currentSaveTarget() {
3517
3552
  return selectable.schedules?.length ? selectable : null;
3518
3553
  }
3519
3554
  function saveOnUnload() {
3555
+ // Still starting up: the live tree is the TypeScript defaults, not the user's work.
3556
+ // A backup of it would be newer than the real state, so the next startup would load it,
3557
+ // and everything made in the Visual Editor would disappear. The last session's backup,
3558
+ // if there was one, is still in sessionStorage until initFromDB() has saved it.
3559
+ if (_loadSource === undefined)
3560
+ return;
3520
3561
  // When the history dialog is open the live tree may hold a transient preview from
3521
3562
  // _dialogSelectItem. Back up the pre-dialog state instead, so a preview is never
3522
3563
  // persisted as if the user had accepted it.
@@ -6773,6 +6814,16 @@ function applyMarkerDrag(localX, localY, shiftKey = false) {
6773
6814
  }
6774
6815
  finally {
6775
6816
  sessionStorage.clear();
6817
+ // Keep the unload backup until initFromDB() has saved it. If the page reloads before
6818
+ // then, saveOnUnload() saves nothing, so this is the only copy.
6819
+ if (unloadBackup !== null) {
6820
+ try {
6821
+ sessionStorage.setItem("pendingScheduleSave", unloadBackup);
6822
+ }
6823
+ catch {
6824
+ // It fit a moment ago.
6825
+ }
6826
+ }
6776
6827
  }
6777
6828
  }
6778
6829
  // MARK: User interactions with the canvas.