From 0e9473119ebd0bcfd77eaf78e27ab33827b33586 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 12 Feb 2018 23:53:00 -0500 Subject: [PATCH] global keyboard shortcuts for quick creating sub-notes under day note --- electron.js | 36 +++++++++++++++++ src/public/javascripts/init.js | 20 +++++++++- src/public/javascripts/note_editor.js | 56 ++++++++++++++------------- src/public/javascripts/note_tree.js | 26 +++++++------ src/services/utils.js | 9 +++++ 5 files changed, 109 insertions(+), 38 deletions(-) diff --git a/electron.js b/electron.js index 7758b9a66..d66ae3824 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 reload require('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, "

"); + + 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.js index 5eb906804..966ac8575 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 file diff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.js index e243c9c0e..048566aee 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 : "

"); + + 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 : "

"); - - 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 file diff --git a/src/public/javascripts/note_tree.js b/src/public/javascripts/note_tree.js index 5e4ab44b7..e2a4bf3ba 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 file diff --git a/src/services/utils.js b/src/services/utils.js index 0e5ee19e5..5596ca933 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,