From 37a869fa2426c651e6b959a201d4c4a134b65931 Mon Sep 17 00:00:00 2001 From: CorreyL Date: Sun, 21 Jan 2024 10:37:15 -0800 Subject: [PATCH 1/2] 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 d9c716844..badb74bf0 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 ( From 8e8b695cbc43c09380f1d40135583298e334ac8d Mon Sep 17 00:00:00 2001 From: CorreyL Date: Sun, 21 Jan 2024 10:38:12 -0800 Subject: [PATCH 2/2] Add a keyboard shortcut for adding a hyperlink Use the conventional Ctrl+K keyboard shortcut for adding a hyperlink --- libs/ui/src/components/rich-input.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/ui/src/components/rich-input.tsx b/libs/ui/src/components/rich-input.tsx index badb74bf0..a68738301 100644 --- a/libs/ui/src/components/rich-input.tsx +++ b/libs/ui/src/components/rich-input.tsx @@ -444,7 +444,16 @@ export const RichInput = forwardRef( Image, Underline, Highlight, - Link.configure({ openOnClick: false }), + Link.extend({ + addKeyboardShortcuts() { + return { + "Mod-k": () => { + setLinkGlobal(this.editor as Editor); + return true; + }, + }; + }, + }).configure({ openOnClick: false }), TextAlign.configure({ types: ["heading", "paragraph"] }), ], editorProps: {