@braccato/core 1.21.6 → 2.0.0-beta.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.
Files changed (105) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +202 -600
  3. package/dist/styles/lyrics.css +6 -0
  4. package/dist/util.d.ts +2 -0
  5. package/dist/util.js +4 -0
  6. package/js/element.js +234 -0
  7. package/js/renderer.d.ts +157 -0
  8. package/js/renderer.js +535 -0
  9. package/package.json +22 -17
  10. package/pkg/braccato_core_rs.d.ts +236 -0
  11. package/pkg/braccato_core_rs.js +1735 -0
  12. package/pkg/braccato_core_rs_bg.wasm +0 -0
  13. package/pkg/braccato_core_rs_bg.wasm.d.ts +55 -0
  14. package/dist/LICENSE +0 -21
  15. package/dist/README.md +0 -632
  16. package/dist/element.d.ts +0 -168
  17. package/dist/element.js +0 -730
  18. package/dist/engine/animationConfig.d.ts +0 -84
  19. package/dist/engine/animationConfig.js +0 -95
  20. package/dist/engine/animationDrift.d.ts +0 -11
  21. package/dist/engine/animationDrift.js +0 -43
  22. package/dist/engine/animationTiming.d.ts +0 -27
  23. package/dist/engine/animationTiming.js +0 -145
  24. package/dist/engine/autoScroll.d.ts +0 -24
  25. package/dist/engine/autoScroll.js +0 -103
  26. package/dist/engine/clock.d.ts +0 -29
  27. package/dist/engine/clock.js +0 -70
  28. package/dist/engine/contextSweep.d.ts +0 -10
  29. package/dist/engine/contextSweep.js +0 -102
  30. package/dist/engine/cssValues.d.ts +0 -13
  31. package/dist/engine/cssValues.js +0 -47
  32. package/dist/engine/culling.d.ts +0 -6
  33. package/dist/engine/culling.js +0 -76
  34. package/dist/engine/debugOverlay.d.ts +0 -14
  35. package/dist/engine/debugOverlay.js +0 -58
  36. package/dist/engine/decorationSlides.d.ts +0 -6
  37. package/dist/engine/decorationSlides.js +0 -53
  38. package/dist/engine/elementMotion.d.ts +0 -18
  39. package/dist/engine/elementMotion.js +0 -98
  40. package/dist/engine/glow.d.ts +0 -7
  41. package/dist/engine/glow.js +0 -59
  42. package/dist/engine/instance.d.ts +0 -105
  43. package/dist/engine/instance.js +0 -89
  44. package/dist/engine/instrumentalAnimations.d.ts +0 -7
  45. package/dist/engine/instrumentalAnimations.js +0 -59
  46. package/dist/engine/letterPlans.d.ts +0 -31
  47. package/dist/engine/letterPlans.js +0 -62
  48. package/dist/engine/lineAnimationState.d.ts +0 -10
  49. package/dist/engine/lineAnimationState.js +0 -82
  50. package/dist/engine/lineAnimations.d.ts +0 -5
  51. package/dist/engine/lineAnimations.js +0 -63
  52. package/dist/engine/lineLifecycle.d.ts +0 -12
  53. package/dist/engine/lineLifecycle.js +0 -92
  54. package/dist/engine/lineScroll.d.ts +0 -47
  55. package/dist/engine/lineScroll.js +0 -184
  56. package/dist/engine/lineScrollStyle.d.ts +0 -32
  57. package/dist/engine/lineScrollStyle.js +0 -133
  58. package/dist/engine/liveViews.d.ts +0 -3
  59. package/dist/engine/liveViews.js +0 -5
  60. package/dist/engine/motionCurves.d.ts +0 -19
  61. package/dist/engine/motionCurves.js +0 -85
  62. package/dist/engine/motionPreference.d.ts +0 -1
  63. package/dist/engine/motionPreference.js +0 -1
  64. package/dist/engine/passiveScroll.d.ts +0 -10
  65. package/dist/engine/passiveScroll.js +0 -93
  66. package/dist/engine/renderedLines.d.ts +0 -16
  67. package/dist/engine/renderedLines.js +0 -130
  68. package/dist/engine/scrollViewport.d.ts +0 -36
  69. package/dist/engine/scrollViewport.js +0 -104
  70. package/dist/engine/stageMotion.d.ts +0 -7
  71. package/dist/engine/stageMotion.js +0 -319
  72. package/dist/engine/sweepPlans.d.ts +0 -38
  73. package/dist/engine/sweepPlans.js +0 -66
  74. package/dist/engine/tick.d.ts +0 -16
  75. package/dist/engine/tick.js +0 -270
  76. package/dist/engine/timingLogs.d.ts +0 -7
  77. package/dist/engine/timingLogs.js +0 -81
  78. package/dist/engine/userScroll.d.ts +0 -12
  79. package/dist/engine/userScroll.js +0 -62
  80. package/dist/engine/wavePool.d.ts +0 -7
  81. package/dist/engine/wavePool.js +0 -22
  82. package/dist/engine/wordAnimations.d.ts +0 -5
  83. package/dist/engine/wordAnimations.js +0 -206
  84. package/dist/imageHighlights.d.ts +0 -4
  85. package/dist/imageHighlights.js +0 -34
  86. package/dist/index.d.ts +0 -17
  87. package/dist/index.js +0 -12
  88. package/dist/inject.d.ts +0 -98
  89. package/dist/inject.js +0 -591
  90. package/dist/instrumental.d.ts +0 -11
  91. package/dist/instrumental.js +0 -121
  92. package/dist/language.d.ts +0 -9
  93. package/dist/language.js +0 -47
  94. package/dist/letters.d.ts +0 -2
  95. package/dist/letters.js +0 -25
  96. package/dist/renderer.d.ts +0 -29
  97. package/dist/renderer.js +0 -412
  98. package/dist/seek.d.ts +0 -1
  99. package/dist/seek.js +0 -27
  100. package/dist/stage.d.ts +0 -58
  101. package/dist/stage.js +0 -121
  102. package/dist/types.d.ts +0 -236
  103. package/dist/types.js +0 -1
  104. package/dist/view.d.ts +0 -51
  105. package/dist/view.js +0 -396
