From c5002d8cadfcf5fd305111c8a1f99b07cf2675e9 Mon Sep 17 00:00:00 2001 From: harttle Date: Sat, 25 Jul 2020 01:23:47 +0800 Subject: [PATCH] docs: location.hash for source code --- docs/themes/navy/source/js/main.js | 19 +++++++++++++++++-- docs/themes/navy/source/js/playground.js | 0 2 files changed, 17 insertions(+), 2 deletions(-) delete mode 100644 docs/themes/navy/source/js/playground.js diff --git a/docs/themes/navy/source/js/main.js b/docs/themes/navy/source/js/main.js index 30643c110..4bdebe6ca 100644 --- a/docs/themes/navy/source/js/main.js +++ b/docs/themes/navy/source/js/main.js @@ -66,9 +66,8 @@ preview.renderer.setShowGutter(false); preview.renderer.setPadding(20); - editor.setValue(sourceEl.textContent, 1); + editor.setValue(getInitialValue(), 1); editor.on('change', update); - editor.focus(); update(); function createEditor(id, lang) { @@ -83,8 +82,16 @@ return editor; } + function getInitialValue() { + try { + return atou(location.hash.slice(1)) || sourceEl.textContent + } catch (e) {} + return sourceEl.textContent + } + async function update() { const tpl = editor.getValue(); + history.replaceState({}, '', '#' + utoa(tpl)); try { const html = await engine.parseAndRender(tpl); preview.setValue(html, 1); @@ -93,6 +100,14 @@ throw err; } } + + function atou(str) { + return decodeURIComponent(escape(atob(str))) + } + + function utoa(str) { + return btoa(unescape(encodeURIComponent(str))) + } }()); if ('serviceWorker' in navigator) { diff --git a/docs/themes/navy/source/js/playground.js b/docs/themes/navy/source/js/playground.js deleted file mode 100644 index e69de29bb..000000000