Skip to content

Commit 0cb39c5

Browse files
committed
Fill playground editor
1 parent 35cd655 commit 0cb39c5

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

web/src/js/hljs-run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class RunButtonPlugin {
1616
runButton.addEventListener('click', (event) => {
1717
event.preventDefault();
1818
const hash = encodeURIComponent(text);
19-
window.location.href = `.${this.playgroundUrl}?hash=${hash}`;
19+
window.location.href = `.${this.playgroundUrl}#${hash}`;
2020
});
2121
const wrapper = document.createElement("div");
2222
wrapper.classList.add('hljs-run');

web/src/js/playground.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const resultBlock = document.querySelector('.result');
1111
codeOutput.textContent = codeInput.value;
1212
hljs.highlightElement(codeOutput);
1313

14-
codeInput.addEventListener('input', (event) => {
14+
codeInput.addEventListener('input', () => {
1515
codeOutput.removeAttribute('data-highlighted');
1616
let val = codeInput.value;
1717
if (val[val.length - 1] === "\n") val += ' ';
1818
codeOutput.textContent = val;
1919
hljs.highlightElement(codeOutput);
2020
});
2121

22-
codeInput.addEventListener('scroll', (event) => {
22+
codeInput.addEventListener('scroll', () => {
2323
codeOutput.scrollTop = codeInput.scrollTop;
2424
codeOutput.scrollLeft = codeInput.scrollLeft;
2525
});
@@ -49,14 +49,27 @@ console.warn = function (...args) {
4949
resultBlock.innerHTML += `<div class="console warn">${arg}</div>`;
5050
});
5151
}
52-
runButton.addEventListener('click', (event) => {
52+
runButton.addEventListener('click', () => {
5353
resultBlock.innerHTML = '';
5454
const code = codeInput.value;
5555
try {
5656
Function(code)();
5757
} catch (e) {
5858
console.error(e);
5959
}
60-
})
60+
});
6161

6262
resizeObserver.observe(codeInput);
63+
64+
addEventListener("DOMContentLoaded", (event) => {
65+
let hash = location.hash.slice(1);
66+
if (hash) {
67+
try {
68+
hash = decodeURIComponent(hash);
69+
} catch (error) {
70+
hash = hash.replace(/%[0-F]{2}/ig, decodeURIComponent);
71+
}
72+
codeInput.value = hash;
73+
codeInput.dispatchEvent(new Event('input', { bubbles: true }));
74+
}
75+
});

0 commit comments

Comments
 (0)