@dsebastien/music-notation 0.20.0 → 0.21.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/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.20.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
@@ -6135,6 +6135,242 @@ async function toPng(el, options = {}) {
6135
6135
  return new Promise((resolve, reject) => canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("The image could not be made")), "image/png"));
6136
6136
  }
6137
6137
 
6138
+ // src/playback/full-screen.ts
6139
+ var SCROLL_KEYS = new Set(["PageUp", "PageDown", "Home", "End", "ArrowUp", "ArrowDown"]);
6140
+ function backgroundBehind(el) {
6141
+ const win = el.ownerDocument.defaultView;
6142
+ if (!win)
6143
+ return "";
6144
+ for (let node = el.parentElement;node; node = node.parentElement) {
6145
+ const color = win.getComputedStyle(node).backgroundColor;
6146
+ if (color && color !== "transparent" && !/^rgba\(.*,\s*0\)$/.test(color))
6147
+ return color;
6148
+ }
6149
+ return "";
6150
+ }
6151
+ function fullScreenControl(doc, block, surface, follow = () => {}, options = {}) {
6152
+ const redraws = options.redraws !== false;
6153
+ const win = doc.defaultView;
6154
+ const button = iconButton(doc, "mn-tab-full", "Full screen", "full");
6155
+ button.classList.add("mn-play-full");
6156
+ let on = false;
6157
+ let browser = false;
6158
+ let following = false;
6159
+ let before = null;
6160
+ let place = null;
6161
+ let held = false;
6162
+ let ownMinHeight = "";
6163
+ const setFollow = (yes) => {
6164
+ if (following === yes)
6165
+ return;
6166
+ following = yes;
6167
+ follow(yes);
6168
+ };
6169
+ const show = () => {
6170
+ block.classList.toggle("mn-fullscreen", on);
6171
+ button.classList.toggle("mn-on", on);
6172
+ button.setAttribute("aria-pressed", String(on));
6173
+ button.title = on ? "Exit full screen" : "Full screen";
6174
+ button.replaceChildren(icon(doc, on ? "exit" : "full"));
6175
+ };
6176
+ const enter = () => {
6177
+ if (on)
6178
+ return;
6179
+ on = true;
6180
+ before = {
6181
+ x: win?.scrollX ?? 0,
6182
+ y: win?.scrollY ?? 0,
6183
+ background: block.style.backgroundColor
6184
+ };
6185
+ if (!held)
6186
+ ownMinHeight = block.style.minHeight;
6187
+ held = false;
6188
+ place = doc.createElement("div");
6189
+ place.className = "mn-fullscreen-place";
6190
+ place.style.height = `${block.offsetHeight}px`;
6191
+ const margins = win?.getComputedStyle(block);
6192
+ if (margins) {
6193
+ place.style.marginTop = margins.marginTop;
6194
+ place.style.marginBottom = margins.marginBottom;
6195
+ }
6196
+ block.before(place);
6197
+ const background = backgroundBehind(block);
6198
+ if (background)
6199
+ block.style.backgroundColor = background;
6200
+ show();
6201
+ surface.tabIndex = 0;
6202
+ surface.focus({ preventScroll: true });
6203
+ setFollow(true);
6204
+ options.changed?.(true);
6205
+ try {
6206
+ block.requestFullscreen?.().catch(() => {});
6207
+ } catch {}
6208
+ };
6209
+ const leave = (focus = true) => {
6210
+ if (!on)
6211
+ return;
6212
+ on = false;
6213
+ setFollow(false);
6214
+ show();
6215
+ block.style.backgroundColor = before?.background ?? "";
6216
+ if (place) {
6217
+ if (redraws) {
6218
+ block.style.minHeight = place.style.height;
6219
+ held = true;
6220
+ }
6221
+ place.remove();
6222
+ place = null;
6223
+ }
6224
+ surface.removeAttribute("tabindex");
6225
+ surface.scrollTop = 0;
6226
+ if (browser && doc.fullscreenElement === block)
6227
+ doc.exitFullscreen?.().catch(() => {});
6228
+ browser = false;
6229
+ if (before && win)
6230
+ win.scrollTo(before.x, before.y);
6231
+ before = null;
6232
+ options.changed?.(false);
6233
+ if (focus)
6234
+ button.focus({ preventScroll: true });
6235
+ };
6236
+ button.addEventListener("click", () => on ? leave() : enter());
6237
+ block.addEventListener("keydown", (event) => {
6238
+ if (!on || event.key !== "Escape")
6239
+ return;
6240
+ event.stopPropagation();
6241
+ event.preventDefault();
6242
+ leave();
6243
+ });
6244
+ const byHand = () => {
6245
+ if (on)
6246
+ setFollow(false);
6247
+ };
6248
+ surface.addEventListener("wheel", byHand, { passive: true });
6249
+ surface.addEventListener("touchmove", byHand, { passive: true });
6250
+ surface.addEventListener("keydown", (event) => {
6251
+ if (SCROLL_KEYS.has(event.key))
6252
+ byHand();
6253
+ });
6254
+ const space = (event) => {
6255
+ if (!on || event.key !== " " || event.ctrlKey || event.metaKey || event.altKey)
6256
+ return;
6257
+ if (!block.isConnected) {
6258
+ leave(false);
6259
+ return;
6260
+ }
6261
+ const target = event.target;
6262
+ if (target?.closest("input:not([type=range]), textarea, select, [contenteditable=true]"))
6263
+ return;
6264
+ event.preventDefault();
6265
+ event.stopPropagation();
6266
+ if (event.type !== "keydown" || event.repeat)
6267
+ return;
6268
+ block.querySelector(".mn-play .mn-play-toggle")?.click();
6269
+ };
6270
+ doc.addEventListener("keydown", space, true);
6271
+ doc.addEventListener("keyup", space, true);
6272
+ const changed = () => {
6273
+ if (doc.fullscreenElement === block)
6274
+ browser = true;
6275
+ else if (browser)
6276
+ leave();
6277
+ };
6278
+ doc.addEventListener("fullscreenchange", changed);
6279
+ show();
6280
+ return {
6281
+ button,
6282
+ active: () => on,
6283
+ playing: () => {
6284
+ if (on)
6285
+ setFollow(true);
6286
+ },
6287
+ drawn: () => {
6288
+ if (on || !held)
6289
+ return;
6290
+ held = false;
6291
+ block.style.minHeight = ownMinHeight;
6292
+ },
6293
+ destroy: () => {
6294
+ leave(false);
6295
+ doc.removeEventListener("fullscreenchange", changed);
6296
+ doc.removeEventListener("keydown", space, true);
6297
+ doc.removeEventListener("keyup", space, true);
6298
+ }
6299
+ };
6300
+ }
6301
+ var ZOOMS = [4, 3.5, 3, 2.5, 2.25, 2, 1.75, 1.5, 1.25, 1];
6302
+ var TALL_ZOOM = 1.5;
6303
+ function chooseZoom(fits) {
6304
+ const wide = [];
6305
+ for (const zoom of ZOOMS) {
6306
+ const fit = fits(zoom);
6307
+ if (fit.width && fit.height)
6308
+ return zoom;
6309
+ if (fit.width)
6310
+ wide.push(zoom);
6311
+ }
6312
+ return wide.find((zoom) => zoom <= TALL_ZOOM) ?? 1;
6313
+ }
6314
+ function blockFullScreen(doc, block) {
6315
+ const win = doc.defaultView;
6316
+ let frame = 0;
6317
+ const fit = () => {
6318
+ frame = 0;
6319
+ if (!block.classList.contains("mn-fullscreen"))
6320
+ return;
6321
+ const zoom = chooseZoom((z) => {
6322
+ block.style.setProperty("--mn-zoom", String(z));
6323
+ return {
6324
+ width: block.scrollWidth <= block.clientWidth + 1,
6325
+ height: block.scrollHeight <= block.clientHeight + 1
6326
+ };
6327
+ });
6328
+ block.style.setProperty("--mn-zoom", String(zoom));
6329
+ for (const bar of nested())
6330
+ bar.style.setProperty("zoom", String(1 / zoom));
6331
+ };
6332
+ const nested = () => [
6333
+ ...block.querySelectorAll(":scope > :not(.mn-play) .mn-play")
6334
+ ];
6335
+ const later = () => {
6336
+ if (!win || frame)
6337
+ return;
6338
+ frame = win.requestAnimationFrame(fit);
6339
+ };
6340
+ const control = fullScreenControl(doc, block, block, undefined, {
6341
+ redraws: false,
6342
+ changed: (on) => {
6343
+ block.classList.toggle("mn-fullscreen-fit", on);
6344
+ if (on) {
6345
+ fit();
6346
+ later();
6347
+ win?.addEventListener("resize", later);
6348
+ } else {
6349
+ win?.removeEventListener("resize", later);
6350
+ block.style.removeProperty("--mn-zoom");
6351
+ for (const bar of nested())
6352
+ bar.style.removeProperty("zoom");
6353
+ }
6354
+ }
6355
+ });
6356
+ return {
6357
+ ...control,
6358
+ destroy: () => {
6359
+ control.destroy();
6360
+ win?.removeEventListener("resize", later);
6361
+ if (frame)
6362
+ win?.cancelAnimationFrame(frame);
6363
+ }
6364
+ };
6365
+ }
6366
+ function addFullScreen(doc, bar, block, on = true) {
6367
+ if (!on || !block || block.querySelector(".mn-play-full"))
6368
+ return () => {};
6369
+ const full = blockFullScreen(doc, block);
6370
+ bar.appendChild(full.button);
6371
+ return () => full.destroy();
6372
+ }
6373
+
6138
6374
  // src/playback/copy-image.ts
