Skip to content

Commit

Permalink
fix: add function
Browse files Browse the repository at this point in the history
  • Loading branch information
sashuk committed Jun 27, 2023
1 parent 73c23c5 commit 49e8fa8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
42 changes: 37 additions & 5 deletions packages/picasso/src/LexicalEditor/LexicalEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { HeadingNode } from '@lexical/rich-text'
import { $generateHtmlFromNodes } from '@lexical/html'
import { ListItemNode, ListNode } from '@lexical/list'
import { $isRootTextContentEmpty } from '@lexical/text'
import { TextNode } from 'lexical'

import { createLexicalTheme } from './utils'
import noop from '../utils/noop'
Expand All @@ -26,14 +25,41 @@ import ToolbarPlugin from '../LexicalEditorToolbarPlugin'
import LexicalTextLengthPlugin from '../LexicalTextLengthPlugin'
import LexicalListPlugin from '../LexicalListPlugin'
import LexicalHeadingsReplacementPlugin from '../LexicalHeadingsReplacementPlugin'
import { PicassoTextNode } from './nodes/TestNode'

const useStyles = makeStyles<Theme>(styles, {
name: 'LexicalEditor',
})

const removeAttributesFromString = (htmlString: string) => {
return htmlString.replace(/\s(class|dir|value)="[^"]*"/g, '')
return htmlString.replace(/\s(class|dir|value|style)="[^"]*"/g, '')
}

// Courtesy of ChatGPT
const removeTagsAndKeepContent = (html: string, tagsToRemove: string[]) => {
const parser = new DOMParser()
const htmlDoc = parser.parseFromString(html, 'text/html')

tagsToRemove.forEach(function (tag) {
const elements = htmlDoc.getElementsByTagName(tag)

for (let counter = elements.length - 1; counter >= 0; counter--) {
const parent = elements[counter].parentNode

if (parent) {
while (elements[counter].firstChild) {
if (elements[counter].firstChild) {
parent.insertBefore(
elements[counter].firstChild as Node,
elements[counter]
)
}
}
parent.removeChild(elements[counter])
}
}
})

return htmlDoc.body.innerHTML
}

export type Props = BaseProps & {
Expand Down Expand Up @@ -141,7 +167,7 @@ const LexicalEditor = forwardRef<HTMLDivElement, Props>(function LexicalEditor(
ListNode,
ListItemNode,
HeadingNode,
PicassoTextNode,
//PicassoTextNode,
// {
// replace: TextNode,
// with: (node: TextNode) => {
Expand All @@ -165,7 +191,13 @@ const LexicalEditor = forwardRef<HTMLDivElement, Props>(function LexicalEditor(
? ''
: removeAttributesFromString($generateHtmlFromNodes(editor, null))

onChange(htmlValue)
const cleanedHtmlValue = removeTagsAndKeepContent(htmlValue, [
'b',
'i',
'span',
])

onChange(cleanedHtmlValue)
})
},
[onChange]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { HeadingNode } from '@lexical/rich-text'
import { useEffect } from 'react'

import { replaceHeadingNodes } from './utils'
import { TextNode } from 'lexical'
import { PicassoTextNode } from '../LexicalEditor/nodes/TestNode'

const LexicalHeadingsReplacementPlugin = () => {
const [editor] = useLexicalComposerContext()
Expand All @@ -13,12 +11,13 @@ const LexicalHeadingsReplacementPlugin = () => {
return editor.registerNodeTransform(HeadingNode, replaceHeadingNodes)
}, [editor])

useEffect(() => {
return editor.registerNodeTransform(TextNode, (node: TextNode) => {
console.log('@@@ replacing')
node.replace(new PicassoTextNode(node.getTextContent()))
})
}, [editor])
// useEffect(() => {
// return editor.registerNodeTransform(TextNode, (node: TextNode) => {
// console.log('@@@ replacing')
// const newNode = PicassoTextNode.clone(node)
// node.replace(newNode)
// })
// }, [editor])

return null
}
Expand Down

0 comments on commit 49e8fa8

Please sign in to comment.