2024-01-04 14:27:04 +01:00
|
|
|
import { SETTINGS_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../electron/constants";
|
|
|
|
|
2023-12-25 14:18:44 +01:00
|
|
|
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)')
|
|
|
|
let themeCallback = null
|
2023-12-28 16:48:56 +01:00
|
|
|
mediaMatch.addEventListener("change", async (event) => {
|
|
|
|
if (themeCallback) {
|
|
|
|
themeCallback((await Heynote.themeMode.get()).computed)
|
|
|
|
}
|
|
|
|
})
|
2023-12-25 14:18:44 +01:00
|
|
|
|
2023-12-28 16:48:56 +01:00
|
|
|
let autoUpdateCallbacks = null
|
2023-12-25 14:18:44 +01:00
|
|
|
let currencyData = null
|
|
|
|
|
2023-12-28 18:01:10 +01:00
|
|
|
let platform
|
|
|
|
const uaPlatform = window.navigator?.userAgentData?.platform || window.navigator.platform
|
|
|
|
if (uaPlatform.indexOf("Win") !== -1) {
|
|
|
|
platform = {
|
|
|
|
isMac: false,
|
|
|
|
isWindows: true,
|
|
|
|
isLinux: false,
|
|
|
|
}
|
|
|
|
} else if (uaPlatform.indexOf("Linux") !== -1) {
|
|
|
|
platform = {
|
|
|
|
isMac: false,
|
|
|
|
isWindows: false,
|
|
|
|
isLinux: true,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
platform = {
|
2023-12-25 14:18:44 +01:00
|
|
|
isMac: true,
|
|
|
|
isWindows: false,
|
|
|
|
isLinux: false,
|
2023-12-28 18:01:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:27:04 +01:00
|
|
|
|
|
|
|
class IpcRenderer {
|
|
|
|
constructor() {
|
|
|
|
this.callbacks = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
on(event, callback) {
|
|
|
|
if (!this.callbacks[event]) {
|
|
|
|
this.callbacks[event] = []
|
|
|
|
}
|
|
|
|
this.callbacks[event].push(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
send(event, ...args) {
|
|
|
|
if (this.callbacks[event]) {
|
|
|
|
for (const callback of this.callbacks[event]) {
|
|
|
|
callback(null, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ipcRenderer = new IpcRenderer()
|
|
|
|
|
|
|
|
// get initial settings
|
|
|
|
let settingsData = localStorage.getItem("settings")
|
|
|
|
let initialSettings = {
|
|
|
|
keymap: "default",
|
|
|
|
emacsMetaKey: "meta",
|
|
|
|
showLineNumberGutter: true,
|
|
|
|
showFoldGutter: true,
|
2024-01-04 14:52:41 +01:00
|
|
|
bracketClosing: false,
|
2024-01-04 14:27:04 +01:00
|
|
|
}
|
|
|
|
if (settingsData !== null) {
|
|
|
|
initialSettings = Object.assign(initialSettings, JSON.parse(settingsData))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-28 18:01:10 +01:00
|
|
|
const Heynote = {
|
|
|
|
platform: platform,
|
2024-01-04 14:27:04 +01:00
|
|
|
isWebApp: true,
|
2023-12-25 14:18:44 +01:00
|
|
|
|
|
|
|
buffer: {
|
|
|
|
async load() {
|
|
|
|
const content = localStorage.getItem("buffer")
|
|
|
|
return content === null ? "\n∞∞∞text-a\n" : content
|
|
|
|
},
|
|
|
|
|
|
|
|
async save(content) {
|
|
|
|
localStorage.setItem("buffer", content)
|
|
|
|
},
|
|
|
|
|
|
|
|
async saveAndQuit(content) {
|
|
|
|
|
|
|
|
},
|
2024-01-01 19:04:40 +01:00
|
|
|
|
|
|
|
onChangeCallback(callback) {
|
|
|
|
|
|
|
|
},
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onWindowClose(callback) {
|
|
|
|
//ipcRenderer.on(WINDOW_CLOSE_EVENT, callback)
|
|
|
|
},
|
|
|
|
|
2024-01-04 14:27:04 +01:00
|
|
|
settings: initialSettings,
|
|
|
|
|
2023-12-25 14:18:44 +01:00
|
|
|
onOpenSettings(callback) {
|
2024-01-04 14:27:04 +01:00
|
|
|
ipcRenderer.on(OPEN_SETTINGS_EVENT, callback)
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onSettingsChange(callback) {
|
2024-01-04 14:27:04 +01:00
|
|
|
ipcRenderer.on(SETTINGS_CHANGE_EVENT, (event, settings) => callback(settings))
|
|
|
|
},
|
|
|
|
|
|
|
|
setSettings(settings) {
|
|
|
|
localStorage.setItem("settings", JSON.stringify(settings))
|
|
|
|
ipcRenderer.send(SETTINGS_CHANGE_EVENT, settings)
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
themeMode: {
|
|
|
|
set: (mode) => {
|
2023-12-28 16:48:56 +01:00
|
|
|
localStorage.setItem("theme", mode)
|
2023-12-25 14:18:44 +01:00
|
|
|
themeCallback(mode)
|
2023-12-28 16:48:56 +01:00
|
|
|
console.log("set theme to", mode)
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
|
|
|
get: async () => {
|
2023-12-28 16:48:56 +01:00
|
|
|
const theme = localStorage.getItem("theme") || "system"
|
|
|
|
const systemTheme = mediaMatch.matches ? "dark" : "light"
|
2023-12-25 14:18:44 +01:00
|
|
|
return {
|
2023-12-28 16:48:56 +01:00
|
|
|
theme: theme,
|
|
|
|
computed: theme === "system" ? systemTheme : theme,
|
2023-12-25 14:18:44 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onChange: (callback) => {
|
|
|
|
themeCallback = callback
|
|
|
|
},
|
|
|
|
removeListener() {
|
2023-12-28 16:48:56 +01:00
|
|
|
themeCallback = null
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
2023-12-28 16:48:56 +01:00
|
|
|
initial: localStorage.getItem("theme") || "system",
|
2023-12-25 14:18:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getCurrencyData: async () => {
|
|
|
|
if (currencyData !== null) {
|
|
|
|
return currencyData
|
|
|
|
}
|
|
|
|
const response = await fetch("https://currencies.heynote.com/rates.json", {cache: "no-cache"})
|
|
|
|
currencyData = JSON.parse(await response.text())
|
|
|
|
return currencyData
|
|
|
|
},
|
|
|
|
}
|
2023-12-28 16:48:56 +01:00
|
|
|
|
2024-01-04 14:27:04 +01:00
|
|
|
export { Heynote, ipcRenderer}
|