mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
refactor(react/collections/table): use class-based API
This commit is contained in:
@@ -2,30 +2,39 @@ import FNote from "../../../entities/fnote";
|
|||||||
import attributes from "../../../services/attributes";
|
import attributes from "../../../services/attributes";
|
||||||
import note_create from "../../../services/note_create";
|
import note_create from "../../../services/note_create";
|
||||||
|
|
||||||
export async function createNewItem(parentNote: FNote, column: string) {
|
export default class BoardApi {
|
||||||
try {
|
|
||||||
// Get the parent note path
|
|
||||||
const parentNotePath = parentNote.noteId;
|
|
||||||
const statusAttribute = parentNote.getLabelValue("board:groupBy") ?? "status";
|
|
||||||
|
|
||||||
// Create a new note as a child of the parent note
|
constructor(
|
||||||
const { note: newNote } = await note_create.createNote(parentNotePath, {
|
private parentNote: FNote,
|
||||||
activate: false,
|
private statusAttribute: string
|
||||||
title: "New item"
|
) {};
|
||||||
});
|
|
||||||
|
|
||||||
if (newNote) {
|
async createNewItem(column: string) {
|
||||||
// Set the status label to place it in the correct column
|
try {
|
||||||
await changeColumn(newNote.noteId, column, statusAttribute);
|
// Get the parent note path
|
||||||
|
const parentNotePath = this.parentNote.noteId;
|
||||||
|
|
||||||
// Start inline editing of the newly created card
|
// Create a new note as a child of the parent note
|
||||||
//this.startInlineEditingCard(newNote.noteId);
|
const { note: newNote } = await note_create.createNote(parentNotePath, {
|
||||||
|
activate: false,
|
||||||
|
title: "New item"
|
||||||
|
});
|
||||||
|
|
||||||
|
if (newNote) {
|
||||||
|
// Set the status label to place it in the correct column
|
||||||
|
await this.changeColumn(newNote.noteId, column);
|
||||||
|
|
||||||
|
// Start inline editing of the newly created card
|
||||||
|
//this.startInlineEditingCard(newNote.noteId);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to create new item:", error);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to create new item:", error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changeColumn(noteId: string, newColumn: string) {
|
||||||
|
await attributes.setLabel(noteId, this.statusAttribute, newColumn);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function changeColumn(noteId: string, newColumn: string, statusAttribute: string) {
|
|
||||||
await attributes.setLabel(noteId, statusAttribute, newColumn);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
|
||||||
import { ViewModeProps } from "../interface";
|
import { ViewModeProps } from "../interface";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { ColumnMap, getBoardData } from "./data";
|
import { ColumnMap, getBoardData } from "./data";
|
||||||
import { useNoteLabel, useTriliumEvent } from "../../react/hooks";
|
import { useNoteLabelWithDefault, useTriliumEvent } from "../../react/hooks";
|
||||||
import FNote from "../../../entities/fnote";
|
import FNote from "../../../entities/fnote";
|
||||||
import FBranch from "../../../entities/fbranch";
|
import FBranch from "../../../entities/fbranch";
|
||||||
import Icon from "../../react/Icon";
|
import Icon from "../../react/Icon";
|
||||||
import { t } from "../../../services/i18n";
|
import { t } from "../../../services/i18n";
|
||||||
import { createNewItem, changeColumn } from "./api";
|
import Api from "./api";
|
||||||
import FormTextBox from "../../react/FormTextBox";
|
import FormTextBox from "../../react/FormTextBox";
|
||||||
import branchService from "../../../services/branches";
|
import branchService from "../../../services/branches";
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ export interface BoardColumnData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function BoardView({ note: parentNote, noteIds, viewConfig, saveConfig }: ViewModeProps<BoardViewData>) {
|
export default function BoardView({ note: parentNote, noteIds, viewConfig, saveConfig }: ViewModeProps<BoardViewData>) {
|
||||||
const [ statusAttribute ] = useNoteLabel(parentNote, "board:groupBy");
|
const [ statusAttribute ] = useNoteLabelWithDefault(parentNote, "board:groupBy", "status");
|
||||||
const [ byColumn, setByColumn ] = useState<ColumnMap>();
|
const [ byColumn, setByColumn ] = useState<ColumnMap>();
|
||||||
const [ columns, setColumns ] = useState<string[]>();
|
const [ columns, setColumns ] = useState<string[]>();
|
||||||
const [ draggedCard, setDraggedCard ] = useState<{ noteId: string, branchId: string, fromColumn: string, index: number } | null>(null);
|
const [ draggedCard, setDraggedCard ] = useState<{ noteId: string, branchId: string, fromColumn: string, index: number } | null>(null);
|
||||||
@@ -28,9 +28,12 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
const [ dropPosition, setDropPosition ] = useState<{ column: string, index: number } | null>(null);
|
const [ dropPosition, setDropPosition ] = useState<{ column: string, index: number } | null>(null);
|
||||||
const [ draggedColumn, setDraggedColumn ] = useState<{ column: string, index: number } | null>(null);
|
const [ draggedColumn, setDraggedColumn ] = useState<{ column: string, index: number } | null>(null);
|
||||||
const [ columnDropPosition, setColumnDropPosition ] = useState<number | null>(null);
|
const [ columnDropPosition, setColumnDropPosition ] = useState<number | null>(null);
|
||||||
|
const api = useMemo(() => {
|
||||||
|
return new Api(parentNote, statusAttribute);
|
||||||
|
}, [ parentNote, statusAttribute ]);
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
getBoardData(parentNote, statusAttribute ?? "status", viewConfig ?? {}).then(({ byColumn, newPersistedData }) => {
|
getBoardData(parentNote, statusAttribute, viewConfig ?? {}).then(({ byColumn, newPersistedData }) => {
|
||||||
setByColumn(byColumn);
|
setByColumn(byColumn);
|
||||||
|
|
||||||
if (newPersistedData) {
|
if (newPersistedData) {
|
||||||
@@ -132,10 +135,10 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
<div className="column-drop-placeholder show" />
|
<div className="column-drop-placeholder show" />
|
||||||
)}
|
)}
|
||||||
<Column
|
<Column
|
||||||
|
api={api}
|
||||||
column={column}
|
column={column}
|
||||||
columnIndex={index}
|
columnIndex={index}
|
||||||
columnItems={byColumn.get(column)}
|
columnItems={byColumn.get(column)}
|
||||||
parentNote={parentNote}
|
|
||||||
statusAttribute={statusAttribute ?? "status"}
|
statusAttribute={statusAttribute ?? "status"}
|
||||||
draggedCard={draggedCard}
|
draggedCard={draggedCard}
|
||||||
setDraggedCard={setDraggedCard}
|
setDraggedCard={setDraggedCard}
|
||||||
@@ -161,7 +164,6 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Column({
|
function Column({
|
||||||
parentNote,
|
|
||||||
column,
|
column,
|
||||||
columnIndex,
|
columnIndex,
|
||||||
columnItems,
|
columnItems,
|
||||||
@@ -175,9 +177,9 @@ function Column({
|
|||||||
onCardDrop,
|
onCardDrop,
|
||||||
draggedColumn,
|
draggedColumn,
|
||||||
setDraggedColumn,
|
setDraggedColumn,
|
||||||
isDraggingColumn
|
isDraggingColumn,
|
||||||
|
api
|
||||||
}: {
|
}: {
|
||||||
parentNote: FNote,
|
|
||||||
column: string,
|
column: string,
|
||||||
columnIndex: number,
|
columnIndex: number,
|
||||||
columnItems?: { note: FNote, branch: FBranch }[],
|
columnItems?: { note: FNote, branch: FBranch }[],
|
||||||
@@ -191,7 +193,8 @@ function Column({
|
|||||||
onCardDrop: () => void,
|
onCardDrop: () => void,
|
||||||
draggedColumn: { column: string, index: number } | null,
|
draggedColumn: { column: string, index: number } | null,
|
||||||
setDraggedColumn: (column: { column: string, index: number } | null) => void,
|
setDraggedColumn: (column: { column: string, index: number } | null) => void,
|
||||||
isDraggingColumn: boolean
|
isDraggingColumn: boolean,
|
||||||
|
api: Api
|
||||||
}) {
|
}) {
|
||||||
const handleColumnDragStart = useCallback((e: DragEvent) => {
|
const handleColumnDragStart = useCallback((e: DragEvent) => {
|
||||||
e.dataTransfer!.effectAllowed = 'move';
|
e.dataTransfer!.effectAllowed = 'move';
|
||||||
@@ -250,7 +253,7 @@ function Column({
|
|||||||
|
|
||||||
if (draggedCard.fromColumn !== column) {
|
if (draggedCard.fromColumn !== column) {
|
||||||
// Moving to a different column
|
// Moving to a different column
|
||||||
await changeColumn(draggedCard.noteId, column, statusAttribute);
|
await api.changeColumn(draggedCard.noteId, column);
|
||||||
|
|
||||||
// If there are items in the target column, reorder
|
// If there are items in the target column, reorder
|
||||||
if (targetItems.length > 0 && targetIndex < targetItems.length) {
|
if (targetItems.length > 0 && targetIndex < targetItems.length) {
|
||||||
@@ -323,7 +326,7 @@ function Column({
|
|||||||
<div className="board-drop-placeholder show" />
|
<div className="board-drop-placeholder show" />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="board-new-item" onClick={() => createNewItem(parentNote, column)}>
|
<div className="board-new-item" onClick={() => api.createNewItem(column)}>
|
||||||
<Icon icon="bx bx-plus" />{" "}
|
<Icon icon="bx bx-plus" />{" "}
|
||||||
{t("board_view.new-item")}
|
{t("board_view.new-item")}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -324,6 +324,11 @@ export function useNoteLabel(note: FNote | undefined | null, labelName: string):
|
|||||||
] as const;
|
] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useNoteLabelWithDefault(note: FNote | undefined | null, labelName: string, defaultValue: string): [string, (newValue: string | null | undefined) => void] {
|
||||||
|
const [ labelValue, setLabelValue ] = useNoteLabel(note, labelName);
|
||||||
|
return [ labelValue ?? defaultValue, setLabelValue];
|
||||||
|
}
|
||||||
|
|
||||||
export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: string): [ boolean, (newValue: boolean) => void] {
|
export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: string): [ boolean, (newValue: boolean) => void] {
|
||||||
const [ labelValue, setLabelValue ] = useState<boolean>(!!note?.hasLabel(labelName));
|
const [ labelValue, setLabelValue ] = useState<boolean>(!!note?.hasLabel(labelName));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user