aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/editor.mjs
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-09-18 23:41:15 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-09-18 23:52:49 +0800
commit4cf84d2fe96c004fef3b508ce344885db74917e2 (patch)
tree87fad915787b4621d0eb2f3e9086f54afdffa5a4 /src/editor.mjs
parentfdf3e2780edf92bd0cdb7aefc7a196e608b9cbd1 (diff)
feat: swith focus map by tab key and click
* Use data-focus attribute and MutationObserver to move map-container between Sementic HTML and Showcase. * Currently set return value of docuement.onkeydown in editor, then reset onkeydown function in dumbymap. Maybe there is a better way? * CSS to make placeholder not so obvious
Diffstat (limited to 'src/editor.mjs')
-rw-r--r--src/editor.mjs32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/editor.mjs b/src/editor.mjs
index b2c4d54..168e66b 100644
--- a/src/editor.mjs
+++ b/src/editor.mjs
@@ -40,7 +40,8 @@ const editor = new EasyMDE({
40 shortcuts: { 40 shortcuts: {
41 "map": "Ctrl-Alt-M", 41 "map": "Ctrl-Alt-M",
42 "debug": "Ctrl-Alt-D", 42 "debug": "Ctrl-Alt-D",
43 "toggleUnorderedList": "Ctrl-Shift-L", 43 "toggleUnorderedList": null,
44 "toggleOrderedList": null,
44 }, 45 },
45 toolbar: [ 46 toolbar: [
46 { 47 {
@@ -156,12 +157,12 @@ const debounceForMap = (() => {
156 } 157 }
157})() 158})()
158 159
159const afterMapRendered = (mapHolder) => { 160const afterMapRendered = (_) => {
160 mapHolder.oncontextmenu = (event) => { 161 // mapHolder.oncontextmenu = (event) => {
161 event.preventDefault() 162 // event.preventDefault()
162 const lonLat = mapHolder.renderer.unproject([event.x, event.y]) 163 // const lonLat = mapHolder.renderer.unproject([event.x, event.y])
163 // TODO... 164 // // TODO...
164 } 165 // }
165} 166}
166 167
167const updateDumbyMap = () => { 168const updateDumbyMap = () => {
@@ -174,7 +175,6 @@ updateDumbyMap()
174 175
175// Re-render HTML by editor content 176// Re-render HTML by editor content
176cm.on("change", (_, change) => { 177cm.on("change", (_, change) => {
177 console.log('change', change.text)
178 updateDumbyMap() 178 updateDumbyMap()
179 addClassToCodeLines() 179 addClassToCodeLines()
180 completeForCodeBlock(change) 180 completeForCodeBlock(change)
@@ -345,7 +345,6 @@ const handleTypingInCodeBlock = (anchor) => {
345// }}} 345// }}}
346// FUNCTION: get suggestions by current input {{{ 346// FUNCTION: get suggestions by current input {{{
347const getSuggestions = (anchor) => { 347const getSuggestions = (anchor) => {
348 let suggestions = []
349 const text = cm.getLine(anchor.line) 348 const text = cm.getLine(anchor.line)
350 349
351 // Clear marks on text 350 // Clear marks on text
@@ -445,7 +444,7 @@ const getSuggestions = (anchor) => {
445 ) 444 )
446 return rendererSuggestions.length > 0 ? rendererSuggestions : [] 445 return rendererSuggestions.length > 0 ? rendererSuggestions : []
447 } 446 }
448 return suggestions 447 return []
449} 448}
450// }}} 449// }}}
451// {{{ FUNCTION: Show element about suggestions 450// {{{ FUNCTION: Show element about suggestions
@@ -505,11 +504,9 @@ cm.on("cursorActivity", (_) => {
505}); 504});
506// }}} 505// }}}
507// EVENT: keydown for suggestions {{{ 506// EVENT: keydown for suggestions {{{
507const keyForSuggestions = ['Tab', 'Enter', 'Escape']
508cm.on('keydown', (_, e) => { 508cm.on('keydown', (_, e) => {
509 509 if (!cm.hasFocus || !keyForSuggestions.includes(e.key) || suggestionsEle.style.display === 'none') return;
510 // Only the following keys are used
511 const keyForSuggestions = ['Tab', 'Enter', 'Escape'].includes(e.key)
512 if (!keyForSuggestions || suggestionsEle.style.display === 'none') return;
513 510
514 // Directly add a newline when no suggestion is selected 511 // Directly add a newline when no suggestion is selected
515 const currentSuggestion = suggestionsEle.querySelector('.container__suggestion.focus') 512 const currentSuggestion = suggestionsEle.querySelector('.container__suggestion.focus')
@@ -545,6 +542,13 @@ cm.on('keydown', (_, e) => {
545document.onkeydown = (e) => { 542document.onkeydown = (e) => {
546 if (e.altKey && e.ctrlKey && e.key === 'm') { 543 if (e.altKey && e.ctrlKey && e.key === 'm') {
547 toggleEditing() 544 toggleEditing()
545 e.preventDefault()
546 return null
547 }
548 if (cm.hasFocus()) {
549 return null
550 } else {
551 return e
548 } 552 }
549} 553}
550 554