diff --git a/src/editor/core/worker/works/wordCount.ts b/src/editor/core/worker/works/wordCount.ts index 69ef70a..2a98eae 100644 --- a/src/editor/core/worker/works/wordCount.ts +++ b/src/editor/core/worker/works/wordCount.ts @@ -11,6 +11,9 @@ enum ControlComponent { VALUE = 'value' } +const ZERO = '\u200B' +const WRAP = '\n' + function pickText(elementList: IElement[]): string { let text = '' let e = 0 @@ -68,28 +71,59 @@ function pickText(elementList: IElement[]): string { } function groupText(text: string): string[] { - const textList: string[] = [] + const characterList: string[] = [] + // 英文或数字整体分隔为一个字数 + const numberReg = /[0-9]/ + const letterReg = /[A-Za-z]/ + const blankReg = /\s/ + // for of 循环字符 + let isPreLetter = false + let isPreNumber = false + let compositionText = '' + // 处理组合文本 + function pushCompositionText() { + if (compositionText) { + characterList.push(compositionText) + compositionText = '' + } + } for (const t of text) { - textList.push(t) + if (letterReg.test(t)) { + if (!isPreLetter) { + pushCompositionText() + } + compositionText += t + isPreLetter = true + isPreNumber = false + } else if (numberReg.test(t)) { + if (!isPreNumber) { + pushCompositionText() + } + compositionText += t + isPreLetter = false + isPreNumber = true + } else { + pushCompositionText() + isPreLetter = false + isPreNumber = false + if (!blankReg.test(t)) { + characterList.push(t) + } + } } - return textList + pushCompositionText() + return characterList } onmessage = (evt) => { const elementList = evt.data - - // 提取所有文本 + // 提取文本 const originText = pickText(elementList) - // 过滤文本 - const ZERO = '\u200B' - const WRAP = '\n' const filterText = originText .replace(new RegExp(`^${ZERO}`), '') .replace(new RegExp(ZERO, 'g'), WRAP) - - // 英文或数字以逗号/换行符分隔为一个字数 + // 文本分组 const textGroup = groupText(filterText) - postMessage(textGroup.length) }