more reliable reimplementation of "create day sub note"

This commit is contained in:
zadam 2019-04-14 12:18:52 +02:00
parent 2b4413a1bd
commit e1e020c1a4
8 changed files with 81 additions and 21 deletions

View File

@ -97,15 +97,10 @@ app.on('ready', async () => {
mainWindow = await createMainWindow();
const result = globalShortcut.register('CommandOrControl+Alt+P', cls.wrap(async () => {
const dateNoteService = require('./src/services/date_notes');
const dateUtils = require('./src/services/date_utils');
const parentNote = await dateNoteService.getDateNote(dateUtils.localNowDate());
// window may be hidden / not in focus
mainWindow.focus();
mainWindow.webContents.send('create-day-sub-note', parentNote.noteId);
mainWindow.webContents.send('create-day-sub-note');
}));
if (!result) {

View File

@ -39,6 +39,7 @@ import linkService from './services/link.js';
import noteAutocompleteService from './services/note_autocomplete.js';
import macInit from './services/mac_init.js';
import cssLoader from './services/css_loader.js';
import dateNoteService from './services/date_notes.js';
// required for CKEditor image upload plugin
window.glob.getActiveNode = treeService.getActiveNode;
@ -114,20 +115,14 @@ $("body").on("click", "a.external", function () {
});
if (utils.isElectron()) {
require('electron').ipcRenderer.on('create-day-sub-note', async function(event, parentNoteId) {
await treeService.activateNote(parentNoteId);
require('electron').ipcRenderer.on('create-day-sub-note', async function(event) {
const todayNote = await dateNoteService.getTodayNote();
const node = await treeService.expandToNote(todayNote.noteId);
setTimeout(async () => {
const parentNode = treeService.getActiveNode();
const {note} = await treeService.createNote(parentNode, parentNode.data.noteId, 'into', {
type: "text",
isProtected: parentNode.data.isProtected
});
await treeService.activateNote(note.noteId);
}, 500);
await treeService.createNote(node, todayNote.noteId, 'into', {
type: "text",
isProtected: node.data.isProtected
});
});
}

View File

@ -3,7 +3,6 @@ import utils from './utils.js';
import server from './server.js';
import infoService from "./info.js";
import treeCache from "./tree_cache.js";
import treeBuilder from "./tree_builder.js";
async function moveBeforeNode(nodesToMove, beforeNode) {
nodesToMove = filterRootNote(nodesToMove);

View File

@ -0,0 +1,41 @@
import treeCache from "./tree_cache.js";
import server from "./server.js";
/** @return {NoteShort} */
async function getTodayNote() {
return await getDateNote(dayjs().format("YYYY-MM-DD"));
}
/** @return {NoteShort} */
async function getDateNote(date) {
const note = await server.get('date-notes/date/' + date);
await treeCache.reloadParents(note.noteId);
return await treeCache.getNote(note.noteId);
}
/** @return {NoteShort} */
async function getMonthNote(month) {
const note = await server.get('date-notes/month/' + month);
await treeCache.reloadParents(note.noteId);
return await treeCache.getNote(note.noteId);
}
/** @return {NoteShort} */
async function getYearNote(year) {
const note = await server.get('date-notes/year/' + year);
await treeCache.reloadParents(note.noteId);
return await treeCache.getNote(note.noteId);
}
export default {
getTodayNote,
getDateNote,
getMonthNote,
getYearNote
}

View File

@ -25,6 +25,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) {
/** @property {object|null} entity whose event triggered this execution */
this.originEntity = originEntity;
// to keep consistency with backend API
this.dayjs = dayjs;
/**
* Activates note in the tree and in the note detail.
*

View File

@ -817,5 +817,6 @@ export default {
getNodesByNoteId,
checkFolderStatus,
reloadNote,
loadTreeCache
loadTreeCache,
expandToNote
};

View File

@ -0,0 +1,21 @@
"use strict";
const dateNoteService = require('../../services/date_notes');
async function getDateNote(req) {
return await dateNoteService.getDateNote(req.params.date);
}
async function getMonthNote(req) {
return await dateNoteService.getMonthNote(req.params.month);
}
async function getYearNote(req) {
return await dateNoteService.getYearNote(req.params.year);
}
module.exports = {
getDateNote,
getMonthNote,
getYearNote
};

View File

@ -30,6 +30,7 @@ const scriptRoute = require('./api/script');
const senderRoute = require('./api/sender');
const filesRoute = require('./api/file_upload');
const searchRoute = require('./api/search');
const dateNotesRoute = require('./api/date_notes');
const log = require('../services/log');
const express = require('express');
@ -153,6 +154,10 @@ function register(app) {
apiRoute(GET, '/api/attributes/names', attributesRoute.getAttributeNames);
apiRoute(GET, '/api/attributes/values/:attributeName', attributesRoute.getValuesForAttribute);
apiRoute(GET, '/api/date-notes/date/:date', dateNotesRoute.getDateNote);
apiRoute(GET, '/api/date-notes/month/:month', dateNotesRoute.getMonthNote);
apiRoute(GET, '/api/date-notes/year/:year', dateNotesRoute.getYearNote);
route(GET, '/api/images/:noteId/:filename', [auth.checkApiAuthOrElectron], imageRoute.returnImage);
route(POST, '/api/images', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], imageRoute.uploadImage, apiResultHandler);