From 37a869fa2426c651e6b959a201d4c4a134b65931 Mon Sep 17 00:00:00 2001 From: CorreyL Date: Sun, 21 Jan 2024 10:37:15 -0800 Subject: [PATCH] Move core logic of setLink to outer scope Will allow code changes in a subsequent commit to access the function and utilize the same core logic --- libs/ui/src/components/rich-input.tsx | 38 +++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/libs/ui/src/components/rich-input.tsx b/libs/ui/src/components/rich-input.tsx index d9c71684..badb74bf 100644 --- a/libs/ui/src/components/rich-input.tsx +++ b/libs/ui/src/components/rich-input.tsx @@ -113,25 +113,29 @@ const InsertImageForm = ({ onInsert }: InsertImageProps) => { ); }; +const setLinkGlobal = (editor: Editor) => { + const previousUrl = editor.getAttributes("link").href; + const url = window.prompt("URL", previousUrl); + + // cancelled + if (url === null) { + return; + } + + // empty + if (url === "") { + editor.chain().focus().extendMarkRange("link").unsetLink().run(); + + return; + } + + // update link + editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run(); +}; + const Toolbar = ({ editor }: { editor: Editor }) => { const setLink = useCallback(() => { - const previousUrl = editor.getAttributes("link").href; - const url = window.prompt("URL", previousUrl); - - // cancelled - if (url === null) { - return; - } - - // empty - if (url === "") { - editor.chain().focus().extendMarkRange("link").unsetLink().run(); - - return; - } - - // update link - editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run(); + setLinkGlobal(editor); }, [editor]); return (