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" import { useThemeDetector } from "../utils/useThemeDetector" import { MarkDownEditor } from "./MarkDownEditor/MarkDownEditor" import { ChangeEvent } from "tiny-markdown-editor" import { Divider } from "./Divider" import { ExportIcon } from "./icons/ExportIcon" import { createFileAndExport } from "../utils/createFileAndExport" 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)) saved.value = true }, 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) } function _handleMdChange(e: ChangeEvent) { NotesSigal.default.updateNoteProperty(itemKey, 'contents', e.content) NotesSigal.default.notes.notify() updateLocalStorage() saved.value = false } function _handleClose(_e: Event) { NotesSigal.default.removeNote(item.id) NotesSigal.default.notes.notify() updateLocalStorage() } function _handleFocusCard() { focusedItem.value = itemKey } function _handleExportClick(_e: MouseEvent) { createFileAndExport("Note", item.contents, "text/markdown") } const cardPositionStyle = { 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`, } const saveIndicatorStyle = { opacity: saved.value ? '0' : '100' } return (