aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-10-27 23:48:19 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-10-28 12:24:12 +0800
commit069b1b74bbc369b3dddcd6fb2d64d77381ba0b17 (patch)
tree35a0cbdd2e0ee75dc0a452e9f6ab5a8cb4a78845
parented1a8d2e4eb77c2e33d5d44118f13331dac43899 (diff)
feat: add menu item for add marker
* remove code about adding reference link in editor.mjs * TODO: use local storage to access reference about marker
-rw-r--r--src/MenuItem.mjs32
-rw-r--r--src/dumbymap.mjs20
-rw-r--r--src/editor.mjs44
3 files changed, 68 insertions, 28 deletions
diff --git a/src/MenuItem.mjs b/src/MenuItem.mjs
index 1d864fd..11ce4be 100644
--- a/src/MenuItem.mjs
+++ b/src/MenuItem.mjs
@@ -1,7 +1,7 @@
1import { shiftByWindow } from './utils.mjs' 1import { shiftByWindow } from './utils.mjs'
2/* eslint-disable no-unused-vars */ 2import { addMarkerByPoint } from './dumbyUtils.mjs'
3/* eslint-disable-next-line no-unused-vars */
3import { GeoLink, getMarkersFromMaps, removeLeaderLines } from './Link.mjs' 4import { GeoLink, getMarkersFromMaps, removeLeaderLines } from './Link.mjs'
4/* eslint-enable */
5import * as markers from './marker.mjs' 5import * as markers from './marker.mjs'
6 6
7/** 7/**
@@ -495,3 +495,31 @@ export const setLeaderLineType = (link) => new Folder({
495 }, 495 },
496 })), 496 })),
497}) 497})
498
499/**
500 * addMarker.
501 *
502 * @param {Object} options
503 * @param {HTMLElement} options.map - map element
504 * @param {Number[]} options.point - xy values in pixel
505 * @param {Function} options.isNameValid - check marker name is valid
506 * @param {Function} options.callback
507 */
508export const addMarker = ({
509 map,
510 point,
511 isNameValid = () => true,
512 callback = null,
513}) => new Item({
514 text: 'Add Marker',
515 onclick: () => {
516 let markerName
517 do {
518 markerName = window.prompt(markerName ? 'Name exists' : 'Marker Name')
519 } while (markerName && !isNameValid(markerName))
520 if (markerName === null) return
521
522 const marker = addMarkerByPoint({ point, map })
523 callback?.(marker)
524 },
525})
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs
index 718758b..0d88f80 100644
--- a/src/dumbymap.mjs
+++ b/src/dumbymap.mjs
@@ -543,10 +543,11 @@ export const generateMaps = (container, {
543 menu.remove() 543 menu.remove()
544 } 544 }
545 container.appendChild(menu) 545 container.appendChild(menu)
546 const containerRect = container.getBoundingClientRect()
546 new window.MutationObserver(() => { 547 new window.MutationObserver(() => {
547 menu.style.display = 'block' 548 menu.style.display = 'block'
548 menu.style.left = (e.layerX + 10) + 'px' 549 menu.style.left = (e.pageX - containerRect.left + 10) + 'px'
549 menu.style.top = (e.layerY + 5) + 'px' 550 menu.style.top = (e.pageY - containerRect.top + 5) + 'px'
550 clearTimeout(menu.timer) 551 clearTimeout(menu.timer)
551 }).observe(menu, { childList: true }) 552 }).observe(menu, { childList: true })
552 menu.timer = setTimeout(() => menu.remove(), 100) 553 menu.timer = setTimeout(() => menu.remove(), 100)
@@ -575,13 +576,22 @@ export const generateMaps = (container, {
575 } 576 }
576 577
577 // Menu Items for map 578 // Menu Items for map
578 if (map?.renderer?.results) { 579 if (map?.dataset?.render === 'fulfilled') {
579 const rect = map.getBoundingClientRect() 580 const rect = map.getBoundingClientRect()
580 const [x, y] = [e.x - rect.left, e.y - rect.top] 581 const [x, y] = [e.x - rect.left, e.y - rect.top]
581 menu.appendChild(menuItem.toggleMapFocus(map)) 582 menu.appendChild(menuItem.toggleMapFocus(map))
582 menu.appendChild(menuItem.renderResults(dumbymap, map)) 583 menu.appendChild(menuItem.renderResults(dumbymap, map))
583 menu.appendChild(menuItem.getCoordinatesByPixels(map, [x, y])) 584 menu.appendChild(new menuItem.Folder({
584 menu.appendChild(menuItem.restoreCamera(map)) 585 text: 'Actions',
586 items: [
587 menuItem.getCoordinatesByPixels(map, [x, y]),
588 menuItem.restoreCamera(map),
589 menuItem.addMarker({
590 point: [e.pageX, e.pageY],
591 map,
592 }),
593 ],
594 }))
585 } else { 595 } else {
586 // Toggle block focus 596 // Toggle block focus
587 if (block) { 597 if (block) {
diff --git a/src/editor.mjs b/src/editor.mjs
index f3b812a..b023d1a 100644
--- a/src/editor.mjs
+++ b/src/editor.mjs
@@ -2,6 +2,7 @@
2import { markdown2HTML, generateMaps } from './dumbymap' 2import { markdown2HTML, generateMaps } from './dumbymap'
3import { defaultAliases, parseConfigsFromYaml } from 'mapclay' 3import { defaultAliases, parseConfigsFromYaml } from 'mapclay'
4import * as menuItem from './MenuItem' 4import * as menuItem from './MenuItem'
5/* eslint-disable-next-line no-unused-vars */
5import { addMarkerByPoint } from './dumbyUtils.mjs' 6import { addMarkerByPoint } from './dumbyUtils.mjs'
6import { shiftByWindow } from './utils.mjs' 7import { shiftByWindow } from './utils.mjs'
7import * as tutorial from './tutorial' 8import * as tutorial from './tutorial'
@@ -54,6 +55,7 @@ let refLinks = []
54 * @param {CodeMirror} cm - The CodeMirror instance 55 * @param {CodeMirror} cm - The CodeMirror instance
55 * @param {RefLink} refLink - The reference link to append 56 * @param {RefLink} refLink - The reference link to append
56 */ 57 */
58/* eslint-disable-next-line no-unused-vars */
57const appendRefLink = (cm, refLink) => { 59const appendRefLink = (cm, refLink) => {
58 editor.dataset.update = 'false' 60 editor.dataset.update = 'false'
59 61
@@ -439,27 +441,27 @@ function menuForEditor (event, menu) {
439 menu.appendChild(switchToEditingMode) 441 menu.appendChild(switchToEditingMode)
440 } 442 }
441 443
442 const map = event.target.closest('.mapclay') 444 // const map = event.target.closest('.mapclay')
443 if (map) { 445 // if (map) {
444 const item = new menuItem.Item({ 446 // const item = new menuItem.Item({
445 text: 'Add Anchor', 447 // text: 'Add Anchor',
446 onclick: () => { 448 // onclick: () => {
447 let anchorName 449 // let anchorName
448 do { 450 // do {
449 anchorName = window.prompt(anchorName ? 'Name exists' : 'Name of Anchor') 451 // anchorName = window.prompt(anchorName ? 'Name exists' : 'Name of Anchor')
450 } while (refLinks.find(ref => ref === anchorName)) 452 // } while (refLinks.find(ref => ref === anchorName))
451 if (anchorName === null) return 453 // if (anchorName === null) return
452 454 //
453 const marker = addMarkerByPoint({ point: [event.clientX, event.clientY], map }) 455 // const marker = addMarkerByPoint({ point: [event.clientX, event.clientY], map })
454 const refLink = { 456 // const refLink = {
455 ref: anchorName, 457 // ref: anchorName,
456 link: `geo:${marker.dataset.xy.split(',').reverse()}`, 458 // link: `geo:${marker.dataset.xy.split(',').reverse()}`,
457 } 459 // }
458 appendRefLink(cm, refLink) 460 // appendRefLink(cm, refLink)
459 }, 461 // },
460 }) 462 // })
461 menu.insertBefore(item, menu.firstChild) 463 // menu.insertBefore(item, menu.firstChild)
462 } 464 // }
463} 465}
464 466
465/** 467/**