Skip to content

Commit b61daac

Browse files
Fixed image resize being possible when editor isn't editable (#390)
1 parent ca1288f commit b61daac

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

packages/core/src/extensions/Blocks/nodes/Block.module.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,6 @@ NESTED BLOCKS
288288
cursor: ew-resize;
289289
}
290290

291-
[data-content-type="image"] .imageWrapper:hover .resizeHandle {
292-
display: block;
293-
}
294-
295291
[data-content-type="image"] .caption {
296292
font-size: 0.8em
297293
}

packages/core/src/extensions/Blocks/nodes/BlockContent/ImageBlockContent/ImageBlockContent.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,34 @@ const renderImage = (
225225
);
226226
};
227227

228+
// Shows the resize handles when hovering over the image with the cursor.
229+
const imageMouseEnterHandler = () => {
230+
if (editor.isEditable) {
231+
leftResizeHandle.style.display = "block";
232+
rightResizeHandle.style.display = "block";
233+
} else {
234+
leftResizeHandle.style.display = "none";
235+
rightResizeHandle.style.display = "none";
236+
}
237+
};
238+
// Hides the resize handles when the cursor leaves the image, unless the
239+
// cursor moves to one of the resize handles.
240+
const imageMouseLeaveHandler = (event: MouseEvent) => {
241+
if (
242+
event.relatedTarget === leftResizeHandle ||
243+
event.relatedTarget === rightResizeHandle
244+
) {
245+
return;
246+
}
247+
248+
if (resizeParams) {
249+
return;
250+
}
251+
252+
leftResizeHandle.style.display = "none";
253+
rightResizeHandle.style.display = "none";
254+
};
255+
228256
// Sets the resize params, allowing the user to begin resizing the image by
229257
// moving the cursor left or right.
230258
const leftResizeHandleMouseDownHandler = (event: MouseEvent) => {
@@ -266,6 +294,8 @@ const renderImage = (
266294
window.addEventListener("mouseup", windowMouseUpHandler);
267295
addImageButton.addEventListener("mousedown", addImageButtonMouseDownHandler);
268296
addImageButton.addEventListener("click", addImageButtonClickHandler);
297+
image.addEventListener("mouseenter", imageMouseEnterHandler);
298+
image.addEventListener("mouseleave", imageMouseLeaveHandler);
269299
leftResizeHandle.addEventListener(
270300
"mousedown",
271301
leftResizeHandleMouseDownHandler

0 commit comments

Comments
 (0)