mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 18:50:41 +01:00
feat(collections/board): allow dragging from note tree
This commit is contained in:
@@ -7,6 +7,13 @@ import { ContextMenuEvent } from "../../../menus/context_menu";
|
|||||||
import { openNoteContextMenu } from "./context_menu";
|
import { openNoteContextMenu } from "./context_menu";
|
||||||
import { t } from "../../../services/i18n";
|
import { t } from "../../../services/i18n";
|
||||||
|
|
||||||
|
export interface CardDragData {
|
||||||
|
noteId: string;
|
||||||
|
branchId: string;
|
||||||
|
index: number;
|
||||||
|
fromColumn: string;
|
||||||
|
}
|
||||||
|
|
||||||
export default function Card({
|
export default function Card({
|
||||||
api,
|
api,
|
||||||
note,
|
note,
|
||||||
@@ -22,7 +29,7 @@ export default function Card({
|
|||||||
index: number,
|
index: number,
|
||||||
isDragging: boolean
|
isDragging: boolean
|
||||||
}) {
|
}) {
|
||||||
const { branchIdToEdit, setBranchIdToEdit, setDraggedCard } = useContext(BoardViewContext);
|
const { branchIdToEdit, setBranchIdToEdit } = useContext(BoardViewContext);
|
||||||
const isEditing = branch.branchId === branchIdToEdit;
|
const isEditing = branch.branchId === branchIdToEdit;
|
||||||
const colorClass = note.getColorClass() || '';
|
const colorClass = note.getColorClass() || '';
|
||||||
const editorRef = useRef<HTMLInputElement>(null);
|
const editorRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -31,13 +38,9 @@ export default function Card({
|
|||||||
|
|
||||||
const handleDragStart = useCallback((e: DragEvent) => {
|
const handleDragStart = useCallback((e: DragEvent) => {
|
||||||
e.dataTransfer!.effectAllowed = 'move';
|
e.dataTransfer!.effectAllowed = 'move';
|
||||||
e.dataTransfer!.setData('text/plain', note.noteId);
|
const data: CardDragData = { noteId: note.noteId, branchId: branch.branchId, fromColumn: column, index };
|
||||||
setDraggedCard({ noteId: note.noteId, branchId: branch.branchId, fromColumn: column, index });
|
e.dataTransfer!.setData('text/plain', JSON.stringify(data));
|
||||||
}, [note.noteId, branch.branchId, column, index, setDraggedCard]);
|
}, [note.noteId, branch.branchId, column, index]);
|
||||||
|
|
||||||
const handleDragEnd = useCallback(() => {
|
|
||||||
setDraggedCard(null);
|
|
||||||
}, [setDraggedCard]);
|
|
||||||
|
|
||||||
const handleContextMenu = useCallback((e: ContextMenuEvent) => {
|
const handleContextMenu = useCallback((e: ContextMenuEvent) => {
|
||||||
openNoteContextMenu(api, e, note.noteId, branch.branchId, column);
|
openNoteContextMenu(api, e, note.noteId, branch.branchId, column);
|
||||||
@@ -65,7 +68,6 @@ export default function Card({
|
|||||||
className={`board-note ${colorClass} ${isDragging ? 'dragging' : ''} ${isEditing ? "editing" : ""} ${isArchived ? "archived" : ""}`}
|
className={`board-note ${colorClass} ${isDragging ? 'dragging' : ''} ${isEditing ? "editing" : ""} ${isArchived ? "archived" : ""}`}
|
||||||
draggable="true"
|
draggable="true"
|
||||||
onDragStart={handleDragStart}
|
onDragStart={handleDragStart}
|
||||||
onDragEnd={handleDragEnd}
|
|
||||||
onContextMenu={handleContextMenu}
|
onContextMenu={handleContextMenu}
|
||||||
onClick={!isEditing ? handleOpen : undefined}
|
onClick={!isEditing ? handleOpen : undefined}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ import { ContextMenuEvent } from "../../../menus/context_menu";
|
|||||||
import Icon from "../../react/Icon";
|
import Icon from "../../react/Icon";
|
||||||
import { t } from "../../../services/i18n";
|
import { t } from "../../../services/i18n";
|
||||||
import BoardApi from "./api";
|
import BoardApi from "./api";
|
||||||
import Card from "./card";
|
import Card, { CardDragData } from "./card";
|
||||||
import { JSX } from "preact/jsx-runtime";
|
import { JSX } from "preact/jsx-runtime";
|
||||||
|
import froca from "../../../services/froca";
|
||||||
|
import { DragData } from "../../note_tree";
|
||||||
|
|
||||||
interface DragContext {
|
interface DragContext {
|
||||||
column: string;
|
column: string;
|
||||||
@@ -149,7 +151,7 @@ function AddNewItem({ column, api }: { column: string, api: BoardApi }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function useDragging({ column, columnIndex, columnItems }: DragContext) {
|
function useDragging({ column, columnIndex, columnItems }: DragContext) {
|
||||||
const { api, draggedColumn, setDraggedColumn, setDropTarget, setDropPosition, draggedCard, dropPosition, setDraggedCard } = useContext(BoardViewContext);
|
const { api, parentNote, draggedColumn, setDraggedColumn, setDropTarget, setDropPosition, dropPosition } = useContext(BoardViewContext);
|
||||||
|
|
||||||
const handleColumnDragStart = useCallback((e: DragEvent) => {
|
const handleColumnDragStart = useCallback((e: DragEvent) => {
|
||||||
e.dataTransfer!.effectAllowed = 'move';
|
e.dataTransfer!.effectAllowed = 'move';
|
||||||
@@ -204,11 +206,44 @@ function useDragging({ column, columnIndex, columnItems }: DragContext) {
|
|||||||
setDropTarget(null);
|
setDropTarget(null);
|
||||||
setDropPosition(null);
|
setDropPosition(null);
|
||||||
|
|
||||||
|
const data = e.dataTransfer?.getData("text");
|
||||||
|
if (!data) return;
|
||||||
|
const draggedCard = JSON.parse(data) as CardDragData | DragData[];
|
||||||
|
|
||||||
|
if (Array.isArray(draggedCard)) {
|
||||||
|
// From note tree.
|
||||||
|
const { noteId, branchId } = draggedCard[0];
|
||||||
|
const targetNote = await froca.getNote(noteId, true);
|
||||||
|
const parentNoteId = parentNote?.noteId;
|
||||||
|
if (!parentNoteId || !dropPosition) return;
|
||||||
|
|
||||||
|
const targetIndex = dropPosition.index - 1;
|
||||||
|
const targetItems = columnItems || [];
|
||||||
|
const targetBranch = targetIndex >= 0 ? targetItems[targetIndex].branch : null;
|
||||||
|
|
||||||
|
await api?.changeColumn(noteId, column);
|
||||||
|
|
||||||
|
const parents = targetNote?.getParentNoteIds();
|
||||||
|
if (!parents?.includes(parentNoteId)) {
|
||||||
|
if (!targetBranch) {
|
||||||
|
// First.
|
||||||
|
await branches.cloneNoteToParentNote(noteId, parentNoteId);
|
||||||
|
} else {
|
||||||
|
await branches.cloneNoteAfter(noteId, targetBranch.branchId);
|
||||||
|
}
|
||||||
|
} else if (targetBranch) {
|
||||||
|
await branches.moveAfterBranch([ branchId ], targetBranch.branchId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// From within the board.
|
||||||
if (draggedCard && dropPosition) {
|
if (draggedCard && dropPosition) {
|
||||||
const targetIndex = dropPosition.index;
|
const targetIndex = dropPosition.index;
|
||||||
const targetItems = columnItems || [];
|
const targetItems = columnItems || [];
|
||||||
|
|
||||||
if (draggedCard.fromColumn !== column) {
|
const note = froca.getNoteFromCache(draggedCard.noteId);
|
||||||
|
if (!note) return;
|
||||||
|
|
||||||
|
if (draggedCard.fromColumn !== column || !draggedCard.index) {
|
||||||
// Moving to a different column
|
// Moving to a different column
|
||||||
await api?.changeColumn(draggedCard.noteId, column);
|
await api?.changeColumn(draggedCard.noteId, column);
|
||||||
|
|
||||||
@@ -235,8 +270,9 @@ function useDragging({ column, columnIndex, columnItems }: DragContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setDraggedCard(null);
|
}
|
||||||
}, [ api, draggedCard, draggedColumn, dropPosition, columnItems, column, setDraggedCard, setDropTarget, setDropPosition ]);
|
|
||||||
|
}, [ api, draggedColumn, dropPosition, columnItems, column, setDropTarget, setDropPosition ]);
|
||||||
|
|
||||||
return { handleColumnDragStart, handleColumnDragEnd, handleDragOver, handleDragLeave, handleDrop };
|
return { handleColumnDragStart, handleColumnDragEnd, handleDragOver, handleDragLeave, handleDrop };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { onWheelHorizontalScroll } from "../../widget_utils";
|
|||||||
import Column from "./column";
|
import Column from "./column";
|
||||||
import BoardApi from "./api";
|
import BoardApi from "./api";
|
||||||
import FormTextArea from "../../react/FormTextArea";
|
import FormTextArea from "../../react/FormTextArea";
|
||||||
|
import FNote from "../../../entities/fnote";
|
||||||
|
|
||||||
export interface BoardViewData {
|
export interface BoardViewData {
|
||||||
columns?: BoardColumnData[];
|
columns?: BoardColumnData[];
|
||||||
@@ -23,6 +24,7 @@ export interface BoardColumnData {
|
|||||||
|
|
||||||
interface BoardViewContextData {
|
interface BoardViewContextData {
|
||||||
api?: BoardApi;
|
api?: BoardApi;
|
||||||
|
parentNote?: FNote;
|
||||||
branchIdToEdit?: string;
|
branchIdToEdit?: string;
|
||||||
columnNameToEdit?: string;
|
columnNameToEdit?: string;
|
||||||
setColumnNameToEdit?: Dispatch<StateUpdater<string | undefined>>;
|
setColumnNameToEdit?: Dispatch<StateUpdater<string | undefined>>;
|
||||||
@@ -31,8 +33,6 @@ interface BoardViewContextData {
|
|||||||
setDraggedColumn: (column: { column: string, index: number } | null) => void;
|
setDraggedColumn: (column: { column: string, index: number } | null) => void;
|
||||||
dropPosition: { column: string, index: number } | null;
|
dropPosition: { column: string, index: number } | null;
|
||||||
setDropPosition: (position: { column: string, index: number } | null) => void;
|
setDropPosition: (position: { column: string, index: number } | null) => void;
|
||||||
draggedCard: { noteId: string, branchId: string, fromColumn: string, index: number } | null;
|
|
||||||
setDraggedCard: (card: { noteId: string, branchId: string, fromColumn: string, index: number } | null) => void;
|
|
||||||
setDropTarget: (target: string | null) => void,
|
setDropTarget: (target: string | null) => void,
|
||||||
dropTarget: string | null
|
dropTarget: string | null
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
}, [ byColumn, columns, parentNote, statusAttribute, viewConfig, saveConfig, setBranchIdToEdit ]);
|
}, [ byColumn, columns, parentNote, statusAttribute, viewConfig, saveConfig, setBranchIdToEdit ]);
|
||||||
const boardViewContext = useMemo<BoardViewContextData>(() => ({
|
const boardViewContext = useMemo<BoardViewContextData>(() => ({
|
||||||
api,
|
api,
|
||||||
|
parentNote,
|
||||||
branchIdToEdit, setBranchIdToEdit,
|
branchIdToEdit, setBranchIdToEdit,
|
||||||
columnNameToEdit, setColumnNameToEdit,
|
columnNameToEdit, setColumnNameToEdit,
|
||||||
draggedColumn, setDraggedColumn,
|
draggedColumn, setDraggedColumn,
|
||||||
@@ -64,6 +65,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
dropTarget, setDropTarget
|
dropTarget, setDropTarget
|
||||||
}), [
|
}), [
|
||||||
api,
|
api,
|
||||||
|
parentNote,
|
||||||
branchIdToEdit, setBranchIdToEdit,
|
branchIdToEdit, setBranchIdToEdit,
|
||||||
columnNameToEdit, setColumnNameToEdit,
|
columnNameToEdit, setColumnNameToEdit,
|
||||||
draggedColumn, setDraggedColumn,
|
draggedColumn, setDraggedColumn,
|
||||||
|
|||||||
Reference in New Issue
Block a user