Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox stores last values #247

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions testHTML/m21jSandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<body>
<div class="header">
<button id="resetButton" role="button" style="float: right">Reset</button>
<h1>Music21j Sandbox</h1>
<div class="main">
<label for="sandbox">
Expand All @@ -47,14 +48,23 @@ <h1>Music21j Sandbox</h1>
<p></p>
<div class="example sandbox">
<textarea id="sandbox" class="editor">
</textarea>
<p></p>
<div class='streamHolding' id='mainSVG'>
<svg width="525" height="220"></svg>
</div>
<p></p>
</div>
</div>
</div>
<script>
let timeout;

const default_text = `
const s = new music21.stream.Measure();
const n = new music21.note.Note("C#4");
n.addLyric('lulli-');
n.addLyric('sad');
n.duration.type = "half";
const n2 = new music21.note.Note("A3");
n2.addLyric('bye');
n2.addLyric('day');
const n2 = new music21.note.Note("A4");
n2.duration.type = "quarter";
const n3 = new music21.note.Note("C#");
n3.duration.type = "quarter";
Expand All @@ -64,32 +74,28 @@ <h1>Music21j Sandbox</h1>
s.append(n3);
s.renderOptions.marginBottom = 80;
s.replaceDOM('#mainSVG');
</textarea>
<p></p>
<div class='streamHolding' id='mainSVG'>
<svg width="525" height="220"></svg>
</div>
<p></p>
</div>
</div>
</div>
<script>
Vex = undefined;
mainSVG = undefined;
let timeout;
let msg = "";
`;

mainSVG = document.querySelector("#mainSVG");
msg = document.querySelector('div.editor-error .text');
const mainSVG = document.querySelector("#mainSVG");
const msg = document.querySelector('div.editor-error .text');
const sandbox = document.querySelector("#sandbox");
const localStorageKey = 'm21j_sandbox_code';
const local_storage_value = localStorage.getItem(localStorageKey);
sandbox.value = local_storage_value || default_text;
document.querySelector('#resetButton').addEventListener('click', () => {
sandbox.value = default_text;
localStorage.removeItem(localStorageKey);
live_code();
});

live_code();

for (const evt of ['blur', 'keyup', 'paste']) {
sandbox.addEventListener(evt, () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(live_code, 200);
timeout = setTimeout(live_code, 250);
});
}

Expand All @@ -100,6 +106,7 @@ <h1>Music21j Sandbox</h1>
eval(code);
msg.innerHTML = '';
msg.style.display = 'none';
localStorage.setItem(localStorageKey, code);
} catch (e) {
msg.innerHTML = e.toString();
msg.style.display = 'block';
Expand Down
Loading