diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-09 16:29:26 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-09 17:06:04 +0800 |
commit | 0808bff26ca6763eed27b4d9225f99f4a3bd7c2e (patch) | |
tree | 83b03c689dac2586b16fff33d1230187e67c4a19 | |
parent | c4d54ab32d181ccd4ac6da386b29ef10508613ca (diff) |
feat: patch 5aede26
* use callback to disable scroll
* only allow action with left click
* disable selection when dragging
* gray-out elements except of maps
* only sync scroll when cm has focus
-rw-r--r-- | src/css/dumbymap.css | 8 | ||||
-rw-r--r-- | src/editor.mjs | 27 |
2 files changed, 27 insertions, 8 deletions
diff --git a/src/css/dumbymap.css b/src/css/dumbymap.css index 0c1fc81..19e3baf 100644 --- a/src/css/dumbymap.css +++ b/src/css/dumbymap.css | |||
@@ -773,3 +773,11 @@ a[href^='http']:not(:has(img))::after, | |||
773 | .bold-options { | 773 | .bold-options { |
774 | font-weight: bold; | 774 | font-weight: bold; |
775 | } | 775 | } |
776 | |||
777 | .dragging-geolink { | ||
778 | user-select: none; | ||
779 | |||
780 | .dumby-block > :not(:has(.mapclay)) { | ||
781 | opacity: 0.3; | ||
782 | } | ||
783 | } | ||
diff --git a/src/editor.mjs b/src/editor.mjs index e39d97c..b93bad8 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
@@ -312,7 +312,9 @@ const setScrollLine = () => { | |||
312 | cm.lineAtHeight(cm.getScrollInfo().top, 'local') | 312 | cm.lineAtHeight(cm.getScrollInfo().top, 'local') |
313 | textArea.dataset.scrollLine = lineNumber | 313 | textArea.dataset.scrollLine = lineNumber |
314 | } | 314 | } |
315 | cm.on('scroll', setScrollLine) | 315 | cm.on('scroll', () => { |
316 | if (cm.hasFocus()) setScrollLine() | ||
317 | }) | ||
316 | 318 | ||
317 | /** Sync scroll from CodeMirror to HTML **/ | 319 | /** Sync scroll from CodeMirror to HTML **/ |
318 | new window.MutationObserver(() => { | 320 | new window.MutationObserver(() => { |
@@ -459,7 +461,7 @@ const menuForEditor = (event, menu) => { | |||
459 | /** | 461 | /** |
460 | * update content of HTML about Dumbymap | 462 | * update content of HTML about Dumbymap |
461 | */ | 463 | */ |
462 | const updateDumbyMap = () => { | 464 | const updateDumbyMap = (callback = null) => { |
463 | markdown2HTML(dumbyContainer, editor.value()) | 465 | markdown2HTML(dumbyContainer, editor.value()) |
464 | // debounceForMap(dumbyContainer, afterMapRendered) | 466 | // debounceForMap(dumbyContainer, afterMapRendered) |
465 | dumbymap = generateMaps(dumbyContainer, {}) | 467 | dumbymap = generateMaps(dumbyContainer, {}) |
@@ -468,14 +470,16 @@ const updateDumbyMap = () => { | |||
468 | htmlHolder.onscroll = htmlOnScroll(htmlHolder) | 470 | htmlHolder.onscroll = htmlOnScroll(htmlHolder) |
469 | // Set oncontextmenu callback | 471 | // Set oncontextmenu callback |
470 | dumbymap.utils.setContextMenu(menuForEditor) | 472 | dumbymap.utils.setContextMenu(menuForEditor) |
471 | // Scroll to proper position | 473 | |
472 | setScrollLine() | 474 | callback?.(dumbymap) |
473 | } | 475 | } |
474 | updateDumbyMap() | 476 | updateDumbyMap() |
475 | 477 | ||
476 | // Re-render HTML by editor content | 478 | // Re-render HTML by editor content |
477 | cm.on('change', (_, change) => { | 479 | cm.on('change', (_, change) => { |
478 | updateDumbyMap() | 480 | updateDumbyMap(() => { |
481 | setScrollLine() | ||
482 | }) | ||
479 | addClassToCodeLines() | 483 | addClassToCodeLines() |
480 | completeForCodeBlock(change) | 484 | completeForCodeBlock(change) |
481 | }) | 485 | }) |
@@ -1066,6 +1070,7 @@ document.addEventListener('selectionchange', () => { | |||
1066 | /** Drag/Drop on map for new reference style link */ | 1070 | /** Drag/Drop on map for new reference style link */ |
1067 | dumbyContainer.onmousedown = (e) => { | 1071 | dumbyContainer.onmousedown = (e) => { |
1068 | // Check should start drag event for GeoLink | 1072 | // Check should start drag event for GeoLink |
1073 | if (e.which !== 1) return | ||
1069 | const selection = document.getSelection() | 1074 | const selection = document.getSelection() |
1070 | if (cm.getSelection() === '' || selection.type !== 'Range') return | 1075 | if (cm.getSelection() === '' || selection.type !== 'Range') return |
1071 | const range = selection.getRangeAt(0) | 1076 | const range = selection.getRangeAt(0) |
@@ -1093,27 +1098,33 @@ dumbyContainer.onmousedown = (e) => { | |||
1093 | lineEnd.style.left = event.clientX + 'px' | 1098 | lineEnd.style.left = event.clientX + 'px' |
1094 | lineEnd.style.top = event.clientY + 'px' | 1099 | lineEnd.style.top = event.clientY + 'px' |
1095 | line.position() | 1100 | line.position() |
1096 | dumbymap.utils.renderedMaps().forEach(map => { map.style.cursor = 'crosshair' }) | ||
1097 | } | 1101 | } |
1098 | 1102 | ||
1103 | context.classList.add('dragging-geolink') | ||
1099 | dumbyContainer.onmousemove = onMouseMove | 1104 | dumbyContainer.onmousemove = onMouseMove |
1105 | dumbymap.utils.renderedMaps().forEach(map => { map.style.cursor = 'crosshair' }) | ||
1100 | dumbyContainer.onmouseup = function (e) { | 1106 | dumbyContainer.onmouseup = function (e) { |
1107 | context.classList.remove('dragging-geolink') | ||
1101 | dumbyContainer.onmousemove = null | 1108 | dumbyContainer.onmousemove = null |
1102 | dumbyContainer.onmouseup = null | 1109 | dumbyContainer.onmouseup = null |
1103 | line?.remove() | 1110 | line?.remove() |
1104 | lineEnd.remove() | 1111 | lineEnd.remove() |
1105 | dumbymap.utils.renderedMaps().forEach(map => map.style.removeProperty('cursor')) | 1112 | dumbymap.utils.renderedMaps().forEach(map => map.style.removeProperty('cursor')) |
1113 | const resumeContent = () => updateDumbyMap(newDumbymap => { | ||
1114 | const scrollTop = dumbymap.htmlHolder.scrollTop | ||
1115 | newDumbymap.htmlHolder.scrollBy(0, scrollTop) | ||
1116 | }) | ||
1106 | 1117 | ||
1107 | const map = document.elementFromPoint(e.clientX, e.clientY).closest('.mapclay') | 1118 | const map = document.elementFromPoint(e.clientX, e.clientY).closest('.mapclay') |
1108 | const selection = cm.getSelection() | 1119 | const selection = cm.getSelection() |
1109 | if (!map || !selection) { | 1120 | if (!map || !selection) { |
1110 | updateDumbyMap() | 1121 | resumeContent() |
1111 | return | 1122 | return |
1112 | } | 1123 | } |
1113 | 1124 | ||
1114 | const refLink = addAnchorByPoint({ point: e, map, validateAnchorName }) | 1125 | const refLink = addAnchorByPoint({ point: e, map, validateAnchorName }) |
1115 | if (!refLink) { | 1126 | if (!refLink) { |
1116 | updateDumbyMap() | 1127 | resumeContent() |
1117 | return | 1128 | return |
1118 | } | 1129 | } |
1119 | 1130 | ||