fix(note_autocomplete): fix wrong definition of types, and resulting bugs esp. improving row editing

This commit is contained in:
Jakob Schlanstedt 2025-10-31 00:38:17 +01:00 committed by Jakob Schlanstedt
parent 57fc867105
commit b8367600ad
4 changed files with 35 additions and 40 deletions

View file

@ -28,7 +28,7 @@ import TouchBarComponent from "./touch_bar.js";
import type { CKTextEditor } from "@triliumnext/ckeditor5"; import type { CKTextEditor } from "@triliumnext/ckeditor5";
import type CodeMirror from "@triliumnext/codemirror"; import type CodeMirror from "@triliumnext/codemirror";
import { StartupChecks } from "./startup_checks.js"; import { StartupChecks } from "./startup_checks.js";
import type { CreateNoteOpts } from "../services/note_create.js"; import type { CreateNoteOpts, CreateNoteWithUrlOpts } from "../services/note_create.js";
import { ColumnComponent } from "tabulator-tables"; import { ColumnComponent } from "tabulator-tables";
import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx"; import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx";
import type RootContainer from "../widgets/containers/root_container.js"; import type RootContainer from "../widgets/containers/root_container.js";
@ -357,8 +357,7 @@ export type CommandMappings = {
// Table view // Table view
addNewRow: CommandData & { addNewRow: CommandData & {
customOpts: CreateNoteOpts; customOpts?: CreateNoteWithUrlOpts;
parentNotePath?: string;
}; };
addNewTableColumn: CommandData & { addNewTableColumn: CommandData & {
columnToEdit?: ColumnComponent; columnToEdit?: ColumnComponent;

View file

@ -83,15 +83,18 @@ type CreateNoteBase = {
* Serves as a base for "into", "before", and "after" variants, * Serves as a base for "into", "before", and "after" variants,
* sharing common URL-related fields. * sharing common URL-related fields.
*/ */
export type CreateNoteWithUrlOpts = CreateNoteBase & { export type CreateNoteWithUrlOpts =
target: "into" | "after" | "before"; | (CreateNoteBase & {
// `Url` may refer to either parentNotePath or parentNoteId. target: "into";
// The vocabulary is inspired by existing function getNoteIdFromUrl. parentNoteUrl?: string;
parentNoteUrl: string; // No branch ID needed for "into"
})
// Disambiguates the position for cloned notes. | (CreateNoteBase & {
targetBranchId?: string; target: "before" | "after";
}; parentNoteUrl?: string;
// Required for "before"/"after"
targetBranchId: string;
});
export type CreateNoteIntoInboxOpts = CreateNoteBase & { export type CreateNoteIntoInboxOpts = CreateNoteBase & {
target: "inbox"; target: "inbox";
@ -134,7 +137,7 @@ async function createNote(
case "into": case "into":
case "before": case "before":
case "after": case "after":
return createNoteWithUrl(resolvedOptions.target, resolvedOptions); return createNoteWithUrl(resolvedOptions);
} }
} }
@ -173,7 +176,6 @@ async function promptForType(
* @returns A promise resolving with the created note and its branch. * @returns A promise resolving with the created note and its branch.
*/ */
async function createNoteWithUrl( async function createNoteWithUrl(
target: "into" | "after" | "before",
options: CreateNoteWithUrlOpts options: CreateNoteWithUrlOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> { ): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
options = Object.assign( options = Object.assign(
@ -210,7 +212,12 @@ async function createNoteWithUrl(
C-->D;`; C-->D;`;
} }
const { note, branch } = await server.post<Response>(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${options.targetBranchId || ""}`, { const query =
options.target === "into"
? `target=${options.target}`
: `target=${options.target}&targetBranchId=${options.targetBranchId}`;
const { note, branch } = await server.post<Response>(`notes/${parentNoteId}/children?${query}`, {
title: options.title, title: options.title,
content: options.content || "", content: options.content || "",
isProtected: options.isProtected, isProtected: options.isProtected,
@ -269,7 +276,7 @@ async function createNoteIntoInbox(
inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable(); inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable();
} }
const result = await createNoteWithUrl("into", const result = await createNoteWithUrl(
{ {
...options, ...options,
target: "into", target: "into",

View file

@ -181,7 +181,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
uiIcon: "bx bx-horizontal-left bx-rotate-90", uiIcon: "bx bx-horizontal-left bx-rotate-90",
enabled: !sorters.length, enabled: !sorters.length,
handler: () => parentComponent?.triggerCommand("addNewRow", { handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId,
customOpts: { customOpts: {
parentNoteUrl: parentNoteId, parentNoteUrl: parentNoteId,
target: "before", target: "before",
@ -199,7 +198,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
return; return;
} }
parentComponent?.triggerCommand("addNewRow", { parentComponent?.triggerCommand("addNewRow", {
parentNotePath: note.noteId,
customOpts: { customOpts: {
parentNoteUrl: note.noteId, parentNoteUrl: note.noteId,
target: "after", target: "after",
@ -213,7 +211,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
uiIcon: "bx bx-horizontal-left bx-rotate-270", uiIcon: "bx bx-horizontal-left bx-rotate-270",
enabled: !sorters.length, enabled: !sorters.length,
handler: () => parentComponent?.triggerCommand("addNewRow", { handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId,
customOpts: { customOpts: {
parentNoteUrl: parentNoteId, parentNoteUrl: parentNoteId,
target: "after", target: "after",

View file

@ -9,29 +9,20 @@ import server from "../../../services/server";
import branches from "../../../services/branches"; import branches from "../../../services/branches";
import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; import AttributeDetailWidget from "../../attribute_widgets/attribute_detail";
export default function useRowTableEditing(api: RefObject<Tabulator>, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial<EventCallBackMethods> { export default function useRowTableEditing(api: RefObject<Tabulator>, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial<EventCallBackMethods> { // Adding new rows
// Adding new rows
useLegacyImperativeHandlers({ useLegacyImperativeHandlers({
addNewRowCommand({ customOpts, parentNotePath: customNotePath }: CommandListenerData<"addNewRow">) { addNewRowCommand({ customOpts }: CommandListenerData<"addNewRow">) {
const notePath = customNotePath ?? parentNotePath; if (!customOpts) {
if (notePath) { customOpts = {
const opts: CreateNoteOpts = { target: "into",
activate: false, };
...customOpts }
}
// Normalize "inbox" targets into standard path-based creation. const noteUrl = customOpts.parentNoteUrl ?? parentNotePath;
// When adding a new row, we always have a concrete parent path (`notePath`), if (noteUrl) {
// so even if the originating command requested an "inbox" creation, customOpts.parentNoteUrl = noteUrl;
// it should instead behave as "into" under the current note. customOpts.activate = false;
const normalizedOpts: CreateNoteWithUrlOpts = note_create.createNote(customOpts).then(({ branch }) => {
opts.target === "inbox"
? { ...opts, target: "into", parentNoteUrl: notePath }
: { ...opts, parentNoteUrl: notePath };
note_create.createNote(
normalizedOpts
).then(({ branch }) => {
if (branch) { if (branch) {
setTimeout(() => { setTimeout(() => {
if (!api.current) return; if (!api.current) return;
@ -39,6 +30,7 @@ export default function useRowTableEditing(api: RefObject<Tabulator>, attributeD
}, 100); }, 100);
} }
}) })
} }
} }
}); });