2019-01-11 04:22:33 -05:00
|
|
|
import {
|
|
|
|
dialog,
|
|
|
|
BrowserWindow,
|
2019-12-19 06:17:21 -05:00
|
|
|
Notification,
|
2020-01-10 09:34:08 -05:00
|
|
|
ipcMain
|
2019-01-11 04:22:33 -05:00
|
|
|
} from 'electron'
|
2019-12-19 06:17:21 -05:00
|
|
|
import db from '#/datastore'
|
2020-04-10 11:28:46 -04:00
|
|
|
import uploader from 'apis/app/uploader'
|
2019-12-19 06:17:21 -05:00
|
|
|
import pasteTemplate from '#/utils/pasteTemplate'
|
2020-03-19 09:03:21 -04:00
|
|
|
import { handleCopyUrl } from '~/main/utils/common'
|
2019-12-31 11:47:47 -05:00
|
|
|
import {
|
|
|
|
getWindowId,
|
|
|
|
getSettingWindowId
|
2020-04-10 11:28:46 -04:00
|
|
|
} from '@core/bus/apis'
|
2020-02-21 09:25:48 -05:00
|
|
|
import {
|
|
|
|
SHOW_INPUT_BOX
|
|
|
|
} from '~/universal/events/constants'
|
2019-01-11 04:22:33 -05:00
|
|
|
|
2020-04-12 10:34:45 -04:00
|
|
|
// Cross-process support may be required in the future
|
2019-12-22 04:22:32 -05:00
|
|
|
class GuiApi implements IGuiApi {
|
2019-12-31 11:47:47 -05:00
|
|
|
private windowId: number = -1
|
|
|
|
private settingWindowId: number = -1
|
2019-12-22 04:22:32 -05:00
|
|
|
private async showSettingWindow () {
|
2019-12-31 11:47:47 -05:00
|
|
|
this.settingWindowId = await getSettingWindowId()
|
|
|
|
const settingWindow = BrowserWindow.fromId(this.settingWindowId)
|
2019-12-22 04:22:32 -05:00
|
|
|
if (settingWindow.isVisible()) {
|
|
|
|
return true
|
2019-01-11 04:22:33 -05:00
|
|
|
}
|
2019-12-22 04:22:32 -05:00
|
|
|
settingWindow.show()
|
2019-01-11 04:22:33 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-12-22 04:22:32 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
resolve()
|
|
|
|
}, 1000) // TODO: a better way to wait page loaded.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:47:47 -05:00
|
|
|
private getWebcontentsByWindowId (id: number) {
|
|
|
|
return BrowserWindow.fromId(id).webContents
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
async showInputBox (options: IShowInputBoxOption = {
|
|
|
|
title: '',
|
|
|
|
placeholder: ''
|
|
|
|
}) {
|
|
|
|
await this.showSettingWindow()
|
2019-12-31 11:47:47 -05:00
|
|
|
this.getWebcontentsByWindowId(this.settingWindowId)
|
2020-02-21 09:25:48 -05:00
|
|
|
.send(SHOW_INPUT_BOX, options)
|
2019-12-22 04:22:32 -05:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
2020-02-21 09:25:48 -05:00
|
|
|
ipcMain.once(SHOW_INPUT_BOX, (event: Event, value: string) => {
|
2019-01-11 04:22:33 -05:00
|
|
|
resolve(value)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
showFileExplorer (options: IShowFileExplorerOption = {}) {
|
2019-12-31 11:47:47 -05:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
async upload (input: IUploadOption) {
|
2019-12-31 11:47:47 -05:00
|
|
|
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'
|
2020-05-04 04:50:29 -04:00
|
|
|
const pasteText: string[] = []
|
2019-12-19 06:17:21 -05:00
|
|
|
for (let i = 0; i < imgs.length; i++) {
|
2020-05-04 04:50:29 -04:00
|
|
|
pasteText.push(pasteTemplate(pasteStyle, imgs[i]))
|
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
|
|
|
}
|
2020-05-04 04:50:29 -04:00
|
|
|
handleCopyUrl(pasteText.join('\n'))
|
2019-12-31 11:47:47 -05:00
|
|
|
webContents.send('uploadFiles', imgs)
|
|
|
|
webContents.send('updateGallery')
|
2019-01-11 04:22:33 -05:00
|
|
|
return imgs
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
2019-01-15 04:28:50 -05:00
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
showNotification (options: IShowNotificationOption = {
|
2019-03-27 04:38:15 -04:00
|
|
|
title: '',
|
|
|
|
body: ''
|
|
|
|
}) {
|
2019-01-15 04:28:50 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: options.title,
|
|
|
|
body: options.body
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
}
|
2019-03-27 04:38:15 -04:00
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
showMessageBox (options: IShowMessageBoxOption = {
|
2019-03-27 04:38:15 -04:00
|
|
|
title: '',
|
|
|
|
message: '',
|
|
|
|
type: 'info',
|
|
|
|
buttons: ['Yes', 'No']
|
|
|
|
}) {
|
2019-12-31 11:47:47 -05:00
|
|
|
return new Promise<IShowMessageBoxResult>(async (resolve, reject) => {
|
|
|
|
this.windowId = await getWindowId()
|
2019-03-27 04:38:15 -04:00
|
|
|
dialog.showMessageBox(
|
2019-12-31 11:47:47 -05:00
|
|
|
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
|