From 15f09ef169d65e8705ccebf50e4e8e5d24045366 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 12 Nov 2019 17:51:57 +0000 Subject: [PATCH] Revert "Desktop: Stop watching external edits when closing editor (#1981)" Due to this issue: https://discourse.joplinapp.org/t/joplin-version-1-0-173/4232/7 This reverts commit 0eb51e6bb0d249d092047025879438ad0d160b05. --- .../lib/services/ExternalEditWatcher.js | 75 +++++-------------- 1 file changed, 19 insertions(+), 56 deletions(-) diff --git a/ReactNativeClient/lib/services/ExternalEditWatcher.js b/ReactNativeClient/lib/services/ExternalEditWatcher.js index 5f929fdd9..ebbce96bf 100644 --- a/ReactNativeClient/lib/services/ExternalEditWatcher.js +++ b/ReactNativeClient/lib/services/ExternalEditWatcher.js @@ -195,8 +195,24 @@ class ExternalEditWatcher { }; } - async spawn(path, args, options) { + async spawnCommand(path, args, options) { return new Promise((resolve, reject) => { + // App bundles need to be opened using the `open` command. + // Additional args can be specified after --args, and the + // -n flag is needed to ensure that the app is always launched + // with the arguments. Without it, if the app is already opened, + // it will just bring it to the foreground without opening the file. + // So the full command is: + // + // open -n /path/to/editor.app --args -app-flag -bla /path/to/file.md + // + if (shim.isMac() && fileExtension(path) === 'app') { + args = args.slice(); + args.splice(0, 0, '--args'); + args.splice(0, 0, path); + args.splice(0, 0, '-n'); + path = 'open'; + } const wrapError = error => { if (!error) return error; @@ -221,61 +237,12 @@ class ExternalEditWatcher { clearInterval(iid); reject(wrapError(error)); }); - if (options.onClose) { - subProcess.on('close', options.onClose); - } } catch (error) { throw wrapError(error); } }); } - async spawnDefault(file, onClose) { - // windows - let path = ''; - - if (shim.isWindows()) { - path = 'start'; - } else if (shim.isMac()) { - path = 'open'; - } else if (shim.isLinux() || shim.isFreeBSD()) { - path = 'xdg-open'; - } else { - // Fallback on the electron open method - return new Promise((resolve, reject) => { - - if (bridge().openExternal(`file://${file}`)) { - resolve(); - } else { - reject(new Error(`Could not open file: ${file}`)); - } - }); - } - - return this.spawn(path, [file], { onClose: onClose }); - } - - async spawnCommand(path, args, options) { - // App bundles need to be opened using the `open` command. - // Additional args can be specified after --args, and the - // -n flag is needed to ensure that the app is always launched - // with the arguments. Without it, if the app is already opened, - // it will just bring it to the foreground without opening the file. - // So the full command is: - // - // open -n /path/to/editor.app --args -app-flag -bla /path/to/file.md - // - if (shim.isMac() && fileExtension(path) === 'app') { - args = args.slice(); - args.splice(0, 0, '--args'); - args.splice(0, 0, path); - args.splice(0, 0, '-n'); - path = 'open'; - } - - return this.spawn(path, args, options); - } - async openAndWatch(note) { if (!note || !note.id) { this.logger().warn('ExternalEditWatcher: Cannot open note: ', note); @@ -285,16 +252,12 @@ class ExternalEditWatcher { const filePath = await this.writeNoteToFile_(note); this.watch(filePath); - const onClose = () => { - this.stopWatching(note.id); - }; - const cmd = this.textEditorCommand(); if (!cmd) { - await this.spawnDefault(filePath, onClose); + bridge().openExternal(`file://${filePath}`); } else { cmd.args.push(filePath); - await this.spawnCommand(cmd.path, cmd.args, { detached: true, onClose: onClose }); + await this.spawnCommand(cmd.path, cmd.args, { detached: true }); } this.dispatch({