@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.
- package/LICENSE +1 -1
- package/README.md +202 -600
- package/dist/styles/lyrics.css +6 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +4 -0
- package/js/element.js +234 -0
- package/js/renderer.d.ts +157 -0
- package/js/renderer.js +535 -0
- package/package.json +22 -17
- package/pkg/braccato_core_rs.d.ts +236 -0
- package/pkg/braccato_core_rs.js +1735 -0
- package/pkg/braccato_core_rs_bg.wasm +0 -0
- package/pkg/braccato_core_rs_bg.wasm.d.ts +55 -0
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -632
- package/dist/element.d.ts +0 -168
- package/dist/element.js +0 -730
- package/dist/engine/animationConfig.d.ts +0 -84
- package/dist/engine/animationConfig.js +0 -95
- package/dist/engine/animationDrift.d.ts +0 -11
- package/dist/engine/animationDrift.js +0 -43
- package/dist/engine/animationTiming.d.ts +0 -27
- package/dist/engine/animationTiming.js +0 -145
- package/dist/engine/autoScroll.d.ts +0 -24
- package/dist/engine/autoScroll.js +0 -103
- package/dist/engine/clock.d.ts +0 -29
- package/dist/engine/clock.js +0 -70
- package/dist/engine/contextSweep.d.ts +0 -10
- package/dist/engine/contextSweep.js +0 -102
- package/dist/engine/cssValues.d.ts +0 -13
- package/dist/engine/cssValues.js +0 -47
- package/dist/engine/culling.d.ts +0 -6
- package/dist/engine/culling.js +0 -76
- package/dist/engine/debugOverlay.d.ts +0 -14
- package/dist/engine/debugOverlay.js +0 -58
- package/dist/engine/decorationSlides.d.ts +0 -6
- package/dist/engine/decorationSlides.js +0 -53
- package/dist/engine/elementMotion.d.ts +0 -18
- package/dist/engine/elementMotion.js +0 -98
- package/dist/engine/glow.d.ts +0 -7
- package/dist/engine/glow.js +0 -59
- package/dist/engine/instance.d.ts +0 -105
- package/dist/engine/instance.js +0 -89
- package/dist/engine/instrumentalAnimations.d.ts +0 -7
- package/dist/engine/instrumentalAnimations.js +0 -59
- package/dist/engine/letterPlans.d.ts +0 -31
- package/dist/engine/letterPlans.js +0 -62
- package/dist/engine/lineAnimationState.d.ts +0 -10
- package/dist/engine/lineAnimationState.js +0 -82
- package/dist/engine/lineAnimations.d.ts +0 -5
- package/dist/engine/lineAnimations.js +0 -63
- package/dist/engine/lineLifecycle.d.ts +0 -12
- package/dist/engine/lineLifecycle.js +0 -92
- package/dist/engine/lineScroll.d.ts +0 -47
- package/dist/engine/lineScroll.js +0 -184
- package/dist/engine/lineScrollStyle.d.ts +0 -32
- package/dist/engine/lineScrollStyle.js +0 -133
- package/dist/engine/liveViews.d.ts +0 -3
- package/dist/engine/liveViews.js +0 -5
- package/dist/engine/motionCurves.d.ts +0 -19
- package/dist/engine/motionCurves.js +0 -85
- package/dist/engine/motionPreference.d.ts +0 -1
- package/dist/engine/motionPreference.js +0 -1
- package/dist/engine/passiveScroll.d.ts +0 -10
- package/dist/engine/passiveScroll.js +0 -93
- package/dist/engine/renderedLines.d.ts +0 -16
- package/dist/engine/renderedLines.js +0 -130
- package/dist/engine/scrollViewport.d.ts +0 -36
- package/dist/engine/scrollViewport.js +0 -104
- package/dist/engine/stageMotion.d.ts +0 -7
- package/dist/engine/stageMotion.js +0 -319
- package/dist/engine/sweepPlans.d.ts +0 -38
- package/dist/engine/sweepPlans.js +0 -66
- package/dist/engine/tick.d.ts +0 -16
- package/dist/engine/tick.js +0 -270
- package/dist/engine/timingLogs.d.ts +0 -7
- package/dist/engine/timingLogs.js +0 -81
- package/dist/engine/userScroll.d.ts +0 -12
- package/dist/engine/userScroll.js +0 -62
- package/dist/engine/wavePool.d.ts +0 -7
- package/dist/engine/wavePool.js +0 -22
- package/dist/engine/wordAnimations.d.ts +0 -5
- package/dist/engine/wordAnimations.js +0 -206
- package/dist/imageHighlights.d.ts +0 -4
- package/dist/imageHighlights.js +0 -34
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -12
- package/dist/inject.d.ts +0 -98
- package/dist/inject.js +0 -591
- package/dist/instrumental.d.ts +0 -11
- package/dist/instrumental.js +0 -121
- package/dist/language.d.ts +0 -9
- package/dist/language.js +0 -47
- package/dist/letters.d.ts +0 -2
- package/dist/letters.js +0 -25
- package/dist/renderer.d.ts +0 -29
- package/dist/renderer.js +0 -412
- package/dist/seek.d.ts +0 -1
- package/dist/seek.js +0 -27
- package/dist/stage.d.ts +0 -58
- package/dist/stage.js +0 -121
- package/dist/types.d.ts +0 -236
- package/dist/types.js +0 -1
- package/dist/view.d.ts +0 -51
- package/dist/view.js +0 -396
package/dist/view.js
DELETED
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
// Builds the view: one container under the mount, one element per lyric line, and the render
|
|
2
|
-
// records the engine animates them through. `inject.ts` builds what goes inside a line; this builds
|
|
3
|
-
// the line and everything around it.
|
|
4
|
-
//
|
|
5
|
-
// A first song builds the whole view. A song given to a view that already holds one is swapped into
|
|
6
|
-
// it line by line: a line that would build the same as one already on screen keeps that element, its
|
|
7
|
-
// record and the animations running against it, and only the lines that differ are built again. The
|
|
8
|
-
// engine is told which records went and which new ones stand where they stood, so nothing it holds
|
|
9
|
-
// keeps pointing into the song before. Anything that changes every line at once (the mount, a theme
|
|
10
|
-
// setting lines are built by, lyrics that lose or gain their timing) builds the whole view again.
|
|
11
|
-
//
|
|
12
|
-
// This is also the one place outside the engine that writes engine view state, because building the
|
|
13
|
-
// lines is where those values are first knowable: the container, the records, the sync type, the
|
|
14
|
-
// size they were measured at, and the scrolls to swallow before the view has settled.
|
|
15
|
-
import { CREDITS_CLASS, CREDITS_NAMES_CLASS, CREDITS_TEXT_CLASS, LINE_CLASS, LYRICS_CLASS, RTL_CLASS, } from "./constants.js";
|
|
16
|
-
import {} from "./engine/instance.js";
|
|
17
|
-
import { elementMotionOffsetPx } from "./engine/elementMotion.js";
|
|
18
|
-
import { replaceRenderedLines } from "./engine/renderedLines.js";
|
|
19
|
-
import { playerTimeForLyricTime } from "./engine/clock.js";
|
|
20
|
-
import { setupLineCullObserver } from "./engine/culling.js";
|
|
21
|
-
import { addSeekHandler, applyDirection, buildLineSyncedParts, createLyricsLine, deriveSyncType, disableRichsync, findNearestAgent, hideCredits, isNearestLyricRtl, moveDecorations, newLineData, } from "./inject.js";
|
|
22
|
-
import { refreshInstrumentalImages } from "./imageHighlights.js";
|
|
23
|
-
import { createInstrumentalElement } from "./instrumental.js";
|
|
24
|
-
import { applyLyricLanguage, resolveLyricLanguages } from "./language.js";
|
|
25
|
-
import { lyricBuildSettingsKey } from "./themeSettings.js";
|
|
26
|
-
import { isGecko } from "./util.js";
|
|
27
|
-
const INITIAL_SKIP_SCROLLS = 2;
|
|
28
|
-
const SKIP_SCROLL_DECAY_MS = 2000;
|
|
29
|
-
function buildInstrumentalLine(doc, lyricElement, lyrics, lineIndex) {
|
|
30
|
-
createInstrumentalElement(doc, lyricElement, lyrics[lineIndex].durationMs);
|
|
31
|
-
lyricElement.dataset.instrumental = "true";
|
|
32
|
-
const agent = findNearestAgent(lyrics, lineIndex);
|
|
33
|
-
if (agent) {
|
|
34
|
-
lyricElement.dataset.agent = agent;
|
|
35
|
-
}
|
|
36
|
-
if (isNearestLyricRtl(lyrics, lineIndex)) {
|
|
37
|
-
lyricElement.classList.add(RTL_CLASS);
|
|
38
|
-
lyricElement.dataset.direction = "rtl";
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function buildSungLine(doc, lyricElement, lyricItem, line) {
|
|
42
|
-
// Rebuilt parts stay local so the provider's lyrics survive injection intact and a second
|
|
43
|
-
// build over the same array produces the same result.
|
|
44
|
-
const parts = lyricItem.parts && lyricItem.parts.length > 0 && !disableRichsync.getBooleanValue()
|
|
45
|
-
? lyricItem.parts
|
|
46
|
-
: buildLineSyncedParts(lyricItem);
|
|
47
|
-
applyDirection(lyricElement, lyricItem.words);
|
|
48
|
-
createLyricsLine(doc, parts, line, lyricElement);
|
|
49
|
-
lyricElement.style.setProperty("--blyrics-duration", lyricItem.durationMs + "ms");
|
|
50
|
-
if (lyricItem.agent) {
|
|
51
|
-
lyricElement.dataset.agent = lyricItem.agent;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function formatSongwriters(names) {
|
|
55
|
-
return names.length > 1 ? `${names.slice(0, -1).join(", ")} & ${names.at(-1)}` : (names[0] ?? "");
|
|
56
|
-
}
|
|
57
|
-
function buildCredits(doc, songwriters) {
|
|
58
|
-
// Not a div: themes style the lines as `.blyrics-container > div`, and none of that is meant for the credits.
|
|
59
|
-
const credits = doc.createElement("p");
|
|
60
|
-
credits.className = CREDITS_CLASS;
|
|
61
|
-
// The smaller size lives on an inner block so the credits keep the lines' em, and with it their inset.
|
|
62
|
-
const text = doc.createElement("span");
|
|
63
|
-
text.className = CREDITS_TEXT_CLASS;
|
|
64
|
-
const names = doc.createElement("span");
|
|
65
|
-
names.className = CREDITS_NAMES_CLASS;
|
|
66
|
-
names.dir = "auto";
|
|
67
|
-
names.textContent = formatSongwriters(songwriters);
|
|
68
|
-
text.appendChild(names);
|
|
69
|
-
credits.appendChild(text);
|
|
70
|
-
return credits;
|
|
71
|
-
}
|
|
72
|
-
/** Everything a line's element is built from, so two lines that answer the same build the same. */
|
|
73
|
-
const lineSignatures = new WeakMap();
|
|
74
|
-
/**
|
|
75
|
-
* A line's words and what a consumer decorates it from. A line rebuilt for its timing alone answers
|
|
76
|
-
* the same, so the translation and romanization hung on the line it replaced still belong to it.
|
|
77
|
-
*/
|
|
78
|
-
const decorationKeys = new WeakMap();
|
|
79
|
-
function decorationKey(item) {
|
|
80
|
-
return JSON.stringify([
|
|
81
|
-
item.words,
|
|
82
|
-
item.translation ?? null,
|
|
83
|
-
item.translations ?? null,
|
|
84
|
-
item.romanization ?? null,
|
|
85
|
-
item.timedRomanization ?? null,
|
|
86
|
-
]);
|
|
87
|
-
}
|
|
88
|
-
/** What the lines on screen were built against: the mount, the settings and whether they carry time. */
|
|
89
|
-
const builtViews = new WeakMap();
|
|
90
|
-
/** A line's own timing as built, so a line whose times a consumer moved since is not kept as it is. */
|
|
91
|
-
const builtTimings = new WeakMap();
|
|
92
|
-
function lineTimings(line, parts = line.parts) {
|
|
93
|
-
return [line.time, line.duration, ...parts.flatMap(part => [part.time, part.duration])];
|
|
94
|
-
}
|
|
95
|
-
function timingsUnchanged(line) {
|
|
96
|
-
const built = builtTimings.get(line);
|
|
97
|
-
if (built === undefined)
|
|
98
|
-
return false;
|
|
99
|
-
const now = lineTimings(line, line.parts.slice(0, (built.length - 2) / 2));
|
|
100
|
-
return now.length === built.length && now.every((value, index) => value === built[index]);
|
|
101
|
-
}
|
|
102
|
-
function buildContext(engine, lyrics, options) {
|
|
103
|
-
return {
|
|
104
|
-
allZero: lyrics.every(item => item.startTimeMs === 0),
|
|
105
|
-
languages: resolveLyricLanguages(lyrics, options.language),
|
|
106
|
-
seek: timeS => engine.host.seek(playerTimeForLyricTime(engine, timeS)),
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
function viewKey(context) {
|
|
110
|
-
return `${context.allZero}|${lyricBuildSettingsKey()}`;
|
|
111
|
-
}
|
|
112
|
-
// The language is left out: it is an attribute rather than anything built, and a swap writes it onto
|
|
113
|
-
// every kept line instead.
|
|
114
|
-
function lineSignature(lyrics, lineIndex) {
|
|
115
|
-
const item = lyrics[lineIndex];
|
|
116
|
-
const parts = item.parts?.map(part => [
|
|
117
|
-
part.startTimeMs,
|
|
118
|
-
part.durationMs,
|
|
119
|
-
part.words,
|
|
120
|
-
part.isBackground === true,
|
|
121
|
-
part.backgroundGroup ?? null,
|
|
122
|
-
part.explicit === true,
|
|
123
|
-
]);
|
|
124
|
-
// An instrumental line takes its side and direction from the lines around it.
|
|
125
|
-
const neighbours = item.isInstrumental
|
|
126
|
-
? [findNearestAgent(lyrics, lineIndex), isNearestLyricRtl(lyrics, lineIndex)]
|
|
127
|
-
: null;
|
|
128
|
-
return JSON.stringify([
|
|
129
|
-
item.startTimeMs,
|
|
130
|
-
item.durationMs,
|
|
131
|
-
item.words,
|
|
132
|
-
item.isInstrumental === true,
|
|
133
|
-
item.agent ?? null,
|
|
134
|
-
parts ?? null,
|
|
135
|
-
neighbours,
|
|
136
|
-
decorationKey(item),
|
|
137
|
-
]);
|
|
138
|
-
}
|
|
139
|
-
function buildLine(doc, lyrics, lineIndex, context) {
|
|
140
|
-
const lyricItem = lyrics[lineIndex];
|
|
141
|
-
const lyricElement = doc.createElement("div");
|
|
142
|
-
const line = newLineData(lyricElement, lyricItem.startTimeMs, lyricItem.durationMs);
|
|
143
|
-
lyricElement.dataset.time = String(line.time);
|
|
144
|
-
lyricElement.dataset.duration = String(line.duration);
|
|
145
|
-
lyricElement.dataset.lineNumber = String(lineIndex);
|
|
146
|
-
lyricElement.classList.add(LINE_CLASS);
|
|
147
|
-
lyricElement.dir = "auto";
|
|
148
|
-
applyLyricLanguage(lyricElement, context.languages[lineIndex]);
|
|
149
|
-
addSeekHandler(context.seek, lyricElement, context.allZero);
|
|
150
|
-
if (lyricItem.isInstrumental) {
|
|
151
|
-
buildInstrumentalLine(doc, lyricElement, lyrics, lineIndex);
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
buildSungLine(doc, lyricElement, lyricItem, line);
|
|
155
|
-
if (!lyricItem.words?.trim())
|
|
156
|
-
lyricElement.dataset.blank = "true";
|
|
157
|
-
}
|
|
158
|
-
lineSignatures.set(line, lineSignature(lyrics, lineIndex));
|
|
159
|
-
decorationKeys.set(line, decorationKey(lyricItem));
|
|
160
|
-
builtTimings.set(line, lineTimings(line));
|
|
161
|
-
return line;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Writes what the container records about the song as a whole, over whatever it recorded about the
|
|
165
|
-
* last one, and puts the credits after the last line.
|
|
166
|
-
*/
|
|
167
|
-
function writeContainerState(engine, container, lyrics, options, creditsBefore = null) {
|
|
168
|
-
const doc = engine.document;
|
|
169
|
-
const syncType = deriveSyncType(lyrics);
|
|
170
|
-
container.dataset.sync = syncType;
|
|
171
|
-
if (engine.layout === "stage") {
|
|
172
|
-
container.dataset.layout = "stage";
|
|
173
|
-
// Sides only when both are sung, so a song with one singer stays centred whichever voice it is.
|
|
174
|
-
const sung = lyrics.filter(item => !item.isInstrumental && item.words?.trim() && item.agent !== "v1000");
|
|
175
|
-
const onRight = (item) => item.agent === "v2" || item.agent === "v3";
|
|
176
|
-
if (sung.some(onRight) && sung.some(item => !onRight(item))) {
|
|
177
|
-
container.dataset.stageDuet = "";
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
delete container.dataset.stageDuet;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
container.dataset.loaderVisible = String(options.loaderVisible);
|
|
184
|
-
if (options.noLyrics) {
|
|
185
|
-
container.dataset.noLyrics = "true";
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
delete container.dataset.noLyrics;
|
|
189
|
-
}
|
|
190
|
-
// Built again on every song, even the same names: consumers hang their own things off the credits
|
|
191
|
-
// once per build, and a kept one would collect them.
|
|
192
|
-
container.querySelector(`:scope > .${CREDITS_CLASS}`)?.remove();
|
|
193
|
-
const songwriters = options.songwriters ?? [];
|
|
194
|
-
if (songwriters.length > 0 && !options.noLyrics && !hideCredits.getBooleanValue()) {
|
|
195
|
-
container.insertBefore(buildCredits(doc, songwriters), creditsBefore);
|
|
196
|
-
}
|
|
197
|
-
engine.syncType = syncType;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Replaces whatever the mount holds with a container built from these lyrics, and hands the render
|
|
201
|
-
* records, the container and its measured size to the engine that animates them.
|
|
202
|
-
*
|
|
203
|
-
* @param engine - Instance that owns the document to build in and the host to seek through
|
|
204
|
-
* @param mount - Element whose children the built container replaces
|
|
205
|
-
* @param lyrics - Lines to render, left untouched
|
|
206
|
-
* @param options - What the container records for CSS to key on
|
|
207
|
-
*/
|
|
208
|
-
export function setLyrics(engine, mount, lyrics, options) {
|
|
209
|
-
const doc = engine.document;
|
|
210
|
-
const container = doc.createElement("div");
|
|
211
|
-
container.className = LYRICS_CLASS;
|
|
212
|
-
if (isGecko())
|
|
213
|
-
container.dataset.browserEngine = "gecko";
|
|
214
|
-
mount.replaceChildren(container);
|
|
215
|
-
const context = buildContext(engine, lyrics, options);
|
|
216
|
-
const lines = [];
|
|
217
|
-
for (const lineIndex of lyrics.keys()) {
|
|
218
|
-
const line = buildLine(doc, lyrics, lineIndex, context);
|
|
219
|
-
lines.push(line);
|
|
220
|
-
container.appendChild(line.lyricElement);
|
|
221
|
-
}
|
|
222
|
-
engine.skipScrolls = INITIAL_SKIP_SCROLLS;
|
|
223
|
-
engine.skipScrollsDecayTimes = Array.from({ length: INITIAL_SKIP_SCROLLS }, () => Date.now() + SKIP_SCROLL_DECAY_MS);
|
|
224
|
-
engine.scrollResumeTime = 0;
|
|
225
|
-
writeContainerState(engine, container, lyrics, options);
|
|
226
|
-
engine.lines = lines;
|
|
227
|
-
engine.lyricsContainer = container;
|
|
228
|
-
builtViews.set(engine, { mount, key: viewKey(context), songKey: options.songKey });
|
|
229
|
-
refreshInstrumentalImages(container, engine.window);
|
|
230
|
-
// Measured last: the container is in the document, filled, and carrying the attributes CSS
|
|
231
|
-
// sizes it by.
|
|
232
|
-
engine.lyricWidth = container.clientWidth;
|
|
233
|
-
engine.lyricHeight = container.clientHeight;
|
|
234
|
-
setupLineCullObserver(engine);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Pairs each new line with an old one that builds the same, keeping their order, so a kept element is
|
|
238
|
-
* never moved: the longest run of matches, found after the ends both songs share are set aside.
|
|
239
|
-
*/
|
|
240
|
-
function matchLines(previous, next) {
|
|
241
|
-
const matches = next.map(() => null);
|
|
242
|
-
let start = 0;
|
|
243
|
-
while (start < previous.length && start < next.length && previous[start] === next[start]) {
|
|
244
|
-
matches[start] = start;
|
|
245
|
-
start++;
|
|
246
|
-
}
|
|
247
|
-
let previousEnd = previous.length;
|
|
248
|
-
let nextEnd = next.length;
|
|
249
|
-
while (previousEnd > start && nextEnd > start && previous[previousEnd - 1] === next[nextEnd - 1]) {
|
|
250
|
-
previousEnd--;
|
|
251
|
-
nextEnd--;
|
|
252
|
-
matches[nextEnd] = previousEnd;
|
|
253
|
-
}
|
|
254
|
-
const rows = previousEnd - start;
|
|
255
|
-
const columns = nextEnd - start;
|
|
256
|
-
// A whole new song is no match for the one before and costs nothing to find out. Past this size
|
|
257
|
-
// the middle is rebuilt rather than searched, which is what every song was before swaps existed.
|
|
258
|
-
if (rows === 0 || columns === 0 || rows * columns > MATCH_SEARCH_LIMIT)
|
|
259
|
-
return matches;
|
|
260
|
-
const lengths = new Uint16Array((rows + 1) * (columns + 1));
|
|
261
|
-
const at = (row, column) => row * (columns + 1) + column;
|
|
262
|
-
for (let row = rows - 1; row >= 0; row--) {
|
|
263
|
-
for (let column = columns - 1; column >= 0; column--) {
|
|
264
|
-
lengths[at(row, column)] =
|
|
265
|
-
previous[start + row] === next[start + column]
|
|
266
|
-
? lengths[at(row + 1, column + 1)] + 1
|
|
267
|
-
: Math.max(lengths[at(row + 1, column)], lengths[at(row, column + 1)]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
let row = 0;
|
|
271
|
-
let column = 0;
|
|
272
|
-
while (row < rows && column < columns) {
|
|
273
|
-
if (previous[start + row] === next[start + column]) {
|
|
274
|
-
matches[start + column] = start + row;
|
|
275
|
-
row++;
|
|
276
|
-
column++;
|
|
277
|
-
}
|
|
278
|
-
else if (lengths[at(row + 1, column)] >= lengths[at(row, column + 1)]) {
|
|
279
|
-
row++;
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
column++;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return matches;
|
|
286
|
-
}
|
|
287
|
-
const MATCH_SEARCH_LIMIT = 250_000;
|
|
288
|
-
/**
|
|
289
|
-
* Takes the first old line sung over the same time as a new one, so a line rebuilt for a retiming or
|
|
290
|
-
* a richer source takes over its predecessor's place, and an unrelated line never inherits a sung state.
|
|
291
|
-
*/
|
|
292
|
-
function takeOverlapping(candidates, line) {
|
|
293
|
-
const end = line.time + line.duration;
|
|
294
|
-
const index = candidates.findIndex(old => {
|
|
295
|
-
const oldEnd = old.time + old.duration;
|
|
296
|
-
return old.time === line.time || (old.time < end && line.time < oldEnd);
|
|
297
|
-
});
|
|
298
|
-
return index === -1 ? undefined : candidates.splice(index, 1)[0];
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Swaps these lyrics into the view already on screen, rebuilding only the lines that differ, and
|
|
302
|
-
* answers where each line was before so the caller can carry the view across the change. Answers
|
|
303
|
-
* null without touching anything when the view cannot be swapped, and the caller builds it whole.
|
|
304
|
-
*
|
|
305
|
-
* Nothing about where the reader is goes back to the start of a song: the scroll, a pause the reader
|
|
306
|
-
* scrolled into, and the scrolls the engine is waiting to swallow are all the song's before.
|
|
307
|
-
*/
|
|
308
|
-
export function swapLyrics(engine, mount, lyrics, options) {
|
|
309
|
-
const container = engine.lyricsContainer;
|
|
310
|
-
const built = builtViews.get(engine);
|
|
311
|
-
if (container === null || built === undefined || built.mount !== mount || container.parentElement !== mount) {
|
|
312
|
-
return null;
|
|
313
|
-
}
|
|
314
|
-
// Another song, or one nobody named, starts from nothing rather than inheriting this one's place.
|
|
315
|
-
if (options.songKey === undefined || options.songKey !== built.songKey)
|
|
316
|
-
return null;
|
|
317
|
-
// Lines a consumer took off the screen without dropping the song are not there to keep.
|
|
318
|
-
const previousLines = engine.lines;
|
|
319
|
-
// An empty song is a fresh start rather than a song to carry anything into.
|
|
320
|
-
if (lyrics.length === 0 || previousLines.length === 0)
|
|
321
|
-
return null;
|
|
322
|
-
if (previousLines.some(line => line.lyricElement.parentNode !== container))
|
|
323
|
-
return null;
|
|
324
|
-
const context = buildContext(engine, lyrics, options);
|
|
325
|
-
if (built.key !== viewKey(context))
|
|
326
|
-
return null;
|
|
327
|
-
const doc = engine.document;
|
|
328
|
-
// A line whose times a consumer moved after it was built is built again, so the move is not applied twice.
|
|
329
|
-
const previousSignatures = previousLines.map(line => timingsUnchanged(line) ? (lineSignatures.get(line) ?? "") : `\u0000moved:${lineSignatures.get(line)}`);
|
|
330
|
-
const nextSignatures = lyrics.map((_, lineIndex) => lineSignature(lyrics, lineIndex));
|
|
331
|
-
// Whatever a consumer put after the lines and the credits, such as a footer, stays after them.
|
|
332
|
-
const children = Array.from(container.childNodes);
|
|
333
|
-
const oldCredits = container.querySelector(`:scope > .${CREDITS_CLASS}`);
|
|
334
|
-
const lastOwned = oldCredits ?? previousLines[previousLines.length - 1].lyricElement;
|
|
335
|
-
const trailing = children[children.indexOf(lastOwned) + 1] ?? null;
|
|
336
|
-
const matches = matchLines(previousSignatures, nextSignatures);
|
|
337
|
-
const kept = new Set();
|
|
338
|
-
const lines = [];
|
|
339
|
-
// A new line stands in for an old one when both sit in the same gap between kept lines, in order,
|
|
340
|
-
// so the engine can hand it what it held for the line it replaced.
|
|
341
|
-
const replaced = new Map();
|
|
342
|
-
const positions = new Map();
|
|
343
|
-
let previousCursor = 0;
|
|
344
|
-
let unmatchedOld = [];
|
|
345
|
-
const collectUnmatchedUntil = (end) => {
|
|
346
|
-
unmatchedOld = previousLines.slice(previousCursor, end);
|
|
347
|
-
previousCursor = end;
|
|
348
|
-
};
|
|
349
|
-
for (const [lineIndex, match] of matches.entries()) {
|
|
350
|
-
if (match !== null) {
|
|
351
|
-
const line = previousLines[match];
|
|
352
|
-
previousCursor = match + 1;
|
|
353
|
-
unmatchedOld = [];
|
|
354
|
-
kept.add(line);
|
|
355
|
-
line.lyricElement.dataset.lineNumber = String(lineIndex);
|
|
356
|
-
applyLyricLanguage(line.lyricElement, context.languages[lineIndex]);
|
|
357
|
-
lines.push(line);
|
|
358
|
-
positions.set(line, { position: line.position, height: line.height, motionOffset: 0 });
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
if (unmatchedOld.length === 0) {
|
|
362
|
-
const nextMatch = matches.slice(lineIndex).find(value => value !== null);
|
|
363
|
-
collectUnmatchedUntil(nextMatch ?? previousLines.length);
|
|
364
|
-
}
|
|
365
|
-
const line = buildLine(doc, lyrics, lineIndex, context);
|
|
366
|
-
const standsFor = takeOverlapping(unmatchedOld, line);
|
|
367
|
-
if (standsFor !== undefined) {
|
|
368
|
-
replaced.set(line, standsFor);
|
|
369
|
-
positions.set(line, {
|
|
370
|
-
position: standsFor.position,
|
|
371
|
-
height: standsFor.height,
|
|
372
|
-
// A kept line finishes its own line scroll; this one starts where the line it replaced was.
|
|
373
|
-
motionOffset: elementMotionOffsetPx(engine, standsFor.lyricElement),
|
|
374
|
-
});
|
|
375
|
-
if (decorationKeys.get(standsFor) === decorationKeys.get(line))
|
|
376
|
-
moveDecorations(standsFor, line);
|
|
377
|
-
}
|
|
378
|
-
lines.push(line);
|
|
379
|
-
}
|
|
380
|
-
const dropped = previousLines.filter(line => !kept.has(line));
|
|
381
|
-
// In order, each new line before whichever kept line follows it, so a kept element never leaves the
|
|
382
|
-
// document and never loses the animations running against it.
|
|
383
|
-
let following = oldCredits ?? trailing;
|
|
384
|
-
for (let index = lines.length - 1; index >= 0; index--) {
|
|
385
|
-
const element = lines[index].lyricElement;
|
|
386
|
-
if (!kept.has(lines[index]))
|
|
387
|
-
container.insertBefore(element, following);
|
|
388
|
-
following = element;
|
|
389
|
-
}
|
|
390
|
-
replaceRenderedLines(engine, lines, dropped, replaced);
|
|
391
|
-
for (const line of dropped)
|
|
392
|
-
line.lyricElement.remove();
|
|
393
|
-
writeContainerState(engine, container, lyrics, options, trailing);
|
|
394
|
-
refreshInstrumentalImages(container, engine.window);
|
|
395
|
-
return positions;
|
|
396
|
-
}
|