@barmark/score 0.2.0 → 0.4.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/CHANGELOG.md +83 -1
- package/README.md +124 -21
- package/dist/download.d.ts +43 -0
- package/dist/download.js +118 -0
- package/dist/element.d.ts +2 -1
- package/dist/element.js +129 -16
- package/dist/engine.d.ts +45 -9
- package/dist/engine.js +67 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/loading.d.ts +17 -0
- package/dist/loading.js +78 -0
- package/dist/shell.js +2 -1
- package/dist/verovio.d.ts +8 -0
- package/dist/verovio.js +8 -0
- package/dist/worker.d.ts +49 -13
- package/dist/worker.js +96 -24
- package/package.json +5 -5
package/dist/element.js
CHANGED
|
@@ -3,12 +3,15 @@
|
|
|
3
3
|
* Grid / Score switch. Its chart comes from the `text` property, a `src` URL or
|
|
4
4
|
* a child `<script type="text/barmark">`, as `<barmark-chart>`'s does. A shadow
|
|
5
5
|
* root drawn by `renderScoreHtml` is reused. The score engine (Verovio, in a
|
|
6
|
-
* worker) loads on the first switch to the score,
|
|
6
|
+
* worker) loads on the first switch to the score, or earlier as `preload`
|
|
7
|
+
* asks, showing its progress while it loads. Importing
|
|
7
8
|
* this module registers the element (once) and the font (once per document).
|
|
8
9
|
*/
|
|
9
10
|
import { FONT_FAMILY, fontFaceCss, renderChartUnwrapped, } from "@barmark/render";
|
|
10
11
|
import { chartSource, inlineChartText, optionsFromAttributes, } from "@barmark/render/element";
|
|
11
12
|
import { parseChart } from "barmark";
|
|
13
|
+
import { isIntegrity } from "./download.js";
|
|
14
|
+
import { showStatus, UNMEASURED } from "./loading.js";
|
|
12
15
|
import { isLeipzigFace, scorePane } from "./markup.js";
|
|
13
16
|
import { decodeCarried, gridError, scoreShell } from "./shell.js";
|
|
14
17
|
import { viewOf } from "./switch.js";
|
|
@@ -18,18 +21,22 @@ export function setEngineForTests(factory) {
|
|
|
18
21
|
testEngine = factory;
|
|
19
22
|
}
|
|
20
23
|
let loading = null;
|
|
24
|
+
/** The page's engine once imported. */
|
|
25
|
+
let loaded = null;
|
|
21
26
|
/** The page's engine, imported on first use; a failed import is tried again next time. */
|
|
22
27
|
function loadEngine() {
|
|
23
28
|
const factory = testEngine;
|
|
24
29
|
if (factory)
|
|
25
30
|
return Promise.resolve().then(factory);
|
|
26
|
-
loading ??= import("./engine.js").then((m) =>
|
|
31
|
+
loading ??= import("./engine.js").then((m) => {
|
|
32
|
+
loaded = m.engine();
|
|
33
|
+
return loaded;
|
|
34
|
+
}, (e) => {
|
|
27
35
|
loading = null;
|
|
28
36
|
throw e;
|
|
29
37
|
});
|
|
30
38
|
return loading;
|
|
31
39
|
}
|
|
32
|
-
const LOADING = "Loading score…";
|
|
33
40
|
/** The score's accessible name when the chart has no title. */
|
|
34
41
|
const UNTITLED = "Score";
|
|
35
42
|
const NOTE = "The score couldn't be drawn";
|
|
@@ -77,13 +84,24 @@ function takeEarlyText(el) {
|
|
|
77
84
|
/** The grid's attributes, as `<barmark-chart>` observes them, then the score's own. */
|
|
78
85
|
const OBSERVED = [
|
|
79
86
|
...["src", "theme", "bar-numbers", "rehearsal-letters", "numerals", "wrap", "show-part"],
|
|
80
|
-
|
|
87
|
+
"columns",
|
|
88
|
+
...["view", "verovio-url", "verovio-integrity", "preload"],
|
|
81
89
|
];
|
|
90
|
+
/** With no `requestIdleCallback`, how long after connecting `preload="idle"` starts the load. */
|
|
91
|
+
const IDLE_FALLBACK_MS = 500;
|
|
92
|
+
/** Whether the reader asked to save data, or is on a 2G line: `preload="idle"` waits for intent there. */
|
|
93
|
+
function sparing() {
|
|
94
|
+
const c = globalThis.navigator?.connection;
|
|
95
|
+
return c?.saveData === true || c?.effectiveType === "2g" || c?.effectiveType === "slow-2g";
|
|
96
|
+
}
|
|
97
|
+
/** The pointer, focus or touch on the Score radio that says a reader may want the score. */
|
|
98
|
+
const INTENT = ["pointerenter", "focus", "touchstart"];
|
|
82
99
|
const messageOf = (e) => (e instanceof Error ? e.message : String(e));
|
|
83
100
|
/** The radio a click or key landed on. */
|
|
84
101
|
const radioOf = (target) => target?.closest?.("[data-view]") ?? null;
|
|
85
102
|
const same = (a, b) => a !== null && a.text === b.text && Math.abs(a.width - b.width) <= SLACK;
|
|
86
103
|
const URL_REASON = "verovio-url must be an http(s) URL";
|
|
104
|
+
const INTEGRITY_REASON = "verovio-integrity must be one sha256-, sha384- or sha512- digest";
|
|
87
105
|
/** `verovio-url` resolved against the page: undefined when absent, null unless http(s). */
|
|
88
106
|
function verovioHref(value, base) {
|
|
89
107
|
if (value === null)
|
|
@@ -122,6 +140,12 @@ function defineElement() {
|
|
|
122
140
|
/** The engraving and theme the pane holds, so an unchanged one is not written again. */
|
|
123
141
|
#revealed = null;
|
|
124
142
|
#asked = null;
|
|
143
|
+
/** What the engine last said about the request being waited on. */
|
|
144
|
+
#status = UNMEASURED;
|
|
145
|
+
/** Whether this element has asked the engine to preload. */
|
|
146
|
+
#preloaded = false;
|
|
147
|
+
/** The pending idle preload's cancel, if one is armed. */
|
|
148
|
+
#idle = null;
|
|
125
149
|
connectedCallback() {
|
|
126
150
|
const early = takeEarlyText(this);
|
|
127
151
|
if (early)
|
|
@@ -133,8 +157,11 @@ function defineElement() {
|
|
|
133
157
|
this.#resize.observe(this);
|
|
134
158
|
}
|
|
135
159
|
this.#schedule();
|
|
160
|
+
this.#armIdle();
|
|
136
161
|
}
|
|
137
162
|
disconnectedCallback() {
|
|
163
|
+
this.#idle?.();
|
|
164
|
+
this.#idle = null;
|
|
138
165
|
this.#observer.disconnect();
|
|
139
166
|
this.#resize?.disconnect();
|
|
140
167
|
this.#resize = null;
|
|
@@ -144,8 +171,10 @@ function defineElement() {
|
|
|
144
171
|
if (!this.isConnected || old === value)
|
|
145
172
|
return;
|
|
146
173
|
// A new URL may be the fix: the Score radio works again, as after a new chart.
|
|
147
|
-
if (name === "verovio-url")
|
|
174
|
+
if (name === "verovio-url" || name === "verovio-integrity")
|
|
148
175
|
this.#clearFailure();
|
|
176
|
+
else if (name === "preload")
|
|
177
|
+
this.#preloadChanged();
|
|
149
178
|
else if (name === "view")
|
|
150
179
|
this.#applyView();
|
|
151
180
|
else if (name === "src")
|
|
@@ -260,6 +289,73 @@ function defineElement() {
|
|
|
260
289
|
this.#wired = switcher;
|
|
261
290
|
switcher.addEventListener("click", (e) => this.#pick(radioOf(e.target)));
|
|
262
291
|
switcher.addEventListener("keydown", (e) => this.#key(e));
|
|
292
|
+
const score = switcher.querySelector('[data-view="score"]');
|
|
293
|
+
for (const type of INTENT)
|
|
294
|
+
score?.addEventListener(type, () => this.#intent(), { passive: true });
|
|
295
|
+
}
|
|
296
|
+
/** `preload`: "none" unless "intent" or "idle"; "idle" is "intent" on a sparing connection. */
|
|
297
|
+
#preloadMode() {
|
|
298
|
+
const v = this.getAttribute("preload");
|
|
299
|
+
if (v === "idle")
|
|
300
|
+
return sparing() ? "intent" : "idle";
|
|
301
|
+
return v === "intent" ? "intent" : "none";
|
|
302
|
+
}
|
|
303
|
+
/** A reader reaching for Score: any preload but "none" starts now. */
|
|
304
|
+
#intent() {
|
|
305
|
+
if (this.#preloadMode() !== "none")
|
|
306
|
+
this.#preload();
|
|
307
|
+
}
|
|
308
|
+
/** A new `preload`: an idle load armed for "idle" is cancelled unless it is still "idle". */
|
|
309
|
+
#preloadChanged() {
|
|
310
|
+
if (this.#preloadMode() !== "idle") {
|
|
311
|
+
this.#idle?.();
|
|
312
|
+
this.#idle = null;
|
|
313
|
+
}
|
|
314
|
+
this.#armIdle();
|
|
315
|
+
}
|
|
316
|
+
/** `preload="idle"`: start the load when the page is idle (or shortly, without requestIdleCallback). */
|
|
317
|
+
#armIdle() {
|
|
318
|
+
if (this.#idle || this.#preloaded || this.#preloadMode() !== "idle")
|
|
319
|
+
return;
|
|
320
|
+
const run = () => {
|
|
321
|
+
this.#idle = null;
|
|
322
|
+
this.#preload();
|
|
323
|
+
};
|
|
324
|
+
const g = globalThis;
|
|
325
|
+
if (g.requestIdleCallback) {
|
|
326
|
+
const id = g.requestIdleCallback(run);
|
|
327
|
+
this.#idle = () => g.cancelIdleCallback?.(id);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
const t = setTimeout(run, IDLE_FALLBACK_MS);
|
|
331
|
+
this.#idle = () => clearTimeout(t);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** Loads the engine with no job, once per element; the engine shares the one load. */
|
|
335
|
+
#preload() {
|
|
336
|
+
if (this.#preloaded)
|
|
337
|
+
return;
|
|
338
|
+
const source = this.#source();
|
|
339
|
+
if (source === null)
|
|
340
|
+
return;
|
|
341
|
+
this.#preloaded = true;
|
|
342
|
+
loadEngine().then((e) => e.preload(source.url, source.integrity), () => { });
|
|
343
|
+
}
|
|
344
|
+
/** Where Verovio comes from, or null (and why) when `verovio-url` or `verovio-integrity` is refused. */
|
|
345
|
+
#verovio() {
|
|
346
|
+
const url = verovioHref(this.getAttribute("verovio-url"), this.ownerDocument.baseURI);
|
|
347
|
+
if (url === null)
|
|
348
|
+
return { refused: URL_REASON };
|
|
349
|
+
const integrity = url === undefined ? null : this.getAttribute("verovio-integrity");
|
|
350
|
+
if (integrity === null)
|
|
351
|
+
return { url, integrity: undefined };
|
|
352
|
+
return isIntegrity(integrity)
|
|
353
|
+
? { url, integrity: integrity.trim() }
|
|
354
|
+
: { refused: INTEGRITY_REASON };
|
|
355
|
+
}
|
|
356
|
+
#source() {
|
|
357
|
+
const v = this.#verovio();
|
|
358
|
+
return "refused" in v ? null : v;
|
|
263
359
|
}
|
|
264
360
|
#pick(radio) {
|
|
265
361
|
if (!radio || radio.getAttribute("aria-disabled") === "true")
|
|
@@ -330,19 +426,32 @@ function defineElement() {
|
|
|
330
426
|
return this.#reveal(this.#engraved);
|
|
331
427
|
if (same(this.#asked, want))
|
|
332
428
|
return this.#busy();
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
335
|
-
return this.#fail(
|
|
336
|
-
void this.#engrave(want,
|
|
429
|
+
const verovio = this.#verovio();
|
|
430
|
+
if ("refused" in verovio)
|
|
431
|
+
return this.#fail(verovio.refused);
|
|
432
|
+
void this.#engrave(want, verovio);
|
|
337
433
|
}
|
|
338
|
-
/** Engraves `want`; only the newest request this element made is shown. */
|
|
339
|
-
async #engrave(want,
|
|
340
|
-
this.#busy();
|
|
434
|
+
/** Engraves `want`; only the newest request this element made is shown, with its status while it waits. */
|
|
435
|
+
async #engrave(want, verovio) {
|
|
341
436
|
this.#asked = want;
|
|
342
437
|
const ask = ++this.#engraves;
|
|
438
|
+
this.#status = UNMEASURED;
|
|
439
|
+
const onStatus = (s) => {
|
|
440
|
+
if (ask !== this.#engraves)
|
|
441
|
+
return;
|
|
442
|
+
this.#status = s;
|
|
443
|
+
const { pane } = this.#parts();
|
|
444
|
+
if (pane && this.#revealed === null)
|
|
445
|
+
showStatus(pane, s);
|
|
446
|
+
};
|
|
447
|
+
const call = verovio.integrity === undefined ? { onStatus } : { onStatus, integrity: verovio.integrity };
|
|
448
|
+
const run = (e) => e.engrave(this, want.text, want.width, verovio.url, call);
|
|
449
|
+
// A loaded engine is asked at once, so its status (not a provisional one) is the first drawn.
|
|
450
|
+
const request = loaded ? run(loaded) : loadEngine().then(run);
|
|
451
|
+
this.#busy();
|
|
343
452
|
let reply;
|
|
344
453
|
try {
|
|
345
|
-
reply = await
|
|
454
|
+
reply = await request;
|
|
346
455
|
}
|
|
347
456
|
catch (e) {
|
|
348
457
|
reply = { id: 0, ok: false, reason: messageOf(e) };
|
|
@@ -357,13 +466,13 @@ function defineElement() {
|
|
|
357
466
|
if (this.view === "score")
|
|
358
467
|
this.#reveal(this.#engraved);
|
|
359
468
|
}
|
|
360
|
-
/** A score on its way: a shown one stays (busy) until the new one lands, else
|
|
469
|
+
/** A score on its way: a shown one stays (busy) until the new one lands, else its loading status. */
|
|
361
470
|
#busy() {
|
|
362
471
|
const { grid, pane } = this.#parts();
|
|
363
472
|
if (!pane)
|
|
364
473
|
return;
|
|
365
474
|
if (this.#revealed === null)
|
|
366
|
-
pane
|
|
475
|
+
showStatus(pane, this.#status);
|
|
367
476
|
pane.setAttribute("aria-busy", "true");
|
|
368
477
|
pane.toggleAttribute("hidden", false);
|
|
369
478
|
grid?.toggleAttribute("hidden", true);
|
|
@@ -387,7 +496,10 @@ function defineElement() {
|
|
|
387
496
|
pane.toggleAttribute("hidden", false);
|
|
388
497
|
grid?.toggleAttribute("hidden", true);
|
|
389
498
|
}
|
|
390
|
-
/**
|
|
499
|
+
/**
|
|
500
|
+
* The score could not be drawn: the grid again, a note by the switch, Score
|
|
501
|
+
* disabled, and a `scoreerror` event saying why, once per failure.
|
|
502
|
+
*/
|
|
391
503
|
#fail(reason) {
|
|
392
504
|
console.warn(`<barmark-score>: the score couldn't be drawn: ${reason}`);
|
|
393
505
|
this.#failed = true;
|
|
@@ -411,6 +523,7 @@ function defineElement() {
|
|
|
411
523
|
this.#showGrid();
|
|
412
524
|
if (this.view === "score")
|
|
413
525
|
this.view = "grid";
|
|
526
|
+
this.dispatchEvent(new CustomEvent("scoreerror", { detail: { reason }, bubbles: true, composed: true }));
|
|
414
527
|
}
|
|
415
528
|
#clearFailure() {
|
|
416
529
|
if (!this.#failed)
|
package/dist/engine.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { Download } from "./download.js";
|
|
2
|
+
/**
|
|
3
|
+
* Where a worker loads Verovio from: no `verovioUrl`, the package's own;
|
|
4
|
+
* with `verovioIntegrity`, a copy that must have that digest (or the
|
|
5
|
+
* package's own is loaded in its place).
|
|
6
|
+
*/
|
|
7
|
+
export type VerovioSource = {
|
|
8
|
+
verovioUrl?: string;
|
|
9
|
+
verovioIntegrity?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* What a page asks the worker for: one chart, laid out for a width; or,
|
|
13
|
+
* with `preload`, only to load Verovio (`text` and `width` unused).
|
|
14
|
+
*/
|
|
15
|
+
export type Request = VerovioSource & {
|
|
3
16
|
id: number;
|
|
4
17
|
text: string;
|
|
5
18
|
width: number;
|
|
6
|
-
|
|
19
|
+
preload?: true;
|
|
7
20
|
};
|
|
8
21
|
/** What the worker answers. No `xml` or `diagnostics` cross the boundary. */
|
|
9
22
|
export type Reply = {
|
|
@@ -25,12 +38,29 @@ export type EngraveReply = Reply;
|
|
|
25
38
|
*/
|
|
26
39
|
export type Ready = {
|
|
27
40
|
id: number;
|
|
28
|
-
loaded:
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
loaded: VerovioSource;
|
|
42
|
+
};
|
|
43
|
+
/** How a worker's Verovio load is going: downloading (as far as it can be measured), then preparing. */
|
|
44
|
+
export type LoadStatus = Download | {
|
|
45
|
+
phase: "prepare";
|
|
46
|
+
};
|
|
47
|
+
/** What the worker says while it loads Verovio for job `id`. */
|
|
48
|
+
export type Progress = {
|
|
49
|
+
id: number;
|
|
50
|
+
status: LoadStatus;
|
|
51
|
+
};
|
|
52
|
+
/** Every message a worker sends: a job's `Progress` while it loads, its `Ready`, then its `Reply`. */
|
|
53
|
+
export type WorkerMessage = Reply | Ready | Progress;
|
|
54
|
+
/** What a caller hears while it waits: the engine's load, then its engraving. */
|
|
55
|
+
export type EngineStatus = LoadStatus | {
|
|
56
|
+
phase: "engrave";
|
|
57
|
+
};
|
|
58
|
+
export type EngraveCall = {
|
|
59
|
+
/** The SRI digest (`sha384-…`) the `verovioUrl` copy must have; on a mismatch the package's own loads. */
|
|
60
|
+
integrity?: string;
|
|
61
|
+
/** Hears the engine's status now, and as it changes, until the request settles. */
|
|
62
|
+
onStatus?: (status: EngineStatus) => void;
|
|
31
63
|
};
|
|
32
|
-
/** Every message a worker sends: a job's `Ready`, then its `Reply`. */
|
|
33
|
-
export type WorkerMessage = Reply | Ready;
|
|
34
64
|
/**
|
|
35
65
|
* The part of a `Worker` the engine uses, so a test can pass a fake. It is
|
|
36
66
|
* driven by the `onmessage` and `onerror` properties (a real `Worker`
|
|
@@ -49,7 +79,13 @@ export type Engine = {
|
|
|
49
79
|
* with "The score engine failed" if the worker crashes on it, or its
|
|
50
80
|
* Verovio load or its engraving runs past its deadline.
|
|
51
81
|
*/
|
|
52
|
-
engrave(key: object, text: string, width: number, verovioUrl?: string): Promise<EngraveReply | null>;
|
|
82
|
+
engrave(key: object, text: string, width: number, verovioUrl?: string, call?: EngraveCall): Promise<EngraveReply | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Starts loading Verovio (from `verovioUrl`, checked against `integrity`
|
|
85
|
+
* when given) with no job, so a first score is quicker. Does nothing if
|
|
86
|
+
* a worker has Verovio loaded or is busy: there is one load to share.
|
|
87
|
+
*/
|
|
88
|
+
preload(verovioUrl?: string, integrity?: string): void;
|
|
53
89
|
};
|
|
54
90
|
/** How long one job may engrave, from its worker's Verovio being ready, before the worker is cut off and the request rejects. */
|
|
55
91
|
export declare const JOB_DEADLINE_MS = 20000;
|
package/dist/engine.js
CHANGED
|
@@ -11,7 +11,15 @@ export const LOAD_DEADLINE_MS = 60_000;
|
|
|
11
11
|
*/
|
|
12
12
|
export const SUPERSEDED_GRACE_MS = 3_000;
|
|
13
13
|
const FAILED = "The score engine failed";
|
|
14
|
-
const isReady = (m) =>
|
|
14
|
+
const isReady = (m) => "loaded" in m;
|
|
15
|
+
const isProgress = (m) => "status" in m;
|
|
16
|
+
const UNMEASURED = { phase: "download", fraction: null, bytes: null };
|
|
17
|
+
const ENGRAVING = { phase: "engrave" };
|
|
18
|
+
/** `source` without its absent fields (so a request carries no `verovioUrl: undefined`). */
|
|
19
|
+
const present = ({ verovioUrl, verovioIntegrity, }) => ({
|
|
20
|
+
...(verovioUrl === undefined ? {} : { verovioUrl }),
|
|
21
|
+
...(verovioUrl === undefined || verovioIntegrity === undefined ? {} : { verovioIntegrity }),
|
|
22
|
+
});
|
|
15
23
|
const clear = (job) => {
|
|
16
24
|
for (const t of job.timers)
|
|
17
25
|
clearTimeout(t);
|
|
@@ -31,10 +39,16 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
31
39
|
let worker = null;
|
|
32
40
|
let running = null;
|
|
33
41
|
let loaded;
|
|
42
|
+
/** Whether the current worker has its Verovio loaded. */
|
|
43
|
+
let warm = false;
|
|
44
|
+
/** The engine's status, as a new caller first hears it. */
|
|
45
|
+
let status = UNMEASURED;
|
|
34
46
|
let nextId = 0;
|
|
35
47
|
const waiting = [];
|
|
36
48
|
const pending = new Map();
|
|
37
49
|
const newest = new WeakMap();
|
|
50
|
+
/** Preload jobs: no one waits on them, and they end at `Ready`. */
|
|
51
|
+
const preloads = new Set();
|
|
38
52
|
const settle = (id) => {
|
|
39
53
|
const p = pending.get(id);
|
|
40
54
|
pending.delete(id);
|
|
@@ -48,15 +62,32 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
48
62
|
running = null;
|
|
49
63
|
const w = worker;
|
|
50
64
|
worker = null;
|
|
65
|
+
warm = false;
|
|
66
|
+
preloads.delete(id);
|
|
51
67
|
w?.terminate();
|
|
52
68
|
if (reason !== null)
|
|
53
69
|
settle(id)?.reject(new Error(reason));
|
|
70
|
+
// A fresh worker loads Verovio again: the callers still waiting hear it from the start.
|
|
71
|
+
tell(UNMEASURED);
|
|
54
72
|
next();
|
|
55
73
|
};
|
|
56
74
|
const overdue = (id, what) => () => cut(id, `${FAILED}: ${what} took too long`);
|
|
57
|
-
/**
|
|
75
|
+
/** Tells every waiting caller the engine's new status. */
|
|
76
|
+
const tell = (now) => {
|
|
77
|
+
status = now;
|
|
78
|
+
for (const p of pending.values())
|
|
79
|
+
p.onStatus?.(now);
|
|
80
|
+
};
|
|
81
|
+
/** Verovio is ready: the job's engraving deadline and its grace start now; a preload is done. */
|
|
58
82
|
const onReady = (job, ready) => {
|
|
59
83
|
loaded ??= ready.loaded;
|
|
84
|
+
warm = true;
|
|
85
|
+
// A preload's Ready engraves nothing: the job behind it hears "engrave" at its own Ready.
|
|
86
|
+
if (preloads.has(job.id)) {
|
|
87
|
+
finish(job);
|
|
88
|
+
return next();
|
|
89
|
+
}
|
|
90
|
+
tell(ENGRAVING);
|
|
60
91
|
clear(job);
|
|
61
92
|
job.timers = [
|
|
62
93
|
setTimeout(overdue(job.id, "engraving"), deadline),
|
|
@@ -67,9 +98,14 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
67
98
|
}, grace),
|
|
68
99
|
];
|
|
69
100
|
};
|
|
70
|
-
|
|
101
|
+
/** The worker is done with `job`. */
|
|
102
|
+
const finish = (job) => {
|
|
71
103
|
clear(job);
|
|
72
104
|
running = null;
|
|
105
|
+
preloads.delete(job.id);
|
|
106
|
+
};
|
|
107
|
+
const onReply = (job, reply) => {
|
|
108
|
+
finish(job);
|
|
73
109
|
settle(reply.id)?.resolve(reply);
|
|
74
110
|
next();
|
|
75
111
|
};
|
|
@@ -81,7 +117,9 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
81
117
|
const job = running;
|
|
82
118
|
if (worker !== w || job?.id !== data.id)
|
|
83
119
|
return;
|
|
84
|
-
if (
|
|
120
|
+
if (isProgress(data))
|
|
121
|
+
tell(data.status);
|
|
122
|
+
else if (isReady(data))
|
|
85
123
|
onReady(job, data);
|
|
86
124
|
else
|
|
87
125
|
onReply(job, data);
|
|
@@ -98,12 +136,11 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
98
136
|
const request = running ? undefined : waiting.shift();
|
|
99
137
|
if (!request)
|
|
100
138
|
return;
|
|
101
|
-
const { verovioUrl, ...rest } = request;
|
|
102
|
-
const url = loaded ? loaded.verovioUrl : verovioUrl;
|
|
139
|
+
const { verovioUrl, verovioIntegrity, ...rest } = request;
|
|
103
140
|
const id = rest.id;
|
|
104
141
|
running = { id, superseded: false, graceOver: false, timers: [] };
|
|
105
142
|
running.timers = [setTimeout(overdue(id, "loading Verovio"), loadDeadline)];
|
|
106
|
-
start().postMessage(
|
|
143
|
+
start().postMessage({ ...rest, ...present(loaded ?? { verovioUrl, verovioIntegrity }) });
|
|
107
144
|
};
|
|
108
145
|
/** No one waits for `id` any more: drop it if it waits its turn, cut it off past its grace if it runs. */
|
|
109
146
|
const supersede = (id) => {
|
|
@@ -118,18 +155,38 @@ export function createEngine(makeWorker, options = {}) {
|
|
|
118
155
|
cut(id, null);
|
|
119
156
|
};
|
|
120
157
|
return {
|
|
121
|
-
engrave(key, text, width, verovioUrl) {
|
|
158
|
+
engrave(key, text, width, verovioUrl, call = {}) {
|
|
122
159
|
const id = ++nextId;
|
|
123
160
|
const before = newest.get(key);
|
|
124
161
|
newest.set(key, id);
|
|
125
162
|
return new Promise((resolve, reject) => {
|
|
126
|
-
pending.set(id, { resolve, reject });
|
|
163
|
+
pending.set(id, { resolve, reject, onStatus: call.onStatus });
|
|
164
|
+
call.onStatus?.(warm ? ENGRAVING : status);
|
|
127
165
|
if (before !== undefined)
|
|
128
166
|
supersede(before);
|
|
129
|
-
waiting.push(
|
|
167
|
+
waiting.push({
|
|
168
|
+
id,
|
|
169
|
+
text,
|
|
170
|
+
width,
|
|
171
|
+
...present({ verovioUrl, verovioIntegrity: call.integrity }),
|
|
172
|
+
});
|
|
130
173
|
next();
|
|
131
174
|
});
|
|
132
175
|
},
|
|
176
|
+
preload(verovioUrl, integrity) {
|
|
177
|
+
if (warm || running || waiting.length > 0)
|
|
178
|
+
return;
|
|
179
|
+
const id = ++nextId;
|
|
180
|
+
preloads.add(id);
|
|
181
|
+
waiting.push({
|
|
182
|
+
id,
|
|
183
|
+
text: "",
|
|
184
|
+
width: 0,
|
|
185
|
+
preload: true,
|
|
186
|
+
...present({ verovioUrl, verovioIntegrity: integrity }),
|
|
187
|
+
});
|
|
188
|
+
next();
|
|
189
|
+
},
|
|
133
190
|
};
|
|
134
191
|
}
|
|
135
192
|
let shared;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { createEngine, type Engine, type EngineOptions, type EngraveReply, engine, JOB_DEADLINE_MS, LOAD_DEADLINE_MS, type Ready, type Reply, type Request, SUPERSEDED_GRACE_MS, type WorkerLike, type WorkerMessage, } from "./engine.js";
|
|
1
|
+
export { createEngine, type Engine, type EngineOptions, type EngineStatus, type EngraveCall, type EngraveReply, engine, JOB_DEADLINE_MS, LOAD_DEADLINE_MS, type LoadStatus, type Progress, type Ready, type Reply, type Request, SUPERSEDED_GRACE_MS, type VerovioSource, type WorkerLike, type WorkerMessage, } from "./engine.js";
|
|
2
2
|
export { type Engraved, type EngraveOptions, engraveChart, engraveXml, LAYOUT, MAX_SCORE_COST, MAX_XML_LENGTH, scoreCost, } from "./engrave.js";
|
|
3
3
|
export { cleanSvg, type ScoreTheme, scorePane, scoreStyles } from "./markup.js";
|
|
4
|
+
export { VEROVIO_CDN_INTEGRITY, VEROVIO_CDN_URL, VEROVIO_MODULE_BYTES, VEROVIO_VERSION, } from "./verovio.js";
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createEngine, engine, JOB_DEADLINE_MS, LOAD_DEADLINE_MS, SUPERSEDED_GRACE_MS, } from "./engine.js";
|
|
2
2
|
export { engraveChart, engraveXml, LAYOUT, MAX_SCORE_COST, MAX_XML_LENGTH, scoreCost, } from "./engrave.js";
|
|
3
3
|
export { cleanSvg, scorePane, scoreStyles } from "./markup.js";
|
|
4
|
+
export { VEROVIO_CDN_INTEGRITY, VEROVIO_CDN_URL, VEROVIO_MODULE_BYTES, VEROVIO_VERSION, } from "./verovio.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EngineStatus } from "./engine.js";
|
|
2
|
+
/** Before anything is heard: the engine downloading, unmeasured. */
|
|
3
|
+
export declare const UNMEASURED: EngineStatus;
|
|
4
|
+
/**
|
|
5
|
+
* Shows `status` in `pane`, building the region the first time and then
|
|
6
|
+
* updating it in place: the live region's text changes only with the
|
|
7
|
+
* phase, so a screen reader hears each phase once, and the percentage is
|
|
8
|
+
* the bar's value (and the amount beside it, hidden from assistive
|
|
9
|
+
* technology, which reads the bar).
|
|
10
|
+
*/
|
|
11
|
+
export declare function showStatus(pane: Element, status: EngineStatus): void;
|
|
12
|
+
/**
|
|
13
|
+
* The region's look, in the switch's ink and rule. An indeterminate bar
|
|
14
|
+
* (no `aria-valuenow`) slides; with reduced motion it stands still, a
|
|
15
|
+
* faint full bar, and a measured one jumps rather than glides.
|
|
16
|
+
*/
|
|
17
|
+
export declare const loadingStyles = ".bms-loading{display:flex;flex-wrap:wrap;align-items:center;gap:.375rem .75rem;padding:.75rem 0;color:var(--bms-ink);font-family:var(--barmark-label-font,system-ui,-apple-system,\"Segoe UI\",sans-serif);font-size:.875em}\n.bms-status{margin:0;flex:1 0 100%}\n.bms-bar{position:relative;flex:0 1 16rem;height:.375rem;border:1px solid var(--bms-rule);border-radius:.375rem;overflow:hidden}\n.bms-bar>span{position:absolute;top:0;bottom:0;left:0;width:var(--bms-done,0%);background:var(--bms-ink);transition:width .2s linear}\n.bms-bar:not([aria-valuenow])>span{width:30%;animation:bms-slide 1.2s ease-in-out infinite alternate}\n@keyframes bms-slide{from{left:0}to{left:70%}}\n.bms-amount{font-variant-numeric:tabular-nums}\n@media (prefers-reduced-motion:reduce){.bms-bar>span{transition:none}.bms-bar:not([aria-valuenow])>span{animation:none;width:100%;opacity:.35}}";
|
package/dist/loading.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// What `<barmark-score>` shows while its score is on its way: the phase in a status region,
|
|
2
|
+
// a progress bar and, while the download can be measured, how far it has come.
|
|
3
|
+
// Verovio-free, like ./shell.js.
|
|
4
|
+
import { progressText } from "./download.js";
|
|
5
|
+
/** Each phase's words: shown (and spoken, once per phase) and the bar's name. */
|
|
6
|
+
const PHASES = {
|
|
7
|
+
download: { text: "Loading the score engine…", label: "Loading the score engine" },
|
|
8
|
+
prepare: { text: "Preparing…", label: "Preparing" },
|
|
9
|
+
engrave: { text: "Engraving…", label: "Engraving" },
|
|
10
|
+
};
|
|
11
|
+
/** Before anything is heard: the engine downloading, unmeasured. */
|
|
12
|
+
export const UNMEASURED = { phase: "download", fraction: null, bytes: null };
|
|
13
|
+
function build(doc) {
|
|
14
|
+
const make = (tag, cls, attrs = {}) => {
|
|
15
|
+
const e = doc.createElement(tag);
|
|
16
|
+
e.className = cls;
|
|
17
|
+
for (const [k, v] of Object.entries(attrs))
|
|
18
|
+
e.setAttribute(k, v);
|
|
19
|
+
return e;
|
|
20
|
+
};
|
|
21
|
+
const box = make("div", "bms-loading");
|
|
22
|
+
const bar = make("div", "bms-bar", {
|
|
23
|
+
role: "progressbar",
|
|
24
|
+
"aria-valuemin": "0",
|
|
25
|
+
"aria-valuemax": "100",
|
|
26
|
+
});
|
|
27
|
+
bar.append(doc.createElement("span"));
|
|
28
|
+
box.append(make("p", "bms-status", { role: "status" }), bar, make("span", "bms-amount", { "aria-hidden": "true" }));
|
|
29
|
+
return box;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Shows `status` in `pane`, building the region the first time and then
|
|
33
|
+
* updating it in place: the live region's text changes only with the
|
|
34
|
+
* phase, so a screen reader hears each phase once, and the percentage is
|
|
35
|
+
* the bar's value (and the amount beside it, hidden from assistive
|
|
36
|
+
* technology, which reads the bar).
|
|
37
|
+
*/
|
|
38
|
+
export function showStatus(pane, status) {
|
|
39
|
+
let box = pane.querySelector(":scope > .bms-loading");
|
|
40
|
+
if (!box) {
|
|
41
|
+
box = build(pane.ownerDocument);
|
|
42
|
+
pane.replaceChildren(box);
|
|
43
|
+
}
|
|
44
|
+
const { text, label } = PHASES[status.phase];
|
|
45
|
+
const live = box.querySelector(".bms-status");
|
|
46
|
+
if (live && live.textContent !== text)
|
|
47
|
+
live.textContent = text;
|
|
48
|
+
const bar = box.querySelector(".bms-bar");
|
|
49
|
+
bar?.setAttribute("aria-label", label);
|
|
50
|
+
const fraction = status.phase === "download" ? status.fraction : null;
|
|
51
|
+
const amount = status.phase === "download" ? progressText(status) : "";
|
|
52
|
+
if (fraction === null) {
|
|
53
|
+
for (const a of ["aria-valuenow", "aria-valuetext"])
|
|
54
|
+
bar?.removeAttribute(a);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const percent = Math.floor(fraction * 100);
|
|
58
|
+
bar?.setAttribute("aria-valuenow", String(percent));
|
|
59
|
+
bar?.setAttribute("aria-valuetext", `${percent}%`);
|
|
60
|
+
bar?.style?.setProperty("--bms-done", `${percent}%`);
|
|
61
|
+
}
|
|
62
|
+
const shown = box.querySelector(".bms-amount");
|
|
63
|
+
if (shown)
|
|
64
|
+
shown.textContent = amount;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The region's look, in the switch's ink and rule. An indeterminate bar
|
|
68
|
+
* (no `aria-valuenow`) slides; with reduced motion it stands still, a
|
|
69
|
+
* faint full bar, and a measured one jumps rather than glides.
|
|
70
|
+
*/
|
|
71
|
+
export const loadingStyles = `.bms-loading{display:flex;flex-wrap:wrap;align-items:center;gap:.375rem .75rem;padding:.75rem 0;color:var(--bms-ink);font-family:var(--barmark-label-font,system-ui,-apple-system,"Segoe UI",sans-serif);font-size:.875em}
|
|
72
|
+
.bms-status{margin:0;flex:1 0 100%}
|
|
73
|
+
.bms-bar{position:relative;flex:0 1 16rem;height:.375rem;border:1px solid var(--bms-rule);border-radius:.375rem;overflow:hidden}
|
|
74
|
+
.bms-bar>span{position:absolute;top:0;bottom:0;left:0;width:var(--bms-done,0%);background:var(--bms-ink);transition:width .2s linear}
|
|
75
|
+
.bms-bar:not([aria-valuenow])>span{width:30%;animation:bms-slide 1.2s ease-in-out infinite alternate}
|
|
76
|
+
@keyframes bms-slide{from{left:0}to{left:70%}}
|
|
77
|
+
.bms-amount{font-variant-numeric:tabular-nums}
|
|
78
|
+
@media (prefers-reduced-motion:reduce){.bms-bar>span{transition:none}.bms-bar:not([aria-valuenow])>span{animation:none;width:100%;opacity:.35}}`;
|
package/dist/shell.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// What the server and the element both write inside `<barmark-score>`'s shadow root,
|
|
2
2
|
// so client-drawn and server-drawn markup are the same. Verovio-free, like ./server.js.
|
|
3
3
|
import { stylesheet } from "@barmark/render";
|
|
4
|
+
import { loadingStyles } from "./loading.js";
|
|
4
5
|
import { escapeHtml, scoreStyles } from "./markup.js";
|
|
5
6
|
import { switchHtml, switchStyles } from "./switch.js";
|
|
6
7
|
/** Chart text for a `<script>`: the two sequences that would end or hide it are written with a backslash. */
|
|
@@ -8,7 +9,7 @@ export const carry = (text) => text.replace(/<\/(script)/gi, "<\\/$1").replaceAl
|
|
|
8
9
|
/** The text `renderScoreHtml` carried, as it was given. */
|
|
9
10
|
export const decodeCarried = (text) => text.replace(/<\\\/(script)/gi, "</$1").replaceAll("<\\!--", "<!--");
|
|
10
11
|
/** The shadow root's content around a drawn grid: styles, the switch, the grid, the (empty) score pane. */
|
|
11
|
-
export const scoreShell = (grid, view) => `<style>${stylesheet}${switchStyles}${scoreStyles}</style>${switchHtml(view)}<div class="bms-grid">${grid}</div><div class="bms-pane" hidden></div>`;
|
|
12
|
+
export const scoreShell = (grid, view) => `<style>${stylesheet}${switchStyles}${scoreStyles}${loadingStyles}</style>${switchHtml(view)}<div class="bms-grid">${grid}</div><div class="bms-pane" hidden></div>`;
|
|
12
13
|
/** A chart that could not be loaded, drawn where the grid would be, as `<barmark-chart>` draws it. */
|
|
13
14
|
export function gridError(message, theme) {
|
|
14
15
|
const m = escapeHtml(message);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The Verovio this package is pinned to (tests/download.test.ts holds these to the installed copy). */
|
|
2
|
+
export declare const VEROVIO_VERSION = "6.4.1";
|
|
3
|
+
/** `verovio/dist/verovio-module.mjs`'s size, decoded: what a compressed download is measured against. */
|
|
4
|
+
export declare const VEROVIO_MODULE_BYTES = 7400749;
|
|
5
|
+
/** The same module on jsDelivr (CORS `*`, immutable), for `verovio-url`. */
|
|
6
|
+
export declare const VEROVIO_CDN_URL = "https://cdn.jsdelivr.net/npm/verovio@6.4.1/dist/verovio-module.mjs";
|
|
7
|
+
/** Its digest, for `verovio-integrity`. */
|
|
8
|
+
export declare const VEROVIO_CDN_INTEGRITY = "sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk";
|
package/dist/verovio.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The Verovio this package is pinned to (tests/download.test.ts holds these to the installed copy). */
|
|
2
|
+
export const VEROVIO_VERSION = "6.4.1";
|
|
3
|
+
/** `verovio/dist/verovio-module.mjs`'s size, decoded: what a compressed download is measured against. */
|
|
4
|
+
export const VEROVIO_MODULE_BYTES = 7_400_749;
|
|
5
|
+
/** The same module on jsDelivr (CORS `*`, immutable), for `verovio-url`. */
|
|
6
|
+
export const VEROVIO_CDN_URL = `https://cdn.jsdelivr.net/npm/verovio@${VEROVIO_VERSION}/dist/verovio-module.mjs`;
|
|
7
|
+
/** Its digest, for `verovio-integrity`. */
|
|
8
|
+
export const VEROVIO_CDN_INTEGRITY = "sha384-iGNyhtxpKLNw90cjbI9ES538TJtbNZjLxsFVUuI8PwE2TTNQ+i2j9CjQnRMhZXHk";
|