2023-08-19 13:28:54 +00:00
|
|
|
import { Editor } from "@tiptap/react";
|
|
|
|
import Moveable from "react-moveable";
|
|
|
|
|
|
|
|
export const ImageResizer = ({ editor }: { editor: Editor }) => {
|
|
|
|
const updateMediaSize = () => {
|
2023-09-03 13:20:30 +00:00
|
|
|
const imageInfo = document.querySelector(".ProseMirror-selectednode") as HTMLImageElement;
|
2023-08-19 13:28:54 +00:00
|
|
|
if (imageInfo) {
|
|
|
|
const selection = editor.state.selection;
|
|
|
|
editor.commands.setImage({
|
|
|
|
src: imageInfo.src,
|
|
|
|
width: Number(imageInfo.style.width.replace("px", "")),
|
|
|
|
height: Number(imageInfo.style.height.replace("px", "")),
|
|
|
|
} as any);
|
|
|
|
editor.commands.setNodeSelection(selection.from);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Moveable
|
|
|
|
target={document.querySelector(".ProseMirror-selectednode") as any}
|
|
|
|
container={null}
|
|
|
|
origin={false}
|
|
|
|
edge={false}
|
|
|
|
throttleDrag={0}
|
|
|
|
keepRatio={true}
|
|
|
|
resizable={true}
|
|
|
|
throttleResize={0}
|
2023-09-03 13:20:30 +00:00
|
|
|
onResize={({ target, width, height, delta }: any) => {
|
2023-08-19 13:28:54 +00:00
|
|
|
delta[0] && (target!.style.width = `${width}px`);
|
|
|
|
delta[1] && (target!.style.height = `${height}px`);
|
|
|
|
}}
|
|
|
|
onResizeEnd={() => {
|
|
|
|
updateMediaSize();
|
|
|
|
}}
|
|
|
|
scalable={true}
|
|
|
|
renderDirections={["w", "e"]}
|
2023-09-03 13:20:30 +00:00
|
|
|
onScale={({ target, transform }: any) => {
|
2023-08-19 13:28:54 +00:00
|
|
|
target!.style.transform = transform;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|