Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-playground] Bug Fix: Disable table hover actions in read-only mode #6706

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
import {
$getTableColumnIndexFromTableCellNode,
$getTableRowIndexFromTableCellNode,
Expand All @@ -32,8 +33,9 @@ function TableHoverActionsContainer({
anchorElem,
}: {
anchorElem: HTMLElement;
}): JSX.Element {
}): JSX.Element | null {
const [editor] = useLexicalComposerContext();
const isEditable = useLexicalEditable();
const [isShownRow, setShownRow] = useState<boolean>(false);
const [isShownColumn, setShownColumn] = useState<boolean>(false);
const [shouldListenMouseMove, setShouldListenMouseMove] =
Expand Down Expand Up @@ -193,6 +195,10 @@ function TableHoverActionsContainer({
});
};

if (!isEditable) {
return null;
}

return (
<>
{isShownRow && (
Expand Down Expand Up @@ -246,8 +252,12 @@ export default function TableHoverActionsPlugin({
}: {
anchorElem?: HTMLElement;
}): React.ReactPortal | null {
return createPortal(
<TableHoverActionsContainer anchorElem={anchorElem} />,
anchorElem,
);
const isEditable = useLexicalEditable();

return isEditable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for all the additional isEditable checks? Wouldn't the check here suffice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this check is sufficient to prevent rendering the plugin when the editor is not editable, but the additional checks help with defensive programming. For example, when switching from editing to read-only mode, the main check will eventually unmount the component, but there is a brief period before the re-render completes, during this time, the internal checks ensure that not table modifications can occur. Basically preventing race conditions. For example, in a collobrative environment if one person sets the document as read-only, and at the same time another person trys to add a column before the rerendering has occurred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good explanation, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the defensive programming is useful in this case, the resolved state of the document over the network should override whatever the local settings are for any specific client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i guess i dont really understand how collaborative editors work, I have removed the isEditable checks.

? createPortal(
<TableHoverActionsContainer anchorElem={anchorElem} />,
anchorElem,
)
: null;
}
Loading