Skip to content

Commit

Permalink
Refactor ClickableLinkPlugin (#5800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivailop7 authored Apr 3, 2024
1 parent 4f5fb58 commit a08fed7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/lexical-playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default function Editor(): JSX.Element {
<TwitterPlugin />
<YouTubePlugin />
<FigmaPlugin />
{!isEditable && <LexicalClickableLinkPlugin />}
<LexicalClickableLinkPlugin disabled={isEditable} />
<HorizontalRulePlugin />
<EquationsPlugin />
<ExcalidrawPlugin />
Expand Down
11 changes: 9 additions & 2 deletions packages/lexical-react/src/LexicalClickableLinkPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ function findMatchingDOM<T extends Node>(

export default function LexicalClickableLinkPlugin({
newTab = true,
disabled = false,
}: {
newTab?: boolean;
disabled?: boolean;
}): null {
const [editor] = useLexicalComposerContext();

useEffect(() => {
const onClick = (event: MouseEvent) => {
if (disabled) {
event.preventDefault();
return;
}

const target = event.target;
if (!(target instanceof Node)) {
return;
Expand Down Expand Up @@ -99,7 +106,7 @@ export default function LexicalClickableLinkPlugin({
};

const onMouseUp = (event: MouseEvent) => {
if (event.button === 1 && editor.isEditable()) {
if (!disabled && event.button === 1) {
onClick(event);
}
};
Expand All @@ -114,7 +121,7 @@ export default function LexicalClickableLinkPlugin({
rootElement.addEventListener('mouseup', onMouseUp);
}
});
}, [editor, newTab]);
}, [editor, newTab, disabled]);

return null;
}

0 comments on commit a08fed7

Please sign in to comment.