mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-03 08:35:29 +02:00
250 lines
5.9 KiB
JavaScript
250 lines
5.9 KiB
JavaScript
require('app-module-path').addPath(__dirname);
|
|
|
|
const { BaseApplication } = require('lib/BaseApplication');
|
|
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
|
|
const { Setting } = require('lib/models/setting.js');
|
|
const { BaseModel } = require('lib/base-model.js');
|
|
const { _ } = require('lib/locale.js');
|
|
const os = require('os');
|
|
const fs = require('fs-extra');
|
|
const { Logger } = require('lib/logger.js');
|
|
const { Tag } = require('lib/models/tag.js');
|
|
const { reg } = require('lib/registry.js');
|
|
const { sprintf } = require('sprintf-js');
|
|
const { JoplinDatabase } = require('lib/joplin-database.js');
|
|
const { DatabaseDriverNode } = require('lib/database-driver-node.js');
|
|
const { ElectronAppWrapper } = require('./ElectronAppWrapper');
|
|
const { defaultState } = require('lib/reducer.js');
|
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
|
const Menu = bridge().Menu;
|
|
const MenuItem = bridge().MenuItem;
|
|
|
|
const appDefaultState = Object.assign({}, defaultState, {
|
|
route: {
|
|
type: 'NAV_GO',
|
|
routeName: 'Main',
|
|
props: {},
|
|
},
|
|
navHistory: [],
|
|
fileToImport: null,
|
|
windowCommand: null,
|
|
});
|
|
|
|
class Application extends BaseApplication {
|
|
|
|
constructor() {
|
|
super();
|
|
this.lastMenuScreen_ = null;
|
|
}
|
|
|
|
hasGui() {
|
|
return true;
|
|
}
|
|
|
|
reducer(state = appDefaultState, action) {
|
|
let newState = state;
|
|
|
|
try {
|
|
switch (action.type) {
|
|
|
|
case 'NAV_BACK':
|
|
case 'NAV_GO':
|
|
|
|
const goingBack = action.type === 'NAV_BACK';
|
|
|
|
if (goingBack && !state.navHistory.length) break;
|
|
|
|
const currentRoute = state.route;
|
|
|
|
newState = Object.assign({}, state);
|
|
let newNavHistory = state.navHistory.slice();
|
|
|
|
if (goingBack) {
|
|
let newAction = null;
|
|
while (newNavHistory.length) {
|
|
newAction = newNavHistory.pop();
|
|
if (newAction.routeName !== state.route.routeName) break;
|
|
}
|
|
|
|
if (!newAction) break;
|
|
|
|
action = newAction;
|
|
}
|
|
|
|
if (!goingBack) newNavHistory.push(currentRoute);
|
|
newState.navHistory = newNavHistory
|
|
newState.route = action;
|
|
break;
|
|
|
|
case 'WINDOW_CONTENT_SIZE_SET':
|
|
|
|
newState = Object.assign({}, state);
|
|
newState.windowContentSize = action.size;
|
|
break;
|
|
|
|
case 'WINDOW_COMMAND':
|
|
|
|
newState = Object.assign({}, state);
|
|
let command = Object.assign({}, action);
|
|
delete command.type;
|
|
newState.windowCommand = command;
|
|
break;
|
|
|
|
}
|
|
} catch (error) {
|
|
error.message = 'In reducer: ' + error.message + ' Action: ' + JSON.stringify(action);
|
|
throw error;
|
|
}
|
|
|
|
return super.reducer(newState, action);
|
|
}
|
|
|
|
async generalMiddleware(store, next, action) {
|
|
if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
|
|
if (!await reg.syncStarted()) reg.scheduleSync();
|
|
}
|
|
|
|
const result = await super.generalMiddleware(store, next, action);
|
|
const newState = store.getState();
|
|
|
|
if (action.type === 'NAV_GO' || action.type === 'NAV_BACK') {
|
|
app().updateMenu(newState.route.routeName);
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
updateMenu(screen) {
|
|
if (this.lastMenuScreen_ === screen) return;
|
|
|
|
const template = [
|
|
{
|
|
label: 'File',
|
|
submenu: [{
|
|
label: _('New note'),
|
|
accelerator: 'CommandOrControl+N',
|
|
screens: ['Main'],
|
|
click: () => {
|
|
this.dispatch({
|
|
type: 'WINDOW_COMMAND',
|
|
name: 'newNote',
|
|
});
|
|
}
|
|
}, {
|
|
label: _('New to-do'),
|
|
accelerator: 'CommandOrControl+T',
|
|
screens: ['Main'],
|
|
click: () => {
|
|
this.dispatch({
|
|
type: 'WINDOW_COMMAND',
|
|
name: 'newTodo',
|
|
});
|
|
}
|
|
}, {
|
|
label: _('New notebook'),
|
|
screens: ['Main'],
|
|
click: () => {
|
|
this.dispatch({
|
|
type: 'WINDOW_COMMAND',
|
|
name: 'newNotebook',
|
|
});
|
|
}
|
|
}, {
|
|
type: 'separator',
|
|
}, {
|
|
label: _('Import Evernote notes'),
|
|
click: () => {
|
|
const filePaths = bridge().showOpenDialog({
|
|
properties: ['openFile', 'createDirectory'],
|
|
filters: [
|
|
{ name: _('Evernote Export Files'), extensions: ['enex'] },
|
|
]
|
|
});
|
|
if (!filePaths || !filePaths.length) return;
|
|
|
|
this.dispatch({
|
|
type: 'NAV_GO',
|
|
routeName: 'Import',
|
|
props: {
|
|
filePath: filePaths[0],
|
|
},
|
|
});
|
|
}
|
|
}, {
|
|
type: 'separator',
|
|
}, {
|
|
label: _('Quit'),
|
|
accelerator: 'CommandOrControl+Q',
|
|
click: () => { bridge().electronApp().exit() }
|
|
}]
|
|
}, {
|
|
label: 'Help',
|
|
submenu: [{
|
|
label: _('Documentation'),
|
|
accelerator: 'F1',
|
|
click () { bridge().openExternal('http://joplin.cozic.net') }
|
|
}, {
|
|
label: _('About Joplin'),
|
|
click () { }
|
|
}]
|
|
},
|
|
];
|
|
|
|
function removeUnwantedItems(template, screen) {
|
|
let output = [];
|
|
for (let i = 0; i < template.length; i++) {
|
|
const t = Object.assign({}, template[i]);
|
|
if (t.screens && t.screens.indexOf(screen) < 0) continue;
|
|
if (t.submenu) t.submenu = removeUnwantedItems(t.submenu, screen);
|
|
output.push(t);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
let screenTemplate = removeUnwantedItems(template, screen);
|
|
|
|
const menu = Menu.buildFromTemplate(screenTemplate);
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
this.lastMenuScreen_ = screen;
|
|
}
|
|
|
|
async start(argv) {
|
|
argv = await super.start(argv);
|
|
|
|
this.updateMenu('Main');
|
|
|
|
this.initRedux();
|
|
|
|
// Since the settings need to be loaded before the store is created, it will never
|
|
// receive the SETTING_UPDATE_ALL even, which mean state.settings will not be
|
|
// initialised. So we manually call dispatchUpdateAll() to force an update.
|
|
Setting.dispatchUpdateAll();
|
|
|
|
await FoldersScreenUtils.refreshFolders();
|
|
|
|
const tags = await Tag.allWithNotes();
|
|
|
|
this.dispatch({
|
|
type: 'TAG_UPDATE_ALL',
|
|
tags: tags,
|
|
});
|
|
|
|
this.store().dispatch({
|
|
type: 'FOLDER_SELECT',
|
|
id: Setting.value('activeFolderId'),
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
let application_ = null;
|
|
|
|
function app() {
|
|
if (!application_) application_ = new Application();
|
|
return application_;
|
|
}
|
|
|
|
module.exports = { app }; |