PicList/src/main/apis/gui/index.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-01-11 04:22:33 -05:00
import {
dialog,
BrowserWindow,
clipboard,
2019-12-19 06:17:21 -05:00
Notification,
ipcMain
2019-01-11 04:22:33 -05:00
} from 'electron'
2019-12-19 06:17:21 -05:00
import db from '#/datastore'
import uploader from '../uploader'
2019-12-19 06:17:21 -05:00
import pasteTemplate from '#/utils/pasteTemplate'
import {
getWindowId,
getSettingWindowId
} from '~/main/apis/bus'
import {
SHOW_INPUT_BOX
} from '~/universal/events/constants'
2019-01-11 04:22:33 -05:00
class GuiApi implements IGuiApi {
private windowId: number = -1
private settingWindowId: number = -1
private async showSettingWindow () {
this.settingWindowId = await getSettingWindowId()
const settingWindow = BrowserWindow.fromId(this.settingWindowId)
if (settingWindow.isVisible()) {
return true
2019-01-11 04:22:33 -05:00
}
settingWindow.show()
2019-01-11 04:22:33 -05:00
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000) // TODO: a better way to wait page loaded.
})
}
private getWebcontentsByWindowId (id: number) {
return BrowserWindow.fromId(id).webContents
}
async showInputBox (options: IShowInputBoxOption = {
title: '',
placeholder: ''
}) {
await this.showSettingWindow()
this.getWebcontentsByWindowId(this.settingWindowId)
.send(SHOW_INPUT_BOX, options)
return new Promise<string>((resolve, reject) => {
ipcMain.once(SHOW_INPUT_BOX, (event: Event, value: string) => {
2019-01-11 04:22:33 -05:00
resolve(value)
})
})
}
showFileExplorer (options: IShowFileExplorerOption = {}) {
return new Promise<string>(async (resolve, reject) => {
this.windowId = await getWindowId()
dialog.showOpenDialog(BrowserWindow.fromId(this.windowId), options, (filename: string) => {
2019-01-11 04:22:33 -05:00
resolve(filename)
})
})
}
async upload (input: IUploadOption) {
this.windowId = await getWindowId()
const webContents = this.getWebcontentsByWindowId(this.windowId)
const imgs = await uploader.setWebContents(webContents).upload(input)
2019-01-11 04:22:33 -05:00
if (imgs !== false) {
2019-09-11 07:30:08 -04:00
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
2019-01-11 04:22:33 -05:00
let pasteText = ''
2019-12-19 06:17:21 -05:00
for (let i = 0; i < imgs.length; i++) {
pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n'
2019-01-11 04:22:33 -05:00
const notification = new Notification({
title: '上传成功',
2019-12-19 06:17:21 -05:00
body: imgs[i].imgUrl as string,
2019-01-11 04:22:33 -05:00
icon: imgs[i].imgUrl
})
setTimeout(() => {
notification.show()
}, i * 100)
2019-09-11 07:30:08 -04:00
db.insert('uploaded', imgs[i])
2019-01-11 04:22:33 -05:00
}
clipboard.writeText(pasteText)
webContents.send('uploadFiles', imgs)
webContents.send('updateGallery')
2019-01-11 04:22:33 -05:00
return imgs
}
return []
}
showNotification (options: IShowNotificationOption = {
2019-03-27 04:38:15 -04:00
title: '',
body: ''
}) {
const notification = new Notification({
title: options.title,
body: options.body
})
notification.show()
}
2019-03-27 04:38:15 -04:00
showMessageBox (options: IShowMessageBoxOption = {
2019-03-27 04:38:15 -04:00
title: '',
message: '',
type: 'info',
buttons: ['Yes', 'No']
}) {
return new Promise<IShowMessageBoxResult>(async (resolve, reject) => {
this.windowId = await getWindowId()
2019-03-27 04:38:15 -04:00
dialog.showMessageBox(
BrowserWindow.fromId(this.windowId),
2019-12-19 06:17:21 -05:00
options
).then((res) => {
resolve({
result: res.response,
checkboxChecked: res.checkboxChecked
2019-03-27 04:38:15 -04:00
})
2019-12-19 06:17:21 -05:00
})
2019-03-27 04:38:15 -04:00
})
}
2019-01-11 04:22:33 -05:00
}
export default GuiApi