From af4831dd8ad2df0b25a64ae3529a20b921e26c7c Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 8 Oct 2024 16:47:29 +0800 Subject: refactor: move addAnchorByEvent into dumbyUtils --- src/dumbyUtils.mjs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src/dumbyUtils.mjs') diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 95bb3dd..be45139 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs @@ -6,7 +6,7 @@ import { insideWindow, insideParent } from './utils' * * @param {Boolean} reverse -- focus previous map */ -export function focusNextMap (reverse = false) { +export function focusNextMap(reverse = false) { const renderedList = this.utils.renderedMaps() const index = renderedList.findIndex(e => e.classList.contains('focus')) const nextIndex = (index + (reverse ? -1 : 1)) % renderedList.length @@ -21,7 +21,7 @@ export function focusNextMap (reverse = false) { * * @param {Boolean} reverse -- focus previous block */ -export function focusNextBlock (reverse = false) { +export function focusNextBlock(reverse = false) { const blocks = this.blocks.filter(b => b.checkVisibility({ contentVisibilityAuto: true, @@ -56,7 +56,7 @@ export const scrollToBlock = block => { /** * focusDelay. Delay of throttle, value changes by cases */ -export function focusDelay () { +export function focusDelay() { return window.window.getComputedStyle(this.showcase).display === 'none' ? 50 : 300 } @@ -65,7 +65,7 @@ export function focusDelay () { * * @param {Boolean} reverse -- Switch to previous one */ -export function switchToNextLayout (reverse = false) { +export function switchToNextLayout(reverse = false) { const layouts = this.layouts const currentLayoutName = this.container.getAttribute('data-layout') const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName) @@ -81,7 +81,7 @@ export function switchToNextLayout (reverse = false) { /** * removeBlockFocus. */ -export function removeBlockFocus () { +export function removeBlockFocus() { this.blocks.forEach(b => b.classList.remove('focus')) } @@ -243,3 +243,33 @@ const isAnchorVisible = anchor => { const mapContainer = anchor.closest('.mapclay') return insideWindow(anchor) && insideParent(anchor, mapContainer) } + +export const addAnchorByEvent = ({ + event, + map, + validateAnchorName = () => true +}) => { + const rect = map.getBoundingClientRect() + const [x, y] = map.renderer + .unproject([event.x - rect.left, event.y - rect.top]) + .map(coord => Number(coord.toFixed(7))) + + let prompt + let anchorName + + do { + prompt = prompt ? 'Anchor name exists' : 'Name this anchor' + anchorName = window.prompt(prompt, `${x}, ${y}`) + } + while (anchorName !== null && !validateAnchorName(anchorName)) + if (anchorName === null) return + + const link = `geo:${y},${x}?xy=${x},${y}&id=${map.id} "${anchorName}"` + map.renderer.addMarker({ + xy: [x, y], + title: `${map.id}@${x}, ${y}`, + type: 'circle', + }) + + return { ref: anchorName, link } +} -- cgit v1.2.3-70-g09d2