Merge pull request #1729 from CorreyL/feature/ctrl-k-for-hyperlink

[Feature] Keyboard Shortcut for Hyperlink
This commit is contained in:
Amruth Pillai
2024-03-10 10:07:33 +01:00
committed by GitHub
+32 -18
View File
@@ -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 (
@@ -440,7 +444,17 @@ export const RichInput = forwardRef<Editor, RichInputProps>(
Image,
Underline,
Highlight,
Link.extend({ inclusive: false }).configure({ openOnClick: false }),
Link.extend({
inclusive: false,
addKeyboardShortcuts() {
return {
"Mod-k": () => {
setLinkGlobal(this.editor as Editor);
return true;
},
};
},
}).configure({ openOnClick: false }),
TextAlign.configure({ types: ["heading", "paragraph"] }),
],
editorProps: {