Skip to content

Commit

Permalink
feat(richtext-lexical)!: Update lexical from 0.12.6 to 0.13.1, port o…
Browse files Browse the repository at this point in the history
…ver all useful changes from playground (#5066)

* feat(richtext-lexical): Update lexical from 0.12.6 to 0.13.1, port over all useful changes from playground

* chore: upgrade lexical version used in monorepo
  • Loading branch information
AlessioGr committed Feb 12, 2024
1 parent 00fc034 commit 0d18822
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 228 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jwt-decode": "3.1.2",
"lexical": "0.12.5",
"lexical": "0.13.1",
"lint-staged": "^14.0.1",
"minimist": "1.2.8",
"mongodb-memory-server": "^9",
Expand Down
24 changes: 12 additions & 12 deletions packages/richtext-lexical/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@
},
"dependencies": {
"@faceless-ui/modal": "2.0.1",
"@lexical/headless": "0.12.6",
"@lexical/link": "0.12.6",
"@lexical/list": "0.12.6",
"@lexical/mark": "0.12.6",
"@lexical/markdown": "0.12.6",
"@lexical/react": "0.12.6",
"@lexical/rich-text": "0.12.6",
"@lexical/selection": "0.12.6",
"@lexical/utils": "0.12.6",
"@lexical/headless": "0.13.1",
"@lexical/link": "0.13.1",
"@lexical/list": "0.13.1",
"@lexical/mark": "0.13.1",
"@lexical/markdown": "0.13.1",
"@lexical/react": "0.13.1",
"@lexical/rich-text": "0.13.1",
"@lexical/selection": "0.13.1",
"@lexical/utils": "0.13.1",
"bson-objectid": "2.0.4",
"classnames": "^2.3.2",
"deep-equal": "2.2.3",
"i18next": "22.5.1",
"json-schema": "^0.4.0",
"lexical": "0.12.6",
"lexical": "0.13.1",
"lodash": "4.17.21",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-error-boundary": "^4.0.11",
"react-error-boundary": "4.0.12",
"react-i18next": "11.18.6",
"ts-essentials": "7.0.3"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/json-schema": "7.0.12",
"@types/json-schema": "7.0.15",
"@types/node": "20.6.2",
"@types/react": "18.2.15",
"payload": "workspace:*"
Expand Down
12 changes: 4 additions & 8 deletions packages/richtext-lexical/src/field/features/BlockQuote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SerializedQuoteNode } from '@lexical/rich-text'

import { $createQuoteNode, QuoteNode } from '@lexical/rich-text'
import { $setBlocksType } from '@lexical/selection'
import { $INTERNAL_isPointSelection, $getSelection } from 'lexical'
import { $getSelection } from 'lexical'

