From 4f0e588efa52b891f6998e8a31a05236e134e9a8 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Sat, 5 Oct 2024 14:33:07 -0400 Subject: [PATCH] refactor and improve image logic --- src/components/CardSelector.tsx | 54 +++++++++++++++++++++------------ src/components/ImageCard.tsx | 12 +++++--- src/components/MiniMap.tsx | 3 +- src/utils/localStorage.ts | 6 ++++ 4 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 src/utils/localStorage.ts diff --git a/src/components/CardSelector.tsx b/src/components/CardSelector.tsx index 31213ac..e9ceea1 100644 --- a/src/components/CardSelector.tsx +++ b/src/components/CardSelector.tsx @@ -1,5 +1,8 @@ import { useRef } from "kaioken" import { ImagesSignal, NotesSigal } from "../signals" +import { updateLocalStorage } from "../utils/localStorage" +import notes from "../signals/notes" +import images from "../signals/images" export function CardSelector() { const containerRef = useRef(null) @@ -32,6 +35,7 @@ function StickyNote() { h: 200 } }) + updateLocalStorage("notes", notes.notes.value) } return ( @@ -58,32 +62,44 @@ function StickyNote() { } function Image() { - function _handleClick() { + function _handleClick(mouseEvent: MouseEvent) { const input = document.createElement('input') input.type = 'file' input.onchange = (e: any) => { const file = e.target.files[0] const reader = new FileReader() reader.readAsDataURL(file) - reader.onload = readerEvent => { - const content = readerEvent.target?.result; - let img: string = ''; - if (typeof content == 'string') img = content?.split(':')[1] - if (!img) return + reader.onload = function(readerEvent) { + let image = document.createElement('img') + image.onload = function() { + const { width, height } = image - ImagesSignal.default.addImage({ - type: "image", - title: "New Image", - contents: content as string, - position: { - x: e.pageX - 100, - y: e.pageY + (window.innerHeight / 2) - 100 - }, - dimensions: { - w: 200, - h: 200 - } - }) + // normalize the dimensions so that they fit within a constraint + const len = Math.sqrt(width * width + height * height) + const normalizedW = (width / len) * 300 + const normalizedH = (height / len) * 300 + + const content = readerEvent.target?.result; + let img: string = ''; + if (typeof content == 'string') img = content?.split(':')[1] + if (!img) return + + ImagesSignal.default.addImage({ + type: "image", + title: "New Image", + contents: content as string, + position: { + x: mouseEvent.pageX - 100, + y: mouseEvent.pageY + (window.innerHeight / 2) - 100 + }, + dimensions: { + w: normalizedW, + h: normalizedH + } + }) + updateLocalStorage("images", images.images.value) + } + image.src = readerEvent.target?.result as string } } input.click() diff --git a/src/components/ImageCard.tsx b/src/components/ImageCard.tsx index fcf575e..7a0b855 100644 --- a/src/components/ImageCard.tsx +++ b/src/components/ImageCard.tsx @@ -3,6 +3,7 @@ import { ImagesSignal, focusedItem } from "../signals" import { useDebounce } from "../utils/useDebounce" import { LayerEnum } from "../utils/enums" import images, { ImageCardType } from "../signals/images" +import { updateLocalStorage } from "../utils/localStorage" namespace ImageCard { export interface ImageCardProps { @@ -19,9 +20,9 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps const offsetY = useRef(0) const { debounce } = useDebounce() - function updateLocalStorage(time?: number) { + function debounceLSUpdate(time?: number) { debounce(() => { - localStorage.setItem("images", JSON.stringify(images.images.value)) + updateLocalStorage("images", images.images.value) }, time) } @@ -34,7 +35,7 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps const newPos = { x: newX.current, y: newY.current } ImagesSignal.default.updateImageProperty(itemKey, 'position', newPos) - updateLocalStorage() + debounceLSUpdate() } function _handleMouseUp(e: MouseEvent) { @@ -62,7 +63,8 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps zIndex: `${focusedItem.value == itemKey ? LayerEnum.CARD_ELEVATED : LayerEnum.CARD}`, top: `${item.position.y}px`, left: `${item.position.x}px`, - maxWidth: '300px', + width: `${item.dimensions.w}px`, + height: `${item.dimensions.h}px`, backgroundColor: '#181818' }} > @@ -70,7 +72,7 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps { const image = images.images.value[imageKey] + const el = useRef(null) function _handleItemClick(_e: MouseEvent) { window.scrollTo({ @@ -56,7 +57,7 @@ export function MiniMap() { } return ( -
+) { + localStorage.setItem(location, JSON.stringify(collection)) +}