add code to the location hash (#13)

* feat: add url

* fix: format
This commit is contained in:
martinoooo
2020-06-21 13:21:27 +08:00
committed by GitHub
parent 620496f790
commit 20f508e917

View File

@ -40,7 +40,7 @@ monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
}); });
const editor = monaco.editor.create(document.getElementById('source'), { const editor = monaco.editor.create(document.getElementById('source'), {
value: localStorage.getItem('state') || 'const App = () => <div>Hello World</div>', value: decodeURIComponent(window.location.hash.slice(1)) || localStorage.getItem('state') || 'const App = () => <div>Hello World</div>',
language: 'javascript', language: 'javascript',
tabSize: 2, tabSize: 2,
...sharedEditorOptions, ...sharedEditorOptions,
@ -57,6 +57,7 @@ const output = monaco.editor.create(document.getElementById('output'), {
const compile = () => { const compile = () => {
const src = editor.getValue(); const src = editor.getValue();
localStorage.setItem('state', src); localStorage.setItem('state', src);
window.location.hash = encodeURIComponent(src);
console.clear(); console.clear();
transform(src, { transform(src, {
babelrc: false, babelrc: false,
@ -83,7 +84,6 @@ compile();
// update compile output when input changes // update compile output when input changes
editor.onDidChangeModelContent(debounce(compile)); editor.onDidChangeModelContent(debounce(compile));
function debounce(fn, delay = 300) { function debounce(fn, delay = 300) {
let prevTimer = null; let prevTimer = null;
return ((...args) => { return ((...args) => {