package/js/renderer.js ADDED
@@ -0,0 +1,535 @@
1
+ // `createLyricsRenderer` over the Rust renderer, with the shape `@braccato/core` gives it, so a
2
+ // consumer written against core can switch renderers without changing how it drives one.
3
+ //
4
+ // What differs underneath: the renderer draws on its own animation frames, so `tick` only hands it
5
+ // the player snapshot (time, play state and when it was read) and frames in between carry the time
6
+ // forward; the lines it hands out (`lines`) are records over the Rust song, whose `time` writes go
7
+ // back to it; and decorations are hung on a line by this module's `injectTranslation` and
8
+ // `injectRomanization` rather than written into its element.
9
+
10
+ import init, { initSync, Renderer } from "../pkg/braccato_core_rs.js";
11
+
12
+ // -- Loading --------------------------------------------
13
+
14
+ let ready = null;
15
+ let loaded = false;
16
+
17
+ /**
18
+ * Loads the wasm. `source` is anything wasm-bindgen's loader takes: a URL (by default the file next
19
+ * to this module), a `Response`, bytes or a compiled `WebAssembly.Module`. Resolves once renderers can
20
+ * be created; later calls return the same promise.
21
+ */
22
+ export function load(source) {
23
+ ready ??= init(source === undefined ? undefined : { module_or_path: source }).then(() => {
24
+ loaded = true;
25
+ });
26
+ return ready;
27
+ }
28
+
29
+ /** Loads the wasm from bytes or a compiled module without waiting. */
30
+ export function loadSync(module) {
31
+ if (!loaded) {
32
+ initSync({ module });
33
+ loaded = true;
34
+ ready ??= Promise.resolve();
35
+ }
36
+ }
37
+
38
+ /** Whether `createLyricsRenderer` can be called. */
39
+ export function isLoaded() {
40
+ return loaded;
41
+ }
42
+
43
+ // -- Module-wide state --------------------------------------------
44
+
45
+ /** Every renderer not yet destroyed. */
46
+ const live = new Set();
47
+
48
+ /** The last player snapshot any renderer here was ticked with, as core keeps it. */
49
+ const NO_SNAPSHOT = -1;
50
+ const snapshot = { timeS: 0, isPlaying: false, eventCreationTime: NO_SNAPSHOT };
51
+
52
+ /** Forgets the last player snapshot, as a new song should. */
53
+ export function resetPlaybackClock() {
54
+ snapshot.timeS = 0;
55
+ snapshot.isPlaying = false;
56
+ snapshot.eventCreationTime = NO_SNAPSHOT;
57
+ for (const view of live) view.resetClock();
58
+ }
59
+
60
+ /** Resumes autoscroll in every view a reader scrolled away in. */
61
+ export function resumeAllAutoscroll() {
62
+ for (const view of live) view.resumeAutoscroll();
63
+ }
64
+
65
+ function owner(lyricElement) {
66
+ for (const view of live) {
67
+ const index = view.indexOf(lyricElement);
68
+ if (index >= 0) return { view, index };
69
+ }
70
+ return null;
71
+ }
72
+
73
+ /**
74
+ * A 2D copy of what the renderer drawing on `canvas` shows now, styled like it, or null when no
75
+ * renderer here draws on it. For a host that clones the page to animate it away: a WebGL canvas
76
+ * clones blank.
77
+ */
78
+ export function snapshotCanvas(canvas) {
79
+ for (const view of live) {
80
+ if (view.canvas() === canvas) return view.snapshot();
81
+ }
82
+ return null;
83
+ }
84
+
85
+ /** The canvas the renderer that built `container` draws on, or null. */
86
+ export function canvasFor(container) {
87
+ for (const view of live) {
88
+ if (view.container() === container) return view.canvas();
89
+ }
90
+ return null;
91
+ }
92
+
93
+ /** Whether `lyricElement` is a line one of this module's renderers built. */
94
+ export function ownsLine(lyricElement) {
95
+ return owner(lyricElement) !== null;
96
+ }
97
+
98
+ // -- Decorations --------------------------------------------
99
+
100
+ function normalizeLanguage(language) {
101
+ if (!language || language === "auto" || language === "und") return "";
102
+ try {
103
+ return Intl.getCanonicalLocales(language.trim().replaceAll("_", "-"))[0] ?? "";
104
+ } catch {
105
+ return "";
106
+ }
107
+ }
108
+
109
+ /** Hangs a translation on a line. A translation that only repeats the line is not shown. */
110
+ export function injectTranslation(_doc, lyricElement, text, language) {
111
+ owner(lyricElement)?.view.hangTranslation(lyricElement, text, normalizeLanguage(language));
112
+ }
113
+
114
+ /** Hangs a romanization on a line, swept by `timedRomanization` when it has timed parts. */
115
+ export function injectRomanization(_doc, lyricElement, _lineData, text, timedRomanization = null) {
116
+ owner(lyricElement)?.view.hangRomanization(lyricElement, text, timedRomanization);
117
+ }
118
+
119
+ /** Takes a line's translation off. Answers whether it had one. */
120
+ export function removeTranslation(lyricElement) {
121
+ return owner(lyricElement)?.view.hangTranslation(lyricElement, null) ?? false;
122
+ }
123
+
124
+ /** Takes a line's romanization off. Answers whether it had one. */
125
+ export function removeRomanization(lyricElement, _lineData) {
126
+ return owner(lyricElement)?.view.hangRomanization(lyricElement, null) ?? false;
127
+ }
128
+
129
+ // -- Line records --------------------------------------------
130
+
131
+ // Core's `LineData` and `PartData` are its live render state. Here they are views of the Rust song:
132
+ // `time` and `duration` read it, and writing `time` moves the line or part. The other fields exist
133
+ // so code that sets them keeps working, and mean nothing to this renderer.
134
+
135
+ class PartRecord {
136
+ #view;
137
+ #line;
138
+ #index;
139
+ lyricElement;
140
+ animations = [];
141
+
142
+ constructor(view, line, index, element) {
143
+ this.#view = view;
144
+ this.#line = line;
145
+ this.#index = index;
146
+ this.lyricElement = element;
147
+ }
148
+
149
+ get time() {
150
+ return this.#view.timing(this.#line.lyricElement, this.#index)[0];
151
+ }
152
+ set time(value) {
153
+ this.#view.setStart(this.#line.lyricElement, this.#index, value);
154
+ }
155
+ get duration() {
156
+ return this.#view.timing(this.#line.lyricElement, this.#index)[1];
157
+ }
158
+ }
159
+
160
+ class LineRecord {
161
+ #view;
162
+ lyricElement;
163
+ parts = [];
164
+ isScrolled = false;
165
+ isAnimationPlayStatePlaying = false;
166
+ accumulatedOffsetMs = 0;
167
+ isAnimating = false;
168
+ lastAnimSetupAt = 0;
169
+ isSelected = false;
170
+ height = -1;
171
+ position = -1;
172
+ decorations = new Map();
173
+ animations = [];
174
+
175
+ constructor(view, element) {
176
+ this.#view = view;
177
+ this.lyricElement = element;
178
+ }
179
+
180
+ setParts(elements) {
181
+ const same =
182
+ elements.length === this.parts.length && elements.every((element, i) => element === this.parts[i].lyricElement);
183
+ if (!same) this.parts = elements.map((element, i) => new PartRecord(this.#view, this, i, element));
184
+ }
185
+
186
+ get time() {
187
+ return this.#view.timing(this.lyricElement)[0];
188
+ }
189
+ set time(value) {
190
+ this.#view.setStart(this.lyricElement, undefined, value);
191
+ }
192
+ get duration() {
193
+ return this.#view.timing(this.lyricElement)[1];
194
+ }
195
+ }
196
+
197
+ // -- The renderer --------------------------------------------
198
+
199
+ const SEEK_EVENT = "braccato:seek";
200
+ const SCROLLABLE_OVERFLOW = new Set(["auto", "scroll"]);
201
+
202
+ function noop() {}
203
+
204
+ function findScrollElement(win, mount) {
205
+ for (let element = mount; element; element = element.parentElement) {
206
+ if (SCROLLABLE_OVERFLOW.has(win.getComputedStyle(element).overflowY)) return element;
207
+ }
208
+ return mount?.ownerDocument.scrollingElement ?? null;
209
+ }
210
+
211
+ function resolveTickOptions(options) {
212
+ return {
213
+ eventCreationTime: options.eventCreationTime ?? NO_SNAPSHOT,
214
+ isPlaying: Boolean(options.isPlaying),
215
+ smoothScroll: options.smoothScroll ?? true,
216
+ globalLyricOffset: options.globalLyricOffset ?? 0,
217
+ lyricOffset: options.lyricOffset ?? 0,
218
+ richsyncOffsetTrim: options.richsyncOffsetTrim ?? 0,
219
+ lineOffsetTrim: options.lineOffsetTrim ?? 0,
220
+ passiveScrollEnabled: options.passiveScrollEnabled ?? false,
221
+ playbackRate: options.playbackRate ?? 1,
222
+ };
223
+ }
224
+
225
+ /**
226
+ * A lyrics view with `@braccato/core`'s `LyricsRenderer` interface, drawn by the Rust renderer.
227
+ * `load()` must have resolved first.
228
+ */
229
+ export function createLyricsRenderer(rendererOptions) {
230
+ if (!loaded) throw new Error("@braccato/core: call load() before createLyricsRenderer()");
231
+ const doc = rendererOptions.document;
232
+ const win = rendererOptions.window;
233
+ const layout = rendererOptions.layout ?? "scroll";
234
+ const given = rendererOptions.host ?? {};
235
+ let mount = rendererOptions.mount ?? null;
236
+ let rust = null;
237
+ let attached = false;
238
+ let destroyed = false;
239
+ let listening = null;
240
+ let scrollElement;
241
+ let tickKey = "";
242
+ let records = [];
243
+ let recordsGeneration = -1;
244
+ let pendingPosition = null;
245
+ let lastCss = null;
246
+
247
+ let walkedMount;
248
+ let walkedScrollElement = null;
249
+ const host = {
250
+ isViewVisible: given.isViewVisible ?? (() => true),
251
+ isLoaderActive: given.isLoaderActive ?? (() => false),
252
+ syncAdState: given.syncAdState ?? (() => false),
253
+ getScrollElement:
254
+ given.getScrollElement ??
255
+ (() => {
256
+ if (mount !== walkedMount) {
257
+ walkedMount = mount;
258
+ walkedScrollElement = findScrollElement(win, mount);
259
+ }
260
+ return walkedScrollElement;
261
+ }),
262
+ setResumeAffordanceVisible: given.setResumeAffordanceVisible ?? noop,
263
+ seek:
264
+ given.seek ??
265
+ ((timeS) => mount?.dispatchEvent(new win.CustomEvent(SEEK_EVENT, { detail: timeS, bubbles: true }))),
266
+ log: given.log ?? noop,
267
+ onStageLayout: given.onStageLayout,
268
+ };
269
+
270
+ const onEvent = (event) => {
271
+ const detail = event.detail ?? {};
272
+ switch (event.type) {
273
+ case "braccato:line-click":
274
+ host.seek(detail.timeS);
275
+ break;
276
+ case "braccato:scroll-state":
277
+ host.setResumeAffordanceVisible(Boolean(detail.userScrolling));
278
+ break;
279
+ case "braccato:stage-layout":
280
+ host.onStageLayout?.(detail.box ?? null);
281
+ break;
282
+ }
283
+ };
284
+ const EVENTS = ["braccato:line-click", "braccato:scroll-state", "braccato:stage-layout"];
285
+
286
+ function listenOn(element) {
287
+ if (listening === element) return;
288
+ for (const name of EVENTS) listening?.removeEventListener(name, onEvent);
289
+ listening = element;
290
+ for (const name of EVENTS) element.addEventListener(name, onEvent);
291
+ }
292
+
293
+ function syncScrollElement() {
294
+ const element = host.getScrollElement() ?? null;
295
+ if (element !== scrollElement) {
296
+ scrollElement = element;
297
+ rust.setScrollElement(element);
298
+ }
299
+ }
300
+
301
+ function ensure(nextMount) {
302
+ if (rust && nextMount.ownerDocument !== doc) {
303
+ throw new Error("@braccato/core: a renderer stays in the document it was created for");
304
+ }
305
+ if (!rust) {
306
+ rust = new Renderer(nextMount);
307
+ rust.setLayout(layout);
308
+ rust.setDecorations(false, false);
309
+ // Core leaves telling a reader's scroll apart to the host, through noteUserScroll.
310
+ rust.setScrollDetection(false);
311
+ if (lastCss !== null) rust.setTheme(lastCss);
312
+ scrollElement = undefined;
313
+ } else if (nextMount !== mount || !attached) {
314
+ rust.setHost(nextMount);
315
+ }
316
+ mount = nextMount;
317
+ attached = true;
318
+ listenOn(nextMount);
319
+ syncScrollElement();
320
+ }
321
+
322
+ function syncRecords() {
323
+ if (!rust || !attached) {
324
+ records = [];
325
+ recordsGeneration = -1;
326
+ return records;
327
+ }
328
+ const generation = rust.generation;
329
+ if (generation === recordsGeneration) return records;
330
+ recordsGeneration = generation;
331
+ const byElement = new Map(records.map((record) => [record.lyricElement, record]));
332
+ records = rust.lineElements().map(({ element, parts }) => {
333
+ const record = byElement.get(element) ?? new LineRecord(view, element);
334
+ record.setParts(parts);
335
+ return record;
336
+ });
337
+ Object.freeze(records);
338
+ return records;
339
+ }
340
+
341
+ function indexOf(lyricElement) {
342
+ return rust && attached ? rust.lineIndex(lyricElement) : -1;
343
+ }
344
+
345
+ // What the records and the module-level helpers reach this renderer through.
346
+ const view = {
347
+ indexOf,
348
+ timing(lyricElement, part) {
349
+ const index = indexOf(lyricElement);
350
+ return (index >= 0 && rust.timing(index, part)) || [0, 0];
351
+ },
352
+ setStart(lyricElement, part, value) {
353
+ const index = indexOf(lyricElement);
354
+ if (index >= 0 && Number.isFinite(value)) rust.setStart(index, part, value);
355
+ },
356
+ hangTranslation(lyricElement, text, language) {
357
+ const index = indexOf(lyricElement);
358
+ return index >= 0 ? rust.hangTranslation(index, text ?? undefined, language ?? undefined) : false;
359
+ },
360
+ hangRomanization(lyricElement, text, timed) {
361
+ const index = indexOf(lyricElement);
362
+ return index >= 0 ? rust.hangRomanization(index, text ?? undefined, timed ?? null) : false;
363
+ },
364
+ resetClock() {
365
+ rust?.resetClock();
366
+ },
367
+ canvas() {
368
+ return rust?.canvas ?? null;
369
+ },
370
+ container() {
371
+ return rust && attached ? rust.container : null;
372
+ },
373
+ snapshot() {
374
+ return rust?.snapshot() ?? null;
375
+ },
376
+ resumeAutoscroll() {
377
+ rust?.resumeAutoscroll();
378
+ },
379
+ };
380
+ live.add(view);
381
+
382
+ /** The snapshot last handed to Rust. */
383
+ let sent = null;
384
+
385
+ function tick(currentTimeS, options) {
386
+ if (destroyed) return "lyrics-missing";
387
+ const resolved = resolveTickOptions(options ?? {});
388
+ snapshot.timeS = currentTimeS;
389
+ snapshot.isPlaying = resolved.isPlaying;
390
+ snapshot.eventCreationTime = resolved.eventCreationTime;
391
+ if (currentTimeS === 0 && !resolved.isPlaying) {
392
+ // As in core, a stop at zero (a player loading, or between songs) moves nothing. Core
393
+ // simply skips the tick; this renderer runs its own clock from the last snapshot, so it is
394
+ // told to hold where that clock had got to.
395
+ if (sent?.isPlaying) {
396
+ const now = Date.now();
397
+ const elapsedS = sent.eventCreationTime > 0 ? Math.max(0, now - sent.eventCreationTime) / 1000 : 0;
398
+ sent = {
399
+ timeS: sent.timeS + elapsedS * (resolved.playbackRate ?? 1),
400
+ isPlaying: false,
401
+ eventCreationTime: now,
402
+ };
403
+ rust?.setSnapshot(sent.timeS, false, now);
404
+ }
405
+ return "ok";
406
+ }
407
+ if (!host.isViewVisible() || host.syncAdState()) {
408
+ rust?.setHidden(true);
409
+ return "ok";
410
+ }
411
+ if (!rust || !attached || !rust.hasSong()) {
412
+ host.log("No lyrics have been built for this view to render");
413
+ return "lyrics-missing";
414
+ }
415
+ rust.setHidden(false);
416
+ syncScrollElement();
417
+ const key = JSON.stringify([
418
+ resolved.smoothScroll,
419
+ resolved.globalLyricOffset,
420
+ resolved.lyricOffset,
421
+ resolved.richsyncOffsetTrim,
422
+ resolved.lineOffsetTrim,
423
+ resolved.passiveScrollEnabled,
424
+ resolved.playbackRate,
425
+ ]);
426
+ if (key !== tickKey) {
427
+ tickKey = key;
428
+ rust.setTickOptions(resolved);
429
+ }
430
+ rust.setSnapshot(currentTimeS, resolved.isPlaying, resolved.eventCreationTime);
431
+ sent = { timeS: currentTimeS, isPlaying: resolved.isPlaying, eventCreationTime: resolved.eventCreationTime };
432
+ return "ok";
433
+ }
434
+
435
+ return {
436
+ setLyrics(lyrics, options = {}) {
437
+ if (destroyed) {
438
+ host.log("Lyrics were handed to a renderer that has been destroyed; nothing was built");
439
+ return;
440
+ }
441
+ const nextMount = options.mount ?? mount;
442
+ if (!nextMount) {
443
+ throw new Error("A lyrics renderer needs a mount: give one to createLyricsRenderer or to setLyrics");
444
+ }
445
+ ensure(nextMount);
446
+ rust.setLyrics(lyrics, {
447
+ noLyrics: options.noLyrics ?? false,
448
+ language: normalizeLanguage(options.language) || undefined,
449
+ songwriters: options.songwriters ? [...options.songwriters] : undefined,
450
+ songKey: options.songKey,
451
+ });
452
+ rust.container.dataset.loaderVisible = String(options.loaderVisible ?? false);
453
+ },
454
+ setLanguage(language) {
455
+ if (!destroyed) rust?.setLanguage(normalizeLanguage(language));
456
+ },
457
+ setTheme(css) {
458
+ if (destroyed) return false;
459
+ lastCss = css;
460
+ rust?.setTheme(css);
461
+ // The Rust renderer keeps the lyrics and rebuilds them itself.
462
+ return false;
463
+ },
464
+ tick,
465
+ relayout() {
466
+ if (!destroyed) rust?.relayout(false);
467
+ },
468
+ clear() {
469
+ if (destroyed || !rust) return;
470
+ rust.clear();
471
+ attached = false;
472
+ syncRecords();
473
+ },
474
+ destroy() {
475
+ if (destroyed) return;
476
+ destroyed = true;
477
+ live.delete(view);
478
+ for (const name of EVENTS) listening?.removeEventListener(name, onEvent);
479
+ if (pendingPosition !== null) win.cancelAnimationFrame(pendingPosition.frame);
480
+ rust?.destroy();
481
+ rust?.free();
482
+ rust = null;
483
+ records = [];
484
+ },
485
+ noteUserScroll() {
486
+ // As core: a scroll while the host shows its loader (a new source on its way) is not a reader's.
487
+ if (destroyed || host.isLoaderActive()) return;
488
+ rust?.noteScroll();
489
+ },
490
+ noteVisibilityChange() {},
491
+ resumeAutoscroll() {
492
+ if (!destroyed) rust?.resumeAutoscroll();
493
+ },
494
+ clearOnScreenLyrics() {
495
+ if (destroyed || !rust || !attached) return false;
496
+ const had = rust.clearOnScreen();
497
+ syncRecords();
498
+ return had;
499
+ },
500
+ scheduleLyricPositionUpdate(isTicking, retick) {
501
+ if (destroyed) return;
502
+ if (pendingPosition !== null) {
503
+ pendingPosition.isTicking = isTicking;
504
+ pendingPosition.retick = retick;
505
+ return;
506
+ }
507
+ pendingPosition = { isTicking, retick, frame: 0 };
508
+ pendingPosition.frame = win.requestAnimationFrame(() => {
509
+ const { isTicking: ticking, retick: after } = pendingPosition;
510
+ pendingPosition = null;
511
+ if (destroyed || !ticking()) return;
512
+ rust?.relayout(true);
513
+ after();
514
+ });
515
+ },
516
+ retickFromPlaybackClock(buildOptions) {
517
+ if (destroyed) return "lyrics-missing";
518
+ const options = buildOptions(snapshot.eventCreationTime, snapshot.isPlaying);
519
+ return tick(snapshot.timeS, options);
520
+ },
521
+ get container() {
522
+ return rust && attached ? rust.container : null;
523
+ },
524
+ get lines() {
525
+ return syncRecords();
526
+ },
527
+ get syncType() {
528
+ return rust && attached ? rust.syncType() : "none";
529
+ },
530
+ /** The Rust renderer underneath, for `setProfiling()`, `stats()` and `takeProfile()`; null before the first `setLyrics`. */
531
+ get renderer() {
532
+ return rust;
533
+ },
534
+ };
535
+ }
package/package.json CHANGED
@@ -1,36 +1,34 @@
1
1
  {
2
2
  "name": "@braccato/core",
3
- "version": "1.21.6",
4
- "description": "Synchronized lyrics renderer with word-by-word animations",
3
+ "version": "2.0.0-beta.0",
4
+ "description": "Synchronized lyrics renderer with word-by-word animations, in Rust and WebGL2",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/better-lyrics/braccato.git",
10
- "directory": "packages/core"
10
+ "directory": "packages/core-rs"
11
11
  },
12
12
  "homepage": "https://braccato.boidu.dev",
13
13
  "keywords": [
14
14
  "lyrics",
15
15
  "sync",
16
- "web-component",
17
- "music",
18
16
  "karaoke",
19
- "ttml"
17
+ "ttml",
18
+ "webgl",
19
+ "wasm"
20
20
  ],
21
21
  "sideEffects": [
22
- "./dist/element.js",
22
+ "./js/element.js",
23
23
  "./dist/styles/*.css"
24
24
  ],
25
25
  "exports": {
26
26
  ".": {
27
- "types": "./dist/index.d.ts",
28
- "import": "./dist/index.js"
29
- },
30
- "./element": {
31
- "types": "./dist/element.d.ts",
32
- "import": "./dist/element.js"
27
+ "types": "./js/renderer.d.ts",
28
+ "import": "./js/renderer.js"
33
29
  },
30
+ "./element": "./js/element.js",
31
+ "./wasm": "./pkg/braccato_core_rs_bg.wasm",
34
32
  "./constants": {
35
33
  "types": "./dist/constants.d.ts",
36
34
  "import": "./dist/constants.js"
@@ -51,6 +49,10 @@
51
49
  "./package.json": "./package.json"
52
50
  },
53
51
  "files": [
52
+ "js",
53
+ "pkg/*.js",
54
+ "pkg/*.wasm",
55
+ "pkg/*.d.ts",
54
56
  "dist"
55
57
  ],
56
58
  "publishConfig": {
@@ -59,10 +61,13 @@
59
61
  "dependencies": {
60
62
  "@braccato/types": "^1.1.0"
61
63
  },
64
+ "devDependencies": {
65
+ "@braccato/core-dom": "1.22.0"
66
+ },
62
67
  "scripts": {
63
- "build": "tsx ../../tooling/build-package.ts",
64
- "typecheck": "tsc --noEmit",
65
- "selfcheck": "tsx ../../tooling/run-selfchecks.ts",
66
- "clean": "rm -rf dist"
68
+ "build": "node scripts/shared.mjs && node scripts/run.mjs build",
69
+ "build:profiling": "node scripts/run.mjs build:profiling",
70
+ "lint": "node scripts/run.mjs lint",
71
+ "test": "node scripts/run.mjs test"
67
72
  }
68
73
  }