Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(client/ts): port components/app_context
Elian Doranec685db0938b
diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.tsindex ca4f9e1ce7..d755d826e4 100644--- a/src/public/app/components/app_context.ts+++ b/src/public/app/components/app_context.ts@@ -1,7 +1,7 @@import froca from "../services/froca.js";import bundleService from "../services/bundle.js";import RootCommandExecutor from "./root_command_executor.js";-import Entrypoints from "./entrypoints.js";+import Entrypoints, { SqlExecuteResults } from "./entrypoints.js";import options from "../services/options.js";import utils from "../services/utils.js";import zoomComponent from "./zoom.js";@@ -52,6 +52,12 @@ export interface ContextMenuCommandData extends CommandData {selectedOrActiveNoteIds: any; // TODO: Remove any once type is defined}+export interface NoteCommandData extends CommandData {+ notePath: string;+ hoistedNoteId?: string;+ viewScope?: ViewScope;+}+/*** The keys represent the different commands that can be triggered via {@link AppContext#triggerCommand} (first argument), and the values represent the data or arguments definition of the given command. All data for commands must extend {@link CommandData}.*/@@ -79,17 +85,8 @@ export type CommandMappings = {showPromptDialog: PromptDialogOptions;showInfoDialog: ConfirmWithMessageOptions;showConfirmDialog: ConfirmWithMessageOptions;- openNewNoteSplit: CommandData & {- ntxId: string;- notePath: string;- hoistedNoteId?: string;- viewScope?: ViewScope;- };- openInWindow: CommandData & {- notePath: string;- hoistedNoteId: string;- viewScope: ViewScope;- },+ openNewNoteSplit: NoteCommandData;+ openInWindow: NoteCommandData,openNoteInNewTab: CommandData;openNoteInNewSplit: CommandData;openNoteInNewWindow: CommandData;@@ -173,6 +170,10 @@ type EventMappings = {};addNewLabel: CommandData;addNewRelation: CommandData;+ sqlQueryResults: {+ ntxId: string;+ results: SqlExecuteResults;+ }}export type EventListener<T extends EventNames> = {diff --git a/src/public/app/components/entrypoints.js b/src/public/app/components/entrypoints.tssimilarity index 86%rename from src/public/app/components/entrypoints.jsrename to src/public/app/components/entrypoints.tsindex 6ee06ff654..1aae586a30 100644--- a/src/public/app/components/entrypoints.js+++ b/src/public/app/components/entrypoints.ts@@ -2,7 +2,7 @@ import utils from "../services/utils.js";import dateNoteService from "../services/date_notes.js";import protectedSessionHolder from '../services/protected_session_holder.js';import server from "../services/server.js";-import appContext from "./app_context.js";+import appContext, { NoteCommandData } from "./app_context.js";import Component from "./component.js";import toastService from "../services/toast.js";import ws from "../services/ws.js";@@ -10,6 +10,22 @@ import bundleService from "../services/bundle.js";import froca from "../services/froca.js";import linkService from "../services/link.js";import { t } from "../services/i18n.js";+import FNote from "../entities/fnote.js";++// TODO: Move somewhere else nicer.+export type SqlExecuteResults = unknown[];++// TODO: Deduplicate with server.+interface SqlExecuteResponse {+ success: boolean;+ error?: string;+ results: SqlExecuteResults;+}++// TODO: Deduplicate with server.+interface CreateChildrenResponse {+ note: FNote;+}export default class Entrypoints extends Component {constructor() {@@ -31,8 +47,12 @@ export default class Entrypoints extends Component {async createNoteIntoInboxCommand() {const inboxNote = await dateNoteService.getInboxNote();+ if (!inboxNote) {+ console.warn("Missing inbox note.");+ return;+ }- const {note} = await server.post(`notes/${inboxNote.noteId}/children?target=into`, {+ const {note} = await server.post<CreateChildrenResponse>(`notes/${inboxNote.noteId}/children?target=into`, {content: '',type: 'text',isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()@@ -46,18 +66,21 @@ export default class Entrypoints extends Component {}async toggleNoteHoistingCommand({noteId = appContext.tabManager.getActiveContextNoteId()}) {+ if (!noteId) {+ return;+ }+const noteToHoist = await froca.getNote(noteId);const activeNoteContext = appContext.tabManager.getActiveContext();- if (noteToHoist.noteId === activeNoteContext.hoistedNoteId) {+ if (noteToHoist?.noteId === activeNoteContext.hoistedNoteId) {await activeNoteContext.unhoist();- }- else if (noteToHoist.type !== 'search') {+ } else if (noteToHoist?.type !== 'search') {await activeNoteContext.setHoistedNoteId(noteId);}}- async hoistNoteCommand({noteId}) {+ async hoistNoteCommand({noteId}: { noteId: string }) {const noteContext = appContext.tabManager.getActiveContext();if (noteContext.hoistedNoteId !== noteId) {@@ -137,7 +160,7 @@ export default class Entrypoints extends Component {utils.reloadFrontendApp("Switching to mobile version");}- async openInWindowCommand({notePath, hoistedNoteId, viewScope}) {+ async openInWindowCommand({notePath, hoistedNoteId, viewScope}: NoteCommandData) {const extraWindowHash = linkService.calculateHash({notePath, hoistedNoteId, viewScope});if (utils.isElectron()) {@@ -170,7 +193,7 @@ export default class Entrypoints extends Component {} else if (note.mime.endsWith("env=backend")) {await server.post(`script/run/${note.noteId}`);} else if (note.mime === 'text/x-sqlite;schema=trilium') {- const resp = await server.post(`sql/execute/${note.noteId}`);+ const resp = await server.post<SqlExecuteResponse>(`sql/execute/${note.noteId}`);if (!resp.success) {toastService.showError(t("entrypoints.sql-error", { message: resp.error }));diff --git a/src/public/app/services/link.ts b/src/public/app/services/link.tsindex d62e902500..671980242c 100644--- a/src/public/app/services/link.ts+++ b/src/public/app/services/link.ts@@ -1,6 +1,6 @@import treeService from './tree.js';import linkContextMenuService from "../menus/link_context_menu.js";-import appContext from "../components/app_context.js";+import appContext, { NoteCommandData } from "../components/app_context.js";import froca from "./froca.js";import utils from "./utils.js";@@ -141,14 +141,7 @@ async function createLink(notePath: string, options: CreateLinkOptions = {}) {return $container;}-interface CalculateHashOpts {- notePath: string;- ntxId?: string;- hoistedNoteId?: string;- viewScope: ViewScope;-}--function calculateHash({notePath, ntxId, hoistedNoteId, viewScope = {}}: CalculateHashOpts) {+function calculateHash({notePath, ntxId, hoistedNoteId, viewScope = {}}: NoteCommandData) {notePath = notePath || "";const params = [ntxId ? { ntxId: ntxId } : null,diff --git a/src/public/app/types.d.ts b/src/public/app/types.d.tsindex c6c540d83d..38870b375f 100644--- a/src/public/app/types.d.ts+++ b/src/public/app/types.d.ts@@ -93,6 +93,16 @@ declare global {})}+ interface JQueryStatic {+ hotkeys: {+ options: {+ filterInputAcceptingElements: boolean;+ filterContentEditable: boolean;+ filterTextInputs: boolean;+ }+ }+ }+var logError: (message: string, e?: Error) => void;var logInfo: (message: string) => void;var glob: CustomGlobals;