refactor(typeerror): resolve typeerrors by refactoring code

This commit is contained in:
Jakob Schlanstedt 2025-10-22 00:53:36 +02:00
parent 9d40c0cb26
commit 1e15585a24
13 changed files with 73 additions and 52 deletions

View File

@ -11,7 +11,7 @@ import froca from "../services/froca.js";
import linkService from "../services/link.js";
import { t } from "../services/i18n.js";
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons";
import noteCreateService, { CreateNoteTarget } from "../services/note_create.js";
import noteCreateService, { CreateNoteIntoInboxURLOpts, CreateNoteTarget } from "../services/note_create.js";
export default class Entrypoints extends Component {
constructor() {
@ -26,7 +26,7 @@ export default class Entrypoints extends Component {
async createNoteIntoInboxCommand() {
await noteCreateService.createNote(
CreateNoteTarget.IntoInbox
{ target: CreateNoteTarget.IntoInbox } as CreateNoteIntoInboxURLOpts
);
}

View File

@ -49,8 +49,8 @@ export default class MainTreeExecutors extends Component {
}
await noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: activeNoteContext.notePath,
isProtected: activeNoteContext.note.isProtected,
saveSelection: false,
@ -78,8 +78,8 @@ export default class MainTreeExecutors extends Component {
}
await noteCreateService.createNote(
CreateNoteTarget.AfterNoteURL,
{
target: CreateNoteTarget.AfterNoteURL,
parentNoteUrl: parentNotePath,
targetBranchId: node.data.branchId,
isProtected: isProtected,

View File

@ -241,8 +241,8 @@ export default class RootCommandExecutor extends Component {
const rootNoteId = "root";
const result = await noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
title: "New AI Chat",
type: "aiChat",
content: JSON.stringify({

View File

@ -285,8 +285,8 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Tree
noteCreateService.createNote(
CreateNoteTarget.AfterNoteURL,
{
target: CreateNoteTarget.AfterNoteURL,
parentNoteUrl: parentNotePath,
targetBranchId: this.node.data.branchId,
type: type,
@ -299,14 +299,13 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Tree
const parentNotePath = treeService.getNotePath(this.node);
noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: parentNotePath,
type: type,
isProtected: this.node.data.isProtected,
templateNoteId: templateNoteId,
promptForType: false,
placement:
} as CreateNoteIntoURLOpts
);
} else if (command === "openNoteInSplit") {

View File

@ -1,6 +1,6 @@
import server from "./server.js";
import appContext from "../components/app_context.js";
import noteCreateService, { CreateNoteIntoURLOpts, CreateNoteTarget, InboxNoteOpts } from "./note_create.js";
import noteCreateService, { CreateNoteIntoURLOpts, CreateNoteTarget, CreateNoteIntoInboxURLOpts } from "./note_create.js";
import froca from "./froca.js";
import { t } from "./i18n.js";
import commandRegistry from "./command_registry.js";
@ -482,12 +482,12 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
// --- CREATE NOTE INTO INBOX ---
case SuggestionAction.CreateNoteIntoInbox: {
const { note } = await noteCreateService.createNote(
CreateNoteTarget.IntoInbox,
{
target: CreateNoteTarget.IntoInbox,
title: suggestion.noteTitle,
activate: true,
promptForType: true,
} as InboxNoteOpts
} as CreateNoteIntoInboxURLOpts
);
if (!note) return;
@ -503,12 +503,12 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
// --- CREATE AND LINK NOTE INTO INBOX ---
case SuggestionAction.CreateAndLinkNoteIntoInbox: {
const { note } = await noteCreateService.createNote(
CreateNoteTarget.IntoInbox,
{
target: CreateNoteTarget.IntoInbox,
title: suggestion.noteTitle,
activate: false,
promptForType: true,
} as InboxNoteOpts,
} as CreateNoteIntoInboxURLOpts,
);
if (!note) return;
@ -524,14 +524,14 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
// --- CREATE NOTE INTO PATH ---
case SuggestionAction.CreateNoteIntoPath: {
const { note } = await noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: suggestion.parentNoteId,
title: suggestion.noteTitle,
activate: true,
promptForType: true,
} as CreateNoteIntoURLOpts
)
} as CreateNoteIntoURLOpts,
);
if (!note) return;
@ -546,15 +546,16 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
// --- CREATE AND LINK NOTE INTO PATH ---
case SuggestionAction.CreateAndLinkNoteIntoPath: {
const { note } = await noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: suggestion.parentNoteId,
title: suggestion.noteTitle,
activate: false,
promptForType: true,
} as CreateNoteIntoURLOpts
);
if (!note) return
if (!note) return;
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
suggestion.notePath = note?.getBestNotePathString(hoistedNoteId);

View File

@ -23,6 +23,7 @@ import { CreateChildrenResponse } from "@triliumnext/commons";
export enum CreateNoteTarget {
IntoNoteURL,
AfterNoteURL,
BeforeNoteURL,
IntoInbox,
}
@ -37,6 +38,7 @@ export type BaseCreateNoteOpts =
} & BaseCreateNoteSharedOpts);
export interface BaseCreateNoteSharedOpts {
target: CreateNoteTarget;
isProtected?: boolean;
saveSelection?: boolean;
title?: string | null;
@ -57,15 +59,18 @@ type CreateNoteAtURLOpts = BaseCreateNoteSharedOpts & {
}
export type CreateNoteIntoURLOpts = CreateNoteAtURLOpts;
export type CreateNoteAfterURLOpts = Omit<CreateNoteAtURLOpts, "targetBranchId"> & {
// targetBranchId disambiguates the position for cloned notes, thus it must
// only be specified for a sibling
// This is also specified in the backend
// targetBranchId disambiguates the position for cloned notes, thus it must
// only be specified for a sibling
// This is also specified in the backend
type CreateNoteSiblingURLOpts = Omit<CreateNoteAtURLOpts, "targetBranchId"> & {
targetBranchId: string;
};
export type CreateNoteBeforeURLOpts = CreateNoteSiblingURLOpts;
export type CreateNoteAfterURLOpts = CreateNoteSiblingURLOpts;
// For creating *in the inbox*
export type InboxNoteOpts = BaseCreateNoteSharedOpts & {
export type CreateNoteIntoInboxURLOpts = BaseCreateNoteSharedOpts & {
// disallowed
parentNoteUrl?: never;
}
@ -170,6 +175,12 @@ async function createNoteIntoNote(
return createNoteAtNote("into", {...options} as CreateNoteAtURLOpts);
}
async function createNoteBeforeNote(
options: CreateNoteBeforeURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
return createNoteAtNote("before", {...options} as CreateNoteAtURLOpts);
}
async function createNoteAfterNote(
options: CreateNoteAfterURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
@ -184,7 +195,7 @@ async function createNoteAfterNote(
* Resolves with the created note and its branch, or `{ note: null, branch: undefined }` if the inbox is missing.
*/
async function createNoteIntoInbox(
options: InboxNoteOpts
options: CreateNoteIntoInboxURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
const inboxNote = await dateNoteService.getInboxNote();
if (!inboxNote) {
@ -215,27 +226,26 @@ async function chooseNoteType() {
}
async function createNote(
target: CreateNoteTarget.IntoNoteURL,
options: CreateNoteIntoURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }>;
async function createNote(
target: CreateNoteTarget.AfterNoteURL,
options: CreateNoteAfterURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }>;
async function createNote(
target: CreateNoteTarget.IntoInbox,
options?: InboxNoteOpts
options: CreateNoteBeforeURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }>;
async function createNote(
target: CreateNoteTarget,
options: BaseCreateNoteOpts = {promptForType: true}
options: CreateNoteIntoInboxURLOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }>;
async function createNote(
options: BaseCreateNoteOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
let resolvedOptions = { ...options };
let resolvedTarget = target;
// handle prompts centrally to write once fix for all
if (options.promptForType) {
@ -256,24 +266,27 @@ async function createNote(
resolvedOptions = resolvedOptions as CreateNoteIntoURLOpts;
resolvedOptions = {
...resolvedOptions,
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: notePath,
} as CreateNoteIntoURLOpts;
resolvedTarget = CreateNoteTarget.IntoNoteURL;
}
}
switch (resolvedTarget) {
switch (resolvedOptions.target) {
case CreateNoteTarget.IntoNoteURL:
return await createNoteIntoNote(resolvedOptions as CreateNoteIntoURLOpts);
case CreateNoteTarget.BeforeNoteURL:
return await createNoteBeforeNote(resolvedOptions as CreateNoteBeforeURLOpts);
case CreateNoteTarget.AfterNoteURL:
return await createNoteAfterNote(resolvedOptions as CreateNoteAfterURLOpts);
case CreateNoteTarget.IntoInbox:
return await createNoteIntoInbox(resolvedOptions as InboxNoteOpts);
return await createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxURLOpts);
default: {
console.warn("[createNote] Unknown target:", target, resolvedOptions);
console.warn("[createNote] Unknown target:", options.target, resolvedOptions);
toastService.showMessage("Unknown note creation target."); // optional
return { note: null, branch: undefined };
}

View File

@ -39,7 +39,8 @@ export default class BoardApi {
const parentNotePath = this.parentNote.noteId;
// Create a new note as a child of the parent note
const { note: newNote, branch: newBranch } = await note_create.createNote(CreateNoteTarget.IntoNoteURL, {
const { note: newNote, branch: newBranch } = await note_create.createNote({
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: parentNotePath,
activate: false,
title,
@ -140,14 +141,14 @@ export default class BoardApi {
async insertRowAtPosition(
column: string,
relativeToBranchId: string,
direction: "before" | "after") {
direction: CreateNoteTarget.BeforeNoteURL | CreateNoteTarget.AfterNoteURL
) {
const { note, branch } = await note_create.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: direction,
parentNoteUrl: this.parentNote.noteId,
activate: false,
targetBranchId: relativeToBranchId,
target: direction,
title: t("board_view.new-item"),
} as CreateNoteIntoURLOpts
);

View File

@ -6,6 +6,7 @@ import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n";
import { CreateNoteTarget } from "../../../services/note_create";
import Api from "./api";
export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: string) {
@ -57,12 +58,18 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo
{
title: t("board_view.insert-above"),
uiIcon: "bx bx-list-plus",
handler: () => api.insertRowAtPosition(column, branchId, "before")
handler: () => api.insertRowAtPosition(
column,
branchId,
CreateNoteTarget.BeforeNoteURL)
},
{
title: t("board_view.insert-below"),
uiIcon: "bx bx-empty",
handler: () => api.insertRowAtPosition(column, branchId, "after")
handler: () => api.insertRowAtPosition(
column,
branchId,
CreateNoteTarget.AfterNoteURL)
},
{ kind: "separator" },
{

View File

@ -9,6 +9,7 @@ import branches from "../../../services/branches.js";
import Component from "../../../components/component.js";
import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker.jsx";
import { RefObject } from "preact";
import { CreateNoteTarget } from "../../../services/note_create.js";
export function useContextMenu(parentNote: FNote, parentComponent: Component | null | undefined, tabulator: RefObject<Tabulator>): Partial<EventCallBackMethods> {
const events: Partial<EventCallBackMethods> = {};
@ -183,7 +184,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId,
customOpts: {
target: "before",
target: CreateNoteTarget.BeforeNoteURL,
targetBranchId: rowData.branchId,
}
})
@ -197,7 +198,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
parentComponent?.triggerCommand("addNewRow", {
parentNotePath: note?.noteId,
customOpts: {
target: "after",
target: CreateNoteTarget.AfterNoteURL,
targetBranchId: branchId,
}
});
@ -210,7 +211,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId,
customOpts: {
target: "after",
target: CreateNoteTarget.AfterNoteURL,
targetBranchId: rowData.branchId,
}
})

View File

@ -20,7 +20,6 @@ export default function useRowTableEditing(api: RefObject<Tabulator>, attributeD
...customOpts
}
note_create.createNote(
CreateNoteTarget.IntoNoteURL,
{
parentNoteUrl: notePath,
...opts

View File

@ -30,8 +30,8 @@ export default function MobileDetailMenu() {
selectMenuItemHandler: async ({ command }) => {
if (command === "insertChildNote") {
note_create.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: appContext.tabManager.getActiveContextNotePath() ?? undefined
} as CreateNoteIntoURLOpts
);

View File

@ -225,8 +225,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
const node = $.ui.fancytree.getNode(e as unknown as Event);
const parentNotePath = treeService.getNotePath(node);
noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: parentNotePath,
isProtected: node.data.isProtected
} as CreateNoteIntoURLOpts,
@ -1843,8 +1843,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
if (!node) return;
const notePath = treeService.getNotePath(node);
noteCreateService.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: notePath,
isProtected: node.data.isProtected
} as CreateNoteIntoURLOpts

View File

@ -19,7 +19,7 @@ import contextMenu from "../../../menus/context_menu";
import type { CommandData, FilteredCommandNames } from "../../../components/app_context";
import { AttributeType } from "@triliumnext/commons";
import attributes from "../../../services/attributes";
import note_create, { CreateNoteAfterURLOpts, CreateNoteIntoURLOpts, CreateNoteTarget, InboxNoteOpts } from "../../../services/note_create";
import note_create, { CreateNoteAfterURLOpts, CreateNoteIntoURLOpts, CreateNoteTarget, CreateNoteIntoInboxURLOpts } from "../../../services/note_create";
import { CreateNoteAction } from "@triliumnext/commons";
type AttributeCommandNames = FilteredCommandNames<CommandData>;
@ -262,11 +262,11 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
case CreateNoteAction.CreateNoteIntoInbox:
case CreateNoteAction.CreateAndLinkNoteIntoInbox: {
const { note } = await note_create.createNote(
CreateNoteTarget.IntoInbox,
{
target: CreateNoteTarget.IntoInbox,
title,
activate: false
} as InboxNoteOpts
} as CreateNoteIntoInboxURLOpts
);
return note?.getBestNotePathString() ?? "";
}
@ -274,8 +274,8 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
case CreateNoteAction.CreateNoteIntoPath:
case CreateNoteAction.CreateAndLinkNoteIntoPath: {
const resp = await note_create.createNote(
CreateNoteTarget.IntoNoteURL,
{
target: CreateNoteTarget.IntoNoteURL,
parentNoteUrl: parentNotePath,
title,
activate: false,