@barmark/score 0.0.0 → 0.1.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 +66 -0
- package/LICENSE +21 -0
- package/README.md +141 -1
- package/dist/element.d.ts +13 -0
- package/dist/element.js +416 -0
- package/dist/engine.d.ts +43 -0
- package/dist/engine.js +54 -0
- package/dist/engrave.d.ts +35 -0
- package/dist/engrave.js +53 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/markup.d.ts +23 -0
- package/dist/markup.js +240 -0
- package/dist/server.d.ts +14 -0
- package/dist/server.js +26 -0
- package/dist/shell.d.ts +9 -0
- package/dist/shell.js +16 -0
- package/dist/switch.d.ts +7 -0
- package/dist/switch.js +14 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.js +52 -0
- package/package.json +59 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@barmark/score` are recorded here. The format
|
|
4
|
+
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the
|
|
5
|
+
package uses [semantic versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-11-08
|
|
10
|
+
|
|
11
|
+
First release.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- `<barmark-score>` (import `@barmark/score/element`, which also registers
|
|
16
|
+
`<barmark-chart>`): the chart as the bar grid or as a full score, with a
|
|
17
|
+
Grid / Score switch (a keyboard radio group). Its chart comes from `text`,
|
|
18
|
+
`src` or a script child, as `<barmark-chart>`'s does, and it takes every
|
|
19
|
+
grid attribute plus `view` (`grid` | `score`) and `verovio-url` (resolved
|
|
20
|
+
against the page, http(s) only); a `viewchange` event follows the view. A
|
|
21
|
+
`view="score"` set before the chart arrives waits for it. The score engine
|
|
22
|
+
loads only on the first switch to the score, engraves at the element's
|
|
23
|
+
width (again after a resize), and shows "Loading score…" meanwhile, the
|
|
24
|
+
first time; a re-engrave keeps the score shown, marked `aria-busy`, until
|
|
25
|
+
the new one lands, so a resize does not blank it. The drawn score is one
|
|
26
|
+
image (`role="img"`) named for the chart's title, or "Score". A score that
|
|
27
|
+
can't be drawn leaves the grid with a note by the switch; a new chart or a
|
|
28
|
+
new `verovio-url` lets the Score radio try again. While `view="score"`
|
|
29
|
+
waits for its chart, Grid is the radio checked, as the grid is what
|
|
30
|
+
shows. Every element on a page shares one worker, so the first
|
|
31
|
+
`verovio-url` that loads is the one used. A shadow
|
|
32
|
+
root drawn by `renderScoreHtml` is taken over without redrawing the grid.
|
|
33
|
+
Importing the package root registers no element.
|
|
34
|
+
- `renderScoreHtml` (from `@barmark/score/server`, which never reaches
|
|
35
|
+
Verovio) draws `<barmark-score>` on the server: the bar grid, the Grid /
|
|
36
|
+
Score switch and the chart text, so the grid shows before any script runs;
|
|
37
|
+
any `view` but `"score"` reads as the grid, and `view: "score"` checks the
|
|
38
|
+
Grid radio (the grid is what shows) while the element keeps `view="score"`
|
|
39
|
+
and switches once it loads. `decodeCarried` reads the text back.
|
|
40
|
+
- `engraveChart` turns a chart into a full score (MusicXML from
|
|
41
|
+
`@barmark/musicxml`, engraved by Verovio) as one SVG column, or says why
|
|
42
|
+
it cannot (a chart that will not export, a score Verovio cannot read or
|
|
43
|
+
one with no pages: `ok: false`, never a throw; a refusal about the whole
|
|
44
|
+
chart does not read "Line 0: …").
|
|
45
|
+
- Safe, themed score markup: `cleanSvg` is an allow-list. A hand-written
|
|
46
|
+
scanner keeps only the elements and attributes Verovio emits (and its own
|
|
47
|
+
two stylesheets) and drops anything else with its content: scripts,
|
|
48
|
+
`foreignObject`, `on*` attributes, links that leave the page and
|
|
49
|
+
attributes whose value holds a character reference. It throws on markup it
|
|
50
|
+
cannot read cleanly (a self-closing `style` or `title`), which
|
|
51
|
+
`engraveChart` turns into `ok: false`. `scorePane` wraps the pages in
|
|
52
|
+
render's `.bm-chart[data-theme]` so the score follows the palette (light,
|
|
53
|
+
dark, auto, print), including the notes, which override Verovio's
|
|
54
|
+
`color="black"`; `scoreStyles` is the score's own layout. `scorePane`
|
|
55
|
+
takes the score's accessible name as an optional third argument (default
|
|
56
|
+
"Score").
|
|
57
|
+
- A shared Verovio worker: `engine()` runs every score of a page through one
|
|
58
|
+
module Worker that loads Verovio on first use, and for each element only
|
|
59
|
+
the newest request is answered (`createEngine` takes any `WorkerLike`); a
|
|
60
|
+
crashed worker rejects what is pending and is replaced on the next call.
|
|
61
|
+
- The README says how to use the element with Vite: Vite 8 (and Vite+)
|
|
62
|
+
needs no config; Vite 7 needs `optimizeDeps.exclude` and
|
|
63
|
+
`worker.format: "es"`.
|
|
64
|
+
|
|
65
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/score-v0.1.0...HEAD
|
|
66
|
+
[0.1.0]: https://github.com/kevinpeckham/barmark/releases/tag/score-v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Peckham
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,143 @@
|
|
|
1
1
|
# @barmark/score
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shows a [barmark](https://github.com/kevinpeckham/barmark) chord chart as
|
|
4
|
+
the bar grid or as a full engraved score. The chart is exported to MusicXML
|
|
5
|
+
by `@barmark/musicxml` and engraved by Verovio in a Web Worker; the score
|
|
6
|
+
adds no music of its own.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
npm install @barmark/score
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Two components
|
|
13
|
+
|
|
14
|
+
- **`<barmark-chart>`** from [`@barmark/render`](../render/) is the light
|
|
15
|
+
one: the bar grid, no Verovio.
|
|
16
|
+
- **`<barmark-score>`** from this package is the heavier one: the same grid,
|
|
17
|
+
plus a Grid / Score switch to a full score. Verovio (about 7 MB) loads
|
|
18
|
+
only on the first switch to the score, never on page load, and one copy
|
|
19
|
+
serves every score on the page.
|
|
20
|
+
|
|
21
|
+
Use `<barmark-chart>` when the grid is enough.
|
|
22
|
+
|
|
23
|
+
## As a web component
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<script type="module">import "@barmark/score/element";</script>
|
|
27
|
+
|
|
28
|
+
<barmark-score src="/charts/peaceful-dreams.chart.md"></barmark-score>
|
|
29
|
+
|
|
30
|
+
<barmark-score view="score" theme="dark">
|
|
31
|
+
<script type="text/barmark">
|
|
32
|
+
## Verse
|
|
33
|
+
| D | A | Bm | G |
|
|
34
|
+
</script>
|
|
35
|
+
</barmark-score>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Importing `@barmark/score/element` also registers `<barmark-chart>` (it
|
|
39
|
+
builds on it). Importing the package root registers nothing.
|
|
40
|
+
|
|
41
|
+
The chart comes from the `text` property, the `src` URL or a child
|
|
42
|
+
`<script type="text/barmark">`, exactly as for `<barmark-chart>`. The score
|
|
43
|
+
is engraved at the element's width and again after a resize; the score shown
|
|
44
|
+
stays (with `aria-busy="true"`) until the new one is ready. To assistive
|
|
45
|
+
technology the score is one image named for the chart's title, or "Score".
|
|
46
|
+
|
|
47
|
+
| Attribute (property) | |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `src`, `theme`, `bar-numbers`, `rehearsal-letters`, `numerals`, `wrap`, `show-part` | the grid's own, as in [`@barmark/render`](../render/README.md#options-and-attributes) |
|
|
50
|
+
| `view` (`view`) | `"grid"` (default) or `"score"`; the view being shown. Set before the chart arrives, `"score"` waits for it |
|
|
51
|
+
| `verovio-url` | where to load Verovio from (below) |
|
|
52
|
+
|
|
53
|
+
The `text` property is the chart's text (`null` goes back to `src` or the
|
|
54
|
+
script child). The element fires a `viewchange` event, with
|
|
55
|
+
`detail.view`, whenever the view changes, from the switch or from script.
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const score = document.querySelector("barmark-score");
|
|
59
|
+
score.addEventListener("viewchange", (e) => console.log(e.detail.view));
|
|
60
|
+
score.view = "score";
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Bundling the worker
|
|
64
|
+
|
|
65
|
+
The engine runs in a module Worker, created as
|
|
66
|
+
`new Worker(new URL("./worker.js", import.meta.url), { type: "module" })`.
|
|
67
|
+
That is the pattern Vite and webpack follow, so a bundler finds the worker
|
|
68
|
+
and Verovio for you.
|
|
69
|
+
|
|
70
|
+
Vite 8 (and Vite+) needs no config, in `vite dev` or a build. Vite 7 needs
|
|
71
|
+
two lines: its dev pre-bundler moves the element away from the worker and
|
|
72
|
+
the font it finds by URL, and it builds workers as `iife`, which cannot
|
|
73
|
+
load Verovio on demand.
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
// vite.config.js, for Vite 7 (harmless on Vite 8)
|
|
77
|
+
export default {
|
|
78
|
+
optimizeDeps: { exclude: ["@barmark/score"] },
|
|
79
|
+
worker: { format: "es" },
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Without a bundler, a page's import map does not reach a worker, so the
|
|
84
|
+
worker's bare imports (`verovio/esm`, `barmark`, `@barmark/musicxml`)
|
|
85
|
+
fail. Serve a bundled `worker.js` at the package's URL. `verovio-url` can
|
|
86
|
+
then point at a Verovio module you host (for example
|
|
87
|
+
`verovio/dist/verovio-module.mjs` from the `verovio` package) instead of
|
|
88
|
+
bundling the 7 MB inside the worker; it only changes where that module
|
|
89
|
+
comes from. The URL is resolved against the page and must be http(s);
|
|
90
|
+
anything else is refused and the score is not drawn. The first
|
|
91
|
+
`verovio-url` that loads wins for the whole page: every element shares one
|
|
92
|
+
worker, so a later or different URL is not loaded.
|
|
93
|
+
|
|
94
|
+
### When the score can't be drawn
|
|
95
|
+
|
|
96
|
+
If Verovio fails to load, or rejects the MusicXML, or the chart will not
|
|
97
|
+
export, the grid stays and a note by the switch says "The score couldn't
|
|
98
|
+
be drawn". The reason goes to the console.
|
|
99
|
+
|
|
100
|
+
## On the server
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import { renderScoreHtml } from "@barmark/score/server";
|
|
104
|
+
|
|
105
|
+
const html = renderScoreHtml(chartText, { fontUrl: "/fonts/Edwin-Roman.otf" });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`renderScoreHtml` takes `@barmark/render`'s options plus `view`. It never
|
|
109
|
+
loads Verovio: it draws the bar grid and the Grid / Score switch in a
|
|
110
|
+
declarative shadow root, so the chart shows before any script runs. The
|
|
111
|
+
score is engraved once `<barmark-score>` upgrades in the browser, taking
|
|
112
|
+
over the markup without redrawing the grid. Like `renderChart`, insert it
|
|
113
|
+
by parsing HTML (or `setHTMLUnsafe`), not `innerHTML`.
|
|
114
|
+
|
|
115
|
+
## Engraving directly
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
import createVerovioModule from "verovio/wasm";
|
|
119
|
+
import { VerovioToolkit } from "verovio/esm";
|
|
120
|
+
import { engraveChart } from "@barmark/score";
|
|
121
|
+
|
|
122
|
+
const vrv = new VerovioToolkit(await createVerovioModule());
|
|
123
|
+
const result = engraveChart(vrv, chartText, { width: 800 });
|
|
124
|
+
if (result.ok) document.body.innerHTML = result.svg;
|
|
125
|
+
else console.warn(result.reason);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The SVG is cleaned with an allow-list (`cleanSvg`) before it is returned;
|
|
129
|
+
`scorePane` and `scoreStyles` wrap it so it follows the grid's light and
|
|
130
|
+
dark palette.
|
|
131
|
+
|
|
132
|
+
## Licence
|
|
133
|
+
|
|
134
|
+
This package is MIT. It uses [Verovio](https://github.com/rism-digital/verovio),
|
|
135
|
+
which is LGPL-3.0-only, as an ordinary npm dependency, loaded unchanged at
|
|
136
|
+
run time; its code is never copied into this package. barmark's own code
|
|
137
|
+
stays MIT. Verovio's licence text is
|
|
138
|
+
[COPYING.LESSER](https://github.com/rism-digital/verovio/blob/develop/COPYING.LESSER).
|
|
139
|
+
|
|
140
|
+
A site that bundles the worker ships Verovio inside its bundle. Keeping
|
|
141
|
+
Verovio a separate file (through `verovio-url`, or the bundler's own
|
|
142
|
+
chunk) and keeping its notice is the easy way to meet the LGPL. This is
|
|
143
|
+
not legal advice.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<barmark-score>`: a chart as barmark's bar grid or as a full score, with a
|
|
3
|
+
* Grid / Score switch. Its chart comes from the `text` property, a `src` URL or
|
|
4
|
+
* a child `<script type="text/barmark">`, as `<barmark-chart>`'s does. A shadow
|
|
5
|
+
* root drawn by `renderScoreHtml` is reused. The score engine (Verovio, in a
|
|
6
|
+
* worker) loads on the first switch to the score, never before. Importing
|
|
7
|
+
* this module registers the element (once) and the font (once per document).
|
|
8
|
+
*/
|
|
9
|
+
import type { Engine } from "./engine.js";
|
|
10
|
+
type EngineFactory = () => Engine | Promise<Engine>;
|
|
11
|
+
/** For tests only: an engine to use in place of the worker's, or null for the real one. */
|
|
12
|
+
export declare function setEngineForTests(factory: EngineFactory | null): void;
|
|
13
|
+
export {};
|
package/dist/element.js
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<barmark-score>`: a chart as barmark's bar grid or as a full score, with a
|
|
3
|
+
* Grid / Score switch. Its chart comes from the `text` property, a `src` URL or
|
|
4
|
+
* a child `<script type="text/barmark">`, as `<barmark-chart>`'s does. A shadow
|
|
5
|
+
* root drawn by `renderScoreHtml` is reused. The score engine (Verovio, in a
|
|
6
|
+
* worker) loads on the first switch to the score, never before. Importing
|
|
7
|
+
* this module registers the element (once) and the font (once per document).
|
|
8
|
+
*/
|
|
9
|
+
import { FONT_FAMILY, fontFaceCss, renderChartUnwrapped, } from "@barmark/render";
|
|
10
|
+
import { chartSource, inlineChartText, optionsFromAttributes, } from "@barmark/render/element";
|
|
11
|
+
import { parseChart } from "barmark";
|
|
12
|
+
import { scorePane } from "./markup.js";
|
|
13
|
+
import { decodeCarried, gridError, scoreShell } from "./shell.js";
|
|
14
|
+
import { viewOf } from "./switch.js";
|
|
15
|
+
let testEngine = null;
|
|
16
|
+
/** For tests only: an engine to use in place of the worker's, or null for the real one. */
|
|
17
|
+
export function setEngineForTests(factory) {
|
|
18
|
+
testEngine = factory;
|
|
19
|
+
}
|
|
20
|
+
let loading = null;
|
|
21
|
+
/** The page's engine, imported on first use; a failed import is tried again next time. */
|
|
22
|
+
function loadEngine() {
|
|
23
|
+
const factory = testEngine;
|
|
24
|
+
if (factory)
|
|
25
|
+
return Promise.resolve().then(factory);
|
|
26
|
+
loading ??= import("./engine.js").then((m) => m.engine(), (e) => {
|
|
27
|
+
loading = null;
|
|
28
|
+
throw e;
|
|
29
|
+
});
|
|
30
|
+
return loading;
|
|
31
|
+
}
|
|
32
|
+
const LOADING = "Loading score…";
|
|
33
|
+
/** The score's accessible name when the chart has no title. */
|
|
34
|
+
const UNTITLED = "Score";
|
|
35
|
+
const NOTE = "The score couldn't be drawn";
|
|
36
|
+
/** A width change smaller than this keeps the score as engraved. */
|
|
37
|
+
const SLACK = 8;
|
|
38
|
+
const RESIZE_DELAY = 150;
|
|
39
|
+
const ARROWS = new Set(["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"]);
|
|
40
|
+
/** Edwin, once per document, under the same marker `<barmark-chart>` uses, so either element adds it once. */
|
|
41
|
+
function addFont(doc) {
|
|
42
|
+
if (doc.querySelector("style[data-barmark-font]") !== null)
|
|
43
|
+
return;
|
|
44
|
+
// From dist/ (or src/), ../../render is @barmark/render, in the workspace and under node_modules/@barmark/.
|
|
45
|
+
const url = new URL("../../render/fonts/Edwin-Roman.otf", import.meta.url);
|
|
46
|
+
const font = Object.assign(doc.createElement("style"), { textContent: fontFaceCss(url.href) });
|
|
47
|
+
font.setAttribute("data-barmark-font", FONT_FAMILY);
|
|
48
|
+
doc.head.append(font);
|
|
49
|
+
}
|
|
50
|
+
const load = (src) => fetch(src).then((r) => r.ok ? r.text() : Promise.reject(new Error(`${r.status} ${r.statusText}`.trim())));
|
|
51
|
+
/** Text set on the element before it was defined is an own property; it is taken off and returned. */
|
|
52
|
+
function takeEarlyText(el) {
|
|
53
|
+
if (!Object.hasOwn(el, "text"))
|
|
54
|
+
return null;
|
|
55
|
+
const early = el.text;
|
|
56
|
+
Reflect.deleteProperty(el, "text");
|
|
57
|
+
return { text: early === null || early === undefined ? null : String(early) };
|
|
58
|
+
}
|
|
59
|
+
/** The grid's attributes, as `<barmark-chart>` observes them, then the score's own. */
|
|
60
|
+
const OBSERVED = [
|
|
61
|
+
...["src", "theme", "bar-numbers", "rehearsal-letters", "numerals", "wrap", "show-part"],
|
|
62
|
+
...["view", "verovio-url"],
|
|
63
|
+
];
|
|
64
|
+
const messageOf = (e) => (e instanceof Error ? e.message : String(e));
|
|
65
|
+
/** The radio a click or key landed on. */
|
|
66
|
+
const radioOf = (target) => target?.closest?.("[data-view]") ?? null;
|
|
67
|
+
const same = (a, b) => a !== null && a.text === b.text && Math.abs(a.width - b.width) <= SLACK;
|
|
68
|
+
const URL_REASON = "verovio-url must be an http(s) URL";
|
|
69
|
+
/** `verovio-url` resolved against the page: undefined when absent, null unless http(s). */
|
|
70
|
+
function verovioHref(value, base) {
|
|
71
|
+
if (value === null)
|
|
72
|
+
return undefined;
|
|
73
|
+
try {
|
|
74
|
+
const url = new URL(value, base);
|
|
75
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url.href : null;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function defineElement() {
|
|
82
|
+
class BarmarkScore extends HTMLElement {
|
|
83
|
+
static observedAttributes = OBSERVED;
|
|
84
|
+
// Only the children are observed; some DOMs report attribute changes anyway, which are not the chart's.
|
|
85
|
+
#observer = new MutationObserver((records) => {
|
|
86
|
+
if (records.some((r) => r.type !== "attributes"))
|
|
87
|
+
this.#chartChanged();
|
|
88
|
+
});
|
|
89
|
+
#resize = null;
|
|
90
|
+
#resizeTimer;
|
|
91
|
+
#queued = null;
|
|
92
|
+
#renders = 0;
|
|
93
|
+
#engraves = 0;
|
|
94
|
+
#fetched = null;
|
|
95
|
+
#text = null;
|
|
96
|
+
/** The chart the grid shows: null when it could not be loaded, undefined until there is a source. */
|
|
97
|
+
#chart;
|
|
98
|
+
/** Whether the shadow root has been drawn (or a server-drawn one taken over). */
|
|
99
|
+
#drawn = false;
|
|
100
|
+
/** The view last shown, null before the first: a change from it fires `viewchange`. */
|
|
101
|
+
#shown = null;
|
|
102
|
+
#failed = false;
|
|
103
|
+
#engraved = null;
|
|
104
|
+
/** The engraving and theme the pane holds, so an unchanged one is not written again. */
|
|
105
|
+
#revealed = null;
|
|
106
|
+
#asked = null;
|
|
107
|
+
connectedCallback() {
|
|
108
|
+
const early = takeEarlyText(this);
|
|
109
|
+
if (early)
|
|
110
|
+
this.#text = early.text;
|
|
111
|
+
addFont(this.ownerDocument);
|
|
112
|
+
this.#observer.observe(this, { childList: true, subtree: true, characterData: true });
|
|
113
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
114
|
+
this.#resize = new ResizeObserver(() => this.#resized());
|
|
115
|
+
this.#resize.observe(this);
|
|
116
|
+
}
|
|
117
|
+
this.#schedule();
|
|
118
|
+
}
|
|
119
|
+
disconnectedCallback() {
|
|
120
|
+
this.#observer.disconnect();
|
|
121
|
+
this.#resize?.disconnect();
|
|
122
|
+
this.#resize = null;
|
|
123
|
+
clearTimeout(this.#resizeTimer);
|
|
124
|
+
}
|
|
125
|
+
attributeChangedCallback(name, old, value) {
|
|
126
|
+
if (!this.isConnected || old === value)
|
|
127
|
+
return;
|
|
128
|
+
// A new URL may be the fix: the Score radio works again, as after a new chart.
|
|
129
|
+
if (name === "verovio-url")
|
|
130
|
+
this.#clearFailure();
|
|
131
|
+
else if (name === "view")
|
|
132
|
+
this.#applyView();
|
|
133
|
+
else if (name === "src")
|
|
134
|
+
this.#chartChanged();
|
|
135
|
+
else
|
|
136
|
+
this.#schedule();
|
|
137
|
+
}
|
|
138
|
+
/** Chart text to draw; while it is a string it wins over `src` and a script child. */
|
|
139
|
+
get text() {
|
|
140
|
+
return this.#text;
|
|
141
|
+
}
|
|
142
|
+
set text(value) {
|
|
143
|
+
this.#text = value == null ? null : `${value}`;
|
|
144
|
+
if (this.isConnected)
|
|
145
|
+
this.#chartChanged();
|
|
146
|
+
}
|
|
147
|
+
/** "grid" or "score"; anything else is the grid. */
|
|
148
|
+
get view() {
|
|
149
|
+
return viewOf(this.getAttribute("view"));
|
|
150
|
+
}
|
|
151
|
+
set view(v) {
|
|
152
|
+
this.setAttribute("view", viewOf(v));
|
|
153
|
+
}
|
|
154
|
+
#chartChanged() {
|
|
155
|
+
this.#clearFailure();
|
|
156
|
+
this.#schedule();
|
|
157
|
+
}
|
|
158
|
+
/** The `src` attribute, "" when absent. */
|
|
159
|
+
#src() {
|
|
160
|
+
return this.getAttribute("src") ?? "";
|
|
161
|
+
}
|
|
162
|
+
/** Changes made in one task (an upgrade sets each attribute in turn) draw once, in a microtask. */
|
|
163
|
+
#schedule() {
|
|
164
|
+
this.#queued ??= Promise.resolve().then(() => {
|
|
165
|
+
this.#queued = null;
|
|
166
|
+
return this.#render();
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async #render() {
|
|
170
|
+
const ticket = ++this.#renders;
|
|
171
|
+
const options = optionsFromAttributes(this);
|
|
172
|
+
const script = this.querySelector('script[type="text/barmark"]');
|
|
173
|
+
const from = { text: this.#text, src: this.#src(), script: script !== null };
|
|
174
|
+
const source = chartSource(from);
|
|
175
|
+
const grid = await this.#grid(source, script, options);
|
|
176
|
+
if (ticket === this.#renders) {
|
|
177
|
+
this.#draw(source, grid, options);
|
|
178
|
+
this.#chart = grid.text;
|
|
179
|
+
this.#applyView();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async #grid(source, script, options) {
|
|
183
|
+
if (source === "none")
|
|
184
|
+
return { text: undefined, html: null };
|
|
185
|
+
try {
|
|
186
|
+
const text = await this.#chartText(source, script);
|
|
187
|
+
return { text, html: renderChartUnwrapped(text, options) };
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
this.#fetched = null;
|
|
191
|
+
const message = `Could not load ${this.#src()}: ${messageOf(e)}`;
|
|
192
|
+
return { text: null, html: gridError(message, options.theme ?? "auto") };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
#chartText(source, script) {
|
|
196
|
+
if (source === "text")
|
|
197
|
+
return this.#text ?? "";
|
|
198
|
+
if (source === "script")
|
|
199
|
+
return inlineChartText(decodeCarried(script?.textContent ?? ""));
|
|
200
|
+
const src = this.#src();
|
|
201
|
+
if (this.#fetched?.src !== src)
|
|
202
|
+
this.#fetched = { src, text: load(src) };
|
|
203
|
+
return this.#fetched.text;
|
|
204
|
+
}
|
|
205
|
+
/** Draws the grid, keeping a server-drawn one on upgrade (its script child is what it drew). */
|
|
206
|
+
#draw(source, grid, options) {
|
|
207
|
+
const hydrating = !this.#drawn && source === "script";
|
|
208
|
+
this.#drawn = true;
|
|
209
|
+
const { switcher, grid: pane, pane: score } = this.#parts();
|
|
210
|
+
if (pane && switcher && score) {
|
|
211
|
+
if (!hydrating)
|
|
212
|
+
this.#redraw(pane, grid.html, options);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
216
|
+
// Grid is checked, as the server draws it: #applyView checks the view actually shown.
|
|
217
|
+
root.innerHTML = scoreShell(grid.html ?? renderChartUnwrapped("", options), "grid");
|
|
218
|
+
}
|
|
219
|
+
this.#wire();
|
|
220
|
+
}
|
|
221
|
+
/** New grid markup, or with no source of its own, the drawn grid takes the theme. */
|
|
222
|
+
#redraw(pane, html, options) {
|
|
223
|
+
if (html !== null)
|
|
224
|
+
pane.innerHTML = html;
|
|
225
|
+
else
|
|
226
|
+
pane.querySelector(".bm-chart")?.setAttribute("data-theme", options.theme ?? "auto");
|
|
227
|
+
}
|
|
228
|
+
#parts() {
|
|
229
|
+
const root = this.shadowRoot;
|
|
230
|
+
return {
|
|
231
|
+
switcher: root?.querySelector(".bms-switch") ?? null,
|
|
232
|
+
grid: root?.querySelector(".bms-grid") ?? null,
|
|
233
|
+
pane: root?.querySelector(".bms-pane") ?? null,
|
|
234
|
+
radios: [...(root?.querySelectorAll(".bms-switch [data-view]") ?? [])],
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
#wired = null;
|
|
238
|
+
#wire() {
|
|
239
|
+
const { switcher } = this.#parts();
|
|
240
|
+
if (!switcher || switcher === this.#wired)
|
|
241
|
+
return;
|
|
242
|
+
this.#wired = switcher;
|
|
243
|
+
switcher.addEventListener("click", (e) => this.#pick(radioOf(e.target)));
|
|
244
|
+
switcher.addEventListener("keydown", (e) => this.#key(e));
|
|
245
|
+
}
|
|
246
|
+
#pick(radio) {
|
|
247
|
+
if (!radio || radio.getAttribute("aria-disabled") === "true")
|
|
248
|
+
return;
|
|
249
|
+
this.view = viewOf(radio.getAttribute("data-view"));
|
|
250
|
+
}
|
|
251
|
+
/** The radio-group keys: an arrow moves to the other view and selects it; Space and Enter select. */
|
|
252
|
+
#key(e) {
|
|
253
|
+
const radio = radioOf(e.target);
|
|
254
|
+
if (!radio)
|
|
255
|
+
return;
|
|
256
|
+
if (e.key === " " || e.key === "Enter") {
|
|
257
|
+
e.preventDefault();
|
|
258
|
+
this.#pick(radio);
|
|
259
|
+
}
|
|
260
|
+
else if (ARROWS.has(e.key)) {
|
|
261
|
+
e.preventDefault();
|
|
262
|
+
const other = this.#parts().radios.find((r) => r !== radio) ?? null;
|
|
263
|
+
other?.focus?.();
|
|
264
|
+
this.#pick(other);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/** Shows the view the attribute names, firing `viewchange` when it differs from the last. */
|
|
268
|
+
#applyView() {
|
|
269
|
+
if (!this.#drawn)
|
|
270
|
+
return;
|
|
271
|
+
const view = this.view;
|
|
272
|
+
if (view === "score" && this.#failed) {
|
|
273
|
+
this.view = "grid";
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// A score waiting for its chart shows the grid, so Grid is the radio checked.
|
|
277
|
+
this.#check(view === "score" && this.#chart === undefined ? "grid" : view);
|
|
278
|
+
const was = this.#shown;
|
|
279
|
+
this.#shown = view;
|
|
280
|
+
if (was !== null && was !== view)
|
|
281
|
+
this.dispatchEvent(new CustomEvent("viewchange", { detail: { view }, bubbles: true, composed: true }));
|
|
282
|
+
if (view === "score")
|
|
283
|
+
this.#showScore();
|
|
284
|
+
else
|
|
285
|
+
this.#showGrid();
|
|
286
|
+
}
|
|
287
|
+
/** Checks the radio for `view` and makes it the one Tab reaches. */
|
|
288
|
+
#check(view) {
|
|
289
|
+
for (const r of this.#parts().radios) {
|
|
290
|
+
const on = r.getAttribute("data-view") === view;
|
|
291
|
+
r.setAttribute("aria-checked", String(on));
|
|
292
|
+
if (on)
|
|
293
|
+
r.removeAttribute("tabindex");
|
|
294
|
+
else
|
|
295
|
+
r.setAttribute("tabindex", "-1");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
#showGrid() {
|
|
299
|
+
const { grid, pane } = this.#parts();
|
|
300
|
+
pane?.toggleAttribute("hidden", true);
|
|
301
|
+
grid?.toggleAttribute("hidden", false);
|
|
302
|
+
}
|
|
303
|
+
/** Shows the score, engraving it first unless the one in hand is for this chart and width. */
|
|
304
|
+
#showScore() {
|
|
305
|
+
// No source yet: the grid stays and the score is engraved once the chart arrives.
|
|
306
|
+
if (this.#chart === undefined)
|
|
307
|
+
return this.#showGrid();
|
|
308
|
+
if (this.#chart === null)
|
|
309
|
+
return this.#fail("there is no chart to engrave");
|
|
310
|
+
const want = { text: this.#chart, width: this.clientWidth || 0 };
|
|
311
|
+
if (this.#engraved && same(this.#engraved, want))
|
|
312
|
+
return this.#reveal(this.#engraved);
|
|
313
|
+
if (same(this.#asked, want))
|
|
314
|
+
return this.#busy();
|
|
315
|
+
const url = verovioHref(this.getAttribute("verovio-url"), this.ownerDocument.baseURI);
|
|
316
|
+
if (url === null)
|
|
317
|
+
return this.#fail(URL_REASON);
|
|
318
|
+
void this.#engrave(want, url);
|
|
319
|
+
}
|
|
320
|
+
/** Engraves `want`; only the newest request this element made is shown. */
|
|
321
|
+
async #engrave(want, url) {
|
|
322
|
+
this.#busy();
|
|
323
|
+
this.#asked = want;
|
|
324
|
+
const ask = ++this.#engraves;
|
|
325
|
+
let reply;
|
|
326
|
+
try {
|
|
327
|
+
reply = await (await loadEngine()).engrave(this, want.text, want.width, url);
|
|
328
|
+
}
|
|
329
|
+
catch (e) {
|
|
330
|
+
reply = { id: 0, ok: false, reason: messageOf(e) };
|
|
331
|
+
}
|
|
332
|
+
if (reply === null || ask !== this.#engraves)
|
|
333
|
+
return;
|
|
334
|
+
this.#asked = null;
|
|
335
|
+
if (!reply.ok)
|
|
336
|
+
return this.#fail(reply.reason);
|
|
337
|
+
const label = parseChart(want.text).title ?? UNTITLED;
|
|
338
|
+
this.#engraved = { ...want, svg: reply.svg, label };
|
|
339
|
+
if (this.view === "score")
|
|
340
|
+
this.#reveal(this.#engraved);
|
|
341
|
+
}
|
|
342
|
+
/** A score on its way: a shown one stays (busy) until the new one lands, else "Loading score…". */
|
|
343
|
+
#busy() {
|
|
344
|
+
const { grid, pane } = this.#parts();
|
|
345
|
+
if (!pane)
|
|
346
|
+
return;
|
|
347
|
+
if (this.#revealed === null)
|
|
348
|
+
pane.textContent = LOADING;
|
|
349
|
+
pane.setAttribute("aria-busy", "true");
|
|
350
|
+
pane.toggleAttribute("hidden", false);
|
|
351
|
+
grid?.toggleAttribute("hidden", true);
|
|
352
|
+
}
|
|
353
|
+
#reveal({ svg, label }) {
|
|
354
|
+
const { grid, pane } = this.#parts();
|
|
355
|
+
if (!pane)
|
|
356
|
+
return;
|
|
357
|
+
const theme = optionsFromAttributes(this).theme ?? "auto";
|
|
358
|
+
if (this.#revealed?.svg !== svg || this.#revealed.theme !== theme) {
|
|
359
|
+
try {
|
|
360
|
+
pane.innerHTML = scorePane(svg, theme, label);
|
|
361
|
+
}
|
|
362
|
+
catch (e) {
|
|
363
|
+
return this.#fail(messageOf(e));
|
|
364
|
+
}
|
|
365
|
+
this.#revealed = { svg, theme };
|
|
366
|
+
}
|
|
367
|
+
pane.removeAttribute("aria-busy");
|
|
368
|
+
pane.toggleAttribute("hidden", false);
|
|
369
|
+
grid?.toggleAttribute("hidden", true);
|
|
370
|
+
}
|
|
371
|
+
/** The score could not be drawn: the grid again, a note by the switch, Score disabled. */
|
|
372
|
+
#fail(reason) {
|
|
373
|
+
console.warn(`<barmark-score>: the score couldn't be drawn: ${reason}`);
|
|
374
|
+
this.#failed = true;
|
|
375
|
+
this.#asked = null;
|
|
376
|
+
this.#engraved = null;
|
|
377
|
+
this.#engraves++;
|
|
378
|
+
const { switcher, pane, radios } = this.#parts();
|
|
379
|
+
if (switcher && !this.shadowRoot?.querySelector(".bms-note")) {
|
|
380
|
+
const note = this.ownerDocument.createElement("p");
|
|
381
|
+
note.className = "bms-note";
|
|
382
|
+
note.setAttribute("role", "status");
|
|
383
|
+
note.textContent = NOTE;
|
|
384
|
+
switcher.after(note);
|
|
385
|
+
}
|
|
386
|
+
radios
|
|
387
|
+
.find((r) => r.getAttribute("data-view") === "score")
|
|
388
|
+
?.setAttribute("aria-disabled", "true");
|
|
389
|
+
this.#revealed = null;
|
|
390
|
+
pane?.replaceChildren();
|
|
391
|
+
pane?.removeAttribute("aria-busy");
|
|
392
|
+
this.#showGrid();
|
|
393
|
+
if (this.view === "score")
|
|
394
|
+
this.view = "grid";
|
|
395
|
+
}
|
|
396
|
+
#clearFailure() {
|
|
397
|
+
if (!this.#failed)
|
|
398
|
+
return;
|
|
399
|
+
this.#failed = false;
|
|
400
|
+
this.shadowRoot?.querySelector(".bms-note")?.remove();
|
|
401
|
+
for (const r of this.#parts().radios)
|
|
402
|
+
r.removeAttribute("aria-disabled");
|
|
403
|
+
}
|
|
404
|
+
/** A resize engraves again (if the width moved more than SLACK) once it settles. */
|
|
405
|
+
#resized() {
|
|
406
|
+
clearTimeout(this.#resizeTimer);
|
|
407
|
+
this.#resizeTimer = setTimeout(() => {
|
|
408
|
+
if (this.#shown === "score")
|
|
409
|
+
this.#showScore();
|
|
410
|
+
}, RESIZE_DELAY);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
customElements.define("barmark-score", BarmarkScore);
|
|
414
|
+
}
|
|
415
|
+
if (typeof customElements !== "undefined" && !customElements.get("barmark-score"))
|
|
416
|
+
defineElement();
|