server-ts: Port services/window

This commit is contained in:
Elian Doran 2024-02-18 12:19:09 +02:00
parent ddcbb29a67
commit 330334dcb4
No known key found for this signature in database
6 changed files with 32 additions and 29 deletions

View File

@ -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

View File

@ -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();

View File

@ -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);

View File

@ -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 = [

View File

@ -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,

View File

@ -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