feat(notes): make basic hotkeys work

works on delete, backspace, and export
This commit is contained in:
Triston Armstrong 2024-10-12 15:51:08 -04:00
parent be10f376f4
commit a662b5abc0
Signed by: tristonarmstrong
GPG Key ID: A23B48AE45EB6EFE

View File

@ -1,4 +1,4 @@
import { signal, useRef } from "kaioken" import { signal, useCallback, useEffect, useRef } from "kaioken"
import { NotesSigal, focusedItem } from "../signals" import { NotesSigal, focusedItem } from "../signals"
import { useDebounce } from "../utils/useDebounce" import { useDebounce } from "../utils/useDebounce"
import notes, { NoteCardType } from "../signals/notes" import notes, { NoteCardType } from "../signals/notes"
@ -111,10 +111,14 @@ export function NoteCard({ key: itemKey, data: item }: NoteCard.NoteCardProps) {
focusedItem.value = itemKey focusedItem.value = itemKey
} }
function _handleExportClick(_e: MouseEvent) { function _exportFile() {
createFileAndExport("Note", item.contents, "text/markdown") createFileAndExport("Note", item.contents, "text/markdown")
} }
function _handleExportClick(_e: MouseEvent) {
_exportFile()
}
function _handleMouseClick(e: MouseEvent) { function _handleMouseClick(e: MouseEvent) {
e.preventDefault() e.preventDefault()
openContextMenu.value = !openContextMenu.value openContextMenu.value = !openContextMenu.value
@ -124,6 +128,39 @@ export function NoteCard({ key: itemKey, data: item }: NoteCard.NoteCardProps) {
openContextMenu.value = false openContextMenu.value = false
} }
const _handleKeyDown = useCallback((e: KeyboardEvent) => {
if (!e.ctrlKey) return
// TODO: add support for other os
// TODO: add modal popup
switch (e.key) {
case 'Delete':
e.preventDefault()
_handleClose(e)
break
case 'Backspace':
e.preventDefault()
_handleClose(e)
break
case 'e':
e.preventDefault()
_exportFile()
break
default:
break
}
}, [itemKey, item.position, NotesSigal.default])
useEffect(() => {
if (focusedItem.value !== itemKey) return
window.addEventListener('keydown', _handleKeyDown)
return () => {
window.removeEventListener('keydown', _handleKeyDown)
}
}, [focusedItem.value, itemKey])
const cardPositionStyle = { const cardPositionStyle = {
zIndex: `${focusedItem.value == itemKey ? LayerEnum.CARD_ELEVATED : LayerEnum.CARD}`, zIndex: `${focusedItem.value == itemKey ? LayerEnum.CARD_ELEVATED : LayerEnum.CARD}`,
width: `${item.dimensions.w}px`, width: `${item.dimensions.w}px`,
@ -198,7 +235,6 @@ export function NoteCard({ key: itemKey, data: item }: NoteCard.NoteCardProps) {
</div> </div>
</ContextMenuPortal> </ContextMenuPortal>
</div > </div >
) )
} }
@ -228,3 +264,4 @@ function ExpandIcon({ cb }: {
) )
} }