Notes/src/public/app/services/entrypoints.js

205 lines
6.5 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import dateNoteService from "./date_notes.js";
import protectedSessionHolder from './protected_session_holder.js';
import server from "./server.js";
2020-01-12 11:15:23 +01:00
import appContext from "./app_context.js";
2020-01-21 22:54:16 +01:00
import Component from "../widgets/component.js";
2020-02-28 00:11:34 +01:00
import toastService from "./toast.js";
2020-03-17 21:39:26 +01:00
import ws from "./ws.js";
import bundleService from "./bundle.js";
import froca from "./froca.js";
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();
2019-11-19 20:53:04 +01:00
2020-11-27 22:21:13 +01:00
const {note} = await server.post(`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();
2021-05-22 12:35:41 +02:00
const hoistedNoteId = appContext.tabManager.getActiveContext()
? appContext.tabManager.getActiveContext().hoistedNoteId
: 'root';
2021-05-22 12:35:41 +02:00
await appContext.tabManager.openContextWithNote(note.noteId, true, null, hoistedNoteId);
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()}) {
const noteToHoist = await froca.getNote(noteId);
const activeNoteContext = appContext.tabManager.getActiveContext();
if (noteToHoist.noteId === activeNoteContext.hoistedNoteId) {
await activeNoteContext.unhoist();
2020-02-10 20:57:56 +01:00
}
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
2020-11-29 22:32:31 +01:00
async hoistNoteCommand({noteId}) {
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());
}
}
else {
// outside of electron this is handled by the browser
this.$widget.find(".toggle-fullscreen-button").hide();
}
}
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">')
.append($(`<input type="hidden" name="_csrf" value="${glob.csrfToken}"/>`));
$("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.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.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}) {
if (!hoistedNoteId) {
hoistedNoteId = 'root';
}
if (utils.isElectron()) {
const {ipcRenderer} = utils.dynamicRequire('electron');
ipcRenderer.send('create-extra-window', {notePath, hoistedNoteId});
}
else {
const url = window.location.protocol + '//' + window.location.host + window.location.pathname + '?extra=1#' + notePath;
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();
// 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') {
2021-12-21 13:22:13 +01:00
const resp = await server.post("sql/execute/" + note.noteId);
2021-12-21 13:22:13 +01:00
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError("Error occurred while executing SQL query: " + resp.message);
2021-12-21 13:22:13 +01:00
}
await appContext.triggerEvent('sqlQueryResults', {ntxId: ntxId, results: resp.results});
}
toastService.showMessage("Note executed");
}
hideAllTooltips() {
$(".tooltip").removeClass("show");
}
2021-05-22 12:35:41 +02:00
noteSwitchedEvent() {
this.hideAllTooltips();
}
2021-05-22 17:58:46 +02:00
activeContextChangedEvent() {
this.hideAllTooltips();
}
2020-01-22 19:41:19 +01:00
}