* Add window.heynote.mainProcess.on() and off() which can be used in browser land to communicate with the main process. Use these methods to remove intermediate onOpenSettings() and onSettingsChange() methods from window.heynote objects.

* Convert some pesky TypeScript to JavaScript
This commit is contained in:
Jonatan Heyman 2025-01-06 16:13:36 +01:00
parent 64740192bf
commit 9bf3a5dacc
7 changed files with 47 additions and 34 deletions

View File

@ -1,5 +1,5 @@
const { app, Menu } = require("electron") const { app, Menu } = require("electron")
import { OPEN_SETTINGS_EVENT } from "@/src/common/constants"; import { OPEN_SETTINGS_EVENT, REDO_EVENT } from '@/src/common/constants'
import { openAboutWindow } from "./about"; import { openAboutWindow } from "./about";
import { quit } from "./index" import { quit } from "./index"
@ -63,7 +63,15 @@ const template = [
label: 'Edit', label: 'Edit',
submenu: [ submenu: [
{ role: 'undo' }, { role: 'undo' },
{ role: 'redo' }, //{ role: 'undo' },
{
label: 'Redo',
accelerator: 'CommandOrControl+Shift+z',
click: (menuItem, window, event) => {
window?.webContents.send(REDO_EVENT)
console.log("redo")
},
},
{ type: 'separator' }, { type: 'separator' },
{ role: 'cut' }, { role: 'cut' },
{ role: 'copy' }, { role: 'copy' },

View File

@ -2,10 +2,7 @@ const { contextBridge } = require('electron')
import themeMode from "./theme-mode" import themeMode from "./theme-mode"
import { isMac, isWindows, isLinux, isDev } from "../detect-platform" import { isMac, isWindows, isLinux, isDev } from "../detect-platform"
import { ipcRenderer } from "electron" import { ipcRenderer } from "electron"
import { import {
WINDOW_CLOSE_EVENT,
OPEN_SETTINGS_EVENT,
SETTINGS_CHANGE_EVENT,
UPDATE_AVAILABLE_EVENT, UPDATE_AVAILABLE_EVENT,
UPDATE_ERROR, UPDATE_ERROR,
UPDATE_DOWNLOAD_PROGRESS, UPDATE_DOWNLOAD_PROGRESS,
@ -43,12 +40,14 @@ contextBridge.exposeInMainWorld("heynote", {
}) })
}, },
onWindowClose(callback) { mainProcess: {
ipcRenderer.on(WINDOW_CLOSE_EVENT, callback) on(event, callback) {
}, ipcRenderer.on(event, callback)
},
onOpenSettings(callback) {
ipcRenderer.on(OPEN_SETTINGS_EVENT, callback) off(event, callback) {
ipcRenderer.off(event, callback)
},
}, },
buffer: { buffer: {
@ -125,10 +124,6 @@ contextBridge.exposeInMainWorld("heynote", {
return await getCurrencyData() return await getCurrencyData()
}, },
onSettingsChange(callback) {
ipcRenderer.on(SETTINGS_CHANGE_EVENT, (event, settings) => callback(settings))
},
autoUpdate: { autoUpdate: {
callbacks(callbacks) { callbacks(callbacks) {
ipcRenderer.on(UPDATE_AVAILABLE_EVENT, (event, info) => callbacks?.updateAvailable(info)) ipcRenderer.on(UPDATE_AVAILABLE_EVENT, (event, info) => callbacks?.updateAvailable(info))
@ -163,7 +158,7 @@ contextBridge.exposeInMainWorld("heynote", {
}) })
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) { function domReady(condition=['complete', 'interactive']) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (condition.includes(document.readyState)) { if (condition.includes(document.readyState)) {
resolve(true) resolve(true)
@ -178,12 +173,12 @@ function domReady(condition: DocumentReadyState[] = ['complete', 'interactive'])
} }
const safeDOM = { const safeDOM = {
append(parent: HTMLElement, child: HTMLElement) { append(parent, child) {
if (!Array.from(parent.children).find(e => e === child)) { if (!Array.from(parent.children).find(e => e === child)) {
return parent.appendChild(child) return parent.appendChild(child)
} }
}, },
remove(parent: HTMLElement, child: HTMLElement) { remove(parent, child) {
if (Array.from(parent.children).find(e => e === child)) { if (Array.from(parent.children).find(e => e === child)) {
return parent.removeChild(child) return parent.removeChild(child)
} }

View File

@ -5,6 +5,8 @@
import { useHeynoteStore } from "../stores/heynote-store" import { useHeynoteStore } from "../stores/heynote-store"
import { useErrorStore } from "../stores/error-store" import { useErrorStore } from "../stores/error-store"
import { OPEN_SETTINGS_EVENT, SETTINGS_CHANGE_EVENT } from '@/src/common/constants'
import StatusBar from './StatusBar.vue' import StatusBar from './StatusBar.vue'
import Editor from './Editor.vue' import Editor from './Editor.vue'
import LanguageSelector from './LanguageSelector.vue' import LanguageSelector from './LanguageSelector.vue'
@ -52,10 +54,10 @@
} }
onThemeChange(window.heynote.themeMode.initial) onThemeChange(window.heynote.themeMode.initial)
window.heynote.themeMode.onChange(onThemeChange) window.heynote.themeMode.onChange(onThemeChange)
window.heynote.onSettingsChange((settings) => { window.heynote.mainProcess.on(SETTINGS_CHANGE_EVENT, (event, settings) => {
this.settings = settings this.settings = settings
}) })
window.heynote.onOpenSettings(() => { window.heynote.mainProcess.on(OPEN_SETTINGS_EVENT, () => {
this.showSettings = true this.showSettings = true
}) })
}, },

View File

@ -6,6 +6,7 @@
import { useErrorStore } from "../stores/error-store" import { useErrorStore } from "../stores/error-store"
import { useHeynoteStore } from "../stores/heynote-store.js" import { useHeynoteStore } from "../stores/heynote-store.js"
import { useEditorCacheStore } from "../stores/editor-cache" import { useEditorCacheStore } from "../stores/editor-cache"
import { REDO_EVENT, WINDOW_CLOSE_EVENT } from '@/src/common/constants';
const NUM_EDITOR_INSTANCES = 5 const NUM_EDITOR_INSTANCES = 5
@ -46,6 +47,7 @@
return { return {
syntaxTreeDebugContent: null, syntaxTreeDebugContent: null,
editor: null, editor: null,
onWindowClose: null,
} }
}, },
@ -53,12 +55,13 @@
this.loadBuffer(this.currentBufferPath) this.loadBuffer(this.currentBufferPath)
// set up window close handler that will save the buffer and quit // set up window close handler that will save the buffer and quit
window.heynote.onWindowClose(() => { this.onWindowClose = () => {
window.heynote.buffer.saveAndQuit([ window.heynote.buffer.saveAndQuit([
[this.editor.path, this.editor.getContent()], [this.editor.path, this.editor.getContent()],
]) ])
}) }
window.heynote.mainProcess.on(WINDOW_CLOSE_EVENT, this.onWindowClose)
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded) window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)
// if debugSyntaxTree prop is set, display syntax tree for debugging // if debugSyntaxTree prop is set, display syntax tree for debugging
@ -82,6 +85,7 @@
}, },
beforeUnmount() { beforeUnmount() {
window.heynote.mainProcess.off(WINDOW_CLOSE_EVENT, this.onWindowClose)
window.document.removeEventListener("currenciesLoaded", this.onCurrenciesLoaded) window.document.removeEventListener("currenciesLoaded", this.onCurrenciesLoaded)
}, },

