From 22be38a505183a5615b2934571e4c95b55c07583 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 25 Sep 2024 14:55:19 +0800 Subject: feat: add MenuItem for GeoLink --- src/MenuItem.mjs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/MenuItem.mjs') diff --git a/src/MenuItem.mjs b/src/MenuItem.mjs index 50afdb5..1b04d1c 100644 --- a/src/MenuItem.mjs +++ b/src/MenuItem.mjs @@ -1,3 +1,39 @@ +import { createGeoLink } from './dumbymap' + +export class GeoLink { + + constructor({ range }) { + this.range = range + } + + createElement = () => { + const element = document.createElement('div') + element.className = 'menu-item-add-geolink' + element.innerText = "Add GeoLink" + element.onclick = this.addGeoLinkbyRange + + return element + } + + addGeoLinkbyRange = () => { + const range = this.range + const content = range.toString() + // FIXME Apply geolink only on matching sub-range + const match = content.match(/(^\D*[\d.]+)\D+([\d.]+)\D*$/) + if (!match) return false + + const [x, y] = match.slice(1) + const anchor = document.createElement('a') + anchor.textContent = content + // FIXME apply WGS84 + anchor.href = `geo:${y},${x}?xy=${x},${y}` + + if (createGeoLink(anchor)) { + range.deleteContents() + range.insertNode(anchor) + } + } +} export class Suggestion { constructor({ text, replace }) { this.text = text @@ -13,7 +49,8 @@ export class Suggestion { } option.classList.add('container__suggestion'); option.onmouseover = () => { - Array.from(menu.children).forEach(s => s.classList.remove('focus')) + Array.from(option.parentElement?.children ?? []) + .forEach(s => s.classList.remove('focus')) option.classList.add('focus') } option.onmouseout = () => { -- cgit v1.2.3-70-g09d2