From 71623eca7acd3cfa8d9647b61cd178a75375fa62 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 20 Oct 2024 20:44:40 +0800 Subject: refactor: move method about adding geoscheme into DunbyUtils --- src/dumbyUtils.mjs | 13 ++++++++++++- src/dumbymap.mjs | 22 +++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 13c1142..46594bd 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs @@ -1,5 +1,5 @@ import LeaderLine from 'leader-line' -import { insideWindow, insideParent } from './utils' +import { insideWindow, insideParent, replaceTextNodes } from './utils' import proj4 from 'proj4' export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ @@ -439,3 +439,14 @@ export const dragForAnchor = (container, range, endOfLeaderLine) => { createGeoLink(geoLink) } } + +export const addGeoSchemeByText = async (element) => { + const coordPatterns = /(-?\d+\.?\d*)([,\x2F\uFF0C])(-?\d+\.?\d*)/ + const re = new RegExp(coordPatterns, 'g') + replaceTextNodes(element, re, match => { + const a = document.createElement('a') + a.href = `geo:0,0?xy=${match.at(1)},${match.at(3)}` + a.textContent = match.at(0) + return a + }) +} diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 3c3b172..46a532d 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs @@ -4,7 +4,7 @@ import MarkdownItFootnote from 'markdown-it-footnote' import MarkdownItFrontMatter from 'markdown-it-front-matter' import MarkdownItInjectLinenumbers from 'markdown-it-inject-linenumbers' import * as mapclay from 'mapclay' -import { replaceTextNodes, onRemove, animateRectTransition, throttle, shiftByWindow } from './utils' +import { onRemove, animateRectTransition, throttle, shiftByWindow } from './utils' import { Layout, SideBySide, Overlay, Sticky } from './Layout' import * as utils from './dumbyUtils' import * as menuItem from './MenuItem' @@ -240,25 +240,13 @@ export const generateMaps = (container, { }) /** LINK: Set CRS and GeoLinks */ - const setCRS = new Promise(resolve => { + const setCRS = (async () => { register(proj4) - fromEPSGCode(crs).then(() => resolve()) - }) - const addGeoSchemeByText = (async () => { - const coordPatterns = /(-?\d+\.?\d*)([,\x2F\uFF0C])(-?\d+\.?\d*)/ - const re = new RegExp(coordPatterns, 'g') - htmlHolder.querySelectorAll('.dumby-block') - .forEach(p => { - replaceTextNodes(p, re, match => { - const a = document.createElement('a') - a.href = `geo:0,0?xy=${match.at(1)},${match.at(3)}` - a.textContent = match.at(0) - return a - }) - }) + await fromEPSGCode(crs) })() + const addGeoScheme = utils.addGeoSchemeByText(htmlHolder) - Promise.all([setCRS, addGeoSchemeByText]).then(() => { + Promise.all([setCRS, addGeoScheme]).then(() => { Array.from(container.querySelectorAll(geoLinkSelector)) .map(utils.setGeoSchemeByCRS(crs)) .filter(link => link instanceof window.HTMLAnchorElement) -- cgit v1.2.3-70-g09d2