From df606a1f7bcb32747085972b740c6685ffabcb71 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 23 Oct 2024 17:19:31 +0800 Subject: feat: add support for full-width digits --- src/dumbyUtils.mjs | 8 +++++--- src/utils.mjs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 12b5905..2f64e38 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs @@ -1,5 +1,5 @@ import LeaderLine from 'leader-line' -import { insideWindow, insideParent, replaceTextNodes } from './utils' +import { insideWindow, insideParent, replaceTextNodes, full2Half } from './utils' import proj4 from 'proj4' export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ @@ -439,11 +439,13 @@ export const dragForAnchor = (container, range, endOfLeaderLine) => { } export const addGeoSchemeByText = async (node) => { - const coordPatterns = /(-?\d+\.?\d*)([,\x2F\uFF0C])(-?\d+\.?\d*)/ + const digit = '[\\d\\uFF10-\\uFF19]' + const decimal = '[.\\uFF0E]' + const coordPatterns = `(-?${digit}+${decimal}?${digit}*)([,\x2F\uFF0C])(-?${digit}+${decimal}?${digit}*)` const re = new RegExp(coordPatterns, 'g') return replaceTextNodes(node, re, match => { - const [x, y] = [match.at(1), match.at(3)] + const [x, y] = [full2Half(match.at(1)), full2Half(match.at(3))] // Don't process string which can be used as date if (Date.parse(match.at(0) + ' 1990')) return null diff --git a/src/utils.mjs b/src/utils.mjs index dbb3881..d2c5d8f 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -235,3 +235,20 @@ export function getCommonAncestor (selector) { // Return the common ancestor return range.commonAncestorContainer } + +/** + * full2Half: full-Width Digits To Half-Width. + * + * @param {String} str + */ +export const full2Half = (str) => { + // Create a regular expression to match full-width digits (U+FF10 to U+FF19) + const fullWidthDigitsRegex = /[\uFF0E\uFF10-\uFF19]/g + + // Replace full-width digits with their half-width equivalents + return str.replace(fullWidthDigitsRegex, (match) => { + const fullWidthDigit = match.charCodeAt(0) + const halfWidthDigit = fullWidthDigit - 65248 // Offset to convert full-width to half-width + return String.fromCharCode(halfWidthDigit) + }) +} -- cgit v1.2.3-70-g09d2