@barmark/score 0.0.0 → 0.1.1
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 +96 -0
- package/LICENSE +21 -0
- package/README.md +144 -1
- package/dist/element.d.ts +13 -0
- package/dist/element.js +435 -0
- package/dist/engine.d.ts +43 -0
- package/dist/engine.js +54 -0
- package/dist/engrave.d.ts +41 -0
- package/dist/engrave.js +78 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/markup.d.ts +25 -0
- package/dist/markup.js +242 -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 +18 -0
- package/dist/switch.js +32 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.js +52 -0
- package/package.json +59 -5
package/dist/markup.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ScoreTheme = "auto" | "light" | "dark";
|
|
2
|
+
/** Whether a stylesheet is Verovio's embedded Leipzig face, exactly (and nothing else). */
|
|
3
|
+
export declare const isLeipzigFace: (css: string) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Verovio's SVG reduced to what Verovio emits: only the listed elements and
|
|
6
|
+
* attributes survive, an unlisted element goes with its content, links keep
|
|
7
|
+
* only `#` targets. Hand-scanned, no DOM. Throws if the markup cannot be read
|
|
8
|
+
* cleanly (unbalanced quotes, comments, a stray `<`, unclosed elements): the
|
|
9
|
+
* caller treats that as "could not draw".
|
|
10
|
+
*/
|
|
11
|
+
export declare function cleanSvg(svg: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* The score's own layout. Colour comes from `@barmark/render`'s stylesheet
|
|
14
|
+
* (the `.bm-chart` wrapper that `scorePane` writes), which must be in the same
|
|
15
|
+
* shadow root; Verovio draws in `currentColor`, but its inner page svg carries `color="black"`, which `color:inherit` overrides.
|
|
16
|
+
*/
|
|
17
|
+
export declare const scoreStyles = ".bms-score{padding:1rem;color:inherit;background:transparent}\n.bms-score-page{width:100%}\n.bms-score-page svg{display:block;width:100%;height:auto}\n.bms-score-page svg{fill:currentColor}\n.bms-score-page svg svg{color:inherit}";
|
|
18
|
+
/** Text made safe for HTML content and quoted attribute values (`&` first, so nothing is escaped twice). */
|
|
19
|
+
export declare const escapeHtml: (text: string) => string;
|
|
20
|
+
/**
|
|
21
|
+
* The cleaned pages in a themed pane: render's `.bm-chart` supplies the
|
|
22
|
+
* palette. The score is one image to assistive technology, named `label`
|
|
23
|
+
* (the chart's title, say), or "Score".
|
|
24
|
+
*/
|
|
25
|
+
export declare function scorePane(svg: string, theme: ScoreTheme, label?: string): string;
|
package/dist/markup.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
const GEOMETRY = ["x", "y", "width", "height", "transform", "class", "id"];
|
|
2
|
+
/** The elements Verovio emits, each with the attributes it may carry; everything else goes. */
|
|
3
|
+
const ALLOWED = {
|
|
4
|
+
div: ["class"],
|
|
5
|
+
svg: [
|
|
6
|
+
...GEOMETRY,
|
|
7
|
+
"viewBox",
|
|
8
|
+
"version",
|
|
9
|
+
"xmlns",
|
|
10
|
+
"xmlns:xlink",
|
|
11
|
+
"overflow",
|
|
12
|
+
"color",
|
|
13
|
+
"font-family",
|
|
14
|
+
],
|
|
15
|
+
g: ["id", "class", "transform"],
|
|
16
|
+
defs: [],
|
|
17
|
+
desc: [],
|
|
18
|
+
title: ["class"],
|
|
19
|
+
symbol: ["id", "viewBox"],
|
|
20
|
+
clipPath: ["id"],
|
|
21
|
+
path: ["transform", "d", "stroke-width", "stroke-linecap", "stroke-linejoin", "class"],
|
|
22
|
+
text: ["x", "y", "text-anchor", "font-size", "class"],
|
|
23
|
+
tspan: [
|
|
24
|
+
"id",
|
|
25
|
+
"class",
|
|
26
|
+
"font-size",
|
|
27
|
+
"font-weight",
|
|
28
|
+
"font-style",
|
|
29
|
+
"x",
|
|
30
|
+
"y",
|
|
31
|
+
"text-anchor",
|
|
32
|
+
"font-family",
|
|
33
|
+
"fill",
|
|
34
|
+
"xml:space",
|
|
35
|
+
],
|
|
36
|
+
use: ["xlink:href", "href", "transform", "x", "y"],
|
|
37
|
+
ellipse: ["cx", "cy", "rx", "ry", "transform"],
|
|
38
|
+
rect: ["x", "y", "height", "width", "transform"],
|
|
39
|
+
polygon: ["points"],
|
|
40
|
+
polyline: ["points", "stroke-width"],
|
|
41
|
+
line: ["x1", "y1", "x2", "y2", "stroke-width"],
|
|
42
|
+
style: ["type"],
|
|
43
|
+
};
|
|
44
|
+
/** The stylesheets Verovio writes (one with a random id, one embedding its font); any other `<style>` is dropped. */
|
|
45
|
+
const STYLE_ID = /^#([A-Za-z0-9]+) g\.ending,/;
|
|
46
|
+
const STYLE_TEMPLATE = "#@ g.ending, #@ g.fing, #@ g.reh, #@ g.tempo {font-weight:bold;}#@ g.dir, #@ g.dynam, #@ g.mNum {font-style:italic;}#@ g.label {font-weight:normal;}#@ ellipse, #@ path, #@ polygon, #@ polyline, #@ rect {stroke:currentColor}#@ g.cursor {fill:dodgerblue; color:dodgerblue;}#@ g.cursor.chord {fill:limegreen; color:limegreen;}#@ g.placeholder {visibility:hidden}";
|
|
47
|
+
/** The one embedded font Verovio adds for some scores: Leipzig as a base64 woff2, nothing else. */
|
|
48
|
+
const FONT_FACE = /^@font-face \{\s*font-family: 'Leipzig';\s*src: url\(data:application\/font-woff2;charset=utf-8;base64,[A-Za-z0-9+/=]+\) format\('woff2'\);\s*font-weight: normal;\s*font-style: normal;\s*\}$/;
|
|
49
|
+
/** Whether a stylesheet is Verovio's embedded Leipzig face, exactly (and nothing else). */
|
|
50
|
+
export const isLeipzigFace = (css) => FONT_FACE.test(css);
|
|
51
|
+
function isVerovioStyle(css) {
|
|
52
|
+
if (isLeipzigFace(css))
|
|
53
|
+
return true;
|
|
54
|
+
const id = STYLE_ID.exec(css)?.[1];
|
|
55
|
+
return id !== undefined && css === STYLE_TEMPLATE.replaceAll("@", id);
|
|
56
|
+
}
|
|
57
|
+
const NAME = /^[A-Za-z][A-Za-z0-9:_.-]*/;
|
|
58
|
+
const WHITESPACE = /\s/;
|
|
59
|
+
class Unclean extends Error {
|
|
60
|
+
}
|
|
61
|
+
function fail(why) {
|
|
62
|
+
throw new Unclean(`The score markup is not clean SVG: ${why}`);
|
|
63
|
+
}
|
|
64
|
+
/** Elements whose content the HTML parser reads as raw text, even when written `<x/>`. */
|
|
65
|
+
const RAW_TEXT = new Set([
|
|
66
|
+
"style",
|
|
67
|
+
"script",
|
|
68
|
+
"title",
|
|
69
|
+
"textarea",
|
|
70
|
+
"xmp",
|
|
71
|
+
"iframe",
|
|
72
|
+
"noembed",
|
|
73
|
+
"noframes",
|
|
74
|
+
"noscript",
|
|
75
|
+
"plaintext",
|
|
76
|
+
]);
|
|
77
|
+
function refuseSelfClosingRawText(tag) {
|
|
78
|
+
if (tag.selfClosing && RAW_TEXT.has(tag.name.toLowerCase()))
|
|
79
|
+
fail("a self-closing raw-text element");
|
|
80
|
+
}
|
|
81
|
+
function keepAttribute(name, value) {
|
|
82
|
+
// Verovio writes no entities in attributes; a character reference could decode to `url(` or `javascript:`.
|
|
83
|
+
if (value.includes("&"))
|
|
84
|
+
return false;
|
|
85
|
+
if ((name === "href" || name === "xlink:href") && !value.startsWith("#"))
|
|
86
|
+
return false;
|
|
87
|
+
if (/url\(/i.test(value.replace(/url\(\s*#/gi, "")))
|
|
88
|
+
return false;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
const skipSpace = (src, from) => {
|
|
92
|
+
let p = from;
|
|
93
|
+
while (WHITESPACE.test(src[p] ?? "x"))
|
|
94
|
+
p++;
|
|
95
|
+
return p;
|
|
96
|
+
};
|
|
97
|
+
/** A quoted attribute value from `q`, free of `<`. */
|
|
98
|
+
function readValue(src, q) {
|
|
99
|
+
const quote = src[q];
|
|
100
|
+
if (quote !== '"' && quote !== "'")
|
|
101
|
+
fail("an unquoted attribute value");
|
|
102
|
+
const close = src.indexOf(quote, q + 1);
|
|
103
|
+
if (close < 0)
|
|
104
|
+
fail("an unbalanced quote");
|
|
105
|
+
const value = src.slice(q + 1, close);
|
|
106
|
+
if (value.includes("<"))
|
|
107
|
+
fail("a < in an attribute value");
|
|
108
|
+
return { value, end: close + 1 };
|
|
109
|
+
}
|
|
110
|
+
/** One `name="value"` from `p`. */
|
|
111
|
+
function readAttribute(src, p) {
|
|
112
|
+
const name = /^[^\s/>=<"']+/.exec(src.slice(p))?.[0] ?? fail("a bad attribute name");
|
|
113
|
+
const eq = skipSpace(src, p + name.length);
|
|
114
|
+
if (src[eq] !== "=")
|
|
115
|
+
fail("an attribute with no value");
|
|
116
|
+
return { name, ...readValue(src, skipSpace(src, eq + 1)) };
|
|
117
|
+
}
|
|
118
|
+
/** Where a start tag's attributes stop (`>` or `/>`), reading each into `attrs`. */
|
|
119
|
+
function readAttributes(src, from, attrs) {
|
|
120
|
+
let p = from;
|
|
121
|
+
while (src[p] !== undefined && src[p] !== ">" && !(src[p] === "/" && src[p + 1] === ">")) {
|
|
122
|
+
if (src[p] === "/" || WHITESPACE.test(src[p] ?? "")) {
|
|
123
|
+
p++;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const attr = readAttribute(src, p);
|
|
127
|
+
attrs.push([attr.name, attr.value]);
|
|
128
|
+
p = attr.end;
|
|
129
|
+
}
|
|
130
|
+
if (src[p] === undefined)
|
|
131
|
+
fail("a tag that never ends");
|
|
132
|
+
return p;
|
|
133
|
+
}
|
|
134
|
+
/** One start tag from `i` (at `<`); `/` and whitespace both separate attributes. Throws on anything unclean. */
|
|
135
|
+
function readStartTag(src, i) {
|
|
136
|
+
const name = NAME.exec(src.slice(i + 1))?.[0] ?? fail("a tag with no name");
|
|
137
|
+
const attrs = [];
|
|
138
|
+
const p = readAttributes(src, i + 1 + name.length, attrs);
|
|
139
|
+
const selfClosing = src[p] === "/";
|
|
140
|
+
const spaced = selfClosing && WHITESPACE.test(src[p - 1] ?? "");
|
|
141
|
+
return { name, attrs, selfClosing, spaced, end: p + (selfClosing ? 2 : 1) };
|
|
142
|
+
}
|
|
143
|
+
function writeTag(tag) {
|
|
144
|
+
const allowed = ALLOWED[tag.name] ?? [];
|
|
145
|
+
const attrs = tag.attrs
|
|
146
|
+
.filter(([n, v]) => allowed.includes(n) && keepAttribute(n, v))
|
|
147
|
+
.map(([n, v]) => ` ${n}="${v.replaceAll('"', """)}"`)
|
|
148
|
+
.join("");
|
|
149
|
+
return `<${tag.name}${attrs}${tag.selfClosing ? (tag.spaced ? " />" : "/>") : ">"}`;
|
|
150
|
+
}
|
|
151
|
+
/** A closing tag from `i`; returns where it ends, writing it if its element was written. */
|
|
152
|
+
function readEndTag(src, i, open, out) {
|
|
153
|
+
const name = NAME.exec(src.slice(i + 2))?.[0] ?? fail("a closing tag with no name");
|
|
154
|
+
const p = skipSpace(src, i + 2 + name.length);
|
|
155
|
+
if (src[p] !== ">")
|
|
156
|
+
fail("a closing tag that never ends");
|
|
157
|
+
const top = open.pop();
|
|
158
|
+
if (top?.name !== name)
|
|
159
|
+
fail("a closing tag that matches nothing");
|
|
160
|
+
if (top.emit)
|
|
161
|
+
out.push(`</${name}>`);
|
|
162
|
+
return p + 1;
|
|
163
|
+
}
|
|
164
|
+
/** A `<style>`'s text after `tag`: kept only if it is one of Verovio's. Returns where its end tag starts. */
|
|
165
|
+
function readStyle(src, tag, emit, open, out) {
|
|
166
|
+
const close = src.indexOf("</style", tag.end);
|
|
167
|
+
const css = src.slice(tag.end, close < 0 ? undefined : close);
|
|
168
|
+
if (close < 0 || css.includes("<"))
|
|
169
|
+
fail("a style that never ends");
|
|
170
|
+
const write = emit && isVerovioStyle(css);
|
|
171
|
+
if (write)
|
|
172
|
+
out.push(writeTag(tag), css);
|
|
173
|
+
open.push({ name: "style", emit: write });
|
|
174
|
+
return close;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Verovio's SVG reduced to what Verovio emits: only the listed elements and
|
|
178
|
+
* attributes survive, an unlisted element goes with its content, links keep
|
|
179
|
+
* only `#` targets. Hand-scanned, no DOM. Throws if the markup cannot be read
|
|
180
|
+
* cleanly (unbalanced quotes, comments, a stray `<`, unclosed elements): the
|
|
181
|
+
* caller treats that as "could not draw".
|
|
182
|
+
*/
|
|
183
|
+
export function cleanSvg(svg) {
|
|
184
|
+
const out = [];
|
|
185
|
+
// Open elements, each with whether it is written (false inside an unlisted element).
|
|
186
|
+
const open = [];
|
|
187
|
+
let i = 0;
|
|
188
|
+
while (i < svg.length) {
|
|
189
|
+
const lt = svg.indexOf("<", i);
|
|
190
|
+
if (open.every((o) => o.emit))
|
|
191
|
+
out.push(svg.slice(i, lt < 0 ? undefined : lt));
|
|
192
|
+
if (lt < 0)
|
|
193
|
+
break;
|
|
194
|
+
i = readTag(svg, lt, open, out);
|
|
195
|
+
}
|
|
196
|
+
if (open.length)
|
|
197
|
+
fail("an element that never closes");
|
|
198
|
+
return out.join("");
|
|
199
|
+
}
|
|
200
|
+
/** The tag at `i` (a `<`), written or dropped; returns where it ends. */
|
|
201
|
+
function readTag(svg, i, open, out) {
|
|
202
|
+
if (svg[i + 1] === "/")
|
|
203
|
+
return readEndTag(svg, i, open, out);
|
|
204
|
+
if (!/[A-Za-z]/.test(svg[i + 1] ?? ""))
|
|
205
|
+
fail("a < that starts no tag");
|
|
206
|
+
const emit = open.every((o) => o.emit);
|
|
207
|
+
const tag = readStartTag(svg, i);
|
|
208
|
+
refuseSelfClosingRawText(tag);
|
|
209
|
+
if (tag.name === "style" && !tag.selfClosing)
|
|
210
|
+
return readStyle(svg, tag, emit, open, out);
|
|
211
|
+
const write = emit && tag.name in ALLOWED;
|
|
212
|
+
if (write)
|
|
213
|
+
out.push(writeTag(tag));
|
|
214
|
+
if (!tag.selfClosing)
|
|
215
|
+
open.push({ name: tag.name, emit: write });
|
|
216
|
+
return tag.end;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The score's own layout. Colour comes from `@barmark/render`'s stylesheet
|
|
220
|
+
* (the `.bm-chart` wrapper that `scorePane` writes), which must be in the same
|
|
221
|
+
* shadow root; Verovio draws in `currentColor`, but its inner page svg carries `color="black"`, which `color:inherit` overrides.
|
|
222
|
+
*/
|
|
223
|
+
export const scoreStyles = `.bms-score{padding:1rem;color:inherit;background:transparent}
|
|
224
|
+
.bms-score-page{width:100%}
|
|
225
|
+
.bms-score-page svg{display:block;width:100%;height:auto}
|
|
226
|
+
.bms-score-page svg{fill:currentColor}
|
|
227
|
+
.bms-score-page svg svg{color:inherit}`;
|
|
228
|
+
/** Text made safe for HTML content and quoted attribute values (`&` first, so nothing is escaped twice). */
|
|
229
|
+
export const escapeHtml = (text) => text
|
|
230
|
+
.replaceAll("&", "&")
|
|
231
|
+
.replaceAll("<", "<")
|
|
232
|
+
.replaceAll(">", ">")
|
|
233
|
+
.replaceAll('"', """)
|
|
234
|
+
.replaceAll("'", "'");
|
|
235
|
+
/**
|
|
236
|
+
* The cleaned pages in a themed pane: render's `.bm-chart` supplies the
|
|
237
|
+
* palette. The score is one image to assistive technology, named `label`
|
|
238
|
+
* (the chart's title, say), or "Score".
|
|
239
|
+
*/
|
|
240
|
+
export function scorePane(svg, theme, label = "Score") {
|
|
241
|
+
return `<div class="bm-chart" data-theme="${theme}"><div class="bms-score" role="img" aria-label="${escapeHtml(label)}">${cleanSvg(svg)}</div></div>`;
|
|
242
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type RenderOptions } from "@barmark/render";
|
|
2
|
+
import { type ScoreView, switchHtml } from "./switch.js";
|
|
3
|
+
export type ScoreOptions = RenderOptions & {
|
|
4
|
+
view?: ScoreView;
|
|
5
|
+
};
|
|
6
|
+
export { decodeCarried } from "./shell.js";
|
|
7
|
+
export { switchHtml };
|
|
8
|
+
/**
|
|
9
|
+
* `<barmark-score>` drawn on the server: the grid, the Grid / Score switch and
|
|
10
|
+
* the chart text, so the grid shows before any script runs. The switch checks
|
|
11
|
+
* Grid, since the grid is what shows; `view: "score"` is kept as the element's
|
|
12
|
+
* attribute, and the element switches to the score once it loads.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renderScoreHtml(text: string, options?: ScoreOptions): string;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The server entry. It imports only @barmark/render, ./shell.js, ./switch.js and ./markup.js:
|
|
2
|
+
// never ./index.js, whose engine reaches the Verovio worker.
|
|
3
|
+
import { fontFaceCss, renderChart, renderChartUnwrapped, } from "@barmark/render";
|
|
4
|
+
import { carry, scoreShell } from "./shell.js";
|
|
5
|
+
import { switchHtml, viewOf } from "./switch.js";
|
|
6
|
+
export { decodeCarried } from "./shell.js";
|
|
7
|
+
export { switchHtml };
|
|
8
|
+
/** The attributes `renderChart` writes for these options, so the two elements agree. */
|
|
9
|
+
function gridAttributes(options) {
|
|
10
|
+
const html = renderChart("", options);
|
|
11
|
+
return /^<barmark-chart([^>]*)>/.exec(html)?.[1] ?? "";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `<barmark-score>` drawn on the server: the grid, the Grid / Score switch and
|
|
15
|
+
* the chart text, so the grid shows before any script runs. The switch checks
|
|
16
|
+
* Grid, since the grid is what shows; `view: "score"` is kept as the element's
|
|
17
|
+
* attribute, and the element switches to the score once it loads.
|
|
18
|
+
*/
|
|
19
|
+
export function renderScoreHtml(text, options = {}) {
|
|
20
|
+
const { view: asked, fontUrl, ...gridOptions } = options;
|
|
21
|
+
const view = viewOf(asked);
|
|
22
|
+
const font = fontUrl ? `<style>${fontFaceCss(fontUrl)}</style>` : "";
|
|
23
|
+
const grid = renderChartUnwrapped(text, gridOptions);
|
|
24
|
+
const attrs = `${gridAttributes(gridOptions)}${view === "score" ? ' view="score"' : ""}`;
|
|
25
|
+
return `${font}<barmark-score${attrs}><template shadowrootmode="open">${scoreShell(grid, "grid")}</template><script type="text/barmark">${carry(text)}</script></barmark-score>`;
|
|
26
|
+
}
|
package/dist/shell.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ScoreView } from "./switch.js";
|
|
2
|
+
/** Chart text for a `<script>`: the two sequences that would end or hide it are written with a backslash. */
|
|
3
|
+
export declare const carry: (text: string) => string;
|
|
4
|
+
/** The text `renderScoreHtml` carried, as it was given. */
|
|
5
|
+
export declare const decodeCarried: (text: string) => string;
|
|
6
|
+
/** The shadow root's content around a drawn grid: styles, the switch, the grid, the (empty) score pane. */
|
|
7
|
+
export declare const scoreShell: (grid: string, view: ScoreView) => string;
|
|
8
|
+
/** A chart that could not be loaded, drawn where the grid would be, as `<barmark-chart>` draws it. */
|
|
9
|
+
export declare function gridError(message: string, theme: string): string;
|
package/dist/shell.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// What the server and the element both write inside `<barmark-score>`'s shadow root,
|
|
2
|
+
// so client-drawn and server-drawn markup are the same. Verovio-free, like ./server.js.
|
|
3
|
+
import { stylesheet } from "@barmark/render";
|
|
4
|
+
import { escapeHtml, scoreStyles } from "./markup.js";
|
|
5
|
+
import { switchHtml, switchStyles } from "./switch.js";
|
|
6
|
+
/** Chart text for a `<script>`: the two sequences that would end or hide it are written with a backslash. */
|
|
7
|
+
export const carry = (text) => text.replace(/<\/(script)/gi, "<\\/$1").replaceAll("<!--", "<\\!--");
|
|
8
|
+
/** The text `renderScoreHtml` carried, as it was given. */
|
|
9
|
+
export const decodeCarried = (text) => text.replace(/<\\\/(script)/gi, "</$1").replaceAll("<\\!--", "<!--");
|
|
10
|
+
/** 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
|
+
/** A chart that could not be loaded, drawn where the grid would be, as `<barmark-chart>` draws it. */
|
|
13
|
+
export function gridError(message, theme) {
|
|
14
|
+
const m = escapeHtml(message);
|
|
15
|
+
return `<div class="bm-chart" data-theme="${escapeHtml(theme)}"><div class="bm-error" title="${m}"><code></code><span class="bm-sr">Error: ${m}</span></div></div>`;
|
|
16
|
+
}
|
package/dist/switch.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type ScoreView = "grid" | "score";
|
|
2
|
+
/** Anything but `"score"` is the grid. */
|
|
3
|
+
export declare const viewOf: (value: unknown) => ScoreView;
|
|
4
|
+
/** The Grid / Score switch: a radio group of two buttons, the current view checked. */
|
|
5
|
+
export declare function switchHtml(view: ScoreView): string;
|
|
6
|
+
/**
|
|
7
|
+
* The switch's ink and rule: @barmark/render's palettes (its `PALETTES`, which it does not
|
|
8
|
+
* export; tests/switch.test.ts holds the two equal), with its `--barmark-ink` and
|
|
9
|
+
* `--barmark-rule` overrides. The switch sits outside render's `.bm-chart`, whose
|
|
10
|
+
* `data-theme` sets the palette, and render's `:host{all:initial}` resets its colour and
|
|
11
|
+
* font, so it keys the same choice off the element's own `theme` attribute.
|
|
12
|
+
*/
|
|
13
|
+
export declare const SWITCH_PALETTES: Record<"light" | "dark", {
|
|
14
|
+
ink: string;
|
|
15
|
+
rule: string;
|
|
16
|
+
}>;
|
|
17
|
+
/** The switch's own look, in its theme's colours and render's label font. */
|
|
18
|
+
export declare const switchStyles: string;
|
package/dist/switch.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Anything but `"score"` is the grid. */
|
|
2
|
+
export const viewOf = (value) => (value === "score" ? "score" : "grid");
|
|
3
|
+
/** The Grid / Score switch: a radio group of two buttons, the current view checked. */
|
|
4
|
+
export function switchHtml(view) {
|
|
5
|
+
const radio = (value, label) => `<button type="button" role="radio" data-view="${value}" aria-checked="${view === value}"${view === value ? "" : ' tabindex="-1"'}>${label}</button>`;
|
|
6
|
+
return `<div class="bms-switch" role="radiogroup" aria-label="View">${radio("grid", "Grid")}${radio("score", "Score")}</div>`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The switch's ink and rule: @barmark/render's palettes (its `PALETTES`, which it does not
|
|
10
|
+
* export; tests/switch.test.ts holds the two equal), with its `--barmark-ink` and
|
|
11
|
+
* `--barmark-rule` overrides. The switch sits outside render's `.bm-chart`, whose
|
|
12
|
+
* `data-theme` sets the palette, and render's `:host{all:initial}` resets its colour and
|
|
13
|
+
* font, so it keys the same choice off the element's own `theme` attribute.
|
|
14
|
+
*/
|
|
15
|
+
export const SWITCH_PALETTES = {
|
|
16
|
+
light: { ink: "#1d1d1f", rule: "#4a4a4a" },
|
|
17
|
+
dark: { ink: "#ece8e0", rule: "#8f897f" },
|
|
18
|
+
};
|
|
19
|
+
const colours = ({ ink, rule }) => `--bms-ink:var(--barmark-ink,${ink});--bms-rule:var(--barmark-rule,${rule});`;
|
|
20
|
+
const AUTO = ':host(:not([theme="light"]):not([theme="dark"]))';
|
|
21
|
+
/** The switch's own look, in its theme's colours and render's label font. */
|
|
22
|
+
export const switchStyles = `:host{${colours(SWITCH_PALETTES.light)}}
|
|
23
|
+
:host([theme="dark"]){${colours(SWITCH_PALETTES.dark)}}
|
|
24
|
+
@media (prefers-color-scheme:dark){${AUTO}{${colours(SWITCH_PALETTES.dark)}}}
|
|
25
|
+
@media print{:host,:host([theme="dark"]),${AUTO}{${colours(SWITCH_PALETTES.light)}}}
|
|
26
|
+
.bms-switch,.bms-note{font-family:var(--barmark-label-font,system-ui,-apple-system,"Segoe UI",sans-serif)}
|
|
27
|
+
.bms-switch{display:inline-flex;margin:0 0 .5rem;border:1px solid var(--bms-rule);border-radius:.4rem;overflow:hidden}
|
|
28
|
+
.bms-switch button{font:inherit;color:var(--bms-ink);background:transparent;border:0;padding:.25rem .75rem;cursor:pointer}
|
|
29
|
+
.bms-switch button[aria-checked="true"]{font-weight:bold;text-decoration:underline}
|
|
30
|
+
.bms-switch button:focus-visible{outline:2px solid currentColor;outline-offset:-2px}
|
|
31
|
+
.bms-switch button[aria-disabled="true"]{opacity:.5;cursor:not-allowed}
|
|
32
|
+
.bms-note{margin:0 0 .5rem;font-size:.875em;color:var(--bms-ink)}`;
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { VerovioToolkit } from "verovio/esm";
|
|
2
|
+
import type { Reply, Request } from "./engine.js";
|
|
3
|
+
type Toolkit = VerovioToolkit;
|
|
4
|
+
/** Run `load` once and keep its result; a load that fails is forgotten, so the next call tries again. */
|
|
5
|
+
export declare function loadOnce<A, T>(load: (arg: A) => Promise<T>): (arg: A) => Promise<T>;
|
|
6
|
+
/** Answer one request. Never throws: a failure is a reply with its reason. */
|
|
7
|
+
export declare function handle(request: Request, loaded: () => Promise<Toolkit>): Promise<Reply>;
|
|
8
|
+
export {};
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as esm from "verovio/esm";
|
|
2
|
+
import { VerovioToolkit } from "verovio/esm";
|
|
3
|
+
import { engraveChart } from "./engrave.js";
|
|
4
|
+
// Verovio's typings omit the log switch; it is exported all the same.
|
|
5
|
+
const { enableLog, LOG_OFF } = esm;
|
|
6
|
+
/** Run `load` once and keep its result; a load that fails is forgotten, so the next call tries again. */
|
|
7
|
+
export function loadOnce(load) {
|
|
8
|
+
let kept;
|
|
9
|
+
return (arg) => {
|
|
10
|
+
kept ??= load(arg).catch((err) => {
|
|
11
|
+
kept = undefined;
|
|
12
|
+
throw err;
|
|
13
|
+
});
|
|
14
|
+
return kept;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The page's one Verovio toolkit, loaded on first use and reused. By
|
|
19
|
+
* default the package's own `verovio/wasm`; when a request carries
|
|
20
|
+
* `verovioUrl` that module is imported instead. The first request whose
|
|
21
|
+
* load succeeds wins for the worker's life: later URLs are ignored.
|
|
22
|
+
*/
|
|
23
|
+
const ready = loadOnce(async (verovioUrl) => {
|
|
24
|
+
const m = verovioUrl
|
|
25
|
+
? await import(/* @vite-ignore */ verovioUrl)
|
|
26
|
+
: await import("verovio/wasm");
|
|
27
|
+
const module = await m.default();
|
|
28
|
+
enableLog(LOG_OFF, module);
|
|
29
|
+
return new VerovioToolkit(module);
|
|
30
|
+
});
|
|
31
|
+
/** Answer one request. Never throws: a failure is a reply with its reason. */
|
|
32
|
+
export async function handle(request, loaded) {
|
|
33
|
+
const { id, text, width } = request;
|
|
34
|
+
try {
|
|
35
|
+
const r = engraveChart(await loaded(), text, { width });
|
|
36
|
+
return r.ok
|
|
37
|
+
? { id, ok: true, svg: r.svg, pages: r.pages }
|
|
38
|
+
: { id, ok: false, reason: r.reason };
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
return { id, ok: false, reason: String(err) };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const scope = globalThis;
|
|
45
|
+
// Only a worker global answers messages: importing this in a page or a test has no effect.
|
|
46
|
+
if (typeof scope.WorkerGlobalScope === "function" &&
|
|
47
|
+
globalThis instanceof scope.WorkerGlobalScope) {
|
|
48
|
+
scope.onmessage = async (e) => {
|
|
49
|
+
const { verovioUrl } = e.data;
|
|
50
|
+
scope.postMessage(await handle(e.data, () => ready(verovioUrl)));
|
|
51
|
+
};
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@barmark/score",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Show a barmark chord chart as a full engraved score: MusicXML from @barmark/musicxml, engraved by Verovio (LGPL-3.0, a dependency loaded unchanged).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./element": {
|
|
12
|
+
"types": "./dist/element.d.ts",
|
|
13
|
+
"import": "./dist/element.js"
|
|
14
|
+
},
|
|
15
|
+
"./server": {
|
|
16
|
+
"types": "./dist/server.d.ts",
|
|
17
|
+
"import": "./dist/server.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"./dist/element.js"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"CHANGELOG.md"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=20"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"barmark",
|
|
33
|
+
"chord-chart",
|
|
34
|
+
"music",
|
|
35
|
+
"score",
|
|
36
|
+
"engraving",
|
|
37
|
+
"verovio",
|
|
38
|
+
"musicxml"
|
|
39
|
+
],
|
|
40
|
+
"author": "Kevin Peckham",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"homepage": "https://github.com/kevinpeckham/barmark/tree/main/packages/score#readme",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/kevinpeckham/barmark.git",
|
|
46
|
+
"directory": "packages/score"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/kevinpeckham/barmark/issues"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@barmark/musicxml": "^0.7.2",
|
|
53
|
+
"@barmark/render": "^0.17.1",
|
|
54
|
+
"barmark": "^0.21.0",
|
|
55
|
+
"verovio": "^6.4.1"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
59
|
+
"prepack": "bun run build"
|
|
60
|
+
}
|
|
7
61
|
}
|