diff --git a/src/components/ImageCard.tsx b/src/components/ImageCard.tsx index 7a0b855..7f77c6e 100644 --- a/src/components/ImageCard.tsx +++ b/src/components/ImageCard.tsx @@ -4,6 +4,7 @@ import { useDebounce } from "../utils/useDebounce" import { LayerEnum } from "../utils/enums" import images, { ImageCardType } from "../signals/images" import { updateLocalStorage } from "../utils/localStorage" +import { noop } from "kaioken/utils" namespace ImageCard { export interface ImageCardProps { @@ -13,12 +14,14 @@ namespace ImageCard { } export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps) { + const { debounce } = useDebounce() const pressed = signal(false) const newX = useRef(0) const newY = useRef(0) const offsetX = useRef(0) const offsetY = useRef(0) - const { debounce } = useDebounce() + const initialResizeX = useRef(0) + const initialResizeY = useRef(0) function debounceLSUpdate(time?: number) { debounce(() => { @@ -55,6 +58,35 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps window.addEventListener('mouseup', _handleMouseUp) } + function _handleResizeMove(e: MouseEvent) { + const { pageX, pageY } = e + const [newX, newY] = [initialResizeX.current - pageX, initialResizeY.current - pageY] + + const newW = -newX + item.dimensions.w + const newH = -newY + item.dimensions.h + const ratio = Math.min(newW / item.dimensions.w, newH / item.dimensions.h) + const newDim = { w: item.dimensions.w * ratio, h: item.dimensions.h * ratio } + + ImagesSignal.default.updateImageProperty(itemKey, 'dimensions', newDim) + ImagesSignal.default.images.notify() + } + + + function _handleResizeMouseDown(e: MouseEvent) { + initialResizeX.current = e.pageX + initialResizeY.current = e.pageY + pressed.value = true + window.addEventListener('mousemove', _handleResizeMove) + window.addEventListener('mouseup', _handleResizeMouseUp) + } + + function _handleResizeMouseUp() { + pressed.value = false + debounceLSUpdate() + window.removeEventListener('mousemove', _handleResizeMove) + window.removeEventListener('mouseup', _handleResizeMouseUp) + } + return (