Feat: all - support URL protocol

This commit is contained in:
2024-03-19 16:54:36 +08:00
parent 6f53a867c3
commit 8730513340
7 changed files with 95 additions and 8 deletions

13
src/main/global.d.ts vendored Normal file
View File

@@ -0,0 +1,13 @@
type Platform = 'WEB' | 'DESKTOP' | 'ANDROID'
interface ImportMetaEnv {
readonly VITE_PLATFORM: Platform
readonly VITE_PROTOCOL: string
readonly VITE_API_URL: string
readonly VITE_API_TOKEN_URL: string
readonly VITE_TURNSTILE_SITE_KEY: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}

View File

@@ -1,11 +1,58 @@
import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { app, shell, BrowserWindow, ipcMain, webContents } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/logo.ico?asset'
import * as path from 'node:path'
let mainWindow: BrowserWindow
// Application singleton execution
if (!app.requestSingleInstanceLock()) {
app.quit()
}
// Register protocol client
const args: string[] = []
if (!app.isPackaged) {
args.push(path.resolve(process.argv[1]))
}
args.push('--')
app.setAsDefaultProtocolClient(import.meta.env.VITE_PROTOCOL, process.execPath, args)
const handleArgv = (argv: string[]) => {
const prefix = `${import.meta.env.VITE_PROTOCOL}:`
const offset = app.isPackaged ? 1 : 2
const url = argv.find((arg, index) => index >= offset && arg.startsWith(prefix))
if (url) {
handleUrl(url)
}
}
const handleUrl = (url: string) => {
const { hostname, pathname } = new URL(url)
if (hostname === 'openurl') {
webContents.getAllWebContents().forEach((webContent) => {
webContent.send('open-url', pathname)
})
}
}
// Windows
handleArgv(process.argv)
app.on('second-instance', (_, argv) => {
if (process.platform === 'win32') {
handleArgv(argv)
}
})
// macOS
app.on('open-url', (_, argv) => {
handleUrl(argv)
})
function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 900,
height: 670,
show: false,

View File

@@ -9,15 +9,15 @@ const api = {}
// just add to the DOM global.
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('electronAPI', electronAPI)
contextBridge.exposeInMainWorld('api', api)
contextBridge.exposeInMainWorld('notification', Notification)
contextBridge.exposeInMainWorld('Notification', Notification)
} catch (error) {
console.error(error)
}
} else {
// @ts-expect-error (define in dts)
window.electron = electronAPI
window.electronAPI = electronAPI
// @ts-expect-error (define in dts)
window.api = api
// @ts-expect-error (define in dts)

View File

@@ -3,6 +3,7 @@ import { getRedirectUrl } from '@/util/route'
import { getLoginStatus, getVerifyStatus_async } from '@/util/auth'
const AuthRoute = () => {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const matches = useMatches()
const lastMatch = matches.reduce((_, second) => second)
@@ -12,6 +13,12 @@ const AuthRoute = () => {
const isLogin = getLoginStatus()
const isVerify = getVerifyStatus_async()
useEffect(() => {
window.electronAPI.ipcRenderer.on('open-url', (_, url: string) => {
navigate(url)
})
}, [])
return useMemo(() => {
document.title = `${handle?.titlePrefix ?? ''}${
handle?.title ? handle?.title : PRODUCTION_NAME

8
src/renderer/src/electron.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
import { ElectronAPI } from "@electron-toolkit/preload";
import { Notification } from "electron";
declare global {
type _ElectronAPI = ElectronAPI
class _Notification extends Notification {}
}

View File

@@ -1,10 +1,12 @@
/// <reference types="vite/client" />
/// <reference types="./electron" />
/// <reference types="./ant-design" />
type Platform = 'WEB' | 'DESKTOP' | 'ANDROID'
interface ImportMetaEnv {
readonly VITE_PLATFORM: Platform
readonly VITE_PROTOCOL: string
readonly VITE_API_URL: string
readonly VITE_API_TOKEN_URL: string
readonly VITE_TURNSTILE_SITE_KEY: string
@@ -14,6 +16,11 @@ interface ImportMeta {
readonly env: ImportMetaEnv
}
interface Window {
electronAPI: _ElectronAPI
Notification: typeof _Notification
}
interface RouteJsonObject {
path: string
absolutePath: string