Skip to content

Commit

Permalink
[lexical-playground] Bug Fix: Disable editing of Excalidraw Component…
Browse files Browse the repository at this point in the history
… in Read-Only Mode (#6704)

Co-authored-by: Bob Ippolito <bob@redivi.com>
  • Loading branch information
neysanfoo and etrepum authored Oct 22, 2024
1 parent 3055e54 commit 4e1a3f4
Showing 1 changed file with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {NodeKey} from 'lexical';

import {AppState, BinaryFiles} from '@excalidraw/excalidraw/types/types';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
import {mergeRegister} from '@lexical/utils';
import {
Expand Down Expand Up @@ -40,6 +41,7 @@ export default function ExcalidrawComponent({
height: 'inherit' | number;
}): JSX.Element {
const [editor] = useLexicalComposerContext();
const isEditable = useLexicalEditable();
const [isModalOpen, setModalOpen] = useState<boolean>(
data === '[]' && editor.isEditable(),
);
Expand All @@ -66,16 +68,13 @@ export default function ExcalidrawComponent({
[editor, isSelected, nodeKey],
);

// Set editor to readOnly if Excalidraw is open to prevent unwanted changes
useEffect(() => {
if (isModalOpen) {
editor.setEditable(false);
} else {
editor.setEditable(true);
if (!isEditable) {
if (isSelected) {
clearSelection();
}
return;
}
}, [isModalOpen, editor]);

useEffect(() => {
return mergeRegister(
editor.registerCommand(
CLICK_COMMAND,
Expand Down Expand Up @@ -113,7 +112,15 @@ export default function ExcalidrawComponent({
COMMAND_PRIORITY_LOW,
),
);
}, [clearSelection, editor, isSelected, isResizing, $onDelete, setSelected]);
}, [
clearSelection,
editor,
isSelected,
isResizing,
$onDelete,
setSelected,
isEditable,
]);

const deleteNode = useCallback(() => {
setModalOpen(false);
Expand All @@ -130,9 +137,6 @@ export default function ExcalidrawComponent({
aps: Partial<AppState>,
fls: BinaryFiles,
) => {
if (!editor.isEditable()) {
return;
}
return editor.update(() => {
const node = $getNodeByKey(nodeKey);
if ($isExcalidrawNode(node)) {
Expand Down Expand Up @@ -198,20 +202,21 @@ export default function ExcalidrawComponent({

return (
<>
<ExcalidrawModal
initialElements={elements}
initialFiles={files}
initialAppState={appState}
isShown={isModalOpen}
onDelete={deleteNode}
onClose={closeModal}
onSave={(els, aps, fls) => {
editor.setEditable(true);
setData(els, aps, fls);
setModalOpen(false);
}}
closeOnClickOutside={false}
/>
{isEditable && isModalOpen && (
<ExcalidrawModal
initialElements={elements}
initialFiles={files}
initialAppState={appState}
isShown={isModalOpen}
onDelete={deleteNode}
onClose={closeModal}
onSave={(els, aps, fls) => {
setData(els, aps, fls);
setModalOpen(false);
}}
closeOnClickOutside={false}
/>
)}
{elements.length > 0 && (
<button
ref={buttonRef}
Expand All @@ -225,7 +230,7 @@ export default function ExcalidrawComponent({
width={width}
height={height}
/>
{isSelected && (
{isSelected && isEditable && (
<div
className="image-edit-button"
role="button"
Expand All @@ -234,7 +239,7 @@ export default function ExcalidrawComponent({
onClick={openModal}
/>
)}
{(isSelected || isResizing) && (
{(isSelected || isResizing) && isEditable && (
<ImageResizer
buttonRef={captionButtonRef}
showCaption={true}
Expand Down

0 comments on commit 4e1a3f4

Please sign in to comment.