feat:add word count
This commit is contained in:
@@ -11,6 +11,9 @@ enum ControlComponent {
|
|||||||
VALUE = 'value'
|
VALUE = 'value'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ZERO = '\u200B'
|
||||||
|
const WRAP = '\n'
|
||||||
|
|
||||||
function pickText(elementList: IElement[]): string {
|
function pickText(elementList: IElement[]): string {
|
||||||
let text = ''
|
let text = ''
|
||||||
let e = 0
|
let e = 0
|
||||||
@@ -68,28 +71,59 @@ function pickText(elementList: IElement[]): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function groupText(text: string): string[] {
|
function groupText(text: string): string[] {
|
||||||
const textList: string[] = []
|
const characterList: string[] = []
|
||||||
for (const t of text) {
|
// 英文或数字整体分隔为一个字数
|
||||||
textList.push(t)
|
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 = ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return textList
|
for (const t of text) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pushCompositionText()
|
||||||
|
return characterList
|
||||||
}
|
}
|
||||||
|
|
||||||
onmessage = (evt) => {
|
onmessage = (evt) => {
|
||||||
const elementList = <IElement[]>evt.data
|
const elementList = <IElement[]>evt.data
|
||||||
|
// 提取文本
|
||||||
// 提取所有文本
|
|
||||||
const originText = pickText(elementList)
|
const originText = pickText(elementList)
|
||||||
|
|
||||||
// 过滤文本
|
// 过滤文本
|
||||||
const ZERO = '\u200B'
|
|
||||||
const WRAP = '\n'
|
|
||||||
const filterText = originText
|
const filterText = originText
|
||||||
.replace(new RegExp(`^${ZERO}`), '')
|
.replace(new RegExp(`^${ZERO}`), '')
|
||||||
.replace(new RegExp(ZERO, 'g'), WRAP)
|
.replace(new RegExp(ZERO, 'g'), WRAP)
|
||||||
|
// 文本分组
|
||||||
// 英文或数字以逗号/换行符分隔为一个字数
|
|
||||||
const textGroup = groupText(filterText)
|
const textGroup = groupText(filterText)
|
||||||
|
|
||||||
postMessage(textGroup.length)
|
postMessage(textGroup.length)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user