@dsebastien/music-notation 0.20.0 → 0.21.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/README.md CHANGED
@@ -191,7 +191,7 @@ Add this to your site's `publish.js`. It loads the bundle only on pages that hav
191
191
 
192
192
  ```js
193
193
  ;(() => {
194
- const VERSION = '0.19.2'
194
+ const VERSION = '0.21.0'
195
195
  const CDN = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist`
196
196
  const selector = (languages) => languages.map((l) => `pre > code.language-${l}`).join(', ')
197
197
  const BLOCKS = selector([
@@ -34,6 +34,12 @@ export interface RenderOptions extends LayoutDefaults {
34
34
  * bar of its own under each block that draws (it copies the whole block). `false` leaves it out.
35
35
  */
36
36
  copy?: boolean;
37
+ /**
38
+ * Full screen (#113), on by default: a button on each block's bar that makes the block fill
39
+ * the screen, zoomed to fit it. Without `play`, it goes on the small bar of Copy as image.
40
+ * `false` leaves it out. Tabs and scores have their own (`renderTab`'s `fullScreen`).
41
+ */
42
+ fullScreen?: boolean;
37
43
  /**
38
44
  * The chords used row of `chords` blocks, when a chart has no `{diagrams}`: `'top'`,
39
45
  * `'bottom'` or `'off'` (the default). Guitar or piano diagrams follow `instrument`.
package/dist/index.js CHANGED
@@ -4502,6 +4502,12 @@ function lightPlayer(win, random = Math.random) {
4502
4502
 
4503
4503
  // src/playback/follow.ts
4504
4504
  var PLAYING_CLASS = "mn-playing";
4505
+ function raise(el) {
4506
+ const group = el.closest("[data-mn-raise]");
4507
+ const parent = group?.parentNode;
4508
+ if (group && parent && parent.lastChild !== group)
4509
+ parent.appendChild(group);
4510
+ }
4505
4511
  var START_WAIT = 300;
4506
4512
  var reporting = new WeakSet;
4507
4513
  function startsReported(player) {
@@ -4614,6 +4620,7 @@ function followNotes(win, drawing, targets, events, wait = START_WAIT) {
4614
4620
  if (!lit.has(el)) {
4615
4621
  el.classList.add(PLAYING_CLASS);
4616
4622
  lit.add(el);
4623
+ raise(el);
4617
4624
  }
4618
4625
  if (next === Infinity)
4619
4626
  return end();
@@ -6135,6 +6142,242 @@ async function toPng(el, options = {}) {
6135
6142
  return new Promise((resolve, reject) => canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("The image could not be made")), "image/png"));
6136
6143
  }
6137
6144
 
6145
+ // src/playback/full-screen.ts
6146
+ var SCROLL_KEYS = new Set(["PageUp", "PageDown", "Home", "End", "ArrowUp", "ArrowDown"]);
6147
+ function backgroundBehind(el) {
6148
+ const win = el.ownerDocument.defaultView;
6149
+ if (!win)
6150
+ return "";
6151
+ for (let node = el.parentElement;node; node = node.parentElement) {
6152
+ const color = win.getComputedStyle(node).backgroundColor;
6153
+ if (color && color !== "transparent" && !/^rgba\(.*,\s*0\)$/.test(color))
6154
+ return color;
6155
+ }
6156
+ return "";
6157
+ }
6158
+ function fullScreenControl(doc, block, surface, follow = () => {}, options = {}) {
6159
+ const redraws = options.redraws !== false;
6160
+ const win = doc.defaultView;
6161
+ const button = iconButton(doc, "mn-tab-full", "Full screen", "full");
6162
+ button.classList.add("mn-play-full");
6163
+ let on = false;
6164
+ let browser = false;
6165
+ let following = false;
6166
+ let before = null;
6167
+ let place = null;
6168
+ let held = false;
6169
+ let ownMinHeight = "";
6170
+ const setFollow = (yes) => {
6171
+ if (following === yes)
6172
+ return;
6173
+ following = yes;
6174
+ follow(yes);
6175
+ };
6176
+ const show = () => {
6177
+ block.classList.toggle("mn-fullscreen", on);
6178
+ button.classList.toggle("mn-on", on);
6179
+ button.setAttribute("aria-pressed", String(on));
6180
+ button.title = on ? "Exit full screen" : "Full screen";
6181
+ button.replaceChildren(icon(doc, on ? "exit" : "full"));
6182
+ };
6183
+ const enter = () => {
6184
+ if (on)
6185
+ return;
6186
+ on = true;
6187
+ before = {
6188
+ x: win?.scrollX ?? 0,
6189
+ y: win?.scrollY ?? 0,
6190
+ background: block.style.backgroundColor
6191
+ };
6192
+ if (!held)
6193
+ ownMinHeight = block.style.minHeight;
6194
+ held = false;
6195
+ place = doc.createElement("div");
6196
+ place.className = "mn-fullscreen-place";
6197
+ place.style.height = `${block.offsetHeight}px`;
6198
+ const margins = win?.getComputedStyle(block);
6199
+ if (margins) {
6200
+ place.style.marginTop = margins.marginTop;
6201
+ place.style.marginBottom = margins.marginBottom;
6202
+ }
6203
+ block.before(place);
6204
+ const background = backgroundBehind(block);
6205
+ if (background)
6206
+ block.style.backgroundColor = background;
6207
+ show();
6208
+ surface.tabIndex = 0;
6209
+ surface.focus({ preventScroll: true });
6210
+ setFollow(true);
6211
+ options.changed?.(true);
6212
+ try {
6213
+ block.requestFullscreen?.().catch(() => {});
6214
+ } catch {}
6215
+ };
6216
+ const leave = (focus = true) => {
6217
+ if (!on)
6218
+ return;
6219
+ on = false;
6220
+ setFollow(false);
6221
+ show();
6222
+ block.style.backgroundColor = before?.background ?? "";
6223
+ if (place) {
6224
+ if (redraws) {
6225
+ block.style.minHeight = place.style.height;
6226
+ held = true;
6227
+ }
6228
+ place.remove();
6229
+ place = null;
6230
+ }
6231
+ surface.removeAttribute("tabindex");
6232
+ surface.scrollTop = 0;
6233
+ if (browser && doc.fullscreenElement === block)
6234
+ doc.exitFullscreen?.().catch(() => {});
6235
+ browser = false;
6236
+ if (before && win)
6237
+ win.scrollTo(before.x, before.y);
6238
+ before = null;
6239
+ options.changed?.(false);
6240
+ if (focus)
6241
+ button.focus({ preventScroll: true });
6242
+ };
6243
+ button.addEventListener("click", () => on ? leave() : enter());
6244
+ block.addEventListener("keydown", (event) => {
6245
+ if (!on || event.key !== "Escape")
6246
+ return;
6247
+ event.stopPropagation();
6248
+ event.preventDefault();
6249
+ leave();
6250
+ });
6251
+ const byHand = () => {
6252
+ if (on)
6253
+ setFollow(false);
6254
+ };
6255
+ surface.addEventListener("wheel", byHand, { passive: true });
6256
+ surface.addEventListener("touchmove", byHand, { passive: true });
6257
+ surface.addEventListener("keydown", (event) => {
6258
+ if (SCROLL_KEYS.has(event.key))
6259
+ byHand();
6260
+ });
6261
+ const space = (event) => {
6262
+ if (!on || event.key !== " " || event.ctrlKey || event.metaKey || event.altKey)
6263
+ return;
6264
+ if (!block.isConnected) {
6265
+ leave(false);
6266
+ return;
6267
+ }
6268
+ const target = event.target;
6269
+ if (target?.closest("input:not([type=range]), textarea, select, [contenteditable=true]"))
6270
+ return;
6271
+ event.preventDefault();
6272
+ event.stopPropagation();
6273
+ if (event.type !== "keydown" || event.repeat)
6274
+ return;
6275
+ block.querySelector(".mn-play .mn-play-toggle")?.click();
6276
+ };
6277
+ doc.addEventListener("keydown", space, true);
6278
+ doc.addEventListener("keyup", space, true);
6279
+ const changed = () => {
6280
+ if (doc.fullscreenElement === block)
6281
+ browser = true;
6282
+ else if (browser)
6283
+ leave();
6284
+ };
6285
+ doc.addEventListener("fullscreenchange", changed);
6286
+ show();
6287
+ return {
6288
+ button,
6289
+ active: () => on,
6290
+ playing: () => {
6291
+ if (on)
6292
+ setFollow(true);
6293
+ },
6294
+ drawn: () => {
6295
+ if (on || !held)
6296
+ return;
6297
+ held = false;
6298
+ block.style.minHeight = ownMinHeight;
6299
+ },
6300
+ destroy: () => {
6301
+ leave(false);
6302
+ doc.removeEventListener("fullscreenchange", changed);
6303
+ doc.removeEventListener("keydown", space, true);
6304
+ doc.removeEventListener("keyup", space, true);
6305
+ }
6306
+ };
6307
+ }
6308
+ var ZOOMS = [4, 3.5, 3, 2.5, 2.25, 2, 1.75, 1.5, 1.25, 1];
6309
+ var TALL_ZOOM = 1.5;
6310
+ function chooseZoom(fits) {
6311
+ const wide = [];
6312
+ for (const zoom of ZOOMS) {
6313
+ const fit = fits(zoom);
6314
+ if (fit.width && fit.height)
6315
+ return zoom;
6316
+ if (fit.width)
6317
+ wide.push(zoom);
6318
+ }
6319
+ return wide.find((zoom) => zoom <= TALL_ZOOM) ?? 1;
6320
+ }
6321
+ function blockFullScreen(doc, block) {
6322
+ const win = doc.defaultView;
6323
+ let frame = 0;
6324
+ const fit = () => {
6325
+ frame = 0;
6326
+ if (!block.classList.contains("mn-fullscreen"))
6327
+ return;
6328
+ const zoom = chooseZoom((z) => {
6329
+ block.style.setProperty("--mn-zoom", String(z));
6330
+ return {
6331
+ width: block.scrollWidth <= block.clientWidth + 1,
6332
+ height: block.scrollHeight <= block.clientHeight + 1
6333
+ };
6334
+ });
6335
+ block.style.setProperty("--mn-zoom", String(zoom));
6336
+ for (const bar of nested())
6337
+ bar.style.setProperty("zoom", String(1 / zoom));
6338
+ };
6339
+ const nested = () => [
6340
+ ...block.querySelectorAll(":scope > :not(.mn-play) .mn-play")
6341
+ ];
6342
+ const later = () => {
6343
+ if (!win || frame)
6344
+ return;
6345
+ frame = win.requestAnimationFrame(fit);
6346
+ };
6347
+ const control = fullScreenControl(doc, block, block, undefined, {
6348
+ redraws: false,
6349
+ changed: (on) => {
6350
+ block.classList.toggle("mn-fullscreen-fit", on);
6351
+ if (on) {
6352
+ fit();
6353
+ later();
6354
+ win?.addEventListener("resize", later);
6355
+ } else {
6356
+ win?.removeEventListener("resize", later);
6357
+ block.style.removeProperty("--mn-zoom");
6358
+ for (const bar of nested())
6359
+ bar.style.removeProperty("zoom");
6360
+ }
6361
+ }
6362
+ });
6363
+ return {
6364
+ ...control,
6365
+ destroy: () => {
6366
+ control.destroy();
6367
+ win?.removeEventListener("resize", later);
6368
+ if (frame)
6369
+ win?.cancelAnimationFrame(frame);
6370
+ }
6371
+ };
6372
+ }
6373
+ function addFullScreen(doc, bar, block, on = true) {
6374
+ if (!on || !block || block.querySelector(".mn-play-full"))
6375
+ return () => {};
6376
+ const full = blockFullScreen(doc, block);
6377
+ bar.appendChild(full.button);
6378
+ return () => full.destroy();
6379
+ }
6380
+
6138
6381
  // src/playback/copy-image.ts
6139
6382
  var COPY_SAID = {
6140
6383
  copied: "Copied",
@@ -6192,13 +6435,16 @@ function copyImageButton(doc, win, target, make = (el) => toPng(el)) {
6192
6435
  return [button, said];
6193
6436
  }
6194
6437
  var NOT_DRAWN_HERE = ".mn-tab, .mn-tab-holder, .mn-tab-surface, .mn-exercise-tab";
6195
- function attachCopy(el, make) {
6438
+ function attachCopy(el, make, options = {}) {
6196
6439
  const doc = el.ownerDocument;
6197
6440
  const win = doc.defaultView;
6198
6441
  if (!win)
6199
6442
  return () => {};
6200
6443
  const blocks = el.classList.contains("mn-block") ? [el] : [...el.querySelectorAll(".mn-block")];
6201
6444
  const bars = [];
6445
+ const frees = [];
6446
+ if (options.copy === false && options.fullScreen === false)
6447
+ return () => {};
6202
6448
  for (const block of blocks) {
6203
6449
  if (block.querySelector(".mn-play"))
6204
6450
  continue;
@@ -6207,7 +6453,9 @@ function attachCopy(el, make) {
6207
6453
  if (!drawings.length && !timed)
6208
6454
  continue;
6209
6455
  const bar = playBar(doc, "mn-copy-bar", "Image");
6210
- bar.append(...copyImageButton(doc, win, () => block, make));
6456
+ if (options.copy !== false)
6457
+ bar.append(...copyImageButton(doc, win, () => block, make));
6458
+ frees.push(addFullScreen(doc, bar, block, options.fullScreen));
6211
6459
  if (timed)
6212
6460
  block.appendChild(bar);
6213
6461
  else
@@ -6215,6 +6463,8 @@ function attachCopy(el, make) {
6215
6463
  bars.push(bar);
6216
6464
  }
6217
6465
  return () => {
6466
+ for (const free of frees)
6467
+ free();
6218
6468
  for (const bar of bars)
6219
6469
  bar.remove();
6220
6470
  };
@@ -6371,6 +6621,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6371
6621
  bar.appendChild(choice(doc, "mn-play-sound", "Sound", ["guitar", "piano"], settings.sound, { guitar: "Guitar", piano: "Piano" }, (value) => settings.sound = value === "piano" ? "piano" : "guitar"));
6372
6622
  if (options.copy !== false)
6373
6623
  bar.append(...copyImageButton(doc, win, () => wrap.closest(".mn-block") ?? wrap));
6624
+ const freeFull = addFullScreen(doc, bar, wrap.closest(".mn-block"), options.fullScreen);
6374
6625
  const app = handToApp(options, bar, {
6375
6626
  kind: piece.kind,
6376
6627
  drawing,
@@ -6379,6 +6630,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6379
6630
  });
6380
6631
  return () => {
6381
6632
  stop();
6633
+ freeFull();
6382
6634
  app.free();
6383
6635
  };
6384
6636
  }
@@ -6529,6 +6781,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6529
6781
  }));
6530
6782
  if (options.copy !== false)
6531
6783
  bar.append(...copyImageButton(doc, win, () => drawing.closest(".mn-block") ?? drawing));
6784
+ const freeFull = addFullScreen(doc, bar, drawing.closest(".mn-block"), options.fullScreen);
6532
6785
  const app = handToApp(options, bar, {
6533
6786
  kind: drawing.classList.contains("mn-chart") ? "chart" : "progression",
6534
6787
  drawing,
@@ -6537,6 +6790,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6537
6790
  });
6538
6791
  return () => {
6539
6792
  stop();
6793
+ freeFull();
6540
6794
  if (diagrams?.onPlay === stop) {
6541
6795
  diagrams.onPlay = null;
6542
6796
  diagrams.stopOthers = null;
@@ -7432,6 +7686,7 @@ function blockBarFor(doc, win, block, diagrams, options) {
7432
7686
  bar.appendChild(sound);
7433
7687
  if (options.copy !== false)
7434
7688
  bar.append(...copyImageButton(doc, win, (whole) => whole || !several ? block : diagramImage(made.selected.wrap)));
7689
+ const freeFull = addFullScreen(doc, bar, block, options.fullScreen);
7435
7690
  bind({
7436
7691
  changed() {
7437
7692
  const busy = made.busy;
@@ -7452,7 +7707,10 @@ function blockBarFor(doc, win, block, diagrams, options) {
7452
7707
  sound.value = made.sound;
7453
7708
  }
7454
7709
  });
7455
- return () => group.destroy();
7710
+ return () => {
7711
+ freeFull();
7712
+ group.destroy();
7713
+ };
7456
7714
  }
7457
7715
 
7458
7716
  // src/playback/controls.ts
@@ -11011,7 +11269,9 @@ function renderCircle(doc, options = {}) {
11011
11269
  const title = svg(doc, "title");
11012
11270
  title.textContent = chord ? `${chord.numeral}: ${name}` : name.replace("/", " or ");
11013
11271
  cell.appendChild(title);
11014
- g.appendChild(cell);
11272
+ const slot = svg(doc, "g", { class: "mn-circle-slot", "data-mn-raise": "" });
11273
+ slot.appendChild(cell);
11274
+ g.appendChild(slot);
11015
11275
  const mid = (outer + inner) / 2;
11016
11276
  const solid = mark === " mn-circle-tonic" || mark === " mn-circle-neighbour";
11017
11277
  const textClass = `mn-circle-label mn-circle-label-${ring}${solid ? " mn-circle-on" : ""}`;
@@ -11022,7 +11282,7 @@ function renderCircle(doc, options = {}) {
11022
11282
  lines.forEach((line, i) => {
11023
11283
  const [x, y] = point(mid, position * 30, top);
11024
11284
  const isNumeral = chord !== undefined && i === lines.length - 1;
11025
- g.appendChild(svgText(doc, line, {
11285
+ slot.appendChild(svgText(doc, line, {
11026
11286
  class: isNumeral ? `${textClass} mn-circle-numeral` : textClass,
11027
11287
  x,
11028
11288
  y: y + 4 + (i - (lines.length - 1) / 2) * step,
@@ -12492,11 +12752,13 @@ function renderBlock(language, source, host, given = {}) {
12492
12752
  }
12493
12753
  const picks = options.pick ? [...el.querySelectorAll("svg[data-mn-pick]")].map((svg) => attachPick(svg, pickOptions(options))) : [];
12494
12754
  const copy = options.copy !== false;
12755
+ const fullScreen = options.fullScreen !== false;
12495
12756
  const stopPlayback = options.play ? attachPlayback(el, {
12496
12757
  hand: options.hand,
12497
12758
  ...typeof options.play === "object" ? options.play : {},
12498
- copy
12499
- }) : copy ? attachCopy(el) : null;
12759
+ copy,
12760
+ fullScreen
12761
+ }) : copy || fullScreen ? attachCopy(el, undefined, { copy, fullScreen }) : null;
12500
12762
  return {
12501
12763
  ok: error === null,
12502
12764
  el,
@@ -13541,7 +13803,7 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
13541
13803
  .mn-play input[type='range'] {
13542
13804
  all: unset;
13543
13805
  appearance: none;
13544
- width: 44px;
13806
+ width: 36px;
13545
13807
  /* As tall as the thumb, so no host or browser has to center it: the visible 3px track is a
13546
13808
  centered line drawn by the background. */
13547
13809
  height: 9px;
@@ -14478,7 +14740,8 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
14478
14740
  max-width: none;
14479
14741
  height: auto;
14480
14742
  margin: 0;
14481
- padding: 12px 16px 8px;
14743
+ /* Room on the sides, more on a wide screen. */
14744
+ padding: 12px max(24px, 3vw) 8px;
14482
14745
  overflow: hidden;
14483
14746
  background: Canvas;
14484
14747
  }
@@ -14498,6 +14761,54 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
14498
14761
  flex: none;
14499
14762
  margin-top: 8px;
14500
14763
  }
14764
+
14765
+ /* Full screen for the other blocks (#113): diagrams, charts, patterns, progressions, the circle of
14766
+ fifths. What the block holds is zoomed to the largest size that fits the screen (the control
14767
+ sets --mn-zoom), centered; a block that can't fit scrolls. The bar keeps its size, at the
14768
+ bottom. */
14769
+ .mn-block.mn-fullscreen-fit {
14770
+ position: fixed;
14771
+ inset: 0;
14772
+ z-index: 9999;
14773
+ display: flex;
14774
+ flex-direction: column;
14775
+ align-items: center;
14776
+ box-sizing: border-box;
14777
+ width: auto;
14778
+ max-width: none;
14779
+ height: auto;
14780
+ margin: 0;
14781
+ padding: 16px max(32px, 4vw) 0;
14782
+ overflow: auto;
14783
+ overscroll-behavior: contain;
14784
+ background: Canvas;
14785
+ outline: none;
14786
+ }
14787
+
14788
+ .mn-block.mn-fullscreen-fit > :not(.mn-play) {
14789
+ zoom: var(--mn-zoom, 1);
14790
+ flex: none;
14791
+ }
14792
+
14793
+ /* A row of diagrams is centered on the screen. */
14794
+ .mn-block.mn-fullscreen-fit .mn-row {
14795
+ justify-content: center;
14796
+ }
14797
+
14798
+ /* Centered while it fits; from the top when it scrolls. */
14799
+ .mn-block.mn-fullscreen-fit > :first-child {
14800
+ margin-top: auto;
14801
+ }
14802
+
14803
+ .mn-block.mn-fullscreen-fit > .mn-play {
14804
+ position: sticky;
14805
+ bottom: 0;
14806
+ flex: none;
14807
+ align-self: stretch;
14808
+ margin-top: auto;
14809
+ padding: 6px 0 8px;
14810
+ background: inherit;
14811
+ }
14501
14812
  `;
14502
14813
 
14503
14814
  // src/styles.ts
@@ -16124,5 +16435,5 @@ export {
16124
16435
  withTonicizations
16125
16436
  };
16126
16437
 
16127
- //# debugId=BAB348232536EC5A64756E2164756E21
16438
+ //# debugId=454C7243D7F5E48F64756E2164756E21
16128
16439
  //# sourceMappingURL=index.js.map