@braccato/core 1.21.1 → 1.21.3

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.
@@ -4,7 +4,7 @@ export interface LineCulling {
4
4
  scroller: HTMLElement;
5
5
  contentOffset: number;
6
6
  followReaderScroll: () => void;
7
- startObserver: ResizeObserver | null;
7
+ startObserver: IntersectionObserver | null;
8
8
  ready: boolean;
9
9
  }
10
10
  export declare function setupLineCulling(engine: AnimationEngineInstance): void;
@@ -2,7 +2,8 @@ import { hasUnplacedContextSweeps } from "./contextSweep.js";
2
2
  import { resetLineAnimationState } from "./lineAnimationState.js";
3
3
  import { isLineVisibleDuringScroll, LINE_SCROLL_WILL_CHANGE_VALUE } from "./lineScroll.js";
4
4
  // A skipped line keeps the height it last rendered at, so none is skipped before two rendering updates
5
- // have laid every line out; a resize observation is delivered only after one has.
5
+ // have laid every line out. An intersection observation is delivered as its own task after one has, so starting
6
+ // from it never adds a resize observation to a rendering update that is already delivering them.
6
7
  const RENDERING_UPDATES_BEFORE_SKIPPING = 2;
7
8
  export function setupLineCulling(engine) {
8
9
  stopLineCulling(engine);
@@ -70,7 +71,7 @@ export function updateLineCulling(engine, fromScrollTop, toScrollTop, viewportHe
70
71
  }
71
72
  }
