Notes/src/public/app/components/entrypoints.ts

230 lines
7.4 KiB
TypeScript
Raw Normal View History

2022-12-01 13:07:23 +01:00
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, { 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";
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;
}
2020-01-21 22:54:16 +01:00
export default class Entrypoints extends Component {
2020-02-27 10:03:14 +01:00
constructor() {
super();
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;
}
}
openDevToolsCommand() {
2020-01-21 22:54:16 +01:00
if (utils.isElectron()) {
2021-11-16 22:43:08 +01:00
utils.dynamicRequire('@electron/remote').getCurrentWindow().toggleDevTools();
2020-01-21 22:54:16 +01:00
}
}
2020-11-27 22:21:13 +01:00
async createNoteIntoInboxCommand() {
const inboxNote = await dateNoteService.getInboxNote();
if (!inboxNote) {
console.warn("Missing inbox note.");
return;
}
2019-11-19 20:53:04 +01:00
const {note} = await server.post<CreateChildrenResponse>(`notes/${inboxNote.noteId}/children?target=into`, {
2020-03-18 10:08:16 +01:00
content: '',
type: 'text',
isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()
});
await ws.waitForMaxKnownEntityChangeId();
2023-04-03 23:47:24 +02:00
await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, {activate: true});
2019-12-28 10:28:12 +01:00
appContext.triggerEvent('focusAndSelectTitle', {isNewNote: true});
2020-01-21 22:54:16 +01:00
}
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) {
await activeNoteContext.unhoist();
} else if (noteToHoist?.type !== 'search') {
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
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
}
}
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
}
}
copyWithoutFormattingCommand() {
2020-01-21 22:54:16 +01:00
utils.copySelectionToClipboard();
}
2020-01-22 19:41:19 +01:00
toggleFullscreenCommand() {
2020-01-22 19:41:19 +01:00
if (utils.isElectron()) {
2021-11-16 22:43:08 +01: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
}
reloadFrontendAppCommand() {
2021-08-24 22:59:51 +02:00
utils.reloadFrontendApp();
2020-01-22 19:41:19 +01:00
}
logoutCommand() {
2020-01-22 19:41:19 +01:00
const $logoutForm = $('<form action="logout" method="POST">')
2022-12-21 16:11:00 +01:00
.append($(`<input type='_hidden' name="_csrf" value="${glob.csrfToken}"/>`));
2020-01-22 19:41:19 +01:00
$("body").append($logoutForm);
$logoutForm.trigger('submit');
}
backInNoteHistoryCommand() {
if (utils.isElectron()) {
2020-03-17 21:39:26 +01:00
// standard JS version does not work completely correctly in electron
2021-11-16 22:43:08 +01:00
const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
webContents.goToIndex(activeIndex - 1);
}
else {
window.history.back();
}
2020-01-22 19:41:19 +01:00
}
2020-03-08 17:17:18 +01:00
forwardInNoteHistoryCommand() {
if (utils.isElectron()) {
2020-03-17 21:39:26 +01:00
// standard JS version does not work completely correctly in electron
2021-11-16 22:43:08 +01:00
const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
webContents.goToIndex(activeIndex + 1);
}
else {
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() {
utils.setCookie('trilium-device', 'desktop');
2021-09-17 22:34:23 +02:00
utils.reloadFrontendApp("Switching to desktop version");
2020-03-01 11:53:02 +01:00
}
async switchToMobileVersionCommand() {
utils.setCookie('trilium-device', 'mobile');
utils.reloadFrontendApp("Switching to mobile version");
}
async openInWindowCommand({notePath, hoistedNoteId, viewScope}: NoteCommandData) {
const extraWindowHash = linkService.calculateHash({notePath, hoistedNoteId, viewScope});
if (utils.isElectron()) {
const {ipcRenderer} = utils.dynamicRequire('electron');
ipcRenderer.send('create-extra-window', { extraWindowHash });
}
else {
const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}?extraWindow=1${extraWindowHash}`;
window.open(url, '', 'width=1000,height=800');
}
}
async openNewWindowCommand() {
this.openInWindowCommand({notePath: '', hoistedNoteId: 'root'});
}
async runActiveNoteCommand() {
2021-07-27 22:08:41 +02:00
const {ntxId, note} = appContext.tabManager.getActiveContext();
2023-05-05 23:41:11 +02:00
// ctrl+enter is also used elsewhere, so make sure we're running only when appropriate
if (!note || note.type !== 'code') {
return;
}
2021-05-07 21:23:10 +02:00
// TODO: use note.executeScript()
if (note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(note.noteId);
} 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<SqlExecuteResponse>(`sql/execute/${note.noteId}`);
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
}
await appContext.triggerEvent('sqlQueryResults', {ntxId: ntxId, results: resp.results});
}
2024-10-20 02:06:08 +03:00
toastService.showMessage(t("entrypoints.note-executed"));
}
2022-12-13 16:57:46 +01:00
hideAllPopups() {
if (utils.isDesktop()) {
$(".aa-input").autocomplete("close");
}
}
2021-05-22 12:35:41 +02:00
noteSwitchedEvent() {
2022-12-13 16:57:46 +01:00
this.hideAllPopups();
}
2021-05-22 17:58:46 +02:00
activeContextChangedEvent() {
2022-12-13 16:57:46 +01:00
this.hideAllPopups();
}
async forceSaveRevisionCommand() {
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"));
}
2020-01-22 19:41:19 +01:00
}