@dsebastien/music-notation 0.19.2 → 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.
@@ -5426,9 +5426,11 @@ var ICONS = {
5426
5426
  loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
5427
5427
  count: "M5.5 14h5L9 2.5H7zM8 11.5l4.5-7",
5428
5428
  click: "M3 5v6M8 5v6M13 5v6",
5429
- copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01"
5429
+ copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01",
5430
+ full: "M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10",
5431
+ exit: "M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5"
5430
5432
  };
5431
- var STROKED = new Set(["loop", "count", "click", "copy"]);
5433
+ var STROKED = new Set(["loop", "count", "click", "copy", "full", "exit"]);
5432
5434
  function icon(doc, name) {
5433
5435
  const root = svg(doc, "svg", {
5434
5436
  class: `mn-play-icon mn-play-icon-${name}${STROKED.has(name) ? " mn-play-icon-line" : ""}`,
@@ -6133,6 +6135,242 @@ async function toPng(el, options = {}) {
6133
6135
  return new Promise((resolve, reject) => canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("The image could not be made")), "image/png"));
6134
6136
  }
6135
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
+
6136
6374
  // src/playback/copy-image.ts
6137
6375
  var COPY_SAID = {
6138
6376
  copied: "Copied",
@@ -6190,13 +6428,16 @@ function copyImageButton(doc, win, target, make = (el) => toPng(el)) {
6190
6428
  return [button, said];
6191
6429
  }
6192
6430
  var NOT_DRAWN_HERE = ".mn-tab, .mn-tab-holder, .mn-tab-surface, .mn-exercise-tab";
6193
- function attachCopy(el, make) {
6431
+ function attachCopy(el, make, options = {}) {
6194
6432
  const doc = el.ownerDocument;
6195
6433
  const win = doc.defaultView;
6196
6434
  if (!win)
6197
6435
  return () => {};
6198
6436
  const blocks = el.classList.contains("mn-block") ? [el] : [...el.querySelectorAll(".mn-block")];
6199
6437
  const bars = [];
6438
+ const frees = [];
6439
+ if (options.copy === false && options.fullScreen === false)
6440
+ return () => {};
6200
6441
  for (const block of blocks) {
6201
6442
  if (block.querySelector(".mn-play"))
6202
6443
  continue;
@@ -6205,7 +6446,9 @@ function attachCopy(el, make) {
6205
6446
  if (!drawings.length && !timed)
6206
6447
  continue;
6207
6448
  const bar = playBar(doc, "mn-copy-bar", "Image");
6208
- 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));
6209
6452
  if (timed)
6210
6453
  block.appendChild(bar);
6211
6454
  else
@@ -6213,6 +6456,8 @@ function attachCopy(el, make) {
6213
6456
  bars.push(bar);
6214
6457
  }
6215
6458
  return () => {
6459
+ for (const free of frees)
6460
+ free();
6216
6461
  for (const bar of bars)
6217
6462
  bar.remove();
6218
6463
  };
@@ -6369,6 +6614,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6369
6614
  bar.appendChild(choice(doc, "mn-play-sound", "Sound", ["guitar", "piano"], settings.sound, { guitar: "Guitar", piano: "Piano" }, (value) => settings.sound = value === "piano" ? "piano" : "guitar"));
6370
6615
  if (options.copy !== false)
6371
6616
  bar.append(...copyImageButton(doc, win, () => wrap.closest(".mn-block") ?? wrap));
6617
+ const freeFull = addFullScreen(doc, bar, wrap.closest(".mn-block"), options.fullScreen);
6372
6618
  const app = handToApp(options, bar, {
6373
6619
  kind: piece.kind,
6374
6620
  drawing,
@@ -6377,6 +6623,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
6377
6623
  });
6378
6624
  return () => {
6379
6625
  stop();
6626
+ freeFull();
6380
6627
  app.free();
6381
6628
  };
6382
6629
  }
@@ -6527,6 +6774,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6527
6774
  }));
6528
6775
  if (options.copy !== false)
6529
6776
  bar.append(...copyImageButton(doc, win, () => drawing.closest(".mn-block") ?? drawing));
6777
+ const freeFull = addFullScreen(doc, bar, drawing.closest(".mn-block"), options.fullScreen);
6530
6778
  const app = handToApp(options, bar, {
6531
6779
  kind: drawing.classList.contains("mn-chart") ? "chart" : "progression",
6532
6780
  drawing,
@@ -6535,6 +6783,7 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
6535
6783
  });
6536
6784
  return () => {
6537
6785
  stop();
6786
+ freeFull();
6538
6787
  if (diagrams?.onPlay === stop) {
6539
6788
  diagrams.onPlay = null;
6540
6789
  diagrams.stopOthers = null;
@@ -7430,6 +7679,7 @@ function blockBarFor(doc, win, block, diagrams, options) {
7430
7679
  bar.appendChild(sound);
7431
7680
  if (options.copy !== false)
7432
7681
  bar.append(...copyImageButton(doc, win, (whole) => whole || !several ? block : diagramImage(made.selected.wrap)));
7682
+ const freeFull = addFullScreen(doc, bar, block, options.fullScreen);
7433
7683
  bind({
7434
7684
  changed() {
7435
7685
  const busy = made.busy;
@@ -7450,7 +7700,10 @@ function blockBarFor(doc, win, block, diagrams, options) {
7450
7700
  sound.value = made.sound;
7451
7701
  }
7452
7702
  });
7453
- return () => group.destroy();
7703
+ return () => {
7704
+ freeFull();
7705
+ group.destroy();
7706
+ };
7454
7707
  }
7455
7708
 
7456
7709
  // src/playback/controls.ts
@@ -12490,11 +12743,13 @@ function renderBlock(language, source, host, given = {}) {
12490
12743
  }
12491
12744
  const picks = options.pick ? [...el.querySelectorAll("svg[data-mn-pick]")].map((svg) => attachPick(svg, pickOptions(options))) : [];
12492
12745
  const copy = options.copy !== false;
12746
+ const fullScreen = options.fullScreen !== false;
12493
12747
  const stopPlayback = options.play ? attachPlayback(el, {
12494
12748
  hand: options.hand,
12495
12749
  ...typeof options.play === "object" ? options.play : {},
12496
- copy
12497
- }) : copy ? attachCopy(el) : null;
12750
+ copy,
12751
+ fullScreen
12752
+ }) : copy || fullScreen ? attachCopy(el, undefined, { copy, fullScreen }) : null;
12498
12753
  return {
12499
12754
  ok: error === null,
12500
12755
  el,
@@ -13241,7 +13496,9 @@ function fingerprint(text) {
13241
13496
  return `${text.length}:${hash.toString(36)}`;
13242
13497
  }
13243
13498
  var renderingOf = (pre) => {
13244
- const next = pre.nextElementSibling;
13499
+ let next = pre.nextElementSibling;
13500
+ if (next?.classList.contains("mn-fullscreen-place"))
13501
+ next = next.nextElementSibling;
13245
13502
  return next?.classList.contains("mn-block") ? next : null;
13246
13503
  };
13247
13504
  function renderAll(root, options = {}) {
@@ -13309,5 +13566,5 @@ export {
13309
13566
  renderAll
13310
13567
  };
13311
13568
 
13312
- //# debugId=DCB2B3D01E9B461264756E2164756E21
13569
+ //# debugId=03EC35FD01BDA81C64756E2164756E21
13313
13570
  //# sourceMappingURL=index.js.map