Skip to content

Commit b836760

Browse files
Jakob Schlanstedtwerererer
authored andcommitted
fix(note_autocomplete): fix wrong definition of types, and resulting bugs esp. improving row editing
1 parent 57fc867 commit b836760

File tree

4 files changed

+35
-40
lines changed

4 files changed

+35
-40
lines changed

apps/client/src/components/app_context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import TouchBarComponent from "./touch_bar.js";
2828
import type { CKTextEditor } from "@triliumnext/ckeditor5";
2929
import type CodeMirror from "@triliumnext/codemirror";
3030
import { StartupChecks } from "./startup_checks.js";
31-
import type { CreateNoteOpts } from "../services/note_create.js";
31+
import type { CreateNoteOpts, CreateNoteWithUrlOpts } from "../services/note_create.js";
3232
import { ColumnComponent } from "tabulator-tables";
3333
import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx";
3434
import type RootContainer from "../widgets/containers/root_container.js";
@@ -357,8 +357,7 @@ export type CommandMappings = {
357357

358358
// Table view
359359
addNewRow: CommandData & {
360-
customOpts: CreateNoteOpts;
361-
parentNotePath?: string;
360+
customOpts?: CreateNoteWithUrlOpts;
362361
};
363362
addNewTableColumn: CommandData & {
364363
columnToEdit?: ColumnComponent;

apps/client/src/services/note_create.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,18 @@ type CreateNoteBase = {
8383
* Serves as a base for "into", "before", and "after" variants,
8484
* sharing common URL-related fields.
8585
*/
86-
export type CreateNoteWithUrlOpts = CreateNoteBase & {
87-
target: "into" | "after" | "before";
88-
// `Url` may refer to either parentNotePath or parentNoteId.
89-
// The vocabulary is inspired by existing function getNoteIdFromUrl.
90-
parentNoteUrl: string;
91-
92-
// Disambiguates the position for cloned notes.
93-
targetBranchId?: string;
94-
};
86+
export type CreateNoteWithUrlOpts =
87+
| (CreateNoteBase & {
88+
target: "into";
89+
parentNoteUrl?: string;
90+
// No branch ID needed for "into"
91+
})
92+
| (CreateNoteBase & {
93+
target: "before" | "after";
94+
parentNoteUrl?: string;
95+
// Required for "before"/"after"
96+
targetBranchId: string;
97+
});
9598

9699
export type CreateNoteIntoInboxOpts = CreateNoteBase & {
97100
target: "inbox";
@@ -134,7 +137,7 @@ async function createNote(
134137
case "into":
135138
case "before":
136139
case "after":
137-
return createNoteWithUrl(resolvedOptions.target, resolvedOptions);
140+
return createNoteWithUrl(resolvedOptions);
138141
}
139142
}
140143

@@ -173,7 +176,6 @@ async function promptForType(
173176
* @returns A promise resolving with the created note and its branch.
174177
*/
175178
async function createNoteWithUrl(
176-
target: "into" | "after" | "before",
177179
options: CreateNoteWithUrlOpts
178180
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
179181
options = Object.assign(
@@ -210,7 +212,12 @@ async function createNoteWithUrl(
210212
C-->D;`;
211213
}
212214

213-
const { note, branch } = await server.post<Response>(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${options.targetBranchId || ""}`, {
215+
const query =
216+
options.target === "into"
217+
? `target=${options.target}`
218+
: `target=${options.target}&targetBranchId=${options.targetBranchId}`;
219+
220+
const { note, branch } = await server.post<Response>(`notes/${parentNoteId}/children?${query}`, {
214221
title: options.title,
215222
content: options.content || "",
216223
isProtected: options.isProtected,
@@ -269,7 +276,7 @@ async function createNoteIntoInbox(
269276
inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable();
270277
}
271278

272-
const result = await createNoteWithUrl("into",
279+
const result = await createNoteWithUrl(
273280
{
274281
...options,
275282
target: "into",

apps/client/src/widgets/collections/table/context_menu.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
181181
uiIcon: "bx bx-horizontal-left bx-rotate-90",
182182
enabled: !sorters.length,
183183
handler: () => parentComponent?.triggerCommand("addNewRow", {
184-
parentNotePath: parentNoteId,
185184
customOpts: {
186185
parentNoteUrl: parentNoteId,
187186
target: "before",
@@ -199,7 +198,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
199198
return;
200199
}
201200
parentComponent?.triggerCommand("addNewRow", {
202-
parentNotePath: note.noteId,
203201
customOpts: {
204202
parentNoteUrl: note.noteId,
205203
target: "after",
@@ -213,7 +211,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
213211
uiIcon: "bx bx-horizontal-left bx-rotate-270",
214212
enabled: !sorters.length,
215213
handler: () => parentComponent?.triggerCommand("addNewRow", {
216-
parentNotePath: parentNoteId,
217214
customOpts: {
218215
parentNoteUrl: parentNoteId,
219216
target: "after",

apps/client/src/widgets/collections/table/row_editing.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,28 @@ import server from "../../../services/server";
99
import branches from "../../../services/branches";
1010
import AttributeDetailWidget from "../../attribute_widgets/attribute_detail";
1111

12-
export default function useRowTableEditing(api: RefObject<Tabulator>, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial<EventCallBackMethods> {
13-
// Adding new rows
12+
export default function useRowTableEditing(api: RefObject<Tabulator>, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial<EventCallBackMethods> { // Adding new rows
1413
useLegacyImperativeHandlers({
15-
addNewRowCommand({ customOpts, parentNotePath: customNotePath }: CommandListenerData<"addNewRow">) {
16-
const notePath = customNotePath ?? parentNotePath;
17-
if (notePath) {
18-
const opts: CreateNoteOpts = {
19-
activate: false,
20-
...customOpts
21-
}
22-
23-
// Normalize "inbox" targets into standard path-based creation.
24-
// When adding a new row, we always have a concrete parent path (`notePath`),
25-
// so even if the originating command requested an "inbox" creation,
26-
// it should instead behave as "into" under the current note.
27-
const normalizedOpts: CreateNoteWithUrlOpts =
28-
opts.target === "inbox"
29-
? { ...opts, target: "into", parentNoteUrl: notePath }
30-
: { ...opts, parentNoteUrl: notePath };
14+
addNewRowCommand({ customOpts }: CommandListenerData<"addNewRow">) {
15+
if (!customOpts) {
16+
customOpts = {
17+
target: "into",
18+
};
19+
}
3120

32-
note_create.createNote(
33-
normalizedOpts
34-
).then(({ branch }) => {
21+
const noteUrl = customOpts.parentNoteUrl ?? parentNotePath;
22+
if (noteUrl) {
23+
customOpts.parentNoteUrl = noteUrl;
24+
customOpts.activate = false;
25+
note_create.createNote(customOpts).then(({ branch }) => {
3526
if (branch) {
3627
setTimeout(() => {
3728
if (!api.current) return;
3829
focusOnBranch(api.current, branch?.branchId);
3930
}, 100);
4031
}
4132
})
33+
4234
}
4335
}
4436
});

0 commit comments

Comments
 (0)