Skip to content

Commit

Permalink
fix: image inline resizable issue
Browse files Browse the repository at this point in the history
added mouse down event on image resizing.
  • Loading branch information
semanticist21 committed Jun 20, 2024
1 parent 7e952fd commit ef3f383
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/extensions/ResizableImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NodeViewProps } from "@tiptap/core";
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
import { NodeViewWrapper } from "@tiptap/react";
import throttle from "lodash/throttle";
import { useMemo, useRef } from "react";
import { useMemo, useRef, useState } from "react";
import { makeStyles } from "tss-react/mui";
import type ResizableImage from "./ResizableImage";
import { ResizableImageResizer } from "./ResizableImageResizer";
Expand Down Expand Up @@ -80,6 +80,11 @@ function ResizableImageComponent(props: Props) {

const imageRef = useRef<HTMLImageElement | null>(null);

// Moved the mouseDown state here from ResizableImageResizer to control the resizer visibility when "inline" option is enabled.
// Gave more leniency to selected condition to show the resizer by including mouseDown state.
const [mouseDown, setMouseDown] = useState(false);
const selectedOrDragging = selected || mouseDown;

const handleResize = useMemo(
() =>
// Throttle our "on resize" handler, since the event fires very rapidly during
Expand Down Expand Up @@ -163,9 +168,9 @@ function ResizableImageComponent(props: Props) {
classes.image,
// For consistency with the standard Image extension selection
// class/UI:
selected && "ProseMirror-selectednode",
selectedOrDragging && "ProseMirror-selectednode",
// We'll only show the outline when the editor content is selected
selected && classes.imageSelected
selectedOrDragging && classes.imageSelected
)}
style={{
// If no width has been specified, we use auto max-width
Expand Down Expand Up @@ -199,10 +204,12 @@ function ResizableImageComponent(props: Props) {
}}
/>

{selected && (
{selectedOrDragging && (
<ResizableImageResizer
onResize={handleResize}
className={classes.resizer}
mouseDown={mouseDown}
setMouseDown={setMouseDown}
/>
)}

Expand Down
23 changes: 17 additions & 6 deletions src/extensions/ResizableImageResizer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { useCallback, useEffect, useState } from "react";
import {
useCallback,
useEffect,
type Dispatch,
type SetStateAction,
} from "react";
import { makeStyles } from "tss-react/mui";

type ResizableImageResizerProps = {
className?: string;
onResize: (event: MouseEvent) => void;
mouseDown: boolean;
setMouseDown: Dispatch<SetStateAction<boolean>>;
};

const useStyles = makeStyles({ name: { ResizableImageResizer } })((theme) => ({
Expand All @@ -23,9 +30,10 @@ const useStyles = makeStyles({ name: { ResizableImageResizer } })((theme) => ({
export function ResizableImageResizer({
onResize,
className,
mouseDown,
setMouseDown,
}: ResizableImageResizerProps) {
const { classes, cx } = useStyles();
const [mouseDown, setMouseDown] = useState(false);

useEffect(() => {
const handleMouseMove = (event: MouseEvent) => {
Expand All @@ -52,11 +60,14 @@ export function ResizableImageResizer({
return () => {
window.removeEventListener("mouseup", handleMouseUp);
};
}, []);
}, [setMouseDown]);

const handleMouseDown = useCallback((_event: React.MouseEvent) => {
setMouseDown(true);
}, []);
const handleMouseDown = useCallback(
(_event: React.MouseEvent) => {
setMouseDown(true);
},
[setMouseDown]
);

return (
// There isn't a great role to use here (perhaps role="separator" is the
Expand Down

0 comments on commit ef3f383

Please sign in to comment.