trilium/src/public/javascripts/services/entrypoints.js

265 lines
9.2 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import linkService from "./link.js";
import zoomService from "./zoom.js";
import protectedSessionService from "./protected_session.js";
import searchNotesService from "./search_notes.js";
import treeService from "./tree.js";
2019-11-20 03:53:04 +08:00
import server from "./server.js";
const NOTE_REVISIONS = "../dialogs/note_revisions.js";
const OPTIONS = "../dialogs/options.js";
const ADD_LINK = "../dialogs/add_link.js";
const JUMP_TO_NOTE = "../dialogs/jump_to_note.js";
const NOTE_SOURCE = "../dialogs/note_source.js";
const RECENT_CHANGES = "../dialogs/recent_changes.js";
const SQL_CONSOLE = "../dialogs/sql_console.js";
const ATTRIBUTES = "../dialogs/attributes.js";
const HELP = "../dialogs/help.js";
const NOTE_INFO = "../dialogs/note_info.js";
const ABOUT = "../dialogs/about.js";
const LINK_MAP = "../dialogs/link_map.js";
const CLONE_TO = "../dialogs/clone_to.js";
2019-11-12 05:57:51 +08:00
const MOVE_TO = "../dialogs/move_to.js";
function registerEntrypoints() {
// 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;
2019-11-20 06:02:54 +08:00
setActionHandler("AddLinkToText", () => import(ADD_LINK).then(d => d.showDialog()));
2019-11-20 06:02:54 +08:00
const showJumpToNoteDialog = () => import(JUMP_TO_NOTE).then(d => d.showDialog());
$("#jump-to-note-dialog-button").on('click', showJumpToNoteDialog);
setActionHandler("JumpToNote", showJumpToNoteDialog);
2019-11-20 06:02:54 +08:00
const showRecentChanges = () => import(RECENT_CHANGES).then(d => d.showDialog());
$("#recent-changes-button").on('click', showRecentChanges);
setActionHandler("ShowRecentChanges", showRecentChanges);
2019-05-06 00:24:59 +08:00
2019-11-10 00:39:48 +08:00
$("#enter-protected-session-button").on('click', protectedSessionService.enterProtectedSession);
$("#leave-protected-session-button").on('click', protectedSessionService.leaveProtectedSession);
2019-05-06 00:24:59 +08:00
2019-11-10 00:39:48 +08:00
$("#toggle-search-button").on('click', searchNotesService.toggleSearch);
2019-11-20 06:02:54 +08:00
setActionHandler('SearchNotes', searchNotesService.toggleSearch);
2019-05-06 00:24:59 +08:00
const $noteTabContainer = $("#note-tab-container");
2019-11-20 06:02:54 +08:00
const showAttributesDialog = () => import(ATTRIBUTES).then(d => d.showDialog());
$noteTabContainer.on("click", ".show-attributes-button", showAttributesDialog);
setActionHandler("ShowAttributes", showAttributesDialog);
2019-05-06 00:24:59 +08:00
2019-11-20 06:02:54 +08:00
const showNoteInfoDialog = () => import(NOTE_INFO).then(d => d.showDialog());
$noteTabContainer.on("click", ".show-note-info-button", showNoteInfoDialog);
setActionHandler("ShowNoteInfo", showNoteInfoDialog);
const showNoteRevisionsDialog = function() {
if ($(this).hasClass("disabled")) {
return;
}
2019-09-03 01:56:52 +08:00
import(NOTE_REVISIONS).then(d => d.showCurrentNoteRevisions());
2019-11-20 06:02:54 +08:00
};
2019-11-20 06:02:54 +08:00
$noteTabContainer.on("click", ".show-note-revisions-button", showNoteRevisionsDialog);
setActionHandler("ShowNoteRevisions", showNoteRevisionsDialog);
const showNoteSourceDialog = function() {
if ($(this).hasClass("disabled")) {
return;
}
import(NOTE_SOURCE).then(d => d.showDialog());
2019-11-20 06:02:54 +08:00
};
2019-11-20 06:02:54 +08:00
$noteTabContainer.on("click", ".show-source-button", showNoteSourceDialog);
setActionHandler("ShowNoteSource", showNoteSourceDialog);
const showLinkMapDialog = () => import(LINK_MAP).then(d => d.showDialog());
$noteTabContainer.on("click", ".show-link-map-button", showLinkMapDialog);
setActionHandler("ShowLinkMap", showLinkMapDialog);
2019-06-02 21:35:57 +08:00
2019-11-20 06:02:54 +08:00
const showOptionsDialog = () => import(OPTIONS).then(d => d.showDialog());
$("#options-button").on('click', showOptionsDialog);
setActionHandler("ShowOptions", showOptionsDialog);
2019-11-20 06:02:54 +08:00
const showHelpDialog = () => import(HELP).then(d => d.showDialog());
$("#show-help-button").on('click', showHelpDialog);
setActionHandler("ShowHelp", showHelpDialog);
2019-11-20 06:02:54 +08:00
const showSqlConsoleDialog = () => import(SQL_CONSOLE).then(d => d.showDialog());
$("#open-sql-console-button").on('click', showSqlConsoleDialog);
setActionHandler("ShowSQLConsole", showSqlConsoleDialog);
2019-11-10 00:39:48 +08:00
$("#show-about-dialog-button").on('click', () => import(ABOUT).then(d => d.showDialog()));
if (utils.isElectron()) {
$("#history-navigation").show();
2019-11-10 00:39:48 +08:00
$("#history-back-button").on('click', window.history.back);
2019-11-20 06:02:54 +08:00
setActionHandler("BackInNoteHistory", window.history.back);
2019-11-20 06:02:54 +08:00
$("#history-forward-button").on('click', window.history.forward);
setActionHandler("ForwardInNoteHistory", window.history.forward);
}
// hide (toggle) everything except for the note content for zen mode
2019-11-20 06:02:54 +08:00
const toggleZenMode = () => {
$(".hide-in-zen-mode").toggle();
$("#container").toggleClass("zen-mode");
2019-11-20 06:02:54 +08:00
};
2019-11-20 06:33:07 +08:00
$("#toggle-zen-mode-button").on('click', toggleZenMode);
2019-11-20 06:02:54 +08:00
setActionHandler("ToggleZenMode", toggleZenMode);
2019-11-20 06:02:54 +08:00
setActionHandler("InsertDateTime", () => {
const date = new Date();
const dateString = utils.formatDateTime(date);
linkService.addTextToEditor(dateString);
});
2019-11-10 00:39:48 +08:00
$("#reload-frontend-button").on('click', utils.reloadApp);
2019-11-20 06:02:54 +08:00
setActionHandler("ReloadApp", utils.reloadApp);
2019-02-10 02:25:55 +08:00
$("#open-dev-tools-button").toggle(utils.isElectron());
if (utils.isElectron()) {
const openDevTools = () => {
require('electron').remote.getCurrentWindow().toggleDevTools();
return false;
2019-02-10 02:25:55 +08:00
};
2019-11-10 00:39:48 +08:00
$("#open-dev-tools-button").on('click', openDevTools);
2019-11-20 06:02:54 +08:00
setActionHandler("OpenDevTools", openDevTools);
2019-02-10 02:25:55 +08:00
}
let findInPage;
if (utils.isElectron()) {
const { remote } = require('electron');
const { FindInPage } = require('electron-find');
findInPage = new FindInPage(remote.getCurrentWebContents(), {
offsetTop: 10,
offsetRight: 10,
boxBgColor: 'var(--main-background-color)',
boxShadowColor: '#000',
inputColor: 'var(--input-text-color)',
inputBgColor: 'var(--input-background-color)',
inputFocusColor: '#555',
textColor: 'var(--main-text-color)',
textHoverBgColor: '#555',
caseSelectedColor: 'var(--main-border-color)'
});
2019-11-20 06:02:54 +08:00
setActionHandler("FindInText", () => findInPage.openFindWindow());
}
if (utils.isElectron()) {
2019-11-20 06:02:54 +08:00
const toggleFullscreen = () => {
const win = require('electron').remote.getCurrentWindow();
if (win.isFullScreenable()) {
win.setFullScreen(!win.isFullScreen());
}
return false;
};
2019-11-10 00:39:48 +08:00
$("#toggle-fullscreen-button").on('click', toggleFullscreen);
2019-11-20 06:02:54 +08:00
setActionHandler("ToggleFullscreen", toggleFullscreen);
}
else {
// outside of electron this is handled by the browser
$("#toggle-fullscreen-button").hide();
}
if (utils.isElectron()) {
2019-11-20 06:02:54 +08:00
setActionHandler("ZoomOut", zoomService.decreaseZoomFactor);
setActionHandler("ZoomIn", zoomService.increaseZoomFactor);
}
$(document).on('click', "a[data-action='note-revision']", async event => {
const linkEl = $(event.target);
const noteId = linkEl.attr('data-note-path');
const noteRevisionId = linkEl.attr('data-note-revision-id');
const attributesDialog = await import("../dialogs/note_revisions.js");
attributesDialog.showNoteRevisionsDialog(noteId, noteRevisionId);
return false;
});
2019-11-20 06:02:54 +08:00
setActionHandler("CloneNotesTo", () => import(CLONE_TO).then(d => {
const activeNode = treeService.getActiveNode();
2019-11-12 05:57:51 +08:00
const selectedOrActiveNodes = treeService.getSelectedOrActiveNodes(activeNode);
const noteIds = selectedOrActiveNodes.map(node => node.data.noteId);
d.showDialog(noteIds);
}));
2019-11-12 05:57:51 +08:00
2019-11-20 06:02:54 +08:00
setActionHandler("MoveNotesTo", () => import(MOVE_TO).then(d => {
2019-11-12 05:57:51 +08:00
const activeNode = treeService.getActiveNode();
const selectedOrActiveNodes = treeService.getSelectedOrActiveNodes(activeNode);
d.showDialog(selectedOrActiveNodes);
}));
}
2019-11-20 03:53:04 +08:00
class KeyboardAction {
constructor(params) {
/** @property {string} */
2019-11-20 06:02:54 +08:00
this.actionName = params.actionName;
2019-11-20 03:53:04 +08:00
/** @property {string[]} */
2019-11-20 06:02:54 +08:00
this.defaultShortcuts = params.defaultShortcuts;
2019-11-20 03:53:04 +08:00
/** @property {string[]} */
2019-11-20 06:02:54 +08:00
this.effectiveShortcuts = params.effectiveShortcuts;
2019-11-20 03:53:04 +08:00
/** @property {string} */
this.description = params.description;
}
addShortcut(shortcut) {
2019-11-20 06:02:54 +08:00
this.effectiveShortcuts.push(shortcut);
2019-11-20 03:53:04 +08:00
}
/**
* @param {string|string[]} shortcuts
*/
replaceShortcuts(shortcuts) {
2019-11-20 06:02:54 +08:00
this.effectiveShortcuts = Array.isArray(shortcuts) ? shortcuts : [shortcuts];
2019-11-20 03:53:04 +08:00
}
}
2019-11-20 06:02:54 +08:00
const keyboardActionRepo = {};
2019-11-20 03:53:04 +08:00
2019-11-20 06:02:54 +08:00
const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => {
for (const action of actions) {
keyboardActionRepo[action.actionName] = new KeyboardAction(action);
2019-11-20 03:53:04 +08:00
}
});
2019-11-20 06:02:54 +08:00
function setActionHandler(actionName, handler) {
keyboardActionsLoaded.then(() => {
const action = keyboardActionRepo[actionName];
if (!action) {
throw new Error(`Cannot find keyboard action '${actionName}'`);
}
action.handler = handler;
for (const shortcut of action.effectiveShortcuts) {
utils.bindGlobalShortcut(shortcut, handler);
}
});
}
export default {
registerEntrypoints
}