Skip to content

Commit c366198

Browse files
committed
refactor: centralize ensureABCJS loader and fix tests
1 parent affa35f commit c366198

File tree

4 files changed

+30
-52
lines changed

4 files changed

+30
-52
lines changed

js/activity.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
globalActivity, hideArrows, doAnalyzeProject
5050
*/
5151

52-
import { ensureABCJS } from "./utils/abcLoader.js";
53-
5452
const LEADING = 0;
5553
const BLOCKSCALES = [1, 1.5, 2, 3, 4];
5654
const _THIS_IS_MUSIC_BLOCKS_ = true;
@@ -203,25 +201,6 @@ if (_THIS_IS_MUSIC_BLOCKS_) {
203201
// instead of reaching through window.* globals.
204202
let globalActivity;
205203

206-
/**
207-
* Ensures the ABCJS library is loaded.
208-
* If already loaded, resolves immediately.
209-
* Otherwise, dynamically appends the script and resolves on load.
210-
* @returns {Promise<void>}
211-
*/
212-
function ensureABCJS() {
213-
if (typeof window !== "undefined" && window.ABCJS) return Promise.resolve();
214-
if (typeof window === "undefined") return Promise.resolve();
215-
216-
return new Promise((resolve, reject) => {
217-
const script = document.createElement("script");
218-
script.src = "lib/abc.min.js";
219-
script.onload = resolve;
220-
script.onerror = reject;
221-
document.head.appendChild(script);
222-
});
223-
}
224-
225204
/**
226205
* Performs analysis on the project using the global activity.
227206
* @returns {object} - The analysis result.

js/blocks/GraphicsBlocks.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ function setupGraphicsBlocks(activity) {
9797
* @returns {number} - The heading value.
9898
*/
9999
arg(logo, turtle, blk) {
100-
const blockList = activity?.blocks?.blockList || [];
101-
const connections = blockList[blk]?.connections;
100+
const connections = activity.blocks.blockList[blk]?.connections;
102101
const parentId = connections?.[0];
103102
if (
104103
logo.inStatusMatrix &&
@@ -183,8 +182,7 @@ function setupGraphicsBlocks(activity) {
183182
* @returns {number} - The Y-coordinate value.
184183
*/
185184
arg(logo, turtle, blk) {
186-
const blockList = activity?.blocks?.blockList || [];
187-
const connections = blockList[blk]?.connections;
185+
const connections = activity.blocks.blockList[blk]?.connections;
188186
const parentId = connections?.[0];
189187
if (
190188
logo.inStatusMatrix &&
@@ -270,8 +268,7 @@ function setupGraphicsBlocks(activity) {
270268
* @returns {number} - The X-coordinate value.
271269
*/
272270
arg(logo, turtle, blk) {
273-
const blockList = activity?.blocks?.blockList || [];
274-
const connections = blockList[blk]?.connections;
271+
const connections = activity.blocks.blockList[blk]?.connections;
275272
const parentId = connections?.[0];
276273
if (
277274
logo.inStatusMatrix &&

js/utils/abcLoader.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1-
export async function ensureABCJS() {
2-
if (typeof window !== "undefined" && window.ABCJS) return;
3-
if (typeof window === "undefined") return;
1+
function ensureABCJS() {
2+
if (typeof window !== "undefined" && window.ABCJS) {
3+
return Promise.resolve();
4+
}
5+
6+
if (typeof window === "undefined") {
7+
return Promise.resolve();
8+
}
49

510
return new Promise((resolve, reject) => {
611
const existing = document.querySelector('script[src="lib/abc.min.js"]');
12+
713
if (existing) {
8-
if (existing.hasAttribute('data-loaded')) resolve();
9-
else existing.addEventListener('load', resolve);
14+
if (existing.hasAttribute("data-loaded")) {
15+
resolve();
16+
} else {
17+
existing.addEventListener("load", resolve);
18+
}
1019
return;
1120
}
1221

1322
const script = document.createElement("script");
1423
script.src = "lib/abc.min.js";
24+
1525
script.onload = () => {
16-
script.setAttribute('data-loaded', 'true');
26+
script.setAttribute("data-loaded", "true");
1727
resolve();
1828
};
29+
1930
script.onerror = reject;
31+
2032
document.head.appendChild(script);
2133
});
2234
}
35+
36+
if (typeof window !== "undefined") {
37+
window.ensureABCJS = ensureABCJS;
38+
}
39+
40+
if (typeof module !== "undefined" && module.exports) {
41+
module.exports = ensureABCJS;
42+
}

js/widgets/aiwidget.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
/* exported Abhijeet Singh */
21-
import { ensureABCJS } from "../utils/abcLoader.js";
2221

2322
/**
2423
* Represents a AI Widget.
@@ -31,23 +30,6 @@ function AIWidget() {
3130
// Don't include natural when construcing the note name...
3231
const EXPORTACCIDENTALNAMES = [DOUBLEFLAT, FLAT, "", SHARP, DOUBLESHARP];
3332

34-
/**
35-
* Ensures the ABCJS library is loaded.
36-
* Dynamically appends the script if not already present.
37-
* @returns {Promise<void>}
38-
*/
39-
function ensureABCJS() {
40-
if (typeof window !== "undefined" && window.ABCJS) return Promise.resolve();
41-
if (typeof window === "undefined") return Promise.resolve();
42-
43-
return new Promise((resolve, reject) => {
44-
const script = document.createElement("script");
45-
script.src = "lib/abc.min.js";
46-
script.onload = resolve;
47-
script.onerror = reject;
48-
document.head.appendChild(script);
49-
});
50-
}
5133
// ...but display it in the selector.
5234
const ACCIDENTALNAMES = [DOUBLEFLAT, FLAT, NATURAL, SHARP, DOUBLESHARP];
5335
const SOLFEGENAMES = ["do", "re", "mi", "fa", "sol", "la", "ti", "do"];
@@ -895,7 +877,7 @@ function AIWidget() {
895877
* @returns {void}
896878
*/
897879
this._playABCSong = async function () {
898-
await ensureABCJS();
880+
await window.ensureABCJS();
899881
const abc = abcNotationSong;
900882
const stopAudioButton = document.querySelector(".stop-audio");
901883

0 commit comments

Comments
 (0)