@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
@@ -184,6 +184,12 @@
184
184
  position: relative;
185
185
  }
186
186
 
187
+ /* WebKit puts a clipped inline-block's baseline at its bottom edge; paint containment clips without that. */
188
+ .blyrics-container[data-browser-engine="webkit"] .blyrics-sweep-cell {
189
+ contain: paint;
190
+ overflow: visible;
191
+ }
192
+
187
193
  .blyrics-word-highlight--sweep.blyrics-rtl .blyrics-sweep-cell,
188
194
  .blyrics-sweep-window.blyrics-sweep-cell[dir="rtl"] {
189
195
  direction: rtl;
package/dist/util.d.ts CHANGED
@@ -5,6 +5,8 @@ export declare function positiveModulo(value: number, divisor: number): number;
5
5
  * spoofed user agents and Firefox forks resolve the same way.
6
6
  */
7
7
  export declare function isGecko(): boolean;
8
+ /** Whether the page renders with WebKit (Safari and the other WebKit browsers), detected the same way. */
9
+ export declare function isWebKit(): boolean;
8
10
  export declare function roundedMs(value: number): number;
9
11
  /**
10
12
  * Converts CSS duration value to milliseconds.
package/dist/util.js CHANGED
@@ -11,6 +11,10 @@ export function positiveModulo(value, divisor) {
11
11
  export function isGecko() {
12
12
  return typeof CSS !== "undefined" && CSS.supports("-moz-appearance", "none");
13
13
  }
14
+ /** Whether the page renders with WebKit (Safari and the other WebKit browsers), detected the same way. */
15
+ export function isWebKit() {
16
+ return typeof CSS !== "undefined" && CSS.supports("-webkit-nbsp-mode", "space");
17
+ }
14
18
  export function roundedMs(value) {
15
19
  return Math.round(value * 10) / 10;
16
20
  }
package/js/element.js ADDED
@@ -0,0 +1,234 @@
1
+ // The custom element around the Rust renderer. It only forwards: every property is handed to the
2
+ // renderer, which owns the DOM, the canvas and the clock. Properties written before the element
3
+ // connects (or before the wasm has loaded) are kept and applied once the renderer exists.
4
+
5
+ import { Renderer } from "../pkg/braccato_core_rs.js";
6
+ import { load } from "./renderer.js";
7
+
8
+ export { load };
9
+
10
+ const OBSERVED = ["source", "theme", "current-time", "playing", "layout"];
11
+
12
+ export class BraccatoLyricsElement extends HTMLElement {
13
+ static observedAttributes = OBSERVED;
14
+
15
+ #renderer = null;
16
+ #lyrics = null;
17
+ #lyricsOptions = {};
18
+ #source = null;
19
+ #media = null;
20
+ #currentTime = 0;
21
+ #playing = false;
22
+ #tickOptions = {};
23
+ #theme = "";
24
+ #host = {};
25
+ #decorations = { translations: true, romanizations: true };
26
+ #layout = "scroll";
27
+ #connecting = null;
28
+
29
+ connectedCallback() {
30
+ // A disconnect, or a later connect, while the wasm loads makes this connect stale.
31
+ const generation = Symbol();
32
+ this.#connecting = generation;
33
+ load().then(
34
+ () => {
35
+ if (this.#connecting !== generation || !this.isConnected || this.#renderer) return;
36
+ try {
37
+ this.#renderer = new Renderer(this);
38
+ } catch (error) {
39
+ this.#emitError("connect", error);
40
+ return;
41
+ }
42
+ this.#applyAll();
43
+ },
44
+ (error) => this.#emitError("connect", error),
45
+ );
46
+ }
47
+
48
+ disconnectedCallback() {
49
+ this.#connecting = null;
50
+ this.#renderer?.destroy();
51
+ this.#renderer?.free();
52
+ this.#renderer = null;
53
+ }
54
+
55
+ attributeChangedCallback(name, _old, value) {
56
+ switch (name) {
57
+ case "source":
58
+ this.source = value;
59
+ break;
60
+ case "theme":
61
+ this.theme = value ?? "";
62
+ break;
63
+ case "current-time": {
64
+ const seconds = Number.parseFloat(value ?? "");
65
+ if (Number.isFinite(seconds)) this.currentTime = seconds;
66
+ break;
67
+ }
68
+ case "playing":
69
+ this.playing = value !== null;
70
+ break;
71
+ case "layout":
72
+ this.layout = value;
73
+ break;
74
+ }
75
+ }
76
+
77
+ #applyAll() {
78
+ const renderer = this.#renderer;
79
+ renderer.setLayout(this.#layout);
80
+ renderer.setScrollElement(this.#host.getScrollElement?.() ?? null);
81
+ renderer.setTheme(this.#theme);
82
+ renderer.setTickOptions(this.#tickOptions);
83
+ renderer.setDecorations(this.#decorations.translations, this.#decorations.romanizations);
84
+ this.#bindSource();
85
+ renderer.setTime(this.#currentTime, this.#playing);
86
+ if (this.#lyrics !== null) this.#applyLyrics();
87
+ }
88
+
89
+ #applyLyrics() {
90
+ try {
91
+ this.#renderer?.setLyrics(this.#lyrics, this.#lyricsOptions);
92
+ } catch (error) {
93
+ this.#emitError("lyrics", error);
94
+ }
95
+ }
96
+
97
+ #bindSource() {
98
+ // The element's own document and window: in an iframe or a Picture-in-Picture window, a
99
+ // selector names that document's player, and its media elements are that window's class.
100
+ const view = this.ownerDocument.defaultView;
101
+ const isMedia = (node) => view !== null && node instanceof view.HTMLMediaElement;
102
+ let media = null;
103
+ if (typeof this.#source === "string") {
104
+ media = this.isConnected ? this.ownerDocument.querySelector(this.#source) : null;
105
+ if (this.#source && !media) this.#emitError("source", new Error(`No element matches ${this.#source}`));
106
+ } else if (isMedia(this.#source)) {
107
+ media = this.#source;
108
+ }
109
+ this.#media = isMedia(media) ? media : null;
110
+ this.#renderer?.setMedia(this.#media);
111
+ }
112
+
113
+ #emitError(phase, error) {
114
+ queueMicrotask(() =>
115
+ this.dispatchEvent(
116
+ new CustomEvent("braccato:error", { bubbles: true, composed: true, detail: { phase, error } }),
117
+ ),
118
+ );
119
+ }
120
+
121
+ /** The song: a `Lyric[]`. An empty array clears the view. */
122
+ get lyrics() {
123
+ return this.#lyrics;
124
+ }
125
+ set lyrics(value) {
126
+ this.#lyrics = value;
127
+ this.#applyLyrics();
128
+ }
129
+
130
+ /** `{ noLyrics?, language?, songwriters? }`, read by the next `lyrics` write. */
131
+ get lyricsOptions() {
132
+ return this.#lyricsOptions;
133
+ }
134
+ set lyricsOptions(value) {
135
+ this.#lyricsOptions = value ?? {};
136
+ }
137
+
138
+ /** A media element, or a selector for one, that drives the clock. */
139
+ get source() {
140
+ return this.#source;
141
+ }
142
+ set source(value) {
143
+ this.#source = value ?? null;
144
+ this.#bindSource();
145
+ }
146
+
147
+ get mediaElement() {
148
+ return this.#renderer ? this.#media : null;
149
+ }
150
+
151
+ /** Playback position in seconds, for hosts that drive the clock themselves. */
152
+ get currentTime() {
153
+ return this.#currentTime;
154
+ }
155
+ set currentTime(value) {
156
+ this.#currentTime = Number(value) || 0;
157
+ this.#renderer?.setTime(this.#currentTime, this.#playing);
158
+ }
159
+
160
+ get playing() {
161
+ return this.#playing;
162
+ }
163
+ set playing(value) {
164
+ this.#playing = Boolean(value);
165
+ this.#renderer?.setTime(this.#currentTime, this.#playing);
166
+ }
167
+
168
+ /** `{ lyricOffset?, globalLyricOffset?, playbackRate?, smoothScroll? }`. */
169
+ get tickOptions() {
170
+ return this.#tickOptions;
171
+ }
172
+ set tickOptions(value) {
173
+ this.#tickOptions = value ?? {};
174
+ this.#renderer?.setTickOptions(this.#tickOptions);
175
+ }
176
+
177
+ /** A stylesheet of `--blyrics-*` properties and `blyrics-*` comment settings. */
178
+ get theme() {
179
+ return this.#theme;
180
+ }
181
+ set theme(value) {
182
+ this.#theme = String(value ?? "");
183
+ this.#renderer?.setTheme(this.#theme);
184
+ }
185
+
186
+ /** `{ getScrollElement? }`: which element autoscroll moves. */
187
+ get host() {
188
+ return this.#host;
189
+ }
190
+ set host(value) {
191
+ this.#host = value ?? {};
192
+ this.#renderer?.setScrollElement(this.#host.getScrollElement?.() ?? null);
193
+ }
194
+
195
+ /** `"stage"` shows only the lines being sung, bottom-anchored; anything else scrolls. */
196
+ get layout() {
197
+ return this.#layout;
198
+ }
199
+ set layout(value) {
200
+ this.#layout = value === "stage" ? "stage" : "scroll";
201
+ this.#renderer?.setLayout(this.#layout);
202
+ }
203
+
204
+ /** Which of the lyrics' own translations and romanizations to show. */
205
+ get decorations() {
206
+ return { ...this.#decorations };
207
+ }
208
+ set decorations(value) {
209
+ this.#decorations = { translations: Boolean(value?.translations), romanizations: Boolean(value?.romanizations) };
210
+ this.#renderer?.setDecorations(this.#decorations.translations, this.#decorations.romanizations);
211
+ }
212
+
213
+ get renderer() {
214
+ return this.#renderer;
215
+ }
216
+
217
+ get status() {
218
+ return this.#renderer ? "rendering" : "idle";
219
+ }
220
+
221
+ resumeAutoscroll() {
222
+ this.#renderer?.resumeAutoscroll();
223
+ }
224
+
225
+ relayout() {
226
+ this.#renderer?.relayout();
227
+ }
228
+ }
229
+
230
+ /** Defines the element under `tag` (default `braccato-lyrics`) unless something already holds it. */
231
+ export function defineLyricsElement(tag = "braccato-lyrics") {
232
+ if (!customElements.get(tag)) customElements.define(tag, class extends BraccatoLyricsElement {});
233
+ return customElements.get(tag);
234
+ }
@@ -0,0 +1,157 @@
1
+ // Types for js/renderer.js: `@braccato/core`'s renderer interface over the Rust renderer.
2
+ import type { Lyric, LyricPart, LyricSyncType } from "@braccato/types";
3
+ import type { Renderer } from "../pkg/braccato_core_rs.js";
4
+
5
+ export type { Lyric, LyricPart, LyricSyncType };
6
+
7
+ /** What `load` and `loadSync` take: an address, a response, bytes, or a compiled module. */
8
+ export type WasmSource = string | URL | Request | Response | BufferSource | WebAssembly.Module;
9
+
10
+ /** Loads the wasm. Resolves once renderers can be created; later calls return the same promise. */
11
+ export function load(source?: WasmSource): Promise<void>;
12
+ /** Loads the wasm from bytes or a compiled module, without waiting. */
13
+ export function loadSync(module: BufferSource | WebAssembly.Module): void;
14
+ /** Whether `createLyricsRenderer` can be called. */
15
+ export function isLoaded(): boolean;
16
+
17
+ /** Forgets the last player snapshot, as a new song should. */
18
+ export function resetPlaybackClock(): void;
19
+ /** Resumes autoscroll in every view a reader scrolled away in. */
20
+ export function resumeAllAutoscroll(): void;
21
+ /**
22
+ * A 2D copy of what the renderer drawing on `canvas` shows now, styled like it, or null when no
23
+ * renderer here draws on it. A WebGL canvas clones blank, so a host cloning the page uses this.
24
+ */
25
+ export function snapshotCanvas(canvas: HTMLCanvasElement): HTMLCanvasElement | null;
26
+ /** The canvas the renderer that built `container` draws on, or null. */
27
+ export function canvasFor(container: HTMLElement): HTMLCanvasElement | null;
28
+ /** Whether `lyricElement` is a line one of this module's renderers built. */
29
+ export function ownsLine(lyricElement: Element): boolean;
30
+
31
+ export function injectTranslation(
32
+ doc: Document,
33
+ lyricElement: HTMLElement,
34
+ text: string,
35
+ language?: string | null,
36
+ ): void;
37
+ export function injectRomanization(
38
+ doc: Document,
39
+ lyricElement: HTMLElement,
40
+ lineData: unknown,
41
+ text: string,
42
+ timedRomanization?: LyricPart[] | null,
43
+ ): void;
44
+ export function removeTranslation(lyricElement: HTMLElement): boolean;
45
+ export function removeRomanization(lyricElement: HTMLElement, lineData?: unknown): boolean;
46
+
47
+ export interface TickOptions {
48
+ /** `Date.now()` when the time was read off the player; -1 (the default) when it was not. */
49
+ eventCreationTime?: number;
50
+ isPlaying: boolean;
51
+ smoothScroll?: boolean;
52
+ globalLyricOffset?: number;
53
+ lyricOffset?: number;
54
+ richsyncOffsetTrim?: number;
55
+ lineOffsetTrim?: number;
56
+ passiveScrollEnabled?: boolean;
57
+ playbackRate?: number;
58
+ /** Accepted for `@braccato/core` compatibility; the Rust renderer always follows paused seeks. */
59
+ followPausedSeeks?: boolean;
60
+ /** Accepted for `@braccato/core` compatibility. */
61
+ countEveryUserScroll?: boolean;
62
+ }
63
+
64
+ export type AnimationTickStatus = "ok" | "lyrics-missing";
65
+
66
+ export interface StageBox {
67
+ x: number;
68
+ y: number;
69
+ width: number;
70
+ height: number;
71
+ }
72
+
73
+ export interface LyricsRendererHost {
74
+ isViewVisible(): boolean;
75
+ isLoaderActive(): boolean;
76
+ syncAdState(): boolean;
77
+ getScrollElement(): HTMLElement | null;
78
+ setResumeAffordanceVisible(visible: boolean): void;
79
+ seek(timeS: number): void;
80
+ log(...args: unknown[]): void;
81
+ onStageLayout?(box: StageBox | null): void;
82
+ /** Accepted for `@braccato/core` compatibility; nothing is drawn to it. */
83
+ debug?: unknown;
84
+ }
85
+
86
+ export type LyricsLayout = "scroll" | "stage";
87
+
88
+ export interface LyricsRendererOptions {
89
+ document: Document;
90
+ window: Window;
91
+ mount?: HTMLElement;
92
+ host?: Partial<LyricsRendererHost>;
93
+ layout?: LyricsLayout;
94
+ }
95
+
96
+ export interface SetLyricsOptions {
97
+ language?: string | null;
98
+ loaderVisible?: boolean;
99
+ noLyrics?: boolean;
100
+ songwriters?: readonly string[];
101
+ /** Lyrics given with the key of the song on screen are swapped into it. */
102
+ songKey?: string;
103
+ mount?: HTMLElement;
104
+ }
105
+
106
+ /** A word of a line. `time` and `duration` are in seconds; writing `time` moves the word. */
107
+ export interface PartData {
108
+ lyricElement: HTMLElement;
109
+ time: number;
110
+ readonly duration: number;
111
+ animations: Animation[];
112
+ }
113
+
114
+ /** A line the renderer built. `time` and `duration` are in seconds; writing `time` moves the line. */
115
+ export interface LineData {
116
+ lyricElement: HTMLElement;
117
+ time: number;
118
+ readonly duration: number;
119
+ parts: PartData[];
120
+ isScrolled: boolean;
121
+ isAnimationPlayStatePlaying: boolean;
122
+ accumulatedOffsetMs: number;
123
+ isAnimating: boolean;
124
+ lastAnimSetupAt: number;
125
+ isSelected: boolean;
126
+ height: number;
127
+ position: number;
128
+ decorations: Map<HTMLElement, number>;
129
+ animations: Animation[];
130
+ }
131
+
132
+ export interface LyricsRenderer {
133
+ setLyrics(lyrics: Lyric[], options?: SetLyricsOptions): void;
134
+ setLanguage(language?: string | null): void;
135
+ /** Always false: the Rust renderer keeps the lyrics and rebuilds them itself. */
136
+ setTheme(css: string): boolean;
137
+ tick(currentTimeS: number, options: TickOptions): AnimationTickStatus;
138
+ relayout(measureLines?: boolean): void;
139
+ clear(): void;
140
+ destroy(): void;
141
+ noteUserScroll(): void;
142
+ noteVisibilityChange(): void;
143
+ resumeAutoscroll(): void;
144
+ clearOnScreenLyrics(): boolean;
145
+ scheduleLyricPositionUpdate(isTicking: () => boolean, retick: () => void): void;
146
+ retickFromPlaybackClock(
147
+ buildOptions: (eventCreationTime: number, isPlaying: boolean) => TickOptions,
148
+ ): AnimationTickStatus;
149
+ readonly container: HTMLElement | null;
150
+ readonly lines: readonly LineData[];
151
+ readonly syncType: LyricSyncType;
152
+ /** The Rust renderer underneath, for `setProfiling()`, `stats()` and `takeProfile()`; null before the first `setLyrics`. */
153
+ readonly renderer: Renderer | null;
154
+ }
155
+
156
+ /** A lyrics view with `@braccato/core`'s interface, drawn by the Rust renderer. `load()` first. */
157
+ export function createLyricsRenderer(options: LyricsRendererOptions): LyricsRenderer;