Skip to content

Commit 4dc0712

Browse files
committed
fix(characters): entity index in wrap innerHTML
1 parent 8ad2af2 commit 4dc0712

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

packages/better-write-app/src/use/characters.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useTransformer } from './generator/transformer'
88
import { useToast } from 'vue-toastification'
99
import { useI18n } from 'vue-i18n'
1010
import { useStorage } from './storage/storage'
11+
import { useEntity } from './entity'
1112

1213
export const useCharacters = () => {
1314
const CONTEXT = useContextStore()
@@ -19,6 +20,7 @@ export const useCharacters = () => {
1920
const toast = useToast()
2021
const { t } = useI18n()
2122
const storage = useStorage()
23+
const ent = useEntity()
2224

2325
const handler = (index?: ID<number>, inner?: string) => {
2426
const getEntities = (index?: ID<number>): Entities => {
@@ -32,7 +34,12 @@ export const useCharacters = () => {
3234
str: string,
3335
c: ProjectStateCharacter
3436
) => {
35-
const text = utils.text().defaultWhitespace(str)
37+
if (!ent.utils().isTextBlock(entity.type)) return
38+
39+
const text = ASTUtils.normalize(str, { type: 'all', whitespace: true })
40+
41+
if (!text) return
42+
3643
const color = utils.convert().hexToRgbA(c.color, c.colorAlpha)
3744

3845
const isValidImportant =
@@ -64,17 +71,12 @@ export const useCharacters = () => {
6471
default:
6572
break
6673
}
67-
68-
/*
69-
if (!text.toLowerCase().includes(c.name.toLowerCase()))
70-
entity.visual.custom = undefined
71-
*/
7274
}
7375

74-
entities.forEach((e) => {
76+
entities.forEach((e, i) => {
7577
e.visual.custom = undefined
7678

77-
PROJECT.characters?.list?.forEach((character, i) => {
79+
PROJECT.characters?.list?.forEach((character) => {
7880
onSetter(e, i === 0 && inner ? inner : e.raw, character)
7981
})
8082
})

packages/better-write-app/src/use/entity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ export const useEntity = () => {
8989
type === 'paragraph' ||
9090
type === 'heading-one' ||
9191
type === 'heading-two' ||
92-
type === 'heading-three'
92+
type === 'heading-three' ||
93+
type === 'list' ||
94+
type === 'checkbox'
9395
)
9496
}
9597

packages/better-write-contenteditable-ast/src/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ export const occurrences = (
2121
}
2222
return n
2323
}
24+
25+
export const normalize = (
26+
str: string,
27+
options: {
28+
type: 'all' | 'inserts'
29+
whitespace?: boolean
30+
}
31+
) => {
32+
if (options?.whitespace)
33+
str = str.replaceAll('&nbsp;', ' ').replaceAll('&#160', ' ')
34+
35+
switch (options.type) {
36+
case 'all':
37+
return str.replaceAll(/<(?!\/?span(?=>|\s?.*>))\/?.*?>/g, '')
38+
case 'inserts':
39+
return str
40+
.replaceAll('<b>', '')
41+
.replaceAll('</b>', '')
42+
.replaceAll('<i>', '')
43+
.replaceAll('</i>', '')
44+
.replaceAll('<u>', '')
45+
.replaceAll('</u>', '')
46+
}
47+
}

0 commit comments

Comments
 (0)