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
global keyboard shortcuts for quick creating sub-notes under day note
azivner0e9473119ebd
diff --git a/electron.js b/electron.jsindex 7758b9a66b..d66ae38244 100644--- a/electron.js+++ b/electron.js@@ -6,6 +6,8 @@ const config = require('./src/services/config');const url = require("url");const app = electron.app;+const globalShortcut = electron.globalShortcut;+const clipboard = electron.clipboard;// Adds debug features like hotkeys for triggering dev tools and reloadrequire('electron-debug')();@@ -67,6 +69,40 @@ app.on('activate', () => {app.on('ready', () => {mainWindow = createMainWindow();++ const date_notes = require('./src/services/date_notes');+ const utils = require('./src/services/utils');++ globalShortcut.register('CommandOrControl+Alt+P', async () => {+ const parentNoteId = await date_notes.getDateNoteId(utils.nowDate());++ // window may be hidden / not in focus+ mainWindow.focus();++ mainWindow.webContents.send('create-sub-note', JSON.stringify({+ parentNoteId: parentNoteId,+ content: ''+ }));+ });++ globalShortcut.register('CommandOrControl+Alt+C', async () => {+ const parentNoteId = await date_notes.getDateNoteId(utils.nowDate());++ // window may be hidden / not in focus+ mainWindow.focus();++ let content = clipboard.readText();+ content = content.replace(/(\r\n|\n)/g, "<p>");++ mainWindow.webContents.send('create-sub-note', JSON.stringify({+ parentNoteId: parentNoteId,+ content: content+ }));+ });+});++app.on('will-quit', () => {+ globalShortcut.unregisterAll();});require('./src/www');diff --git a/src/public/javascripts/init.js b/src/public/javascripts/init.jsindex 5eb9068042..966ac85758 100644--- a/src/public/javascripts/init.js+++ b/src/public/javascripts/init.js@@ -192,4 +192,22 @@ $(document).ready(() => {executeScript(script);}});-});\ No newline at end of file+});++if (isElectron()) {+ require('electron').ipcRenderer.on('create-sub-note', async function(event, message) {+ const {parentNoteId, content} = JSON.parse(message);++ if (!noteTree.noteExists(parentNoteId)) {+ await noteTree.reload();+ }++ await noteTree.activateNode(parentNoteId);++ const node = noteTree.getCurrentNode();++ await noteTree.createNote(node, node.data.noteId, 'into', node.data.isProtected);++ setTimeout(() => noteEditor.setContent(content), 1000);+ });+}\ No newline at end of filediff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.jsindex e243c9c0ef..048566aee4 100644--- a/src/public/javascripts/note_editor.js+++ b/src/public/javascripts/note_editor.js@@ -116,6 +116,32 @@ const noteEditor = (function() {isNewNoteCreated = true;}+ function setContent(content) {+ if (currentNote.detail.type === 'text') {+ // temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49+ editor.setData(content ? content : "<p></p>");++ noteDetailEl.show();+ noteDetailCodeEl.hide();+ noteDetailRenderEl.html('').hide();+ }+ else if (currentNote.detail.type === 'code') {+ noteDetailEl.hide();+ noteDetailCodeEl.show();+ noteDetailRenderEl.html('').hide();++ // this needs to happen after the element is shown, otherwise the editor won't be refresheds+ codeEditor.setValue(content);++ const info = CodeMirror.findModeByMIME(currentNote.detail.mime);++ if (info) {+ codeEditor.setOption("mode", info.mime);+ CodeMirror.autoLoadMode(codeEditor, info.mode);+ }+ }+ }+async function loadNoteToEditor(noteId) {currentNote = await loadNote(noteId);@@ -146,30 +172,7 @@ const noteEditor = (function() {noteType.setNoteType(currentNote.detail.type);noteType.setNoteMime(currentNote.detail.mime);- if (currentNote.detail.type === 'text') {- // temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49- editor.setData(currentNote.detail.content ? currentNote.detail.content : "<p></p>");-- noteDetailEl.show();- noteDetailCodeEl.hide();- noteDetailRenderEl.html('').hide();- }- else if (currentNote.detail.type === 'code') {- noteDetailEl.hide();- noteDetailCodeEl.show();- noteDetailRenderEl.html('').hide();-- // this needs to happen after the element is shown, otherwise the editor won't be refresheds- codeEditor.setValue(currentNote.detail.content);-- const info = CodeMirror.findModeByMIME(currentNote.detail.mime);-- if (info) {- codeEditor.setOption("mode", info.mime);- CodeMirror.autoLoadMode(codeEditor, info.mode);- }- }- else if (currentNote.detail.type === 'render') {+ if (currentNote.detail.type === 'render') {noteDetailEl.hide();noteDetailCodeEl.hide();noteDetailRenderEl.html('').show();@@ -179,7 +182,7 @@ const noteEditor = (function() {noteDetailRenderEl.html(subTree);}else {- throwError("Unrecognized type " + currentNote.detail.type);+ setContent(currentNote.detail.content);}noteChangeDisabled = false;@@ -314,6 +317,7 @@ const noteEditor = (function() {getEditor,focus,executeCurrentNote,- loadAttributeList+ loadAttributeList,+ setContent};})();\ No newline at end of filediff --git a/src/public/javascripts/note_tree.js b/src/public/javascripts/note_tree.jsindex 5e4ab44b7a..e2a4bf3ba1 100644--- a/src/public/javascripts/note_tree.js+++ b/src/public/javascripts/note_tree.js@@ -640,16 +640,15 @@ const noteTree = (function() {return document.location.hash.substr(1); // strip initial #}- function loadTree() {- return server.get('tree').then(resp => {- startNotePath = resp.start_note_path;+ async function loadTree() {+ const resp = await server.get('tree');+ startNotePath = resp.start_note_path;- if (document.location.hash) {- startNotePath = getNotePathFromAddress();- }+ if (document.location.hash) {+ startNotePath = getNotePathFromAddress();+ }- return prepareNoteTree(resp.notes);- });+ return prepareNoteTree(resp.notes);}$(() => loadTree().then(noteTree => initFancyTree(noteTree)));@@ -775,7 +774,7 @@ const noteTree = (function() {};if (target === 'after') {- node.appendSibling(newNode).setActive(true);+ await node.appendSibling(newNode).setActive(true);}else if (target === 'into') {if (!node.getChildren() && node.isFolder()) {@@ -785,7 +784,7 @@ const noteTree = (function() {node.addChildren(newNode);}- node.getLastChild().setActive(true);+ await node.getLastChild().setActive(true);node.folder = true;node.renderTitle();@@ -803,6 +802,10 @@ const noteTree = (function() {await reload();}+ function noteExists(noteId) {+ return !!childToParents[noteId];+ }+$(document).bind('keydown', 'ctrl+o', e => {const node = getCurrentNode();const parentNoteId = node.data.parentNoteId;@@ -876,6 +879,7 @@ const noteTree = (function() {removeParentChildRelation,setParentChildRelation,getSelectedNodes,- sortAlphabetically+ sortAlphabetically,+ noteExists};})();\ No newline at end of filediff --git a/src/services/utils.js b/src/services/utils.jsindex 0e5ee19e5c..5596ca9330 100644--- a/src/services/utils.js+++ b/src/services/utils.js@@ -43,6 +43,14 @@ function nowDate() {return dateStr(new Date());}+function localDate() {+ const date = new Date();++ return date.getFullYear() + "-"+ + (date.getMonth() < 9 ? "0" : "") + (date.getMonth() + 1) + "-"+ + (date.getDate() < 10 ? "0" : "") + date.getDate();+}+function dateStr(date) {return date.toISOString();}@@ -125,6 +133,7 @@ module.exports = {randomSecureToken,randomString,nowDate,+ localDate,dateStr,parseDate,parseDateTime,