-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
frontend/src/components/common/editor/plugins/LinkPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useEffect } from "react"; | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
import { $isLinkNode } from "@lexical/link"; | ||
import type { LexicalEditor } from "lexical"; | ||
import { $getNearestNodeFromDOMNode } from "lexical"; | ||
import { BrowserOpenURL } from "../../../../../wailsjs/runtime"; | ||
|
||
export default function LinkPlugin() { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
const onClick = (e: MouseEvent) => { | ||
const linkDomNode = getLinkDomNode(e, editor); | ||
|
||
if (linkDomNode === null) { | ||
return; | ||
} | ||
|
||
const href = linkDomNode.getAttribute("href"); | ||
|
||
let linkNode = null; | ||
editor.update(() => { | ||
const maybeLinkNode = $getNearestNodeFromDOMNode(linkDomNode); | ||
|
||
if ($isLinkNode(maybeLinkNode)) { | ||
linkNode = maybeLinkNode; | ||
} | ||
}); | ||
|
||
try { | ||
if (href !== null) { | ||
BrowserOpenURL(href); | ||
e.preventDefault(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
return editor.registerRootListener( | ||
( | ||
rootElement: null | HTMLElement, | ||
prevRootElement: null | HTMLElement, | ||
) => { | ||
if (prevRootElement !== null) { | ||
prevRootElement.removeEventListener("click", onClick); | ||
prevRootElement.removeEventListener("auxclick", onClick); | ||
} | ||
|
||
if (rootElement !== null) { | ||
rootElement.addEventListener("click", onClick); | ||
rootElement.addEventListener("auxclick", onClick); | ||
} | ||
}, | ||
); | ||
}, [editor]); | ||
|
||
return null; | ||
} | ||
|
||
function isLinkDomNode(domNode: Node): boolean { | ||
return domNode.nodeName.toLowerCase() === "a"; | ||
} | ||
|
||
function getLinkDomNode( | ||
event: MouseEvent | PointerEvent, | ||
editor: LexicalEditor, | ||
): HTMLAnchorElement | null { | ||
return editor.getEditorState().read(() => { | ||
const domNode = event.target as Node; | ||
|
||
if (isLinkDomNode(domNode)) { | ||
return domNode as HTMLAnchorElement; | ||
} | ||
|
||
if (domNode.parentNode && isLinkDomNode(domNode.parentNode)) { | ||
return domNode.parentNode as HTMLAnchorElement; | ||
} | ||
|
||
return null; | ||
}); | ||
} |