1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ElectronClient/bridge.ts

233 lines
5.6 KiB
TypeScript
Raw Normal View History

import ElectronAppWrapper from './ElectronAppWrapper';
import shim from 'lib/shim';
import { _, setLocale } from 'lib/locale';
Plugins: Added support for content scripts - For now, supports Markdown-it plugins - Also fixed slow rendering of notes in some cases - Simplified how Markdown-It plugins are created and cleaned MdToHtml code commit 89576de2896c99134f25f2a2db25008514cb1315 Merge: c75aa21f 5292fc14 Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 21 00:23:00 2020 +0100 Merge branch 'release-1.3' into plugin_content_scripts commit c75aa21ffdc42764d71dc9deadba7a7ef4233995 Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 21 00:19:52 2020 +0100 Fixed tests commit 075187729d11a16d385b651cbf1ebb89f14935e0 Author: Laurent Cozic <laurent@cozic.net> Date: Wed Oct 21 00:11:53 2020 +0100 Fixed tests commit 14696b8c651e7afdaf71269bcdbadf0d58d3ef8a Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 20 23:27:58 2020 +0100 Fixed slow rendering of note commit 61c09f5bf856481f91b00cfe87ff05596c63d4bc Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 20 22:35:21 2020 +0100 Clean up commit 9f7ea7d865a990b3a21cc8c59093390d9db61653 Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 20 20:05:31 2020 +0100 Updated doc commit 98bf3bde8d6663f2f91ff965304b4aac00bdd98b Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 20 19:56:34 2020 +0100 Finished converting plugins commit fe90d92e01427bd2b38200393713ea28763507a9 Author: Laurent Cozic <laurent@cozic.net> Date: Tue Oct 20 17:52:02 2020 +0100 Simplified how Markdown-It plugins are created commit 47c7b864cbb864d5df79849f27625aecf312df4b Author: Laurent Cozic <laurent@cozic.net> Date: Mon Oct 19 16:40:11 2020 +0100 Clean up rules commit d927a238bb635a4be45f9216d776f7d07cb0a584 Author: Laurent Cozic <laurent@cozic.net> Date: Mon Oct 19 14:29:40 2020 +0100 Fixed tests commit 388a56c5dde4c382e3ee0035791137150adaba1b Author: Laurent Cozic <laurent@cozic.net> Date: Mon Oct 19 14:00:47 2020 +0100 Add support for content scripts
2020-10-21 01:23:55 +02:00
const { dirname, toSystemSlashes } = require('lib/path-utils');
const { BrowserWindow, nativeTheme } = require('electron');
2017-11-08 19:51:55 +02:00
interface LastSelectedPath {
file: string,
directory: string,
}
interface LastSelectedPaths {
[key:string]: LastSelectedPath,
}
export class Bridge {
private electronWrapper_:ElectronAppWrapper;
private lastSelectedPaths_:LastSelectedPaths;
constructor(electronWrapper:ElectronAppWrapper) {
this.electronWrapper_ = electronWrapper;
this.lastSelectedPaths_ = {
file: null,
directory: null,
};
}
2017-11-11 14:00:37 +02:00
electronApp() {
return this.electronWrapper_;
}
env() {
return this.electronWrapper_.env();
}
processArgv() {
return process.argv;
}
window() {
return this.electronWrapper_.window();
}
showItemInFolder(fullPath:string) {
return require('electron').shell.showItemInFolder(toSystemSlashes(fullPath));
}
newBrowserWindow(options:any) {
return new BrowserWindow(options);
}
windowContentSize() {
if (!this.window()) return { width: 0, height: 0 };
const s = this.window().getContentSize();
return { width: s[0], height: s[1] };
2018-03-02 20:24:02 +02:00
}
2017-11-14 20:02:58 +02:00
windowSize() {
if (!this.window()) return { width: 0, height: 0 };
const s = this.window().getSize();
return { width: s[0], height: s[1] };
}
windowSetSize(width:number, height:number) {
2017-11-14 20:02:58 +02:00
if (!this.window()) return;
return this.window().setSize(width, height);
}
2019-12-17 19:06:55 +02:00
openDevTools() {
return this.window().webContents.openDevTools();
}
closeDevTools() {
return this.window().webContents.closeDevTools();
}
showSaveDialog(options:any) {
const { dialog } = require('electron');
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPaths_.file) options.defaultPath = this.lastSelectedPaths_.file;
2019-12-17 13:08:55 +02:00
const filePath = dialog.showSaveDialogSync(this.window(), options);
if (filePath) {
this.lastSelectedPaths_.file = filePath;
}
return filePath;
}
showOpenDialog(options:any) {
const { dialog } = require('electron');
if (!options) options = {};
let fileType = 'file';
if (options.properties && options.properties.includes('openDirectory')) fileType = 'directory';
if (!('defaultPath' in options) && this.lastSelectedPaths_[fileType]) options.defaultPath = this.lastSelectedPaths_[fileType];
if (!('createDirectory' in options)) options.createDirectory = true;
2019-12-17 13:08:55 +02:00
const filePaths = dialog.showOpenDialogSync(this.window(), options);
if (filePaths && filePaths.length) {
this.lastSelectedPaths_[fileType] = dirname(filePaths[0]);
}
return filePaths;
2017-11-11 00:18:00 +02:00
}
2018-03-02 20:24:02 +02:00
// Don't use this directly - call one of the showXxxxxxxMessageBox() instead
showMessageBox_(window:any, options:any):number {
const { dialog } = require('electron');
2018-03-02 20:24:02 +02:00
if (!window) window = this.window();
2019-12-17 13:08:55 +02:00
return dialog.showMessageBoxSync(window, options);
}
showErrorMessageBox(message:string) {
2018-03-02 20:24:02 +02:00
return this.showMessageBox_(this.window(), {
2017-11-08 19:51:55 +02:00
type: 'error',
message: message,
buttons: [_('OK')],
2017-11-08 19:51:55 +02:00
});
}
showConfirmMessageBox(message:string, options:any = null) {
if (options === null) options = {};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
2017-11-08 19:51:55 +02:00
type: 'question',
message: message,
cancelId: 1,
2017-11-08 19:51:55 +02:00
buttons: [_('OK'), _('Cancel')],
}, options));
2017-11-08 19:51:55 +02:00
return result === 0;
}
/* returns the index of the clicked button */
showMessageBox(message:string, options:any = null) {
if (options === null) options = {};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
type: 'question',
message: message,
buttons: [_('OK'), _('Cancel')],
}, options));
return result;
}
showInfoMessageBox(message:string, options:any = {}) {
2018-03-02 20:24:02 +02:00
const result = this.showMessageBox_(this.window(), Object.assign({}, {
type: 'info',
message: message,
buttons: [_('OK')],
2018-03-02 20:24:02 +02:00
}, options));
return result === 0;
}
setLocale(locale:string) {
setLocale(locale);
}
2017-11-08 19:51:55 +02:00
get Menu() {
return require('electron').Menu;
}
get MenuItem() {
return require('electron').MenuItem;
}
openExternal(url:string) {
return require('electron').shell.openExternal(url);
2017-11-11 14:00:37 +02:00
}
openItem(fullPath:string) {
return require('electron').shell.openItem(fullPath);
}
checkForUpdates(inBackground:boolean, window:any, logFilePath:string, options:any) {
const { checkForUpdates } = require('./checkForUpdates.js');
checkForUpdates(inBackground, window, logFilePath, options);
}
buildDir() {
return this.electronApp().buildDir();
}
2019-12-30 16:10:43 +02:00
screen() {
return require('electron').screen;
}
shouldUseDarkColors() {
return nativeTheme.shouldUseDarkColors;
}
addEventListener(name:string, fn:Function) {
if (name === 'nativeThemeUpdated') {
nativeTheme.on('updated', fn);
} else {
throw new Error(`Unsupported event: ${name}`);
}
}
restart() {
// Note that in this case we are not sending the "appClose" event
// to notify services and component that the app is about to close
// but for the current use-case it's not really needed.
const { app } = require('electron');
2020-08-19 00:51:23 +02:00
if (shim.isPortable()) {
const options = {
execPath: process.env.PORTABLE_EXECUTABLE_FILE,
};
app.relaunch(options);
} else if (shim.isLinux()) {
this.showInfoMessageBox(_('The app is now going to close. Please relaunch it to complete the process.'));
2020-08-19 00:51:23 +02:00
} else {
app.relaunch();
}
app.exit();
}
}
let bridge_:Bridge = null;
export function initBridge(wrapper:ElectronAppWrapper) {
if (bridge_) throw new Error('Bridge already initialized');
bridge_ = new Bridge(wrapper);
return bridge_;
}
export default function bridge() {
if (!bridge_) throw new Error('Bridge not initialized');
return bridge_;
}