Skip to content

Commit

Permalink
feat: split text support multiple languages #593
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed May 23, 2024
1 parent bcf311f commit 1cb07af
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/editor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,27 @@ export function getUUID(): string {

export function splitText(text: string): string[] {
const data: string[] = []
const symbolMap = new Map<number, string>()
for (const match of text.matchAll(UNICODE_SYMBOL_REG)) {
symbolMap.set(match.index!, match[0])
}
let t = 0
while (t < text.length) {
const symbol = symbolMap.get(t)
if (symbol) {
data.push(symbol)
t += symbol.length
} else {
data.push(text[t])
t++
if (Intl.Segmenter) {
const segmenter = new Intl.Segmenter()
const segments = segmenter.segment(text)
for (const { segment } of segments) {
data.push(segment)
}
} else {
const symbolMap = new Map<number, string>()
for (const match of text.matchAll(UNICODE_SYMBOL_REG)) {
symbolMap.set(match.index!, match[0])
}
let t = 0
while (t < text.length) {
const symbol = symbolMap.get(t)
if (symbol) {
data.push(symbol)
t += symbol.length
} else {
data.push(text[t])
t++
}
}
}
return data
Expand Down

0 comments on commit 1cb07af

Please sign in to comment.