diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-23 17:19:31 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-23 20:50:23 +0800 |
| commit | df606a1f7bcb32747085972b740c6685ffabcb71 (patch) | |
| tree | f6920b287c0e138f41fc2a3cf6efadcae4e44220 | |
| parent | 5c02dfcd703c20c304e62a87c0a6796009157d2f (diff) | |
feat: add support for full-width digits
| -rw-r--r-- | src/dumbyUtils.mjs | 8 | ||||
| -rw-r--r-- | 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 @@ | |||
| 1 | import LeaderLine from 'leader-line' | 1 | import LeaderLine from 'leader-line' |
| 2 | import { insideWindow, insideParent, replaceTextNodes } from './utils' | 2 | import { insideWindow, insideParent, replaceTextNodes, full2Half } from './utils' |
| 3 | import proj4 from 'proj4' | 3 | import proj4 from 'proj4' |
| 4 | 4 | ||
| 5 | export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ | 5 | export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ |
| @@ -439,11 +439,13 @@ export const dragForAnchor = (container, range, endOfLeaderLine) => { | |||
| 439 | } | 439 | } |
| 440 | 440 | ||
| 441 | export const addGeoSchemeByText = async (node) => { | 441 | export const addGeoSchemeByText = async (node) => { |
| 442 | const coordPatterns = /(-?\d+\.?\d*)([,\x2F\uFF0C])(-?\d+\.?\d*)/ | 442 | const digit = '[\\d\\uFF10-\\uFF19]' |
| 443 | const decimal = '[.\\uFF0E]' | ||
| 444 | const coordPatterns = `(-?${digit}+${decimal}?${digit}*)([,\x2F\uFF0C])(-?${digit}+${decimal}?${digit}*)` | ||
| 443 | const re = new RegExp(coordPatterns, 'g') | 445 | const re = new RegExp(coordPatterns, 'g') |
| 444 | 446 | ||
| 445 | return replaceTextNodes(node, re, match => { | 447 | return replaceTextNodes(node, re, match => { |
| 446 | const [x, y] = [match.at(1), match.at(3)] | 448 | const [x, y] = [full2Half(match.at(1)), full2Half(match.at(3))] |
| 447 | // Don't process string which can be used as date | 449 | // Don't process string which can be used as date |
| 448 | if (Date.parse(match.at(0) + ' 1990')) return null | 450 | if (Date.parse(match.at(0) + ' 1990')) return null |
| 449 | 451 | ||
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) { | |||
| 235 | // Return the common ancestor | 235 | // Return the common ancestor |
| 236 | return range.commonAncestorContainer | 236 | return range.commonAncestorContainer |
| 237 | } | 237 | } |
| 238 | |||
| 239 | /** | ||
| 240 | * full2Half: full-Width Digits To Half-Width. | ||
| 241 | * | ||
| 242 | * @param {String} str | ||
| 243 | */ | ||
| 244 | export const full2Half = (str) => { | ||
| 245 | // Create a regular expression to match full-width digits (U+FF10 to U+FF19) | ||
| 246 | const fullWidthDigitsRegex = /[\uFF0E\uFF10-\uFF19]/g | ||
| 247 | |||
| 248 | // Replace full-width digits with their half-width equivalents | ||
| 249 | return str.replace(fullWidthDigitsRegex, (match) => { | ||
| 250 | const fullWidthDigit = match.charCodeAt(0) | ||
| 251 | const halfWidthDigit = fullWidthDigit - 65248 // Offset to convert full-width to half-width | ||
| 252 | return String.fromCharCode(halfWidthDigit) | ||
| 253 | }) | ||
| 254 | } | ||