aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/editor.mjs
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-09-14 19:28:46 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-09-15 00:27:48 +0800
commit64f24ee5fb1a27b66de2575a8d4b31646776f54a (patch)
tree62e4b390504e8c4a391c00aa1f43af1272e6969c /src/editor.mjs
parent901ad1a83ae08c259f75c69e5cc957a72e18f762 (diff)
feat: Switch layout by attribute
Diffstat (limited to 'src/editor.mjs')
-rw-r--r--src/editor.mjs32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/editor.mjs b/src/editor.mjs
index 915ce24..b88e488 100644
--- a/src/editor.mjs
+++ b/src/editor.mjs
@@ -9,15 +9,13 @@ import { createDocLinks } from './dumbymap.mjs'
9const HtmlContainer = document.querySelector(".DumbyMap") 9const HtmlContainer = document.querySelector(".DumbyMap")
10const textArea = document.querySelector(".editor textarea") 10const textArea = document.querySelector(".editor textarea")
11 11
12const toggleMaps = (container) => { 12const toggleEditing = () => {
13 if (!container.querySelector('.Showcase')) { 13 if (document.body.getAttribute("data-layout") === "editing") {
14 generateMaps(container) 14 document.body.removeAttribute("data-layout")
15 document.activeElement.blur();
16 } else { 15 } else {
17 markdown2HTML(HtmlContainer, editor.value()) 16 document.body.setAttribute("data-layout", "editing")
18 createDocLinks(container)
19 container.setAttribute('data-layout', 'none')
20 } 17 }
18 HtmlContainer.setAttribute("data-layout", "none")
21} 19}
22// }}} 20// }}}
23// Set up EasyMDE {{{ 21// Set up EasyMDE {{{
@@ -66,7 +64,7 @@ const editor = new EasyMDE({
66 name: 'map', 64 name: 'map',
67 title: 'Toggle Map Generation', 65 title: 'Toggle Map Generation',
68 text: "🌏", 66 text: "🌏",
69 action: () => toggleMaps(HtmlContainer), 67 action: () => toggleEditing(),
70 }, 68 },
71 { 69 {
72 name: 'debug', 70 name: 'debug',
@@ -88,10 +86,6 @@ const cm = editor.codemirror
88markdown2HTML(HtmlContainer, editor.value()) 86markdown2HTML(HtmlContainer, editor.value())
89createDocLinks(HtmlContainer) 87createDocLinks(HtmlContainer)
90 88
91if (queryParams.get('render')) {
92 toggleMaps(HtmlContainer)
93}
94
95// Quick hack to style lines inside code block 89// Quick hack to style lines inside code block
96const addClassToCodeLines = () => { 90const addClassToCodeLines = () => {
97 const lines = cm.getLineHandle(0).parent.lines 91 const lines = cm.getLineHandle(0).parent.lines
@@ -509,11 +503,23 @@ cm.on('keydown', (_, e) => {
509 503
510document.onkeydown = (e) => { 504document.onkeydown = (e) => {
511 if (e.altKey && e.ctrlKey && e.key === 'm') { 505 if (e.altKey && e.ctrlKey && e.key === 'm') {
512 toggleMaps(HtmlContainer) 506 toggleEditing()
513 } 507 }
514} 508}
515 509
516// }}} 510// }}}
517// }}} 511// }}}
512const layoutObserver = new MutationObserver(() => {
513 const layout = HtmlContainer.getAttribute('data-layout')
514 if (layout !== 'none') {
515 document.body.removeAttribute('data-layout')
516 }
517})
518
519layoutObserver.observe(HtmlContainer, {
520 attributes: true,
521 attributeFilter: ["data-layout"],
522 attributeOldValue: true
523});
518 524
519// vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} 525// vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}}