2022-12-01 13:07:23 +01:00
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import dateNoteService from "../services/date_notes.js";
|
2025-01-09 18:07:02 +02:00
|
|
|
import protectedSessionHolder from "../services/protected_session_holder.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import server from "../services/server.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import appContext, { type NoteCommandData } from "./app_context.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import Component from "./component.js";
|
|
|
|
import toastService from "../services/toast.js";
|
|
|
|
import ws from "../services/ws.js";
|
|
|
|
import bundleService from "../services/bundle.js";
|
|
|
|
import froca from "../services/froca.js";
|
2023-04-11 21:41:55 +02:00
|
|
|
import linkService from "../services/link.js";
|
2024-10-23 20:33:55 +03:00
|
|
|
import { t } from "../services/i18n.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type FNote from "../entities/fnote.js";
|
2024-12-23 14:10:57 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2019-08-20 21:40:47 +02:00
|
|
|
|
2020-01-21 22:54:16 +01:00
|
|
|
export default class Entrypoints extends Component {
|
2020-02-27 10:03:14 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2020-03-01 11:04:42 +01:00
|
|
|
if (jQuery.hotkeys) {
|
|
|
|
// hot keys are active also inside inputs and content editables
|
|
|
|
jQuery.hotkeys.options.filterInputAcceptingElements = false;
|
|
|
|
jQuery.hotkeys.options.filterContentEditable = false;
|
|
|
|
jQuery.hotkeys.options.filterTextInputs = false;
|
|
|
|
}
|
2018-12-10 20:44:50 +01:00
|
|
|
}
|
2019-04-28 21:52:25 +02:00
|
|
|
|
2020-02-16 19:54:11 +01:00
|
|
|
openDevToolsCommand() {
|
2020-01-21 22:54:16 +01:00
|
|
|
if (utils.isElectron()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
utils.dynamicRequire("@electron/remote").getCurrentWindow().toggleDevTools();
|
2020-01-21 22:54:16 +01:00
|
|
|
}
|
2018-06-02 13:02:20 -04:00
|
|
|
}
|
2019-10-10 20:00:06 +02:00
|
|
|
|
2020-11-27 22:21:13 +01:00
|
|
|
async createNoteIntoInboxCommand() {
|
|
|
|
const inboxNote = await dateNoteService.getInboxNote();
|
2024-12-23 14:10:57 +02:00
|
|
|
if (!inboxNote) {
|
|
|
|
console.warn("Missing inbox note.");
|
|
|
|
return;
|
|
|
|
}
|
2019-11-19 20:53:04 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const { note } = await server.post<CreateChildrenResponse>(`notes/${inboxNote.noteId}/children?target=into`, {
|
|
|
|
content: "",
|
|
|
|
type: "text",
|
2020-12-21 23:00:39 +01:00
|
|
|
isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()
|
2019-11-21 21:12:07 +01:00
|
|
|
});
|
2019-11-24 21:40:50 +01:00
|
|
|
|
2020-08-02 23:27:48 +02:00
|
|
|
await ws.waitForMaxKnownEntityChangeId();
|
2019-12-20 20:13:21 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, { activate: true });
|
2019-12-28 10:28:12 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });
|
2020-01-21 22:54:16 +01:00
|
|
|
}
|
2019-11-23 23:06:25 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async toggleNoteHoistingCommand({ noteId = appContext.tabManager.getActiveContextNoteId() }) {
|
2025-03-03 23:34:54 +02:00
|
|
|
if (!noteId) {
|
2024-12-23 14:10:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:19:16 +01:00
|
|
|
const noteToHoist = await froca.getNote(noteId);
|
2025-03-03 23:34:54 +02:00
|
|
|
const activeNoteContext = appContext.tabManager.getActiveContext();
|
2019-11-23 23:06:25 +01:00
|
|
|
|
2024-12-23 14:10:57 +02:00
|
|
|
if (noteToHoist?.noteId === activeNoteContext.hoistedNoteId) {
|
2022-11-08 22:19:16 +01:00
|
|
|
await activeNoteContext.unhoist();
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (noteToHoist?.type !== "search") {
|
2022-11-08 22:19:16 +01:00
|
|
|
await activeNoteContext.setHoistedNoteId(noteId);
|
2020-02-10 20:57:56 +01:00
|
|
|
}
|
2020-01-21 22:54:16 +01:00
|
|
|
}
|
2019-11-19 23:02:54 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async hoistNoteCommand({ noteId }: { noteId: string }) {
|
2021-05-22 12:35:41 +02:00
|
|
|
const noteContext = appContext.tabManager.getActiveContext();
|
2020-11-29 22:32:31 +01:00
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
if (noteContext.hoistedNoteId !== noteId) {
|
|
|
|
await noteContext.setHoistedNoteId(noteId);
|
2020-11-29 22:32:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:41:55 +02:00
|
|
|
async unhoistCommand() {
|
2021-05-22 12:35:41 +02:00
|
|
|
const activeNoteContext = appContext.tabManager.getActiveContext();
|
2020-11-23 22:52:48 +01:00
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
if (activeNoteContext) {
|
|
|
|
activeNoteContext.unhoist();
|
2020-11-23 22:52:48 +01:00
|
|
|
}
|
2020-10-13 23:41:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 19:54:11 +01:00
|
|
|
copyWithoutFormattingCommand() {
|
2020-01-21 22:54:16 +01:00
|
|
|
utils.copySelectionToClipboard();
|
|
|
|
}
|
2020-01-22 19:41:19 +01:00
|
|
|
|
2020-02-16 19:54:11 +01:00
|
|
|
toggleFullscreenCommand() {
|
2020-01-22 19:41:19 +01:00
|
|
|
if (utils.isElectron()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const win = utils.dynamicRequire("@electron/remote").getCurrentWindow();
|
2020-01-22 19:41:19 +01:00
|
|
|
|
|
|
|
if (win.isFullScreenable()) {
|
|
|
|
win.setFullScreen(!win.isFullScreen());
|
|
|
|
}
|
2022-12-14 10:29:14 +01:00
|
|
|
} // outside of electron this is handled by the browser
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 19:54:11 +01:00
|
|
|
reloadFrontendAppCommand() {
|
2021-08-24 22:59:51 +02:00
|
|
|
utils.reloadFrontendApp();
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|
|
|
|
|
2025-01-12 11:43:41 +01:00
|
|
|
async logoutCommand() {
|
|
|
|
await server.post("../logout");
|
|
|
|
window.location.replace(`/login`);
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 19:54:11 +01:00
|
|
|
backInNoteHistoryCommand() {
|
2020-03-17 21:15:57 +01:00
|
|
|
if (utils.isElectron()) {
|
2020-03-17 21:39:26 +01:00
|
|
|
// standard JS version does not work completely correctly in electron
|
2025-01-09 18:07:02 +02:00
|
|
|
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
|
2024-08-06 18:44:19 +03:00
|
|
|
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
|
2020-03-17 21:15:57 +01:00
|
|
|
|
|
|
|
webContents.goToIndex(activeIndex - 1);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2020-03-17 21:15:57 +01:00
|
|
|
window.history.back();
|
|
|
|
}
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 17:17:18 +01:00
|
|
|
forwardInNoteHistoryCommand() {
|
2020-03-17 21:15:57 +01:00
|
|
|
if (utils.isElectron()) {
|
2020-03-17 21:39:26 +01:00
|
|
|
// standard JS version does not work completely correctly in electron
|
2025-01-09 18:07:02 +02:00
|
|
|
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
|
2024-08-06 18:44:19 +03:00
|
|
|
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
|
2020-03-17 21:15:57 +01:00
|
|
|
|
|
|
|
webContents.goToIndex(activeIndex + 1);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2020-03-17 21:15:57 +01:00
|
|
|
window.history.forward();
|
|
|
|
}
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|
2020-02-28 00:11:34 +01:00
|
|
|
|
2020-03-01 11:53:02 +01:00
|
|
|
async switchToDesktopVersionCommand() {
|
2025-01-09 18:07:02 +02:00
|
|
|
utils.setCookie("trilium-device", "desktop");
|
2020-03-01 11:53:02 +01:00
|
|
|
|
2021-09-17 22:34:23 +02:00
|
|
|
utils.reloadFrontendApp("Switching to desktop version");
|
2020-03-01 11:53:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-31 16:56:23 +01:00
|
|
|
async switchToMobileVersionCommand() {
|
2025-01-09 18:07:02 +02:00
|
|
|
utils.setCookie("trilium-device", "mobile");
|
2021-10-31 16:56:23 +01:00
|
|
|
|
|
|
|
utils.reloadFrontendApp("Switching to mobile version");
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async openInWindowCommand({ notePath, hoistedNoteId, viewScope }: NoteCommandData) {
|
|
|
|
const extraWindowHash = linkService.calculateHash({ notePath, hoistedNoteId, viewScope });
|
2021-02-07 21:27:09 +01:00
|
|
|
|
2020-05-05 19:30:03 +02:00
|
|
|
if (utils.isElectron()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const { ipcRenderer } = utils.dynamicRequire("electron");
|
2020-05-05 19:30:03 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ipcRenderer.send("create-extra-window", { extraWindowHash });
|
|
|
|
} else {
|
2023-04-11 21:41:55 +02:00
|
|
|
const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}?extraWindow=1${extraWindowHash}`;
|
2020-05-05 19:30:03 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
window.open(url, "", "width=1000,height=800");
|
2020-05-05 19:30:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async openNewWindowCommand() {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.openInWindowCommand({ notePath: "", hoistedNoteId: "root" });
|
2020-05-05 19:30:03 +02:00
|
|
|
}
|
2020-05-05 23:58:52 +02:00
|
|
|
|
|
|
|
async runActiveNoteCommand() {
|
2025-03-03 23:34:54 +02:00
|
|
|
const { ntxId, note } = appContext.tabManager.getActiveContext();
|
2020-05-05 23:58:52 +02:00
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// ctrl+enter is also used elsewhere, so make sure we're running only when appropriate
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!note || note.type !== "code") {
|
2020-05-05 23:58:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:23:10 +02:00
|
|
|
// TODO: use note.executeScript()
|
2020-05-05 23:58:52 +02:00
|
|
|
if (note.mime.endsWith("env=frontend")) {
|
|
|
|
await bundleService.getAndExecuteBundle(note.noteId);
|
2020-12-29 22:27:31 +01:00
|
|
|
} else if (note.mime.endsWith("env=backend")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
await server.post(`script/run/${note.noteId}`);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (note.mime === "text/x-sqlite;schema=trilium") {
|
2024-12-23 14:10:57 +02:00
|
|
|
const resp = await server.post<SqlExecuteResponse>(`sql/execute/${note.noteId}`);
|
2020-12-29 22:27:31 +01:00
|
|
|
|
2021-12-21 13:22:13 +01:00
|
|
|
if (!resp.success) {
|
2024-10-20 02:19:47 +03:00
|
|
|
toastService.showError(t("entrypoints.sql-error", { message: resp.error }));
|
2021-12-21 13:22:13 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
await appContext.triggerEvent("sqlQueryResults", { ntxId: ntxId, results: resp.results });
|
2020-05-05 23:58:52 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 02:06:08 +03:00
|
|
|
toastService.showMessage(t("entrypoints.note-executed"));
|
2020-05-05 23:58:52 +02:00
|
|
|
}
|
2020-09-05 21:51:00 +02:00
|
|
|
|
2022-12-13 16:57:46 +01:00
|
|
|
hideAllPopups() {
|
|
|
|
if (utils.isDesktop()) {
|
|
|
|
$(".aa-input").autocomplete("close");
|
|
|
|
}
|
2020-09-05 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 12:35:41 +02:00
|
|
|
noteSwitchedEvent() {
|
2022-12-13 16:57:46 +01:00
|
|
|
this.hideAllPopups();
|
2020-09-05 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 17:58:46 +02:00
|
|
|
activeContextChangedEvent() {
|
2022-12-13 16:57:46 +01:00
|
|
|
this.hideAllPopups();
|
2020-09-05 21:51:00 +02:00
|
|
|
}
|
2022-11-08 22:36:15 +01:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
async forceSaveRevisionCommand() {
|
2022-11-08 22:36:15 +01:00
|
|
|
const noteId = appContext.tabManager.getActiveContextNoteId();
|
|
|
|
|
|
|
|
await server.post(`notes/${noteId}/revision`);
|
|
|
|
|
2024-10-20 02:06:08 +03:00
|
|
|
toastService.showMessage(t("entrypoints.note-revision-created"));
|
2022-11-08 22:36:15 +01:00
|
|
|
}
|
2020-01-22 19:41:19 +01:00
|
|
|
}
|