Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/window

Elian Doran330334dcb47e
diff --git a/electron.js b/electron.js
index 13ba5acc58..f8a73d7905 100644
--- a/electron.js
+++ b/electron.js
@@ -3,7 +3,7 @@
const {app, globalShortcut, BrowserWindow} = require('electron');
const sqlInit = require('./src/services/sql_init');
const appIconService = require('./src/services/app_icon.js');
-const windowService = require('./src/services/window.js');
+const windowService = require('./src/services/window');
const tray = require('./src/services/tray.js');
// Adds debug features like hotkeys for triggering dev tools and reload
diff --git a/src/routes/setup.js b/src/routes/setup.js
index d3af71bef4..0a1dca7151 100644
--- a/src/routes/setup.js
+++ b/src/routes/setup.js
@@ -9,7 +9,7 @@ const appPath = require('../services/app_path');
function setupPage(req, res) {
if (sqlInit.isDbInitialized()) {
if (utils.isElectron()) {
- const windowService = require('../services/window.js');
+ const windowService = require('../services/window');
const {app} = require('electron');
windowService.createMainWindow(app);
windowService.closeSetupWindow();
diff --git a/src/services/request.ts b/src/services/request.ts
index ad1e6b5c83..47be2d2ef3 100644
--- a/src/services/request.ts
+++ b/src/services/request.ts
@@ -230,7 +230,7 @@ function getClient(opts: ClientOpts): Client {
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
// so in that case, we always use node's modules
if (utils.isElectron() && !opts.proxy) {
- return require('electron').net;
+ return require('electron').net as Client;
}
else {
const {protocol} = url.parse(opts.url);
diff --git a/src/services/tray.js b/src/services/tray.js
index 7d8e34e1e3..6f7f9f598e 100644
--- a/src/services/tray.js
+++ b/src/services/tray.js
@@ -1,6 +1,6 @@
const { Menu, Tray } = require('electron');
const path = require('path');
-const windowService = require('./window.js');
+const windowService = require('./window');
const optionService = require('./options');
const UPDATE_TRAY_EVENTS = [
diff --git a/src/services/window.js b/src/services/window.ts
similarity index 84%
rename from src/services/window.js
rename to src/services/window.ts
index 0e0fbef45d..bad5d461a3 100644
--- a/src/services/window.js
+++ b/src/services/window.ts
@@ -1,21 +1,20 @@
-const path = require('path');
-const url = require("url");
-const port = require('./port.ts');
-const optionService = require('./options');
-const env = require('./env');
-const log = require('./log');
-const sqlInit = require('./sql_init');
-const cls = require('./cls');
-const keyboardActionsService = require('./keyboard_actions');
-const {ipcMain} = require('electron');
+import path = require('path');
+import url = require("url");
+import port = require('./port');
+import optionService = require('./options');
+import env = require('./env');
+import log = require('./log');
+import sqlInit = require('./sql_init');
+import cls = require('./cls');
+import keyboardActionsService = require('./keyboard_actions');
+import remoteMain = require("@electron/remote/main")
+import { App, BrowserWindow, WebContents, ipcMain } from 'electron';
// Prevent the window being garbage collected
-/** @type {Electron.BrowserWindow} */
-let mainWindow;
-/** @type {Electron.BrowserWindow} */
-let setupWindow;
+let mainWindow: BrowserWindow | null;
+let setupWindow: BrowserWindow | null;
-async function createExtraWindow(extraWindowHash) {
+async function createExtraWindow(extraWindowHash: string) {
const spellcheckEnabled = optionService.getOptionBool('spellCheckEnabled');
const {BrowserWindow} = require('electron');
@@ -25,7 +24,6 @@ async function createExtraWindow(extraWindowHash) {
height: 800,
title: 'Trilium Notes',
webPreferences: {
- enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false,
spellcheck: spellcheckEnabled
@@ -44,7 +42,7 @@ ipcMain.on('create-extra-window', (event, arg) => {
createExtraWindow(arg.extraWindowHash);
});
-async function createMainWindow(app) {
+async function createMainWindow(app: App) {
const windowStateKeeper = require('electron-window-state'); // should not be statically imported
const mainWindowState = windowStateKeeper({
@@ -64,7 +62,6 @@ async function createMainWindow(app) {
height: mainWindowState.height,
title: 'Trilium Notes',
webPreferences: {
- enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false,
spellcheck: spellcheckEnabled,
@@ -95,8 +92,12 @@ async function createMainWindow(app) {
});
}
-function configureWebContents(webContents, spellcheckEnabled) {
- require("@electron/remote/main").enable(webContents);
+function configureWebContents(webContents: WebContents, spellcheckEnabled: boolean) {
+ if (!mainWindow) {
+ return;
+ }
+
+ remoteMain.enable(webContents);
mainWindow.webContents.setWindowOpenHandler((details) => {
require("electron").shell.openExternal(details.url);
@@ -108,8 +109,7 @@ function configureWebContents(webContents, spellcheckEnabled) {
const parsedUrl = url.parse(targetUrl);
// we still need to allow internal redirects from setup and migration pages
- if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname) || (parsedUrl.path && parsedUrl.path !== '/' && parsedUrl.path !== '/?')) {
-
+ if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname || "") || (parsedUrl.path && parsedUrl.path !== '/' && parsedUrl.path !== '/?')) {
ev.preventDefault();
}
});
@@ -168,6 +168,10 @@ async function registerGlobalShortcuts() {
const translatedShortcut = shortcut.substr(7);
const result = globalShortcut.register(translatedShortcut, cls.wrap(() => {
+ if (!mainWindow) {
+ return;
+ }
+
// window may be hidden / not in focus
mainWindow.focus();
@@ -189,8 +193,7 @@ function getMainWindow() {
return mainWindow;
}
-
-module.exports = {
+export = {
createMainWindow,
createSetupWindow,
closeSetupWindow,
diff --git a/src/www.js b/src/www.js
index 84f3abaa97..456bc940bf 100644
--- a/src/www.js
+++ b/src/www.js
@@ -45,7 +45,7 @@ function startTrilium() {
* instead of the new one. This is complicated by the fact that it is possible to run multiple instances of Trilium
* if port and data dir are configured separately. This complication is the source of the following weird usage.
*
- * The line below makes sure that the "second-instance" (process in window.js) is fired. Normally it returns a boolean
+ * The line below makes sure that the "second-instance" (process in window) is fired. Normally it returns a boolean
* indicating whether another instance is running or not, but we ignore that and kill the app only based on the port conflict.
*
* A bit weird is that "second-instance" is triggered also on the valid usecases (different port/data dir) and