Update characters

This commit is contained in:
Astrian Zheng 2024-04-17 20:39:09 +10:00
parent 52b01e4c50
commit 22becb440a
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -1,11 +1,31 @@
export default function (nodeClass: string) {
const nodes = document.querySelectorAll(`.${nodeClass}`)
export default function (selectedNode: string, spacing: number = 10) {
let spacingCharacter = ''
switch (spacing) {
case 3:
spacingCharacter = '\u2004'
break
case 4:
spacingCharacter = '\u2005'
break
case 5:
spacingCharacter = '\u2009'
break
case 6:
spacingCharacter = '\u2006'
break
case 10:
spacingCharacter = '\u200A'
break
default:
spacingCharacter = '\u2006'
}
const nodes = document.querySelectorAll(`${selectedNode}`)
for (let i in nodes) {
processText(nodes[i])
processText(nodes[i], spacingCharacter)
}
}
function processText(node: Node): void {
function processText(node: Node, spacingCharacter: string): void {
if (node.nodeType === Node.TEXT_NODE) {
// detect the characters includes chinese and english, or japanese (kanji or kana) and english
const reg = /[\u4e00-\u9fa5\u3040-\u30FF][a-zA-Z]|\w[\u4e00-\u9fa5\u3040-\u30FF]|\w[a-zA-Z]/
@ -20,18 +40,16 @@ function processText(node: Node): void {
const english_and_number = /[a-zA-Z0-9]/
// if the char is chinese or japanese (kanji or kana)
if (cnchar_and_kanji.test(textArr[i]) || kana.test(textArr[i])) {
console.log(`textArr[i]: ${textArr[i]}`)
// if next char is english
if (/[a-zA-Z]/.test(textArr[i + 1])) {
console.log(`textArr[i + 1]: ${textArr[i + 1]}`)
// 1/8 em space
newText += textArr[i] + '\u2009'
newText += textArr[i] + spacingCharacter
} else {
newText += textArr[i]
}
} else if (english_and_number.test(textArr[i])) {
if (cnchar_and_kanji.test(textArr[i + 1]) || kana.test(textArr[i + 1])) {
newText += textArr[i] + '\u2009'
newText += textArr[i] + spacingCharacter
} else {
newText += textArr[i]
}
@ -44,7 +62,6 @@ function processText(node: Node): void {
}
} else {
if (!node.nodeName) return
console.log(node.nodeName)
for (let i = 0; i < node.childNodes.length; i++) processText(node.childNodes[i])
for (let i = 0; i < node.childNodes.length; i++) processText(node.childNodes[i], spacingCharacter)
}
}