mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	global keyboard shortcuts for quick creating sub-notes under day note
This commit is contained in:
		
							parent
							
								
									7bbfef7af3
								
							
						
					
					
						commit
						0e9473119e
					
				
							
								
								
									
										36
									
								
								electron.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								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, "<p>");
 | 
			
		||||
 | 
			
		||||
        mainWindow.webContents.send('create-sub-note', JSON.stringify({
 | 
			
		||||
            parentNoteId: parentNoteId,
 | 
			
		||||
            content: content
 | 
			
		||||
        }));
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
app.on('will-quit', () => {
 | 
			
		||||
    globalShortcut.unregisterAll();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
require('./src/www');
 | 
			
		||||
 | 
			
		||||
@ -193,3 +193,21 @@ $(document).ready(() => {
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
@ -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
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
@ -640,8 +640,8 @@ const noteTree = (function() {
 | 
			
		||||
        return document.location.hash.substr(1); // strip initial #
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function loadTree() {
 | 
			
		||||
        return server.get('tree').then(resp => {
 | 
			
		||||
    async function loadTree() {
 | 
			
		||||
        const resp = await server.get('tree');
 | 
			
		||||
        startNotePath = resp.start_note_path;
 | 
			
		||||
 | 
			
		||||
        if (document.location.hash) {
 | 
			
		||||
@ -649,7 +649,6 @@ const noteTree = (function() {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
@ -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,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user