Skip to content

Commit

Permalink
feat(richtext-lexical): new FieldsDrawer utility, improve blocks feat…
Browse files Browse the repository at this point in the history
…ure performance (#6967)
  • Loading branch information
AlessioGr authored Jun 27, 2024
2 parents 8c2779c + 0a42281 commit 0017c67
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 298 deletions.
2 changes: 2 additions & 0 deletions packages/richtext-lexical/src/exports/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ export {
$isBlockNode,
BlockNode,
} from '../../features/blocks/nodes/BlocksNode.js'

export { FieldsDrawer } from '../../utilities/fieldsDrawer/Drawer.js'
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
apiRoute: config.routes.api,
body: {
id,
data: JSON.parse(JSON.stringify(formData)),
data: formData,
operation: 'update',
schemaPath: schemaFieldsPath,
},
Expand Down
50 changes: 0 additions & 50 deletions packages/richtext-lexical/src/features/link/drawer/index.scss

This file was deleted.

9 changes: 0 additions & 9 deletions packages/richtext-lexical/src/features/link/drawer/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { LinkPayload } from '../types.js'
import { useEditorConfigContext } from '../../../../../lexical/config/client/EditorConfigProvider.js'
import { getSelectedNode } from '../../../../../lexical/utils/getSelectedNode.js'
import { setFloatingElemPositionForLinkEditor } from '../../../../../lexical/utils/setFloatingElemPositionForLinkEditor.js'
import { LinkDrawer } from '../../../drawer/index.js'
import { FieldsDrawer } from '../../../../../utilities/fieldsDrawer/Drawer.js'
import { $isAutoLinkNode } from '../../../nodes/AutoLinkNode.js'
import { $createLinkNode, $isLinkNode, TOGGLE_LINK_COMMAND } from '../../../nodes/LinkNode.js'
import { TOGGLE_LINK_WITH_MODAL_COMMAND } from './commands.js'
Expand All @@ -44,7 +44,7 @@ export function LinkEditor({ anchorElem }: { anchorElem: HTMLElement }): React.R

const [stateData, setStateData] = useState<{} | (LinkFields & { id?: string; text: string })>({})

const { closeModal, isModalOpen, toggleModal } = useModal()
const { closeModal, toggleModal } = useModal()
const editDepth = useEditDepth()
const [isLink, setIsLink] = useState(false)
const [selectedNodes, setSelectedNodes] = useState<LexicalNode[]>([])
Expand Down Expand Up @@ -312,50 +312,52 @@ export function LinkEditor({ anchorElem }: { anchorElem: HTMLElement }): React.R
)}
</div>
</div>
{isModalOpen(drawerSlug) && (
<LinkDrawer
drawerSlug={drawerSlug}
handleModalSubmit={(fields: FormState, data: Data) => {
closeModal(drawerSlug)

const newLinkPayload = data as LinkFields & { text: string }

const bareLinkFields: LinkFields = {
...newLinkPayload,
}
delete bareLinkFields.text

// 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()
let linkParent = null
if ($isRangeSelection(selection)) {
linkParent = getSelectedNode(selection).getParent()
} else {
if (selectedNodes.length) {
linkParent = selectedNodes[0].getParent()
}
<FieldsDrawer
className="lexical-link-edit-drawer"
data={stateData}
drawerSlug={drawerSlug}
drawerTitle={t('fields:editLink')}
featureKey="link"
handleDrawerSubmit={(fields: FormState, data: Data) => {
closeModal(drawerSlug)

const newLinkPayload = data as LinkFields & { text: string }

const bareLinkFields: LinkFields = {
...newLinkPayload,
}
delete bareLinkFields.text

// 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()
let linkParent = null
if ($isRangeSelection(selection)) {
linkParent = getSelectedNode(selection).getParent()
} else {
if (selectedNodes.length) {
linkParent = selectedNodes[0].getParent()
}
}

if (linkParent && $isAutoLinkNode(linkParent)) {
const linkNode = $createLinkNode({
fields: bareLinkFields,
})
linkParent.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, {
fields: bareLinkFields,
selectedNodes,
text: newLinkPayload.text,
})
}}
stateData={stateData}
/>
)}
if (linkParent && $isAutoLinkNode(linkParent)) {
const linkNode = $createLinkNode({
fields: bareLinkFields,
})
linkParent.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, {
fields: bareLinkFields,
selectedNodes,
text: newLinkPayload.text,
})
}}
schemaPathSuffix="fields"
/>
</React.Fragment>
)
}

This file was deleted.

Loading

0 comments on commit 0017c67

Please sign in to comment.