Skip to content

Commit

Permalink
[Playground] Support clicking on links with middle mouse button (face…
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitstein authored Dec 13, 2022
1 parent 402e082 commit 66d4a2f
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function ClickableLinkPlugin({
const [editor] = useLexicalComposerContext();
useEffect(() => {
function onClick(e: Event) {
const event = e as MouseEvent;
const event = e as MouseEvent | PointerEvent;
const linkDomNode = getLinkDomNode(event, editor);

if (linkDomNode === null) {
Expand Down Expand Up @@ -70,9 +70,10 @@ export default function ClickableLinkPlugin({

try {
if (href !== null) {
const isMiddle = event.type === 'auxclick' && event.button === 1;
window.open(
href,
newTab || event.metaKey || event.ctrlKey ? '_blank' : '_self',
newTab || event.metaKey || event.ctrlKey || isMiddle ? '_blank' : '_self',
);
event.preventDefault();
}
Expand All @@ -88,10 +89,12 @@ export default function ClickableLinkPlugin({
) => {
if (prevRootElement !== null) {
prevRootElement.removeEventListener('click', onClick);
prevRootElement.removeEventListener('auxclick', onClick);
}

if (rootElement !== null) {
rootElement.addEventListener('click', onClick);
rootElement.addEventListener('auxclick', onClick);
}
},
);
Expand All @@ -104,7 +107,7 @@ function isLinkDomNode(domNode: Node): boolean {
}

function getLinkDomNode(
event: MouseEvent,
event: MouseEvent | PointerEvent,
editor: LexicalEditor,
): HTMLAnchorElement | null {
return editor.getEditorState().read(() => {
Expand Down

0 comments on commit 66d4a2f

Please sign in to comment.