Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rich text editor behaviour in forms #3656

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/picasso/src/LexicalEditor/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'
import LexicalEditor from './LexicalEditor'
import type { Props } from './LexicalEditor'
import LexicalTextLengthPlugin from '../LexicalTextLengthPlugin'
import LexicalHeadingsReplacementPlugin from '../LexicalHeadingsReplacementPlugin'
import ToolbarPlugin from '../LexicalEditorToolbarPlugin'
import LexicalListPlugin from '../LexicalListPlugin'

Expand Down Expand Up @@ -43,9 +44,19 @@ jest.mock('../LexicalTextLengthPlugin', () => ({
default: jest.fn(() => <div>LexicalTextLengthPlugin</div>),
}))

jest.mock('../LexicalHeadingsReplacementPlugin', () => ({
__esModule: true,
default: jest.fn(() => <div>LexicalHeadingsReplacementPlugin</div>),
}))

const mockedLexicalTextLengthPlugin =
LexicalTextLengthPlugin as jest.MockedFunction<typeof LexicalTextLengthPlugin>

const mockedLexicalHeadingsReplacementPlugin =
LexicalHeadingsReplacementPlugin as jest.MockedFunction<
typeof LexicalHeadingsReplacementPlugin
>

const mockedToolbarPlugin = ToolbarPlugin as jest.MockedFunction<
typeof ToolbarPlugin
>
Expand All @@ -67,6 +78,7 @@ const renderLexicalEditor = (props: Partial<OmitInternalProps<Props>> = {}) => {
describe('LexicalEditor', () => {
beforeEach(() => {
mockedLexicalTextLengthPlugin.mockImplementation(() => null)
mockedLexicalHeadingsReplacementPlugin.mockImplementation(() => null)
mockedToolbarPlugin.mockImplementation(() => (
<div>LexicalEditorToolbarPlugin</div>
))
Expand Down Expand Up @@ -98,6 +110,14 @@ describe('LexicalEditor', () => {
)
})

it('renders LexicalHeadingsReplacementPlugin', async () => {
renderLexicalEditor()

await waitFor(() =>
expect(mockedLexicalHeadingsReplacementPlugin).toHaveBeenCalled()
)
})

it('renders OnChangePlugin with correct props', () => {
renderLexicalEditor()

Expand Down
2 changes: 2 additions & 0 deletions packages/picasso/src/LexicalEditor/LexicalEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { ChangeHandler, TextLengthChangeHandler } from './types'
import ToolbarPlugin from '../LexicalEditorToolbarPlugin'
import LexicalTextLengthPlugin from '../LexicalTextLengthPlugin'
import LexicalListPlugin from '../LexicalListPlugin'
import LexicalHeadingsReplacementPlugin from '../LexicalHeadingsReplacementPlugin'

const useStyles = makeStyles<Theme>(styles, {
name: 'LexicalEditor',
Expand Down Expand Up @@ -173,6 +174,7 @@ const LexicalEditor = forwardRef<HTMLDivElement, Props>(function LexicalEditor(
<OnChangePlugin ignoreSelectionChange onChange={handleChange} />
{autoFocus && <AutoFocusPlugin />}

<LexicalHeadingsReplacementPlugin />
<LexicalTextLengthPlugin onTextLengthChange={onTextLengthChange} />
<LexicalListPlugin />

Expand Down
1 change: 0 additions & 1 deletion packages/picasso/src/LexicalEditor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export { synchronizeToolbarState } from './synchronizeToolbarState'
export { toolbarStateReducer } from './toolbarState'
export { getLexicalNode } from './getLexicalNode'
export { createLexicalTheme } from './createLexicalTheme'
export { replaceHeadingNodes } from './replaceHeadingNodes'
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { LexicalEditor } from 'lexical'
import { HeadingNode } from '@lexical/rich-text'
import { COMMAND_PRIORITY_CRITICAL, SELECTION_CHANGE_COMMAND } from 'lexical'

import { replaceHeadingNodes } from './replaceHeadingNodes'
import type { LexicalEditor } from 'lexical'

export type LexicalRegisterParams = {
editor: LexicalEditor
Expand Down Expand Up @@ -31,15 +28,9 @@ export const registerLexicalEvents = ({
COMMAND_PRIORITY_CRITICAL
)

const headingListener = editor.registerNodeTransform(
HeadingNode,
replaceHeadingNodes
)

// Cleanup is necessary to avoid listeners piling up with useEffect
return () => {
editorListenerCleanup()
editorCommandsCleanup()
headingListener()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { HeadingNode } from '@lexical/rich-text'
import { useEffect } from 'react'

import { replaceHeadingNodes } from './utils'

const LexicalHeadingsReplacementPlugin = () => {
const [editor] = useLexicalComposerContext()

useEffect(() => {
return editor.registerNodeTransform(HeadingNode, replaceHeadingNodes)
}, [editor])

return null
}

export default LexicalHeadingsReplacementPlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './LexicalHeadingsReplacementPlugin'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { replaceHeadingNodes } from './replaceHeadingNodes'
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ describe('replaceHeadingNodes', () => {
)

const appendMock = jest.fn()
const selectMock = jest.fn()
Copy link
Contributor Author

@sashuk sashuk Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was broken in feature branch, not related to the bug


mockedCreateParagraphNode.mockImplementation(
() =>
({
append: appendMock,
select: selectMock,
} as unknown as ParagraphNode)
)

Expand All @@ -67,8 +69,12 @@ describe('replaceHeadingNodes', () => {

expect(setFormatMock).toHaveBeenCalledWith('bold')
expect(appendMock).toHaveBeenCalledWith({ setFormat: setFormatMock })
expect(selectMock).toHaveBeenCalled()

expect(node.replace).toHaveBeenCalledWith({ append: appendMock })
expect(node.replace).toHaveBeenCalledWith({
append: appendMock,
select: selectMock,
})
})
})
})
Loading