Skip to content

Commit

Permalink
fix(characters): entity index in wrap innerHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Sep 28, 2022
1 parent 8ad2af2 commit 4dc0712
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
18 changes: 10 additions & 8 deletions packages/better-write-app/src/use/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useTransformer } from './generator/transformer'
import { useToast } from 'vue-toastification'
import { useI18n } from 'vue-i18n'
import { useStorage } from './storage/storage'
import { useEntity } from './entity'

export const useCharacters = () => {
const CONTEXT = useContextStore()
Expand All @@ -19,6 +20,7 @@ export const useCharacters = () => {
const toast = useToast()
const { t } = useI18n()
const storage = useStorage()
const ent = useEntity()

const handler = (index?: ID<number>, inner?: string) => {
const getEntities = (index?: ID<number>): Entities => {
Expand All @@ -32,7 +34,12 @@ export const useCharacters = () => {
str: string,
c: ProjectStateCharacter
) => {
const text = utils.text().defaultWhitespace(str)
if (!ent.utils().isTextBlock(entity.type)) return

const text = ASTUtils.normalize(str, { type: 'all', whitespace: true })

if (!text) return

const color = utils.convert().hexToRgbA(c.color, c.colorAlpha)

const isValidImportant =
Expand Down Expand Up @@ -64,17 +71,12 @@ export const useCharacters = () => {
default:
break
}

/*
if (!text.toLowerCase().includes(c.name.toLowerCase()))
entity.visual.custom = undefined
*/
}

entities.forEach((e) => {
entities.forEach((e, i) => {
e.visual.custom = undefined

PROJECT.characters?.list?.forEach((character, i) => {
PROJECT.characters?.list?.forEach((character) => {
onSetter(e, i === 0 && inner ? inner : e.raw, character)
})
})
Expand Down
4 changes: 3 additions & 1 deletion packages/better-write-app/src/use/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const useEntity = () => {
type === 'paragraph' ||
type === 'heading-one' ||
type === 'heading-two' ||
type === 'heading-three'
type === 'heading-three' ||
type === 'list' ||
type === 'checkbox'
)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/better-write-contenteditable-ast/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ export const occurrences = (
}
return n
}

export const normalize = (
str: string,
options: {
type: 'all' | 'inserts'
whitespace?: boolean
}
) => {
if (options?.whitespace)
str = str.replaceAll('&nbsp;', ' ').replaceAll('&#160', ' ')

switch (options.type) {
case 'all':
return str.replaceAll(/<(?!\/?span(?=>|\s?.*>))\/?.*?>/g, '')
case 'inserts':
return str
.replaceAll('<b>', '')
.replaceAll('</b>', '')
.replaceAll('<i>', '')
.replaceAll('</i>', '')
.replaceAll('<u>', '')
.replaceAll('</u>', '')
}
}

0 comments on commit 4dc0712

Please sign in to comment.