2023-01-16 15:31:46 +01:00
|
|
|
const { contextBridge } = require('electron')
|
|
|
|
import darkMode from "./theme-mode"
|
2023-01-19 00:20:50 +01:00
|
|
|
import { isMac, isWindows, isLinux } from "../detect-platform"
|
|
|
|
import { ipcRenderer } from "electron"
|
2023-01-20 15:33:26 +01:00
|
|
|
import { WINDOW_CLOSE_EVENT, KEYMAP_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../constants"
|
|
|
|
import CONFIG from "../config"
|
2023-01-15 11:59:17 +01:00
|
|
|
|
2023-01-25 10:11:51 +01:00
|
|
|
//contextBridge.exposeInMainWorld("platform", )
|
2023-01-16 15:31:46 +01:00
|
|
|
contextBridge.exposeInMainWorld('darkMode', darkMode)
|
2023-01-15 11:59:17 +01:00
|
|
|
|
2023-01-19 00:20:50 +01:00
|
|
|
contextBridge.exposeInMainWorld("heynote", {
|
2023-01-25 10:11:51 +01:00
|
|
|
platform: {
|
|
|
|
isMac,
|
|
|
|
isWindows,
|
|
|
|
isLinux,
|
|
|
|
},
|
|
|
|
|
2023-01-19 00:20:50 +01:00
|
|
|
quit() {
|
|
|
|
console.log("quitting")
|
|
|
|
//ipcRenderer.invoke("app_quit")
|
|
|
|
},
|
|
|
|
|
|
|
|
onWindowClose(callback) {
|
|
|
|
ipcRenderer.on(WINDOW_CLOSE_EVENT, callback)
|
|
|
|
},
|
|
|
|
|
2023-01-20 15:33:26 +01:00
|
|
|
onOpenSettings(callback) {
|
|
|
|
ipcRenderer.on(OPEN_SETTINGS_EVENT, callback)
|
|
|
|
},
|
|
|
|
|
2023-01-19 00:20:50 +01:00
|
|
|
buffer: {
|
|
|
|
async load() {
|
|
|
|
return await ipcRenderer.invoke("buffer-content:load")
|
|
|
|
},
|
2023-01-25 10:11:51 +01:00
|
|
|
|
2023-01-19 00:20:50 +01:00
|
|
|
async save(content) {
|
|
|
|
return await ipcRenderer.invoke("buffer-content:save", content)
|
|
|
|
},
|
|
|
|
|
|
|
|
async saveAndQuit(content) {
|
|
|
|
return await ipcRenderer.invoke("buffer-content:saveAndQuit", content)
|
|
|
|
},
|
2023-01-20 15:33:26 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
keymap: {
|
|
|
|
set(keymap) {
|
|
|
|
ipcRenderer.invoke("keymap:set", keymap);
|
|
|
|
},
|
2023-01-25 10:11:51 +01:00
|
|
|
setEmacsMetaKey(key) {
|
|
|
|
CONFIG.set("emacsMetaKey", key)
|
|
|
|
},
|
2023-01-20 15:33:26 +01:00
|
|
|
initial: CONFIG.get("keymap", "default"),
|
2023-01-25 10:11:51 +01:00
|
|
|
getEmacsMetaKey: () => CONFIG.get("emacsMetaKey", isMac ? "meta" : "alt"),
|
2023-01-20 15:33:26 +01:00
|
|
|
onKeymapChange(callback) {
|
|
|
|
ipcRenderer.on(KEYMAP_CHANGE_EVENT, (event, keymap) => callback(keymap))
|
|
|
|
},
|
|
|
|
},
|
2023-01-19 00:20:50 +01:00
|
|
|
})
|
|
|
|
|
2023-01-15 11:59:17 +01:00
|
|
|
|
2023-01-12 18:55:55 +01:00
|
|
|
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
2023-01-25 10:10:11 +01:00
|
|
|
return new Promise((resolve) => {
|
2023-01-12 18:55:55 +01:00
|
|
|
if (condition.includes(document.readyState)) {
|
2023-01-25 10:10:11 +01:00
|
|
|
resolve(true)
|
|
|
|
} else {
|
|
|
|
document.addEventListener('readystatechange', () => {
|
|
|
|
if (condition.includes(document.readyState)) {
|
|
|
|
resolve(true)
|
|
|
|
}
|
|
|
|
})
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
2023-01-25 10:10:11 +01:00
|
|
|
})
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const safeDOM = {
|
2023-01-25 10:10:11 +01:00
|
|
|
append(parent: HTMLElement, child: HTMLElement) {
|
|
|
|
if (!Array.from(parent.children).find(e => e === child)) {
|
|
|
|
return parent.appendChild(child)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remove(parent: HTMLElement, child: HTMLElement) {
|
|
|
|
if (Array.from(parent.children).find(e => e === child)) {
|
|
|
|
return parent.removeChild(child)
|
|
|
|
}
|
|
|
|
},
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 21:36:31 +01:00
|
|
|
|
2023-01-12 18:55:55 +01:00
|
|
|
function useLoading() {
|
2023-01-25 10:10:11 +01:00
|
|
|
const className = `loaders-css__square-spin`
|
|
|
|
const styleContent = `
|
2023-01-12 18:55:55 +01:00
|
|
|
.app-loading-wrap {
|
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 100vw;
|
|
|
|
height: 100vh;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
2023-01-18 21:36:31 +01:00
|
|
|
background: #fff;
|
2023-01-12 18:55:55 +01:00
|
|
|
z-index: 9;
|
2023-01-18 21:36:31 +01:00
|
|
|
}
|
|
|
|
@media (prefers-color-scheme: dark) {
|
|
|
|
.${className} > div {
|
|
|
|
background: #fff;
|
|
|
|
}
|
|
|
|
.app-loading-wrap {
|
|
|
|
background: #262B37
|
|
|
|
}
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
|
|
|
`
|
2023-01-25 10:10:11 +01:00
|
|
|
const oStyle = document.createElement('style')
|
|
|
|
const oDiv = document.createElement('div')
|
|
|
|
|
|
|
|
oStyle.id = 'app-loading-style'
|
|
|
|
oStyle.innerHTML = styleContent
|
|
|
|
oDiv.className = 'app-loading-wrap'
|
|
|
|
oDiv.innerHTML = `<div class="${className}"></div>`
|
|
|
|
|
|
|
|
return {
|
|
|
|
appendLoading() {
|
|
|
|
safeDOM.append(document.head, oStyle)
|
|
|
|
safeDOM.append(document.body, oDiv)
|
|
|
|
},
|
|
|
|
removeLoading() {
|
|
|
|
safeDOM.remove(document.head, oStyle)
|
|
|
|
safeDOM.remove(document.body, oDiv)
|
|
|
|
},
|
|
|
|
}
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
const { appendLoading, removeLoading } = useLoading()
|
|
|
|
domReady().then(appendLoading)
|
|
|
|
|
|
|
|
window.onmessage = (ev) => {
|
2023-01-25 10:10:11 +01:00
|
|
|
ev.data.payload === 'removeLoading' && removeLoading()
|
2023-01-12 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(removeLoading, 4999)
|