ref: update electron code to TypeScript

This commit is contained in:
alikia2x (寒寒) 2024-12-01 01:04:17 +08:00
parent 2c6c07647a
commit ab9a51bfad
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
11 changed files with 1925 additions and 18 deletions

View File

@ -8,6 +8,6 @@
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>
<script type="module" src="/src/main.tsx"></script> <script type="module" src="/src/web/main.tsx"></script>
</body> </body>
</html> </html>

View File

@ -7,13 +7,17 @@
"dev": "cross-env NODE_ENV=dev bun run dev:all", "dev": "cross-env NODE_ENV=dev bun run dev:all",
"dev:all": "concurrently -n=react,electron -c='#ff3e00',blue \"bun run dev:react\" \"bun run dev:electron\"", "dev:all": "concurrently -n=react,electron -c='#ff3e00',blue \"bun run dev:react\" \"bun run dev:electron\"",
"dev:react": "vite dev", "dev:react": "vite dev",
"dev:electron": "electron src/electron.js" "dev:electron": "tsc && electron dist/dev/index.js",
"build": "cross-env NODE_ENV=production bun run build:all",
"build:all": "concurrently -n=react,electron -c='#ff3e00',blue \"bun run build:react\" \"bun run build:electron\"",
"build:react": "vite build",
"build:electron": "electron-builder",
"start": "cross-env NODE_ENV=production bun run start:all"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"electron": "^33.2.0",
"electron-context-menu": "^4.0.4", "electron-context-menu": "^4.0.4",
"electron-reloader": "^1.2.3", "electron-reloader": "^1.2.3",
"electron-serve": "^2.1.1", "electron-serve": "^2.1.1",
@ -28,12 +32,14 @@
"vite-tsconfig-paths": "^5.1.3" "vite-tsconfig-paths": "^5.1.3"
}, },
"devDependencies": { "devDependencies": {
"electron": "^33.2.0",
"@eslint/js": "^9.13.0", "@eslint/js": "^9.13.0",
"@types/react": "^18.3.12", "@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "^4.3.3", "@vitejs/plugin-react": "^4.3.3",
"concurrently": "^9.0.1", "concurrently": "^9.0.1",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"electron-builder": "^25.1.8",
"eslint": "^9.13.0", "eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0", "eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14", "eslint-plugin-react-refresh": "^0.4.14",

File diff suppressed because it is too large Load Diff

View File

@ -15,14 +15,14 @@ function createTray() {
label: '显示主窗口', label: '显示主窗口',
click: () => { click: () => {
if (!mainWindow) createMainWindow(); if (!mainWindow) createMainWindow();
mainWindow.show(); mainWindow!.show();
} }
}, },
{ {
label: '显示设置', label: '显示设置',
click: () => { click: () => {
if (!settingsWindow) createSettingsWindow(); if (!settingsWindow) createSettingsWindow();
settingsWindow.show(); settingsWindow!.show();
} }
}, },
{ type: 'separator' }, { type: 'separator' },
@ -50,8 +50,8 @@ const serveURL = serve({ directory: '.' });
const port = process.env.PORT || "5173"; const port = process.env.PORT || "5173";
const dev = !app.isPackaged; const dev = !app.isPackaged;
let mainWindow; let mainWindow: BrowserWindow | null;
let settingsWindow; let settingsWindow: BrowserWindow | null;
function createSettingsWindow() { function createSettingsWindow() {
@ -71,7 +71,7 @@ function createSettingsWindow() {
settingsWindow = window; settingsWindow = window;
if (dev) loadVite(window ,port, "settings"); if (dev) loadVite(window ,port, "settings");
else serveURL(mainWindow); else serveURL(mainWindow!);
} }
contextMenu({ contextMenu({
@ -80,7 +80,7 @@ contextMenu({
showCopyImage: true, showCopyImage: true,
}); });
function loadVite(window, port, path = "") { function loadVite(window: BrowserWindow, port: string | undefined, path = "") {
console.log(`http://localhost:${port}/${path}`); console.log(`http://localhost:${port}/${path}`);
window.loadURL(`http://localhost:${port}/${path}`).catch((e) => { window.loadURL(`http://localhost:${port}/${path}`).catch((e) => {
console.log('Error loading URL, retrying', e); console.log('Error loading URL, retrying', e);
@ -134,8 +134,8 @@ function createMainWindow() {
mainWindow = window; mainWindow = window;
if (dev) loadVite(port); if (dev) loadVite(window, port);
else serveURL(mainWindow); else serveURL(mainWindow).then(()=>{});
} }
app.once('ready', () => { app.once('ready', () => {
@ -147,6 +147,7 @@ app.on('activate', () => {
app.on('ready', () => { app.on('ready', () => {
createTray(); createTray();
globalShortcut.register('Escape', () => { globalShortcut.register('Escape', () => {
if (!mainWindow) return;
mainWindow.hide(); mainWindow.hide();
}); });
}); });
@ -155,5 +156,6 @@ app.on('window-all-closed', () => {
}); });
ipcMain.on('to-main', (_event, count) => { ipcMain.on('to-main', (_event, count) => {
if (!mainWindow) return;
return mainWindow.webContents.send('from-main', `next count is ${count + 1}`); return mainWindow.webContents.send('from-main', `next count is ${count + 1}`);
}); });

View File

@ -1,6 +1,6 @@
import { StrictMode } from "react"; import { StrictMode } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { App } from "./app"; import { App } from "./app.tsx";
import "./index.css"; import "./index.css";
const app = createRoot(document.getElementById("root")!); const app = createRoot(document.getElementById("root")!);

View File

@ -15,7 +15,6 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"moduleDetection": "force", "moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx", "jsx": "react-jsx",
/* Linting */ /* Linting */
@ -24,5 +23,5 @@
"noUnusedParameters": true, "noUnusedParameters": true,
"noFallthroughCasesInSwitch": true "noFallthroughCasesInSwitch": true
}, },
"include": ["src", "**/*.ts", "**/*.tsx", "global.d.ts"] "include": ["src/web", "src/web/**/*.ts", "src/web/**/*.tsx", "global.d.ts"],
} }

View File

@ -1,5 +1,19 @@
{ {
"files": [], // "files": [
// "src/electron/index.ts"
// ],
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "./dist/dev",
"rootDir": "./src/electron",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/electron"],
"references": [ "references": [
{ "path": "./tsconfig.app.json"}, { "path": "./tsconfig.app.json"},
{ "path": "./tsconfig.node.json"} { "path": "./tsconfig.node.json"}

View File

@ -5,13 +5,13 @@
"lib": ["ES2023"], "lib": ["ES2023"],
"module": "ESNext", "module": "ESNext",
"skipLibCheck": true, "skipLibCheck": true,
"composite": true,
/* Bundler mode */ /* Bundler mode */
"moduleResolution": "Bundler", "moduleResolution": "Bundler",
"allowImportingTsExtensions": true, "allowImportingTsExtensions": true,
"isolatedModules": true, "isolatedModules": true,
"moduleDetection": "force", "moduleDetection": "force",
"noEmit": true,
/* Linting */ /* Linting */
"strict": true, "strict": true,