hyperbook 0.93.1 → 0.95.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.
@@ -7,34 +7,41 @@
7
7
  * @see hyperbook.store
8
8
  */
9
9
  hyperbook.abc = (function () {
10
- window.codeInput?.registerTemplate(
11
- "abc-highlighted",
12
- codeInput.templates.prism(window.Prism, [
13
- new codeInput.plugins.Indent(true, 2),
14
- ])
15
- );
16
-
17
10
  const initABC = async (el) => {
18
11
  const tuneEl = el.getElementsByClassName("tune")[0];
19
12
  const playerEl = el.getElementsByClassName("player")[0];
20
13
 
21
14
  const tune = atob(el.getAttribute("data-tune"));
22
- const editor = el.getAttribute("data-editor");
15
+ const isEditor = el.getAttribute("data-editor") === "true";
23
16
 
24
- if (editor == "true") {
25
- /**
26
- * @type {HTMLTextAreaElement}
27
- */
17
+ if (isEditor) {
28
18
  const editorEl = el.getElementsByClassName("editor")[0];
29
- const id = editorEl.id;
19
+ const id = el.getAttribute("data-id") || editorEl.id;
30
20
 
31
21
  const copyEl = el.getElementsByClassName("copy")[0];
32
22
  const resetEl = el.getElementsByClassName("reset")[0];
33
23
  const downloadEl = el.getElementsByClassName("download")[0];
34
24
 
25
+ // Restore saved tune or use default
26
+ const storeResult = await hyperbook.store.db.abcMusic
27
+ .where("id")
28
+ .equals(id)
29
+ .first();
30
+ const initialTune = storeResult?.tune || tune;
31
+
32
+ editorEl.textContent = "";
33
+ const cm = HyperbookCM.create(editorEl, {
34
+ lang: "default",
35
+ value: initialTune,
36
+ onChange: (code) => {
37
+ hyperbook.store.db.abcMusic.put({ id, tune: code });
38
+ renderAndPlay(code);
39
+ },
40
+ });
41
+
35
42
  copyEl?.addEventListener("click", async () => {
36
43
  try {
37
- await navigator.clipboard.writeText(editor.value);
44
+ await navigator.clipboard.writeText(cm.getValue());
38
45
  } catch (error) {
39
46
  console.error(error.message);
40
47
  }
@@ -47,43 +54,36 @@ hyperbook.abc = (function () {
47
54
 
48
55
  downloadEl?.addEventListener("click", () => {
49
56
  const a = document.createElement("a");
50
- const blob = new Blob([editor.value], { type: "text/plain" });
57
+ const blob = new Blob([cm.getValue()], { type: "text/plain" });
51
58
  a.href = URL.createObjectURL(blob);
52
59
  a.download = `tones-${id}.abc`;
53
60
  a.click();
54
61
  });
55
62
 
56
- editorEl.addEventListener("code-input_load", async () => {
57
- const storeResult = await hyperbook.store.db.abcMusic
58
- .where("id")
59
- .equals(editorEl.id)
60
- .first();
61
- editorEl.value = storeResult?.tune || tune;
62
-
63
- new ABCJS.Editor(editorEl.id, {
64
- canvas_id: tuneEl,
65
- synth: {
66
- el: playerEl,
67
- options: {
63
+ // Render initial notation + player
64
+ const renderAndPlay = (abcText) => {
65
+ const visualObj = ABCJS.renderAbc(tuneEl, abcText, {
66
+ responsive: "resize",
67
+ selectTypes: false,
68
+ selectionColor: "currentColor",
69
+ })[0];
70
+ if (ABCJS.synth.supportsAudio()) {
71
+ if (!el._synthControl) {
72
+ el._synthControl = new ABCJS.synth.SynthController();
73
+ el._synthControl.load(playerEl, null, {
68
74
  displayRestart: true,
69
75
  displayPlay: true,
70
76
  displayProgress: true,
71
- },
72
- },
73
- abcjsParams: {
74
- responsive: "resize",
75
- selectTypes: false,
76
- selectionColor: "currentColor",
77
- },
78
- });
77
+ });
78
+ }
79
+ el._synthControl.setTune(visualObj, false);
80
+ } else {
81
+ playerEl.innerHTML =
82
+ "<div class='audio-error'>Audio is not supported in this browser.</div>";
83
+ }
84
+ };
79
85
 
80
- editorEl.addEventListener("change", () => {
81
- hyperbook.store.db.abcMusic.put({
82
- id: editorEl.id,
83
- tune: editorEl.value,
84
- });
85
- });
86
- });
86
+ renderAndPlay(initialTune);
87
87
  } else {
88
88
  const visualObj = ABCJS.renderAbc(tuneEl, tune, {
89
89
  responsive: "resize",
@@ -203,6 +203,12 @@
203
203
  width: 100%;
204
204
  border: 1px solid var(--color-spacer);
205
205
  flex: 1;
206
+ min-height: 0;
207
+ overflow: hidden;
208
+ }
209
+
210
+ .directive-abc-music .editor .cm-editor {
211
+ height: 100%;
206
212
  }
207
213
 
208
214
  .directive-abc-music .buttons {