1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Allow setting extra flags and renaming folder

This commit is contained in:
Laurent Cozic 2017-11-16 18:51:11 +00:00
parent eacd0e5cfd
commit 5098c03264
7 changed files with 131 additions and 89 deletions

View File

@ -6,6 +6,7 @@ const { Note } = require('lib/models/note.js');
const { Resource } = require('lib/models/resource.js');
const { cliUtils } = require('./cli-utils.js');
const { reducer, defaultState } = require('lib/reducer.js');
const { splitCommandString } = require('lib/string-utils.js');
const { reg } = require('lib/registry.js');
const { _ } = require('lib/locale.js');
@ -520,7 +521,7 @@ class AppGui {
let note = this.widget('noteList').currentItem;
let folder = this.widget('folderList').currentItem;
let args = cliUtils.splitCommandString(cmd);
let args = splitCommandString(cmd);
for (let i = 0; i < args.length; i++) {
if (args[i] == '$n') {

View File

@ -5,75 +5,6 @@ const stringPadding = require('string-padding');
const cliUtils = {};
cliUtils.splitCommandString = function(command) {
let args = [];
let state = "start"
let current = ""
let quote = "\""
let escapeNext = false;
for (let i = 0; i < command.length; i++) {
let c = command[i]
if (state == "quotes") {
if (c != quote) {
current += c
} else {
args.push(current)
current = ""
state = "start"
}
continue
}
if (escapeNext) {
current += c;
escapeNext = false;
continue;
}
if (c == "\\") {
escapeNext = true;
continue;
}
if (c == '"' || c == '\'') {
state = "quotes"
quote = c
continue
}
if (state == "arg") {
if (c == ' ' || c == '\t') {
args.push(current)
current = ""
state = "start"
} else {
current += c
}
continue
}
if (c != ' ' && c != "\t") {
state = "arg"
current += c
}
}
if (state == "quotes") {
throw new Error("Unclosed quote in command line: " + command)
}
if (current != "") {
args.push(current)
}
if (args.length <= 0) {
throw new Error("Empty command line")
}
return args;
}
cliUtils.printArray = function(logFunction, rows, headers = null) {
if (!rows.length) return '';

View File

@ -280,16 +280,6 @@ class Application extends BaseApplication {
this.initRedux();
// const windowSize = Setting.value('windowSize');
// const width = windowSize && windowSize.width ? windowSize.width : 800;
// const height = windowSize && windowSize.height ? windowSize.height : 800;
// bridge().windowSetSize(width, height);
// this.store().dispatch({
// type: 'WINDOW_CONTENT_SIZE_SET',
// size: bridge().windowContentSize(),
// });
// 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.

View File

@ -97,13 +97,14 @@ class MainScreenComponent extends React.Component {
folder = await Folder.save({ title: answer }, { userSideValidation: true });
} catch (error) {
bridge().showErrorMessageBox(error.message);
return;
}
this.props.dispatch({
type: 'FOLDER_SELECT',
id: folder.id,
});
if (folder) {
this.props.dispatch({
type: 'FOLDER_SELECT',
id: folder.id,
});
}
}
this.setState({ promptOptions: null });
@ -128,6 +129,26 @@ class MainScreenComponent extends React.Component {
}
},
});
} else if (command.name === 'renameNotebook') {
const folder = await Folder.load(command.id);
if (!folder) return;
this.setState({
promptOptions: {
label: _('Rename notebook:'),
value: folder.title,
onClose: async (answer) => {
if (answer !== null) {
try {
await Folder.save({ id: folder.id, title: answer }, { userSideValidation: true });
} catch (error) {
bridge().showErrorMessageBox(error.message);
}
}
this.setState({ promptOptions: null });
}
},
});
} else {
commandProcessed = false;
}

View File

@ -115,6 +115,14 @@ class SideBarComponent extends React.Component {
}
}}))
menu.append(new MenuItem({label: _('Rename'), click: async () => {
this.props.dispatch({
type: 'WINDOW_COMMAND',
name: 'renameNotebook',
id: itemId,
});
}}))
menu.popup(bridge().window());
}

View File

@ -11,6 +11,7 @@ const { Note } = require('lib/models/note.js');
const { Tag } = require('lib/models/tag.js');
const { Setting } = require('lib/models/setting.js');
const { Logger } = require('lib/logger.js');
const { splitCommandString } = require('lib/string-utils.js');
const { sprintf } = require('sprintf-js');
const { reg } = require('lib/registry.js');
const { fileExtension } = require('lib/path-utils.js');
@ -63,7 +64,7 @@ class BaseApplication {
// Handles the initial flags passed to main script and
// returns the remaining args.
async handleStartFlags_(argv) {
async handleStartFlags_(argv, setDefaults = true) {
let matched = {};
argv = argv.slice(0);
argv.splice(0, 2); // First arguments are the node executable, and the node JS file
@ -118,8 +119,10 @@ class BaseApplication {
}
}
if (!matched.logLevel) matched.logLevel = Logger.LEVEL_INFO;
if (!matched.env) matched.env = 'prod';
if (setDefaults) {
if (!matched.logLevel) matched.logLevel = Logger.LEVEL_INFO;
if (!matched.env) matched.env = 'prod';
}
return {
matched: matched,
@ -272,6 +275,22 @@ class BaseApplication {
reg.dispatch = this.store().dispatch;
}
async readFlagsFromFile(flagPath) {
if (!fs.existsSync(flagPath)) return {};
let flagContent = fs.readFileSync(flagPath, 'utf8');
if (!flagContent) return {};
flagContent = flagContent.trim();
let flags = splitCommandString(flagContent);
flags.splice(0, 0, 'cmd');
flags.splice(0, 0, 'node');
flags = await this.handleStartFlags_(flags, false);
return flags.matched;
}
async start(argv) {
let startFlags = await this.handleStartFlags_(argv);
@ -302,6 +321,9 @@ class BaseApplication {
await fs.mkdirp(resourceDir, 0o755);
await fs.mkdirp(tempDir, 0o755);
const extraFlags = await this.readFlagsFromFile(profileDir + '/flags.txt');
initArgs = Object.assign(initArgs, extraFlags);
this.logger_.addTarget('file', { path: profileDir + '/log.txt' });
//this.logger_.addTarget('console');
this.logger_.setLevel(initArgs.logLevel);

View File

@ -122,4 +122,73 @@ function wrap(text, indent, width) {
});
}
module.exports = { removeDiacritics, escapeFilename, wrap };
function splitCommandString(command) {
let args = [];
let state = "start"
let current = ""
let quote = "\""
let escapeNext = false;
for (let i = 0; i < command.length; i++) {
let c = command[i]
if (state == "quotes") {
if (c != quote) {
current += c
} else {
args.push(current)
current = ""
state = "start"
}
continue
}
if (escapeNext) {
current += c;
escapeNext = false;
continue;
}
if (c == "\\") {
escapeNext = true;
continue;
}
if (c == '"' || c == '\'') {
state = "quotes"
quote = c
continue
}
if (state == "arg") {
if (c == ' ' || c == '\t') {
args.push(current)
current = ""
state = "start"
} else {
current += c
}
continue
}
if (c != ' ' && c != "\t") {
state = "arg"
current += c
}
}
if (state == "quotes") {
throw new Error("Unclosed quote in command line: " + command)
}
if (current != "") {
args.push(current)
}
if (args.length <= 0) {
throw new Error("Empty command line")
}
return args;
}
module.exports = { removeDiacritics, escapeFilename, wrap, splitCommandString };