diff --git a/src/common/constant/event.ts b/src/common/constant/event.ts new file mode 100644 index 0000000..fcf6304 --- /dev/null +++ b/src/common/constant/event.ts @@ -0,0 +1,12 @@ +/** + * 主进程、渲染进程公用事件常量 + */ + + /** 退出 */ +export const LOGOUT = '@event/logout' + +/** 登录 */ +export const LOGIN = '@event/login' + +/** 切换开发者工具 */ +export const TOGGLE_DEVTOOLS = 'toggle-devtools' diff --git a/src/common/constant/index.ts b/src/common/constant/index.ts new file mode 100644 index 0000000..ffe56f2 --- /dev/null +++ b/src/common/constant/index.ts @@ -0,0 +1,2 @@ + +export * as event from './event' diff --git a/src/common/patch.ts b/src/common/patch.ts deleted file mode 100644 index e886dfb..0000000 --- a/src/common/patch.ts +++ /dev/null @@ -1,5 +0,0 @@ -/** - * !!! ensure process.cwd() correct - * for load-on preload.js - */ -// process.chdir(__dirname.slice(0, __dirname.lastIndexOf('dist'))) 21-08-05 remove diff --git a/src/common/utils/store.ts b/src/common/utils/store.ts new file mode 100644 index 0000000..9815e78 --- /dev/null +++ b/src/common/utils/store.ts @@ -0,0 +1,7 @@ +import Store from 'electron-store' + +export default new Store + +export const TOKEN = 'token' + +export const USER_INFO = '@user/info' diff --git a/src/main/communication.ts b/src/main/communication.ts new file mode 100644 index 0000000..376cb25 --- /dev/null +++ b/src/main/communication.ts @@ -0,0 +1,16 @@ +/** + * Expose something function to renderer + */ +import { BrowserWindow, ipcMain } from 'electron' +import { + LOGIN, + LOGOUT, + TOGGLE_DEVTOOLS, +} from '@/common/constant/event' + +export function register(win: BrowserWindow) { + ipcMain.handle(TOGGLE_DEVTOOLS, () => { + win.webContents.toggleDevTools() + }) + ipcMain.handle(LOGOUT, () => { }) +} diff --git a/src/main/index.ts b/src/main/index.ts index cf6821f..5c2e7cd 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,31 +1,28 @@ -/** - * electron 主文件 - */ -import { join } from 'path' +import path from 'path' import { app, BrowserWindow } from 'electron' -import dotenv from 'dotenv' +import { register } from './communication' -dotenv.config({ path: join(__dirname, '../../.env') }) +let win: BrowserWindow | null = null -let win: BrowserWindow - -function createWin() { - // 创建浏览器窗口 +function bootstrap() { win = new BrowserWindow({ - width: 1024, - height: 768, webPreferences: { - nodeIntegration: true, - contextIsolation: false, - preload: join(__dirname, '../preload/index.js'), + preload: path.join(__dirname, '../preload/index.js'), }, }) - const URL = app.isPackaged - ? `file://${join(__dirname, '../render/index.html')}` // vite 构建后的静态文件地址 - : `http://localhost:${process.env.PORT}` // vite 启动的服务器地址 + if (app.isPackaged) { + win.loadFile(path.join(__dirname, '../render/index.html')) + } else { + win.maximize() + win.webContents.openDevTools() + win.loadURL(`http://localhost:${process.env.PORT}`) + } - win?.loadURL(URL) + // something init setup + register(win) } -app.whenReady().then(createWin) +app.whenReady().then(bootstrap) + +app.on('window-all-closed', () => { win = null }) diff --git a/src/preload/communication.ts b/src/preload/communication.ts new file mode 100644 index 0000000..bc4ac4d --- /dev/null +++ b/src/preload/communication.ts @@ -0,0 +1,8 @@ +/** + * Renderer and Main bridge + * @refresh https://newbedev.com/how-to-use-preload-js-properly-in-electron + */ + import { contextBridge, ipcRenderer } from 'electron' + + contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer) + \ No newline at end of file diff --git a/src/preload/index.ts b/src/preload/index.ts new file mode 100644 index 0000000..30915cf --- /dev/null +++ b/src/preload/index.ts @@ -0,0 +1,5 @@ +import './communication' +import { domReady } from './utils/dom' +import { loading } from './loading' + +domReady().then(loading) \ No newline at end of file diff --git a/src/preload/loading.html b/src/preload/loading.html index d648f0b..c8251f3 100644 --- a/src/preload/loading.html +++ b/src/preload/loading.html @@ -1,5 +1,4 @@ - - + diff --git a/src/preload/index.js b/src/preload/loading.ts similarity index 61% rename from src/preload/index.js rename to src/preload/loading.ts index af51596..e68da3e 100644 --- a/src/preload/index.js +++ b/src/preload/loading.ts @@ -1,28 +1,11 @@ -// @ts-nocheck +import { contextBridge } from 'electron' -/** docoment 加载完成 */ -function domReady(...args) { - const condition = args.length ? [...args] : ['complete', 'interactive'] - return new Promise(resolve => { - if (condition.includes(document.readyState)) { - resolve(true) - } else { - document.addEventListener('readystatechange', () => { - if (condition.includes(document.readyState)) { - resolve(true) - } - }) - } - }) -} - -/** 插入 loading */ function loadingBootstrap() { - const loadingStyle = document.createElement('style'); - const loadingBox = document.createElement('div'); + const loadingStyle = document.createElement('style') + const loadingBox = document.createElement('div') - loadingStyle.id = 'preload-loading-style'; - loadingBox.id = 'preload-loading-box'; + loadingStyle.id = 'preload-loading-style' + loadingBox.id = 'preload-loading-box' loadingStyle.textContent += ` /* https://projects.lukehaas.me/css-loaders/ */ @@ -96,41 +79,40 @@ function loadingBootstrap() { box-shadow: 0 -2em; height: 5em; } - }`; + }` - loadingBox.classList.add('loading-box', 'load1'); - loadingBox.innerHTML += '
'; + loadingBox.classList.add('loading-box', 'load1') + loadingBox.innerHTML += '' const appendLoading = () => { - document.head.appendChild(loadingStyle); - document.body.appendChild(loadingBox); - }; + document.head.appendChild(loadingStyle) + document.body.appendChild(loadingBox) + } const removeLoading = () => { - const _loadingStyle = document.getElementById('preload-loading-style'); - const _loadingBox = document.getElementById('preload-loading-box'); + const _loadingStyle = document.getElementById('preload-loading-style') + const _loadingBox = document.getElementById('preload-loading-box') // Ensure the remove child exists. - _loadingStyle && document.head.removeChild(_loadingStyle); - _loadingBox && document.body.removeChild(_loadingBox); + _loadingStyle && document.head.removeChild(_loadingStyle) + _loadingBox && document.body.removeChild(_loadingBox) }; return { loadingStyle, loadingBox, removeLoading, appendLoading } } -; (async function () { - await domReady(); - - let _isCallRemoveLoading = false; +/** 闪屏 loading */ +export function loading() { + let _isCallRemoveLoading = false const { removeLoading, appendLoading } = loadingBootstrap(); - window.removeLoading = () => { - _isCallRemoveLoading = true; - removeLoading(); - }; + contextBridge.exposeInMainWorld('removeLoading', () => { + _isCallRemoveLoading = true + removeLoading() + }) // 5 秒超时自动关闭 - setTimeout(() => !_isCallRemoveLoading && removeLoading(), 4999); + setTimeout(() => !_isCallRemoveLoading && removeLoading(), 4999) - appendLoading(); -})(); + appendLoading() +} diff --git a/src/preload/utils/dom.ts b/src/preload/utils/dom.ts new file mode 100644 index 0000000..6c8e0c7 --- /dev/null +++ b/src/preload/utils/dom.ts @@ -0,0 +1,15 @@ + +/** docoment 加载完成 */ +export function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) { + return new Promise(resolve => { + if (condition.includes(document.readyState)) { + resolve(true) + } else { + document.addEventListener('readystatechange', () => { + if (condition.includes(document.readyState)) { + resolve(true) + } + }) + } + }) +} diff --git a/src/render/App.vue b/src/render/App.vue index aa00795..1e4bb4a 100644 --- a/src/render/App.vue +++ b/src/render/App.vue @@ -3,7 +3,7 @@ -