v0.6.0 BigUpdate
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { app, BrowserWindow, ipcMain } from 'electron'
|
||||
import { join } from 'node:path'
|
||||
import { HOST_NOT_CONNECTED, type EditorFile } from '../shared/host'
|
||||
import { openInWordcraft } from './wordcraft'
|
||||
|
||||
function registerHost(): void {
|
||||
for (const channel of ['host:open', 'host:save', 'host:check', 'host:build'] as const) {
|
||||
ipcMain.removeHandler(channel)
|
||||
ipcMain.handle(channel, () => HOST_NOT_CONNECTED)
|
||||
}
|
||||
ipcMain.removeHandler('host:edit')
|
||||
ipcMain.handle('host:edit', (_event, files: EditorFile[], activeId: string) => openInWordcraft(files, activeId))
|
||||
}
|
||||
|
||||
function registerChrome(): void {
|
||||
ipcMain.removeHandler('window:minimize')
|
||||
ipcMain.removeHandler('window:toggleMaximize')
|
||||
ipcMain.removeHandler('window:close')
|
||||
|
||||
ipcMain.handle('window:minimize', (event) => {
|
||||
BrowserWindow.fromWebContents(event.sender)?.minimize()
|
||||
})
|
||||
ipcMain.handle('window:toggleMaximize', (event) => {
|
||||
const win = BrowserWindow.fromWebContents(event.sender)
|
||||
if (!win) return
|
||||
if (win.isMaximized()) win.unmaximize()
|
||||
else win.maximize()
|
||||
})
|
||||
ipcMain.handle('window:close', (event) => {
|
||||
BrowserWindow.fromWebContents(event.sender)?.close()
|
||||
})
|
||||
}
|
||||
|
||||
function createWindow(): void {
|
||||
const win = new BrowserWindow({
|
||||
width: 1440,
|
||||
height: 900,
|
||||
minWidth: 1180,
|
||||
minHeight: 720,
|
||||
show: false,
|
||||
frame: false,
|
||||
backgroundColor: '#0b1326',
|
||||
title: 'GhostEditor',
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/index.cjs'),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: false,
|
||||
},
|
||||
})
|
||||
|
||||
win.on('ready-to-show', () => win.show())
|
||||
|
||||
const devUrl = process.env.ELECTRON_RENDERER_URL
|
||||
if (devUrl) {
|
||||
void win.loadURL(devUrl)
|
||||
} else {
|
||||
void win.loadFile(join(__dirname, '../renderer/index.html'))
|
||||
}
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
registerHost()
|
||||
registerChrome()
|
||||
createWindow()
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
})
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') app.quit()
|
||||
})
|
||||
@@ -0,0 +1,60 @@
|
||||
import { spawn } from 'node:child_process'
|
||||
import { existsSync, readdirSync } from 'node:fs'
|
||||
import { mkdir, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { app } from 'electron'
|
||||
import type { EditorFile, HostResult } from '../shared/host'
|
||||
|
||||
const ROOT = join(tmpdir(), 'ghosteditor-docs')
|
||||
|
||||
export function findWordcraft(): string | null {
|
||||
const fromEnv = process.env.WORDCRAFT_EXE
|
||||
if (fromEnv && existsSync(fromEnv)) return fromEnv
|
||||
const roots = [process.cwd(), app.getAppPath(), join(app.getAppPath(), '..'), join(app.getAppPath(), '../..')]
|
||||
for (const root of roots) {
|
||||
const found = findExe(join(root, '.wordcraft', 'app'))
|
||||
if (found) return found
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function findExe(dir: string): string | null {
|
||||
if (!existsSync(dir)) return null
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = join(dir, entry.name)
|
||||
if (entry.isFile() && entry.name.toLowerCase() === 'wordcraft.exe') return full
|
||||
if (entry.isDirectory()) {
|
||||
const nested = join(full, 'wordcraft.exe')
|
||||
if (existsSync(nested)) return nested
|
||||
const deeper = findExe(full)
|
||||
if (deeper) return deeper
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function safeRelative(id: string): string | null {
|
||||
const parts = id.replace(/\\/g, '/').split('/').filter(Boolean)
|
||||
if (parts.length === 0 || parts.some((part) => part === '..' || part.includes(':'))) return null
|
||||
return parts.join('/')
|
||||
}
|
||||
|
||||
export async function openInWordcraft(files: EditorFile[], activeId: string): Promise<HostResult> {
|
||||
const exe = findWordcraft()
|
||||
if (!exe) return { ok: false, error: 'WordCraft не найден' }
|
||||
const active = safeRelative(activeId)
|
||||
if (!active) return { ok: false, error: 'некорректный путь файла' }
|
||||
|
||||
for (const file of files) {
|
||||
const relative = safeRelative(file.id)
|
||||
if (!relative) continue
|
||||
const path = join(ROOT, relative)
|
||||
await mkdir(dirname(path), { recursive: true })
|
||||
await writeFile(path, file.text, 'utf8')
|
||||
}
|
||||
|
||||
const child = spawn(exe, [join(ROOT, active)], { detached: true, stdio: 'ignore', windowsHide: false })
|
||||
child.unref()
|
||||
return { ok: true }
|
||||
}
|
||||
Reference in New Issue
Block a user