View File

@ -84,7 +84,7 @@ export default defineConfig({
}, },
}, },
{ {
entry: 'electron/preload/index.ts', entry: 'electron/preload/index.js',
onstart(options) { onstart(options) {
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete, // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
// instead of restarting the entire Electron App. // instead of restarting the entire Electron App.

View File

@ -64,6 +64,12 @@ class IpcRenderer {
this.callbacks[event].push(callback) this.callbacks[event].push(callback)
} }
off(event, callback) {
if (this.callbacks[event]) {
this.callbacks[event] = this.callbacks[event].filter(cb => cb !== callback)
}
}
send(event, ...args) { send(event, ...args) {
if (this.callbacks[event]) { if (this.callbacks[event]) {
for (const callback of this.callbacks[event]) { for (const callback of this.callbacks[event]) {
@ -213,20 +219,18 @@ const Heynote = {
}, },
}, },
onWindowClose(callback) { mainProcess: {
//ipcRenderer.on(WINDOW_CLOSE_EVENT, callback) on(event, callback) {
ipcRenderer.on(event, callback)
},
off(event, callback) {
ipcRenderer.off(event, callback)
},
}, },
settings: initialSettings, settings: initialSettings,
onOpenSettings(callback) {
ipcRenderer.on(OPEN_SETTINGS_EVENT, callback)
},
onSettingsChange(callback) {
ipcRenderer.on(SETTINGS_CHANGE_EVENT, (event, settings) => callback(settings))
},
setSettings(settings) { setSettings(settings) {
localStorage.setItem("settings", JSON.stringify(settings)) localStorage.setItem("settings", JSON.stringify(settings))
ipcRenderer.send(SETTINGS_CHANGE_EVENT, settings) ipcRenderer.send(SETTINGS_CHANGE_EVENT, settings)