import { signal, useRef } from "kaioken" import { NotesSigal, focusedItem } from "../signals" import { useDebounce } from "../utils/useDebounce" import notes, { NoteCardType } from "../signals/notes" import { LayerEnum } from "../utils/enums" namespace NoteCard { export interface NoteCardProps { key: NoteCardType['id'] data: NoteCardType } } export function NoteCard({ key: itemKey, data: item }: NoteCard.NoteCardProps) { const saved = signal(true) const pressed = signal(false) const newX = useRef(0) const newY = useRef(0) const offsetX = useRef(0) const offsetY = useRef(0) const initialResizeX = useRef(0) const initialResizeY = useRef(0) const { debounce } = useDebounce() function updateLocalStorage(time?: number) { debounce(() => { localStorage.setItem("notes", JSON.stringify(notes.notes.value)) }, time) } function _handleMouseMove(e: MouseEvent) { e.preventDefault() if (!pressed.value) return newX.current = e.pageX - offsetX.current newY.current = e.pageY - offsetY.current const newPos = { x: newX.current, y: newY.current } NotesSigal.default.updateNoteProperty(itemKey, 'position', newPos) updateLocalStorage() } function _handleMouseUp(e: MouseEvent) { e.preventDefault() pressed.value = false window.removeEventListener('mousemove', _handleMouseMove) window.removeEventListener('mouseup', _handleMouseUp) } function _handleMouseDown(e: MouseEvent) { e.preventDefault() offsetX.current = e.offsetX offsetY.current = e.offsetY pressed.value = true window.addEventListener('mousemove', _handleMouseMove) 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 newDim = { w: newW, h: newH } NotesSigal.default.updateNoteProperty(itemKey, 'dimensions', newDim) NotesSigal.default.notes.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 updateLocalStorage() window.removeEventListener('mousemove', _handleResizeMove) window.removeEventListener('mouseup', _handleResizeMouseUp) } return (
focusedItem.value = itemKey} className="select-none transition flex flex-col justify-stretch shadow-lg rounded border border-[#3c3c3c] absolute" style={{ zIndex: `${focusedItem.value == itemKey ? LayerEnum.CARD_ELEVATED : LayerEnum.CARD}`, width: `${item.dimensions.w}px`, height: `${item.dimensions.h}px`, top: `${item.position.y}px`, left: `${item.position.x}px`, backgroundColor: '#181818', }} >