70 lines
2.3 KiB
TypeScript

import { useCallback, useContext, useEffect, useRef } from "preact/hooks";
import FBranch from "../../../entities/fbranch";
import FNote from "../../../entities/fnote";
import BoardApi from "./api";
import { BoardViewContext, TitleEditor } from ".";
import { ContextMenuEvent } from "../../../menus/context_menu";
import { openNoteContextMenu } from "./context_menu";
export default function Card({
api,
note,
branch,
column,
index,
setDraggedCard,
isDragging
}: {
api: BoardApi,
note: FNote,
branch: FBranch,
column: string,
index: number,
setDraggedCard: (card: { noteId: string, branchId: string, fromColumn: string, index: number } | null) => void,
isDragging: boolean
}) {
const { branchIdToEdit } = useContext(BoardViewContext);
const isEditing = branch.branchId === branchIdToEdit;
const colorClass = note.getColorClass() || '';
const editorRef = useRef<HTMLInputElement>(null);
const handleDragStart = useCallback((e: DragEvent) => {
e.dataTransfer!.effectAllowed = 'move';
e.dataTransfer!.setData('text/plain', note.noteId);
setDraggedCard({ noteId: note.noteId, branchId: branch.branchId, fromColumn: column, index });
}, [note.noteId, branch.branchId, column, index, setDraggedCard]);
const handleDragEnd = useCallback(() => {
setDraggedCard(null);
}, [setDraggedCard]);
const handleContextMenu = useCallback((e: ContextMenuEvent) => {
openNoteContextMenu(api, e, note.noteId, branch.branchId, column);
}, [ api, note, branch, column ]);
useEffect(() => {
editorRef.current?.focus();
}, [ isEditing ]);
return (
<div
className={`board-note ${colorClass} ${isDragging ? 'dragging' : ''} ${isEditing ? "editing" : ""}`}
draggable="true"
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onContextMenu={handleContextMenu}
>
<span class={`icon ${note.getIcon()}`} />
{!isEditing ? (
<>{note.title}</>
) : (
<TitleEditor
currentValue={note.title}
save={newTitle => api.renameCard(note.noteId, newTitle)}
dismiss={() => api.dismissEditingTitle()}
/>
)}
</div>
)
}