TagSpaces is an offline, open source, document manager with tagging support
fix electron main thread
Stanimir74ca0e57961e
diff --git a/.erb/configs/webpack.config.base.ts b/.erb/configs/webpack.config.base.tsindex c7c02c776..80a656c69 100644--- a/.erb/configs/webpack.config.base.ts+++ b/.erb/configs/webpack.config.base.ts@@ -59,13 +59,13 @@ const configuration: webpack.Configuration = {modules: [webpackPaths.srcPath, 'node_modules'],// There is no need to add aliases here, the paths in tsconfig get mirroredplugins: [new TsconfigPathsPlugins()],- alias: {+ /* alias: {// Your custom aliases here// __dirname will stands for your root directory level// path.resolve will concatenate your project folder location with the aliased folder'-': path.resolve(__dirname, '../../src/renderer/'),assets: path.resolve(__dirname, '../../assets/'),- },+ },*/},plugins: [diff --git a/.prettierignore b/.prettierignoreindex 8c97c8590..1861e23d3 100755--- a/.prettierignore+++ b/.prettierignore@@ -3,12 +3,12 @@ buildscordovawebweb/files-app/node_modules-app/version.json-app/tagspaces.namespace.d.ts+release/app/node_modules+src/node_modules+src/renderer/version.json+src/renderer/tagspaces.namespace.d.tsflow-typed-app/dist-app/main.prod.js+release/app/distextensions/extensions/*extensions/_extensions/*extensions/.ts/*@@ -30,7 +30,7 @@ extensions/media-player/build/*extensions/pdf-viewer/generic/build/*extensions/text-editor-react/build/*extensions/md-editor/build/*-dll+.erbapp/utils/js-webdav-client.jscoveragetests/testdatadiff --git a/scripts/jwt_generate.js b/scripts/jwt_generate.jsindex 11e9b7c50..f04bf4076 100644--- a/scripts/jwt_generate.js+++ b/scripts/jwt_generate.js@@ -3,7 +3,7 @@ const fs = require('fs-extra');const pathLib = require('path');const propertiesReader = require('properties-reader');-const propsPath = pathLib.join(__dirname, '..', 'src', 'renderer', '.env');+const propsPath = pathLib.join(__dirname, '..', 'assets', '.env');if (!fs.existsSync(propsPath)) {throw new Error(propsPath + ' file not exist! Please create it and add property KEY=xxx',diff --git a/src/main/main.ts b/src/main/main.tsindex a5a3f2667..c3c12005f 100644--- a/src/main/main.ts+++ b/src/main/main.ts@@ -9,11 +9,26 @@* `./src/main.js` using webpack. This gives us some performance wins.*/import path from 'path';-import { app, BrowserWindow, shell, ipcMain } from 'electron';+import {+ app,+ BrowserWindow,+ shell,+ ipcMain,+ globalShortcut,+ dialog,+} from 'electron';import { autoUpdater } from 'electron-updater';import log from 'electron-log';+import pm2 from '@elife/pm2';+import propertiesReader from 'properties-reader';+import fs from 'fs-extra';import MenuBuilder from './menu';import { resolveHtmlPath } from './util';+import windowStateKeeper from 'electron-window-state';+import findFreePorts from 'find-free-ports';+import settings from '../renderer/settings';+import { getExtensions } from '../renderer/utils/extension-utils';+import i18nInit from '../renderer/services/i18nInit';class AppUpdater {constructor() {@@ -24,12 +39,7 @@ class AppUpdater {}let mainWindow: BrowserWindow | null = null;--ipcMain.on('ipc-example', async (event, arg) => {- const msgTemplate = (pingPong: string) => `IPC test: ${pingPong}`;- console.log(msgTemplate(arg));- event.reply('ipc-example', msgTemplate('pong'));-});+let usedWsPort = undefined;if (process.env.NODE_ENV === 'production') {const sourceMapSupport = require('source-map-support');@@ -43,6 +53,45 @@ if (isDebug) {require('electron-debug')();}+const testMode = process.env.NODE_ENV === 'test';++let startupFilePath;+let portableMode;++process.argv.forEach((arg, count) => {+ console.log('Opening file: ' + arg);+ if (+ arg.toLowerCase() === '-d' ||+ arg.toLowerCase() === '--debug' ||+ arg.startsWith('--remote-debugging-port=') ||+ arg.startsWith('--inspect=')+ ) {+ // debugMode = true;+ } else if (arg.toLowerCase() === '-p' || arg.toLowerCase() === '--portable') {+ app.setPath('userData', process.cwd() + '/tsprofile'); // making the app portable+ portableMode = true;+ } else if (testMode || isDebug) {+ // ignoring the spectron testing+ arg = '';+ } else if (+ arg.endsWith('main.prod.js') ||+ arg === './app/main.dev.babel.js' ||+ arg === '.' ||+ count === 0+ ) {+ // ignoring the first argument+ } else if (arg.length > 2) {+ // console.warn('Opening file: ' + arg);+ if (arg !== './app/main.dev.js' && arg !== './app/') {+ startupFilePath = arg;+ }+ }++ if (portableMode) {+ startupFilePath = undefined;+ }+});+const installExtensions = async () => {const installer = require('electron-devtools-installer');const forceDownload = !!process.env.UPGRADE_EXTENSIONS;@@ -56,26 +105,294 @@ const installExtensions = async () => {.catch(console.log);};+function showTagSpaces() {+ if (mainWindow) {+ if (mainWindow.isMinimized()) {+ mainWindow.restore();+ }+ mainWindow.show();+ }+}++function openLocationManagerPanel() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'open-location-manager-panel');+ }+}+function openTagLibraryPanel() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'open-tag-library-panel');+ }+}+function goBack() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'go-back');+ }+}+function goForward() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'go-forward');+ }+}+function setZoomResetApp() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'set-zoom-reset-app');+ }+}+function setZoomInApp() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'set-zoom-in-app');+ }+}+function setZoomOutApp() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'set-zoom-out-app');+ }+}+function exitFullscreen() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'exit-fullscreen');+ }+}+function toggleSettingsDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-settings-dialog');+ }+}+function openHelpFeedbackPanel() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'open-help-feedback-panel');+ }+}+function toggleKeysDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-keys-dialog');+ }+}+function toggleOnboardingDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-onboarding-dialog');+ }+}+function openURLExternally(data) {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('open-url-externally', data);+ }+}+function toggleLicenseDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-license-dialog');+ }+}+function toggleThirdPartyLibsDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-third-party-libs-dialog');+ }+}+function toggleAboutDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-about-dialog');+ }+}+function showSearch() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'open-search');+ }+}++function newTextFile() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'new-text-file');+ }+}++function getNextFile() {+ if (mainWindow) {+ // showTagSpaces();+ mainWindow.webContents.send('cmd', 'next-file');+ }+}++function getPreviousFile() {+ if (mainWindow) {+ // showTagSpaces();+ mainWindow.webContents.send('cmd', 'previous-file');+ }+}++function showCreateDirectoryDialog() {+ if (mainWindow) {+ // showTagSpaces();+ mainWindow.webContents.send('cmd', 'show-create-directory-dialog');+ }+}++function toggleOpenLinkDialog() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.webContents.send('cmd', 'toggle-open-link-dialog');+ }+}++function resumePlayback() {+ if (mainWindow) {+ // showTagSpaces();+ mainWindow.webContents.send('play-pause', true);+ }+}++function reloadApp() {+ if (mainWindow) {+ showTagSpaces();+ mainWindow.loadURL(resolveHtmlPath('index.html'));+ }+}++function createNewWindowInstance(url?) {+ const mainWindowState = windowStateKeeper({+ defaultWidth: 1280,+ defaultHeight: 800,+ });++ const mainWindowInstance = new BrowserWindow({+ show: true,+ center: true,+ width: mainWindowState.width,+ height: mainWindowState.height,+ webPreferences: {+ spellcheck: true,+ nodeIntegration: true,+ webviewTag: true,+ contextIsolation: false,+ },+ });++ mainWindowInstance.setMenuBarVisibility(false);+ mainWindowInstance.setAutoHideMenuBar(true);+ if (url) {+ mainWindowInstance.loadURL(url);+ } else {+ mainWindowInstance.loadURL(resolveHtmlPath('index.html'));+ }+}++const RESOURCES_PATH = app.isPackaged+ ? path.join(process.resourcesPath, 'assets')+ : path.join(__dirname, '../../assets');++const getAssetPath = (...paths: string[]): string => {+ return path.join(RESOURCES_PATH, ...paths);+};++async function startWS() {+ try {+ let filepath;+ let script;+ //let envPath;+ if (isDebug || testMode) {+ filepath = path.join(+ __dirname,+ '../node_modules/@tagspaces/tagspaces-ws/build',+ );+ script = 'index.js';+ //envPath = path.join(__dirname, '.env');+ } else {+ filepath = process.resourcesPath;+ script = 'app.asar/node_modules/@tagspaces/tagspaces-ws/build/index.js';+ //envPath = path.join(process.resourcesPath, 'app.asar/.env');+ }+ const properties = propertiesReader(getAssetPath('.env')); //envPath);++ const results = await new Promise((resolve, reject) => {+ findFreePorts(1, { startPort: settings.getInitWsPort() }).then(+ ([freePort]) => {+ pm2.start(+ {+ name: 'Tagspaces WS',+ script, // Script to be run+ cwd: filepath, // './node_modules/tagspaces-ws', // './process1', cwd: '/path/to/npm/module/',+ args: ['-p', freePort, '-k', properties.get('KEY')],+ restartAt: [],+ // log: path.join(process.cwd(), 'thumbGen.log')+ },+ (err, pid) => {+ if (err && pid) {+ if (pid && pid.name) console.error(pid.name, err, pid);+ else console.error(err, pid);+ reject(err);+ } else if (err) {+ reject(err);+ } else {+ usedWsPort = freePort;+ if (mainWindow) {+ mainWindow.webContents.send('start_ws', {+ port: freePort,+ });+ }+ resolve(+ `Starting ${pid.name} on ${pid.cwd} - pid (${pid.child.pid})`,+ );+ }+ },+ );+ },+ );+ });+ console.debug(results);+ } catch (ex) {+ console.error('pm2.start Exception:', ex);+ }+}+const createWindow = async () => {+ let startupParameter = '';+ if (startupFilePath) {+ if (startupFilePath.startsWith('./') || startupFilePath.startsWith('.\\')) {+ startupParameter =+ '?cmdopen=' + encodeURIComponent(path.join(__dirname, startupFilePath));+ } else if (startupFilePath !== 'data:,') {+ startupParameter = '?cmdopen=' + encodeURIComponent(startupFilePath);+ }+ }+if (isDebug) {await installExtensions();}- const RESOURCES_PATH = app.isPackaged- ? path.join(process.resourcesPath, 'assets')- : path.join(__dirname, '../../assets');-- const getAssetPath = (...paths: string[]): string => {- return path.join(RESOURCES_PATH, ...paths);- };+ const mainWindowState = windowStateKeeper({+ defaultWidth: 1280,+ defaultHeight: 800,+ });mainWindow = new BrowserWindow({show: false,- width: 1024,- height: 728,+ x: mainWindowState.x,+ y: mainWindowState.y,+ width: mainWindowState.width,+ height: mainWindowState.height,icon: getAssetPath('icon.png'),webPreferences: {+ spellcheck: true,nodeIntegration: true,+ webviewTag: true,contextIsolation: false,preload: app.isPackaged? path.join(__dirname, 'preload.js')@@ -83,7 +400,18 @@ const createWindow = async () => {},});- mainWindow.loadURL(resolveHtmlPath('index.html'));+ const winUserAgent =+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36';+ const testWinOnUnix = false; // set to true to simulate windows os, useful for testing s3 handling++ mainWindow.loadURL(+ resolveHtmlPath('index.html') + startupParameter,+ testWinOnUnix ? { userAgent: winUserAgent } : {},+ );++ mainWindow.webContents.send('start_ws', {+ port: usedWsPort,+ });mainWindow.on('ready-to-show', () => {if (!mainWindow) {@@ -100,6 +428,19 @@ const createWindow = async () => {mainWindow = null;});+ mainWindow.webContents.on(+ 'render-process-gone',+ (_, { reason, exitCode }) => {+ /*console.error(+ `[web ui] render-process-gone: ${reason}, code: ${exitCode}`+ );*/+ // 'crashed'+ pm2.stopAll();+ globalShortcut.unregisterAll();+ app.quit();+ },+ );+const menuBuilder = new MenuBuilder(mainWindow);menuBuilder.buildMenu();@@ -114,6 +455,8 @@ const createWindow = async () => {new AppUpdater();};+app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); // Fix broken autoplay functionality in the av player+/*** Add event listeners...*/@@ -122,18 +465,212 @@ app.on('window-all-closed', () => {// Respect the OSX convention of having the application in memory even// after all windows have been closedif (process.platform !== 'darwin') {+ pm2.stopAll();+ globalShortcut.unregisterAll();app.quit();}});+app.on('quit', () => {+ pm2.stopAll();+ globalShortcut.unregisterAll();+});++startWS();+app.whenReady().then(() => {- createWindow();- app.on('activate', () => {- // On macOS it's common to re-create a window in the app when the- // dock icon is clicked and there are no other windows open.- if (mainWindow === null) createWindow();+ return i18nInit().then((i18n) => {+ createWindow();+ app.on('activate', () => {+ // On macOS it's common to re-create a window in the app when the+ // dock icon is clicked and there are no other windows open.+ if (mainWindow === null) createWindow();+ });++ i18n.on('languageChanged', (lng) => {+ try {+ console.log('languageChanged:' + lng);+ //buildAppMenu(i18n);+ //buildTrayMenu(i18n);+ } catch (ex) {+ console.log('languageChanged', ex);+ }+ });++ ipcMain.on('show-main-window', () => {+ if (mainWindow) {+ if (mainWindow.isMinimized()) {+ mainWindow.restore();+ }+ mainWindow.show();+ }+ });++ ipcMain.on('create-new-window', (e, url) => {+ createNewWindowInstance(url);+ });++ ipcMain.on('load-extensions', () => {+ getExtensions(path.join(app.getPath('userData'), 'tsplugins'), true)+ .then(({ extensions, supportedFileTypes }) => {+ if (mainWindow) {+ mainWindow.webContents.send('set_extensions', {+ extensions,+ supportedFileTypes,+ });+ // mainWindow.webContents.send('set_supported_file_types', supportedFileTypes);+ }+ })+ .catch((err) => console.error('load-extensions', err));+ });++ ipcMain.on('remove-extension', (e, extensionId) => {+ try {+ const extBuildIndex = extensionId.indexOf('/build');+ fs.rmSync(+ path.join(+ app.getPath('userData'),+ 'tsplugins',+ extBuildIndex > -1+ ? extensionId.substring(0, extBuildIndex)+ : extensionId,+ ),+ {+ recursive: true,+ },+ );+ } catch (e) {+ console.debug(e);+ }+ });++ ipcMain.handle('get-user-data', () => {+ return app.getPath('userData');+ });++ ipcMain.on('focus-window', () => {+ if (mainWindow) {+ mainWindow.focus();+ }+ });++ ipcMain.handle('get-device-paths', () => {+ const paths: any = {+ desktopFolder: app.getPath('desktop'),+ documentsFolder: app.getPath('documents'),+ downloadsFolder: app.getPath('downloads'),+ musicFolder: app.getPath('music'),+ picturesFolder: app.getPath('pictures'),+ videosFolder: app.getPath('videos'),+ };+ if (process.platform === 'darwin') {+ paths.iCloudFolder =+ app.getPath('home') ++ '/Library/Mobile Documents/com~apple~CloudDocs';+ }+ return paths;+ });++ ipcMain.on('get-user-home-path', (event) => {+ event.returnValue = app.getPath('home');+ });++ ipcMain.on('worker-response', (event, arg) => {+ if (mainWindow) {+ mainWindow.webContents.send(arg.id, arg);+ }+ });++ ipcMain.handle('select-directory-dialog', async () => {+ const options = {+ properties: ['openDirectory', 'createDirectory'],+ };+ // @ts-ignore+ const resultObject = await dialog.showOpenDialog(options);++ if (resultObject.filePaths && resultObject.filePaths.length) {+ // alert(JSON.stringify(resultObject.filePaths));+ return resultObject.filePaths;+ }+ return false;+ });++ // end electron-io++ ipcMain.on('app-data-path-request', (event) => {+ event.returnValue = app.getPath('appData'); // eslint-disable-line+ });++ ipcMain.on('app-version-request', (event) => {+ event.returnValue = app.getVersion(); // eslint-disable-line+ });++ ipcMain.handle('move-to-trash', async (event, files) => {+ const result = [];+ files.forEach((fullPath) => {+ // console.debug('Trash:' + fullPath);+ result.push(shell.trashItem(fullPath));+ });++ let ret;+ try {+ ret = await Promise.all(result);+ } catch (err) {+ console.error('moveToTrash ' + JSON.stringify(files) + 'error:', err);+ }+ return ret;+ });++ /* ipcMain.on('move-to-trash', async (event, files) => {+ const result = [];+ files.forEach(fullPath => {+ console.debug('Trash:' + fullPath);+ result.push(shell.trashItem(fullPath));+ });+ try {+ event.returnValue = await Promise.all(result);+ } catch (err) {+ console.error('moveToTrash error:', err);+ event.returnValue = undefined;+ }+ }); */++ ipcMain.on('set-language', (e, language) => {+ i18n.changeLanguage(language);+ });++ ipcMain.on('global-shortcuts-enabled', (e, globalShortcutsEnabled) => {+ if (globalShortcutsEnabled) {+ globalShortcut.register('CommandOrControl+Shift+F', showSearch);+ globalShortcut.register('CommandOrControl+Shift+P', resumePlayback);+ globalShortcut.register('MediaPlayPause', resumePlayback);+ globalShortcut.register('CommandOrControl+Shift+N', newTextFile);+ globalShortcut.register('CommandOrControl+Shift+D', getNextFile);+ globalShortcut.register('MediaNextTrack', getNextFile);+ globalShortcut.register('CommandOrControl+Shift+A', getPreviousFile);+ globalShortcut.register('MediaPreviousTrack', getPreviousFile);+ globalShortcut.register('CommandOrControl+Shift+W', showTagSpaces);+ } else {+ globalShortcut.unregisterAll();+ }+ });++ ipcMain.on('relaunch-app', reloadApp);++ ipcMain.on('quit-application', () => {+ globalShortcut.unregisterAll();+ app.quit();+ });++ process.on('uncaughtException', (error) => {+ if (error.stack) {+ console.error('error:', error.stack);+ throw new Error(error.stack);+ }+ reloadApp();+ });});}).catch(console.log);diff --git a/src/renderer/services/i18nInit.ts b/src/renderer/services/i18nInit.tsindex 5a11afa09..e2e6720f7 100644--- a/src/renderer/services/i18nInit.ts+++ b/src/renderer/services/i18nInit.ts@@ -17,7 +17,7 @@*/import i18n from 'i18next';-import i18nOptions from '-/services/i18nOptions';+import i18nOptions from './i18nOptions';import HttpBackend, { HttpBackendOptions } from 'i18next-http-backend';import { initReactI18next } from 'react-i18next';import LanguageDetector from 'i18next-browser-languagedetector';diff --git a/src/renderer/services/i18nOptions.ts b/src/renderer/services/i18nOptions.tsindex bf91abc86..c0daa249c 100644--- a/src/renderer/services/i18nOptions.ts+++ b/src/renderer/services/i18nOptions.ts@@ -16,8 +16,8 @@**/import { InitOptions } from 'i18next';-import enUs from '-/locales/en_US/core.json';-import en from '-/locales/en/core.json';+import enUs from '../locales/en_US/core.json';+import en from '../locales/en/core.json';let defaultLanguage: any = enUs;if (