72
73
  function startCullingAfterRenderingUpdates(engine, culling, container, updates) {
73
- const Observer = engine.window.ResizeObserver;
74
+ const Observer = engine.window.IntersectionObserver;
74
75
  if (typeof Observer !== "function")
75
76
  return;
76
77
  const observer = new Observer(() => {
package/dist/inject.js CHANGED
@@ -14,6 +14,7 @@
14
14
  import { releasePart } from "./engine/lineAnimationState.js";
15
15
  import { normalizeLanguage } from "./language.js";
16
16
  import { BACKGROUND_LINE_CLASS, BACKGROUND_LYRIC_CLASS, BIDI_RUN_CLASS, BIDI_SENSITIVE_CLASS, CONTENT_LINE_CLASS, EXPLICIT_WORD_CLASS, HIGHLIGHT_RUN_CLASS, LETTER_CLASS, LINE_MAIN_CLASS, LINE_SYNCED_WORD_CLASS, LONG_WORD_GROUP_CLASS, ROMANIZED_LYRICS_CLASS, RTL_CLASS, SWEEP_CELL_CLASS, SWEEP_GLOW_CLASS, SWEEP_MASK_CLASS, SWEEP_TEXT_CLASS, TRANSLATED_LYRICS_CLASS, WORD_CLASS, WORD_GROUP_CLASS, WORD_HIGHLIGHT_CLASS, WORD_HIGHLIGHT_CONTEXT_CLASS, WORD_HIGHLIGHT_LETTERED_CLASS, WORD_HIGHLIGHT_SWEEP_CLASS, WORD_STATE_ATTR, WORD_STATE_UPCOMING, ZERO_DURATION_ANIMATION_CLASS, } from "./constants.js";
17
+ import { letterClusters } from "./letters.js";
17
18
  import { getSeekTimeFromClick } from "./seek.js";
18
19
  import { isSameText, testJoiningScript, testRtl } from "./text.js";
19
20
  import { registerThemeSetting } from "./themeSettings.js";
@@ -23,7 +24,6 @@ let longWordThreshold = registerThemeSetting("blyrics-long-word-threshold", 1500
23
24
  let longWordWrapThreshold = registerThemeSetting("blyrics-long-word-wrap-threshold", 10, true);
24
25
  let letterWave = registerThemeSetting("blyrics-letter-wave", true, true);
25
26
  export const hideCredits = registerThemeSetting("blyrics-hide-credits", false, true);
26
- const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
27
27
  const LTR_SCRIPT_REGEX = /[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{Script=Han}\p{Script=Hangul}\p{Script=Hiragana}\p{Script=Katakana}]/u;
28
28
  const SPACE_REGEX = /^\s+$/u;
29
29
  export function findNearestAgent(lyrics, fromIndex) {
@@ -194,21 +194,20 @@ function groupTokensByWord(tokens, splitBackgroundGroups) {
194
194
  flush();
195
195
  return groups;
196
196
  }
197
- function appendLongWordBreaks(doc, span, text, threshold) {
198
- if (text.length <= threshold) {
199
- span.textContent = text;
197
+ function appendLongWordBreaks(doc, span, clusters, threshold) {
198
+ if (clusters.length <= threshold) {
199
+ span.textContent = clusters.join("");
200
200
  return false;
201
201
  }
202
- for (let i = 0; i < text.length; i += threshold) {
203
- span.appendChild(doc.createTextNode(text.slice(i, i + threshold)));
204
- if (i + threshold < text.length) {
202
+ for (let i = 0; i < clusters.length; i += threshold) {
203
+ span.appendChild(doc.createTextNode(clusters.slice(i, i + threshold).join("")));
204
+ if (i + threshold < clusters.length) {
205
205
  span.appendChild(doc.createElement("wbr"));
206
206
  }
207
207
  }
208
208
  return true;
209
209
  }
210
- function appendLetters(doc, wordElement, text) {
211
- const chars = [...graphemeSegmenter.segment(text)].map(segment => segment.segment);
210
+ function appendLetters(doc, wordElement, chars) {
212
211
  wordElement.style.setProperty("--letters", String(chars.length));
213
212
  return chars.map(char => {
214
213
  const letter = doc.createElement("span");
@@ -218,13 +217,13 @@ function appendLetters(doc, wordElement, text) {
218
217
  return letter;
219
218
  });
220
219
  }
221
- function fillWordText(doc, host, text, wrapThreshold, perLetter, preserveJoining) {
220
+ function fillWordText(doc, host, clusters, wrapThreshold, perLetter, preserveJoining) {
222
221
  if (perLetter)
223
- return appendLetters(doc, host, text);
222
+ return appendLetters(doc, host, clusters);
224
223
  if (preserveJoining)
225
- host.textContent = text;
224
+ host.textContent = clusters.join("");
226
225
  else
227
- appendLongWordBreaks(doc, host, text, wrapThreshold);
226
+ appendLongWordBreaks(doc, host, clusters, wrapThreshold);
228
227
  return undefined;
229
228
  }
230
229
  const SWEEP_GLOW_RADII = ["wide", "tight"];
@@ -264,6 +263,7 @@ function createTimedWordSpan(doc, part, wrapThreshold, perLetter, preserveJoinin
264
263
  let sweep;
265
264
  let letters;
266
265
  let highlightLetters;
266
+ const clusters = perLetter || (!preserveJoining && part.words.length > wrapThreshold) ? letterClusters(part.words) : [part.words];
267
267
  for (const wordElement of [span, highlight]) {
268
268
  wordElement.classList.add(WORD_CLASS);
269
269
  wordElement.dir = "auto";
@@ -285,12 +285,12 @@ function createTimedWordSpan(doc, part, wrapThreshold, perLetter, preserveJoinin
285
285
  wordElement.classList.add(WORD_HIGHLIGHT_LETTERED_CLASS);
286
286
  }
287
287
  if (wordElement === highlight && atomic) {
288
- const cell = createSweepCell(doc, highlight, into => fillWordText(doc, into, part.words, wrapThreshold, perLetter, preserveJoining));
288
+ const cell = createSweepCell(doc, highlight, into => fillWordText(doc, into, clusters, wrapThreshold, perLetter, preserveJoining));
289
289
  sweep = [cell];
290
290
  highlightLetters = cell.letters;
291
291
  }
292
292
  else {
293
- const collected = fillWordText(doc, wordElement, part.words, wrapThreshold, perLetter, preserveJoining);
293
+ const collected = fillWordText(doc, wordElement, clusters, wrapThreshold, perLetter, preserveJoining);
294
294
  if (wordElement === span)
295
295
  letters = collected;
296
296
  else
@@ -316,9 +316,9 @@ function createWordGroup(doc, group, lineData, wordsFlowInline) {
316
316
  const mixedDirection = testRtl(group.text) && [...group.text].some(char => /[\p{Letter}\p{Number}]/u.test(char) && !testRtl(char));
317
317
  const preserveJoining = testJoiningScript(group.text);
318
318
  const perLetter = letterWave.getBooleanValue() && !preserveJoining && !mixedDirection;
319
+ const isLongGroup = group.text.length > wrapThreshold * 2 && letterClusters(group.text).length > wrapThreshold * 2;
319
320
  const lyricGroup = doc.createElement("span");
320
321
  const highlightGroup = doc.createElement("span");
321
- const isLongGroup = group.text.length > wrapThreshold * 2;
322
322
  // A word that is its own inline-block box can hold the sweep's block layers. Long groups and
323
323
  // bidi-sensitive rows lay their words out inline, where a word can break across lines.
324
324
  const atomic = !wordsFlowInline && !isLongGroup;
@@ -0,0 +1,2 @@
1
+ export declare function letterClusters(text: string): string[];
2
+ export declare function joinLetterSegments(segments: Iterable<string>): string[];
@@ -0,0 +1,25 @@
1
+ const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
2
+ // Canonical_Combining_Class=Virama minus the ones that stay visible; Sinhala al-lakuna joins only beside a ZWJ.
3
+ const CONJUNCT_VIRAMA_REGEX = /(?:[\u{94D}\u{9CD}\u{A4D}\u{ACD}\u{B4D}\u{C4D}\u{CCD}\u{D4D}\u{1039}\u{17D2}\u{1A60}\u{1B44}\u{1BAB}\u{2D7F}\u{A9C0}\u{AAF6}\u{10A3F}\u{11046}\u{1107F}\u{110B9}\u{11133}\u{111C0}\u{11235}\u{1134D}\u{113D0}\u{11442}\u{114C2}\u{115BF}\u{1163F}\u{116B6}\u{11839}\u{1193E}\u{119E0}\u{11A47}\u{11A99}\u{11C3F}\u{11D45}\u{11D97}\u{11F42}]\u200D?|\u200D\u0DCA|\u0DCA\u200D)$/u;
4
+ const LETTER_START_REGEX = /^\p{L}/u;
5
+ const MARK_START_REGEX = /^\p{M}/u;
6
+ const joinsTamilLigature = (previous, next) => (previous.endsWith("க்") && next.startsWith("ஷ")) || (previous.endsWith("ஸ்") && next.startsWith("ரீ"));
7
+ export function letterClusters(text) {
8
+ return joinLetterSegments(Array.from(graphemeSegmenter.segment(text), ({ segment }) => segment));
9
+ }
10
+ export function joinLetterSegments(segments) {
11
+ const clusters = [];
12
+ let previous = "";
13
+ for (const segment of segments) {
14
+ const joins = clusters.length > 0 &&
15
+ (MARK_START_REGEX.test(segment) ||
16
+ (LETTER_START_REGEX.test(segment) &&
17
+ (CONJUNCT_VIRAMA_REGEX.test(previous) || joinsTamilLigature(previous, segment))));
18
+ if (joins)
19
+ clusters[clusters.length - 1] += segment;
20
+ else
21
+ clusters.push(segment);
22
+ previous = segment;
23
+ }
24
+ return clusters;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@braccato/core",
3
- "version": "1.21.1",
3
+ "version": "1.21.3",
4
4
  "description": "Synchronized lyrics renderer with word-by-word animations",
5
5
  "type": "module",
6
6
  "license": "MIT",