Skip to content

fix: Errors when hovering image block while editor is uneditable #533

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

Merged
merged 2 commits into from
Jan 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
34 changes: 24 additions & 10 deletions packages/core/src/blocks/ImageBlockContent/ImageBlockContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ export const renderImage = (
// offset from when the resize began, and which resize handle is being used.
const windowMouseMoveHandler = (event: MouseEvent) => {
if (!resizeParams) {
if (
!editor.isEditable &&
imageWrapper.contains(leftResizeHandle) &&
imageWrapper.contains(rightResizeHandle)
) {
imageWrapper.removeChild(leftResizeHandle);
imageWrapper.removeChild(rightResizeHandle);
}

return;
}

Expand Down Expand Up @@ -192,20 +201,22 @@ export const renderImage = (
// Stops mouse movements from resizing the image and updates the block's
// `width` prop to the new value.
const windowMouseUpHandler = (event: MouseEvent) => {
if (!resizeParams) {
return;
}

// Hides the drag handles if the cursor is no longer over the image.
if (
(!event.target || !imageWrapper.contains(event.target as Node)) &&
(!event.target ||
!imageWrapper.contains(event.target as Node) ||
!editor.isEditable) &&
imageWrapper.contains(leftResizeHandle) &&
imageWrapper.contains(rightResizeHandle)
) {
imageWrapper.removeChild(leftResizeHandle);
imageWrapper.removeChild(rightResizeHandle);
}

if (!resizeParams) {
return;
}

resizeParams = undefined;

editor.updateBlock(block, {
Expand Down Expand Up @@ -235,9 +246,6 @@ export const renderImage = (
if (editor.isEditable) {
imageWrapper.appendChild(leftResizeHandle);
imageWrapper.appendChild(rightResizeHandle);
} else {
imageWrapper.removeChild(leftResizeHandle);
imageWrapper.removeChild(rightResizeHandle);
}
};
// Hides the resize handles when the cursor leaves the image, unless the
Expand All @@ -254,8 +262,14 @@ export const renderImage = (
return;
}

imageWrapper.removeChild(leftResizeHandle);
imageWrapper.removeChild(rightResizeHandle);
if (
editor.isEditable &&
imageWrapper.contains(leftResizeHandle) &&
imageWrapper.contains(rightResizeHandle)
) {
imageWrapper.removeChild(leftResizeHandle);
imageWrapper.removeChild(rightResizeHandle);
}
};

// Sets the resize params, allowing the user to begin resizing the image by
Expand Down