6139
6375
  var COPY_SAID = {
6140
6376
  copied: "Copied",
@@ -6192,13 +6428,16 @@ function copyImageButton(doc, win, target, make = (el) => toPng(el)) {
6192
6428
  return [button, said];
6193
6429
  }
6194
6430
  var NOT_DRAWN_HERE = ".mn-tab, .mn-tab-holder, .mn-tab-surface, .mn-exercise-tab";
6195
- function attachCopy(el, make) {
6431
+ function attachCopy(el, make, options = {}) {
6196
6432
  const doc = el.ownerDocument;
6197
6433
  const win = doc.defaultView;
6198
6434
  if (!win)
6199
6435
  return () => {};
6200
6436
  const blocks = el.classList.contains("mn-block") ? [el] : [...el.querySelectorAll(".mn-block")];
6201
6437
  const bars = [];
6438
+ const frees = [];
6439
+ if (options.copy === false && options.fullScreen === false)
6440
+ return () => {};
6202
6441
  for (const block of blocks) {
6203
6442
  if (block.querySelector(".mn-play"))
6204
6443
  continue;
@@ -6207,7 +6446,9 @@ function attachCopy(el, make) {
6207
6446
  if (!drawings.length && !timed)
6208
6447
  continue;
6209
6448
  const bar = playBar(doc, "mn-copy-bar", "Image");
6210
- bar.append(...copyImageButton(doc, win, () => block, make));
6449
+ if (options.copy !== false)
6450
+ bar.append(...copyImageButton(doc, win, () => block, make));
6451
+ frees.push(addFullScreen(doc, bar, block, options.fullScreen));
6211
6452
  if (timed)
6212
6453
  block.appendChild(bar);
6213
6454
  else
@@ -6215,6 +6456,8 @@ function attachCopy(el, make) {
6215
6456
  bars.push(bar);
6216
6457
  }
6217
6458
  return () => {
6459
+ for (const free of frees)
6460
+ free();
6218
6461
  for (const bar of bars)
6219
6462
  bar.remove();
6220
6463
  };
@@ -6371,6 +6614,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6371
6614
  bar.appendChild(choice(doc, "mn-play-sound", "Sound", ["guitar", "piano"], settings.sound, { guitar: "Guitar", piano: "Piano" }, (value) => settings.sound = value === "piano" ? "piano" : "guitar"));
6372
6615
  if (options.copy !== false)
6373
6616
  bar.append(...copyImageButton(doc, win, () => wrap.closest(".mn-block") ?? wrap));
6617
+ const freeFull = addFullScreen(doc, bar, wrap.closest(".mn-block"), options.fullScreen);
6374
6618
  const app = handToApp(options, bar, {
6375
6619
  kind: piece.kind,
6376
6620
  drawing,
@@ -6379,6 +6623,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6379
6623
  });
6380
6624
  return () => {
6381
6625
  stop();
6626
+ freeFull();
6382
6627
  app.free();
6383
6628
  };
6384
6629
  }
@@ -6529,6 +6774,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6529
6774
  }));
6530
6775
  if (options.copy !== false)
6531
6776
  bar.append(...copyImageButton(doc, win, () => drawing.closest(".mn-block") ?? drawing));
6777
+ const freeFull = addFullScreen(doc, bar, drawing.closest(".mn-block"), options.fullScreen);
6532
6778
  const app = handToApp(options, bar, {
6533
6779
  kind: drawing.classList.contains("mn-chart") ? "chart" : "progression",
6534
6780
  drawing,
@@ -6537,6 +6783,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6537
6783
  });
6538
6784
  return () => {
6539
6785
  stop();
6786
+ freeFull();
6540
6787
  if (diagrams?.onPlay === stop) {
6541
6788
  diagrams.onPlay = null;
6542
6789
  diagrams.stopOthers = null;
@@ -7432,6 +7679,7 @@ function blockBarFor(doc, win, block, diagrams, options) {
7432
7679
  bar.appendChild(sound);
7433
7680
  if (options.copy !== false)
7434
7681
  bar.append(...copyImageButton(doc, win, (whole) => whole || !several ? block : diagramImage(made.selected.wrap)));
7682
+ const freeFull = addFullScreen(doc, bar, block, options.fullScreen);
7435
7683
  bind({
7436
7684
  changed() {
7437
7685
  const busy = made.busy;
@@ -7452,7 +7700,10 @@ function blockBarFor(doc, win, block, diagrams, options) {
7452
7700
  sound.value = made.sound;
7453
7701
  }
7454
7702
  });
7455
- return () => group.destroy();
7703
+ return () => {
7704
+ freeFull();
7705
+ group.destroy();
7706
+ };
7456
7707
  }
7457
7708
 
7458
7709
  // src/playback/controls.ts
@@ -12492,11 +12743,13 @@ function renderBlock(language, source, host, given = {}) {
12492
12743
  }
12493
12744
  const picks = options.pick ? [...el.querySelectorAll("svg[data-mn-pick]")].map((svg) => attachPick(svg, pickOptions(options))) : [];
12494
12745
  const copy = options.copy !== false;
12746
+ const fullScreen = options.fullScreen !== false;
12495
12747
  const stopPlayback = options.play ? attachPlayback(el, {
12496
12748
  hand: options.hand,
12497
12749
  ...typeof options.play === "object" ? options.play : {},
12498
- copy
12499
- }) : copy ? attachCopy(el) : null;
12750
+ copy,
12751
+ fullScreen
12752
+ }) : copy || fullScreen ? attachCopy(el, undefined, { copy, fullScreen }) : null;
12500
12753
  return {
12501
12754
  ok: error === null,
12502
12755
  el,
@@ -13541,7 +13794,7 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
13541
13794
  .mn-play input[type='range'] {
13542
13795
  all: unset;
13543
13796
  appearance: none;
13544
- width: 44px;
13797
+ width: 36px;
13545
13798
  /* As tall as the thumb, so no host or browser has to center it: the visible 3px track is a
13546
13799
  centered line drawn by the background. */
13547
13800
  height: 9px;
@@ -14478,7 +14731,8 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
14478
14731
  max-width: none;
14479
14732
  height: auto;
14480
14733
  margin: 0;
14481
- padding: 12px 16px 8px;
14734
+ /* Room on the sides, more on a wide screen. */
14735
+ padding: 12px max(24px, 3vw) 8px;
14482
14736
  overflow: hidden;
14483
14737
  background: Canvas;
14484
14738
  }
@@ -14498,6 +14752,54 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
14498
14752
  flex: none;
14499
14753
  margin-top: 8px;
14500
14754
  }
14755
+
14756
+ /* Full screen for the other blocks (#113): diagrams, charts, patterns, progressions, the circle of
14757
+ fifths. What the block holds is zoomed to the largest size that fits the screen (the control
14758
+ sets --mn-zoom), centered; a block that can't fit scrolls. The bar keeps its size, at the
14759
+ bottom. */
14760
+ .mn-block.mn-fullscreen-fit {
14761
+ position: fixed;
14762
+ inset: 0;
14763
+ z-index: 9999;
14764
+ display: flex;
14765
+ flex-direction: column;
14766
+ align-items: center;
14767
+ box-sizing: border-box;
14768
+ width: auto;
14769
+ max-width: none;
14770
+ height: auto;
14771
+ margin: 0;
14772
+ padding: 16px max(32px, 4vw) 0;
14773
+ overflow: auto;
14774
+ overscroll-behavior: contain;
14775
+ background: Canvas;
14776
+ outline: none;
14777
+ }
14778
+
14779
+ .mn-block.mn-fullscreen-fit > :not(.mn-play) {
14780
+ zoom: var(--mn-zoom, 1);
14781
+ flex: none;
14782
+ }
14783
+
14784
+ /* A row of diagrams is centered on the screen. */
14785
+ .mn-block.mn-fullscreen-fit .mn-row {
14786
+ justify-content: center;
14787
+ }
14788
+
14789
+ /* Centered while it fits; from the top when it scrolls. */
14790
+ .mn-block.mn-fullscreen-fit > :first-child {
14791
+ margin-top: auto;
14792
+ }
14793
+
14794
+ .mn-block.mn-fullscreen-fit > .mn-play {
14795
+ position: sticky;
14796
+ bottom: 0;
14797
+ flex: none;
14798
+ align-self: stretch;
14799
+ margin-top: auto;
14800
+ padding: 6px 0 8px;
14801
+ background: inherit;
14802
+ }
14501
14803
  `;
14502
14804
 
14503
14805
  // src/styles.ts
@@ -16124,5 +16426,5 @@ export {
16124
16426
  withTonicizations
16125
16427
  };
16126
16428
 
16127
- //# debugId=BAB348232536EC5A64756E2164756E21
16429
+ //# debugId=39249053506929F764756E2164756E21
16128
16430
  //# sourceMappingURL=index.js.map