From 46620a938c9701f6c4055bae5bf60fd92f3378bf Mon Sep 17 00:00:00 2001 From: Josh Wooding <12938082+joshwooding@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:50:56 +0000 Subject: [PATCH] Fix editor --- .changeset/clever-ads-smile.md | 6 ++++ .../src/components/LinkEditor/LinkEditor.tsx | 34 ++++--------------- .../Toolbar/InsertBlockDropdown.tsx | 2 +- 3 files changed, 14 insertions(+), 28 deletions(-) create mode 100644 .changeset/clever-ads-smile.md diff --git a/.changeset/clever-ads-smile.md b/.changeset/clever-ads-smile.md new file mode 100644 index 00000000..061ec2f8 --- /dev/null +++ b/.changeset/clever-ads-smile.md @@ -0,0 +1,6 @@ +--- +'@jpmorganchase/mosaic-content-editor-plugin': patch +--- + +- Fixed LinkEditor not working when editing existing links. +- Fixed InsertBlockDropdown not updating correctly. diff --git a/packages/content-editor-plugin/src/components/LinkEditor/LinkEditor.tsx b/packages/content-editor-plugin/src/components/LinkEditor/LinkEditor.tsx index 79d0cc1e..3f97e9e2 100644 --- a/packages/content-editor-plugin/src/components/LinkEditor/LinkEditor.tsx +++ b/packages/content-editor-plugin/src/components/LinkEditor/LinkEditor.tsx @@ -1,12 +1,5 @@ -import { mergeRegister } from '@lexical/utils'; -import { - $getSelection, - $isRangeSelection, - COMMAND_PRIORITY_LOW, - RangeSelection, - SELECTION_CHANGE_COMMAND -} from 'lexical'; -import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react'; +import { $getSelection, $isRangeSelection, RangeSelection } from 'lexical'; +import { ChangeEvent, useCallback, useEffect, useState } from 'react'; import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { useFloatingUI, Input } from '@salt-ds/core'; @@ -20,7 +13,6 @@ import { getSelectedNode } from '../../utils/getSelectedNode'; export function LinkEditor() { const [editor] = useLexicalComposerContext(); - const inputRef = useRef(null); const [linkUrl, setLinkUrl] = useState(''); const [isEdit, setIsEdit] = useState(false); const [lastSelection, setLastSelection] = useState(null); @@ -67,22 +59,11 @@ export function LinkEditor() { useEffect( () => - mergeRegister( - editor.registerUpdateListener(({ editorState }) => { - editorState.read(() => { - updateLinkEditor(); - }); - }), - - editor.registerCommand( - SELECTION_CHANGE_COMMAND, - () => { - updateLinkEditor(); - return true; - }, - COMMAND_PRIORITY_LOW - ) - ), + editor.registerUpdateListener(({ editorState }) => { + editorState.read(() => { + updateLinkEditor(); + }); + }), [editor, updateLinkEditor] ); @@ -116,7 +97,6 @@ export function LinkEditor() { ) : ( } - ref={inputRef} value={linkUrl} onChange={(event: ChangeEvent) => { setLinkUrl(event.target.value); diff --git a/packages/content-editor-plugin/src/components/Toolbar/InsertBlockDropdown.tsx b/packages/content-editor-plugin/src/components/Toolbar/InsertBlockDropdown.tsx index de3115f3..130fb3bd 100644 --- a/packages/content-editor-plugin/src/components/Toolbar/InsertBlockDropdown.tsx +++ b/packages/content-editor-plugin/src/components/Toolbar/InsertBlockDropdown.tsx @@ -122,7 +122,7 @@ export function InsertBlockDropdown({ const selectedBlockSourceIndex = source.findIndex(item => item.type === blockSourceType); return ( - defaultSelected={[source[selectedBlockSourceIndex]]} + selected={[source[selectedBlockSourceIndex]]} valueToString={itemToString} onSelectionChange={handleSelect} style={{ width: 132 }}