feat(editor): open the link popover with cmd+k

This commit is contained in:
pj committed 2026-08-10 18:36:52 +05:30
1 parent dad0ac86d0
commit 2e15dc2dc9
1 file changed
+22 -6
+22 -6
View File
@@ -33,6 +33,12 @@ export function FloatingToolbar({ editor }: { editor: Editor | null }) {
const [alignOpen, setAlignOpen] = useState(false); const [alignOpen, setAlignOpen] = useState(false);
const [, forceUpdate] = useReducer((x) => x + 1, 0); const [, forceUpdate] = useReducer((x) => x + 1, 0);
const openLink = () => {
if (!editor) return;
setLinkValue((editor.getAttributes("link").href as string) ?? "");
setLinkOpen(true);
};
useEffect(() => { useEffect(() => {
if (linkOpen) linkInputRef.current?.focus(); if (linkOpen) linkInputRef.current?.focus();
}, [linkOpen]); }, [linkOpen]);
@@ -46,6 +52,21 @@ export function FloatingToolbar({ editor }: { editor: Editor | null }) {
}; };
}, [editor]); }, [editor]);
useEffect(() => {
if (!editor) return;
const onKey = (e: KeyboardEvent) => {
if (e.key.toLowerCase() !== "k" || !(e.metaKey || e.ctrlKey) || e.altKey || e.shiftKey) return;
if (!editor.isFocused && !linkOpen) return;
e.preventDefault();
e.stopPropagation();
if (linkOpen) setLinkOpen(false);
else openLink();
};
window.addEventListener("keydown", onKey, true);
return () => window.removeEventListener("keydown", onKey, true);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [editor, linkOpen]);
if (!editor) return null; if (!editor) return null;
const insertImage = async (file: File) => { const insertImage = async (file: File) => {
@@ -53,11 +74,6 @@ export function FloatingToolbar({ editor }: { editor: Editor | null }) {
editor.chain().focus().insertContent({ type: "figure", attrs: { src, placement: "full-width" } }).run(); editor.chain().focus().insertContent({ type: "figure", attrs: { src, placement: "full-width" } }).run();
}; };
const openLink = () => {
setLinkValue((editor.getAttributes("link").href as string) ?? "");
setLinkOpen(true);
};
const applyLink = () => { const applyLink = () => {
const href = normalizeUrl(linkValue); const href = normalizeUrl(linkValue);
if (!href) { if (!href) {
@@ -126,7 +142,7 @@ export function FloatingToolbar({ editor }: { editor: Editor | null }) {
</span> </span>
<span className="tool-sep" /> <span className="tool-sep" />
<span className="tool-wrap"> <span className="tool-wrap">
{tool(editor.isActive("link") || linkOpen, () => (linkOpen ? setLinkOpen(false) : openLink()), "Link", <Icon d="M10 13a5 5 0 0 0 7 0l2-2a5 5 0 0 0-7-7l-1 1M14 11a5 5 0 0 0-7 0l-2 2a5 5 0 0 0 7 7l1-1" />)} {tool(editor.isActive("link") || linkOpen, () => (linkOpen ? setLinkOpen(false) : openLink()), "Link (⌘K)", <Icon d="M10 13a5 5 0 0 0 7 0l2-2a5 5 0 0 0-7-7l-1 1M14 11a5 5 0 0 0-7 0l-2 2a5 5 0 0 0 7 7l1-1" />)}
{linkOpen && ( {linkOpen && (
<> <>
<div className="link-pop-backdrop" onMouseDown={() => setLinkOpen(false)} /> <div className="link-pop-backdrop" onMouseDown={() => setLinkOpen(false)} />