-
Notifications
You must be signed in to change notification settings - Fork 6
/
preload.js
41 lines (35 loc) · 1.03 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { ipcRenderer, contextBridge } = require('electron')
const { v4: uuidv4 } = require('uuid')
const gql = require('graphql-tag')
const moment = require('moment')
const meta = require('./package.json')
require('./vue/vue.js')
moment.locale('zh-cn')
const get = key => ipcRenderer.invoke('state', key)
const query = async (document, variableValues) => {
const result = await ipcRenderer.invoke('query', document, variableValues)
if (!result) {
throw new Error('query failed')
}
return result
}
contextBridge.exposeInMainWorld('app', {
uuidv4,
gql,
meta,
get,
query,
momentTime: timestamp => {
const momentTime = moment(timestamp)
return { absoluteTime: momentTime.local().format(), calendar: momentTime.calendar(), fromNow: momentTime.fromNow() }
},
send: ipcRenderer.send,
invoke: ipcRenderer.invoke,
on: name => {
ipcRenderer.on(name, (_, ...args) => {
const detail = { name, args }
const event = new CustomEvent('electron-ipc', { detail })
window.dispatchEvent(event)
})
},
})