47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { readFile, writeFile, existsSync } from 'fs'
|
|
import { IpcMainEvent } from 'electron'
|
|
import { LocalSoftwareWindow } from '../LocalSoftwareWindow'
|
|
|
|
const FONT_LIBRARY_FILE_PATH = './fontLibrary.json'
|
|
|
|
export class FontDesignWindow extends LocalSoftwareWindow {
|
|
constructor (options = {}) {
|
|
super('font-design.html', options )
|
|
}
|
|
|
|
bindIpcEvent(): void {
|
|
this.webContents.ipc.on('LOAD_FONT_LIBRARY', this.load )
|
|
this.webContents.ipc.on('SAVE_FONT_LIBRARY', this.save )
|
|
}
|
|
|
|
load(event: IpcMainEvent) {
|
|
if (!existsSync(FONT_LIBRARY_FILE_PATH)) {
|
|
event.returnValue = {}
|
|
return
|
|
}
|
|
|
|
readFile(FONT_LIBRARY_FILE_PATH, 'utf8', (err, data) => {
|
|
if (err) {
|
|
console.error(err);
|
|
|
|
event.returnValue = {}
|
|
} else {
|
|
event.returnValue = { data }
|
|
}
|
|
})
|
|
}
|
|
|
|
save(event: IpcMainEvent, fontLibrary: string) {
|
|
if (!fontLibrary) {
|
|
event.returnValue = { err: new Error('字库为空') }
|
|
return
|
|
}
|
|
|
|
try {
|
|
writeFile( FONT_LIBRARY_FILE_PATH, fontLibrary, 'utf8', () => event.returnValue = 'OK')
|
|
} catch (err) {
|
|
event.returnValue = { err }
|
|
}
|
|
}
|
|
}
|