import type { HTMLConverter } from '../converters/html/converter/types'
import type { FeatureProvider } from '../types'
Expand Down Expand Up @@ -31,9 +31,7 @@ export const BlockQuoteFeature = (): FeatureProvider => {
onClick: ({ editor }) => {
editor.update(() => {
const selection = $getSelection()
if ($INTERNAL_isPointSelection(selection)) {
$setBlocksType(selection, () => $createQuoteNode())
}
$setBlocksType(selection, () => $createQuoteNode())
})
},
order: 20,
Expand All @@ -44,6 +42,7 @@ export const BlockQuoteFeature = (): FeatureProvider => {
markdownTransformers: [MarkdownTransformer],
nodes: [
{
type: QuoteNode.getType(),
converters: {
html: {
converter: async ({ converters, node, parent }) => {
Expand All @@ -62,7 +61,6 @@ export const BlockQuoteFeature = (): FeatureProvider => {
} as HTMLConverter<SerializedQuoteNode>,
},
node: QuoteNode,
type: QuoteNode.getType(),
},
],
props: null,
Expand All @@ -82,9 +80,7 @@ export const BlockQuoteFeature = (): FeatureProvider => {
keywords: ['quote', 'blockquote'],
onSelect: () => {
const selection = $getSelection()
if ($INTERNAL_isPointSelection(selection)) {
$setBlocksType(selection, () => $createQuoteNode())
}
$setBlocksType(selection, () => $createQuoteNode())
},
}),
],
Expand Down
8 changes: 3 additions & 5 deletions packages/richtext-lexical/src/field/features/Heading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { HeadingTagType, SerializedHeadingNode } from '@lexical/rich-text'

import { $createHeadingNode, HeadingNode } from '@lexical/rich-text'
import { $setBlocksType } from '@lexical/selection'
import { $INTERNAL_isPointSelection, $getSelection } from 'lexical'
import { $getSelection } from 'lexical'

import type { HTMLConverter } from '../converters/html/converter/types'
import type { FeatureProvider } from '../types'
Expand All @@ -14,9 +14,7 @@ import { MarkdownTransformer } from './markdownTransformer'

const setHeading = (headingSize: HeadingTagType) => {
const selection = $getSelection()
if ($INTERNAL_isPointSelection(selection)) {
$setBlocksType(selection, () => $createHeadingNode(headingSize))
}
$setBlocksType(selection, () => $createHeadingNode(headingSize))
}

type Props = {
Expand Down Expand Up @@ -67,6 +65,7 @@ export const HeadingFeature = (props: Props): FeatureProvider => {
markdownTransformers: [MarkdownTransformer(enabledHeadingSizes)],
nodes: [
{
type: HeadingNode.getType(),
converters: {
html: {
converter: async ({ converters, node, parent }) => {
Expand All @@ -85,7 +84,6 @@ export const HeadingFeature = (props: Props): FeatureProvider => {
} as HTMLConverter<SerializedHeadingNode>,
},
node: HeadingNode,
type: HeadingNode.getType(),
},
],
props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {

import type { LinkPayload } from '../plugins/floatingLinkEditor/types'

import { $isAutoLinkNode } from './AutoLinkNode'

export type LinkFields = {
// unknown, custom fields:
[key: string]: unknown
Expand Down Expand Up @@ -140,8 +142,8 @@ export class LinkNode extends ElementNode {
exportJSON(): SerializedLinkNode {
return {
...super.exportJSON(),
fields: this.getFields(),
type: this.getType(),
fields: this.getFields(),
version: 2,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ function handleLinkCreation(
onChange: ChangeHandler,
): void {
let currentNodes = [...nodes]
let text = currentNodes.map((node) => node.getTextContent()).join('')
const initialText = currentNodes.map((node) => node.getTextContent()).join('')
let text = initialText

let match
let invalidMatchEnd = 0
Expand All @@ -247,7 +248,7 @@ function handleLinkCreation(
const isValid = isContentAroundIsValid(
invalidMatchEnd + matchStart,
invalidMatchEnd + matchEnd,
text,
initialText,
currentNodes,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { useEditorConfigContext } from '../../../../../lexical/config/EditorConf
import { getSelectedNode } from '../../../../../lexical/utils/getSelectedNode'
import { setFloatingElemPositionForLinkEditor } from '../../../../../lexical/utils/setFloatingElemPositionForLinkEditor'
import { LinkDrawer } from '../../../drawer'
import { $isAutoLinkNode } from '../../../nodes/AutoLinkNode'
import { $createLinkNode } from '../../../nodes/LinkNode'
import { $isLinkNode, TOGGLE_LINK_COMMAND } from '../../../nodes/LinkNode'
import { transformExtraFields } from '../utilities'
import { TOGGLE_LINK_WITH_MODAL_COMMAND } from './commands'
Expand Down Expand Up @@ -73,7 +75,7 @@ export function LinkEditor({
// Sanitize custom fields here
const validRelationships = config.collections.map((c) => c.slug) || []
const fields = sanitizeFields({
config: config,
config,
fields: fieldsUnsanitized,
validRelationships,
})
Expand All @@ -84,10 +86,11 @@ export function LinkEditor({
const { closeModal, toggleModal } = useModal()
const editDepth = useEditDepth()
const [isLink, setIsLink] = useState(false)
const [isAutoLink, setIsAutoLink] = useState(false)

const drawerSlug = formatDrawerSlug({
depth: editDepth,
slug: `lexical-rich-text-link-` + uuid,
depth: editDepth,
})

const updateLinkEditor = useCallback(async () => {
Expand All @@ -98,9 +101,10 @@ export function LinkEditor({
if ($isRangeSelection(selection)) {
const node = getSelectedNode(selection)
selectedNodeDomRect = editor.getElementByKey(node.getKey())?.getBoundingClientRect()
const linkParent: LinkNode = $findMatchingParent(node, $isLinkNode) as LinkNode
const linkParent: LinkNode = $findMatchingParent(node, $isLinkNode)
if (linkParent == null) {
setIsLink(false)
setIsAutoLink(false)
setLinkUrl('')
setLinkLabel('')
return
Expand Down Expand Up @@ -152,6 +156,11 @@ export function LinkEditor({
})
setInitialState(state)
setIsLink(true)
if ($isAutoLinkNode(linkParent)) {
setIsAutoLink(true)
} else {
setIsAutoLink(false)
}
}

const editorElem = editorRef.current
Expand Down Expand Up @@ -265,6 +274,7 @@ export function LinkEditor({
() => {
if (isLink) {
setIsLink(false)
setIsAutoLink(false)
return true
}
return false
Expand Down Expand Up @@ -301,18 +311,20 @@ export function LinkEditor({
tabIndex={0}
type="button"
/>
<button
aria-label="Remove link"
className="link-trash"
onClick={() => {
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null)
}}
onMouseDown={(event) => {
event.preventDefault()
}}
tabIndex={0}
type="button"
/>
{!isAutoLink && (
<button
aria-label="Remove link"
className="link-trash"
onClick={() => {
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null)
}}
onMouseDown={(event) => {
event.preventDefault()
}}
tabIndex={0}
type="button"
/>
)}
</React.Fragment>
)}
</div>
Expand All @@ -325,6 +337,22 @@ export function LinkEditor({

const newLinkPayload: LinkPayload = data as LinkPayload

// See: https://github.com/facebook/lexical/pull/5536. This updates autolink nodes to link nodes whenever a change was made (which is good!).
editor.update(() => {
const selection = $getSelection()
if ($isRangeSelection(selection)) {
const parent = getSelectedNode(selection).getParent()
if ($isAutoLinkNode(parent)) {
const linkNode = $createLinkNode({
fields: newLinkPayload.fields,
})
parent.replace(linkNode, true)
}
}
})

// Needs to happen AFTER a potential auto link => link node conversion, as otherwise, the updated text to display may be lost due to
// it being applied to the auto link node instead of the link node.
editor.dispatchCommand(TOGGLE_LINK_COMMAND, newLinkPayload)
}}
initialState={initialState}
Expand Down
10 changes: 3 additions & 7 deletions packages/richtext-lexical/src/field/features/Paragraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $setBlocksType } from '@lexical/selection'
import { $INTERNAL_isPointSelection, $createParagraphNode, $getSelection } from 'lexical'
import { $createParagraphNode, $getSelection } from 'lexical'

import type { FeatureProvider } from '../types'

Expand All @@ -23,9 +23,7 @@ export const ParagraphFeature = (): FeatureProvider => {
onClick: ({ editor }) => {
editor.update(() => {
const selection = $getSelection()
if ($INTERNAL_isPointSelection(selection)) {
$setBlocksType(selection, () => $createParagraphNode())
}
$setBlocksType(selection, () => $createParagraphNode())
})
},
order: 1,
Expand All @@ -49,9 +47,7 @@ export const ParagraphFeature = (): FeatureProvider => {
onSelect: ({ editor }) => {
editor.update(() => {
const selection = $getSelection()
if ($INTERNAL_isPointSelection(selection)) {
$setBlocksType(selection, () => $createParagraphNode())
}
$setBlocksType(selection, () => $createParagraphNode())
})
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const LexicalEditorTheme: EditorThemeClasses = {
inlineImage: 'LexicalEditor__inline-image',
link: 'LexicalEditorTheme__link',
list: {
checklist: 'LexicalEditorTheme__checklist',
listitem: 'LexicalEditorTheme__listItem',
listitemChecked: 'LexicalEditorTheme__listItemChecked',
listitemUnchecked: 'LexicalEditorTheme__listItemUnchecked',
Expand Down

0 comments on commit 0d18822

Please sign in to comment.