-
Notifications
You must be signed in to change notification settings - Fork 7
/
preference.js
147 lines (134 loc) · 4.23 KB
/
preference.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const {ipcRenderer} = require('electron')
const {Emoji} = require('emoji-mart-vue')
const CustomEmoji = require('./preference/CustomEmoji')
const Vue = require('vue/dist/vue.common')
const storage = require('electron-json-storage')
const assign = require('object-assign')
const types = require('./store/types')
const components = require('./preference/index')
const queryString = require('query-string')
const axios = require('axios')
Vue.component('Emoji', Emoji)
Vue.component('CustomEmoji', CustomEmoji)
Vue.mixin({
methods: {
emojiSheet(set, size) {
return `${__dirname}/img/${set}64.png`
}
}
})
// API token取得方法をREADMEに書いてリンク貼る
storage.get(types.STORAGE_DATA, (err, data) => {
if (err) return console.log(err)
initialize(data)
})
function initialize(data) {
new Vue({
el: '#preference',
template: `
<div class="Preference">
<header class="Header">
<div class="Header__Title">
Preference
</div>
<MenuList :emojiSet="emojiSet" v-model="selectedMenu"/>
</header>
<main class="Main">
<ErrorReport v-if="error" :error="error" :emojiSet="emojiSet"/>
<Account v-show="isSelectedMenu('account')"
:apiToken="apiToken"
:tokenVerified="tokenVerified"
:team="team"
:user="user"
:emojiSet="emojiSet"
:signOut="signOut"
/>
<Auto v-show="isSelectedMenu('auto')" :emojiSet="emojiSet" v-model="auto" :customEmojis="team.customEmojis"/>
<Suspend v-show="isSelectedMenu('suspend')" :emojiSet="emojiSet" v-model="suspend" :customEmojis="team.customEmojis"/>
<EmojiStyle v-show="isSelectedMenu('emoji')" v-model="emojiSet"/>
<Preset v-show="isSelectedMenu('preset')" v-model="preset" :emojiSet="emojiSet" :customEmojis="team.customEmojis"/>
<ResetStorage v-show="isSelectedMenu('reset')" :emojiSet="emojiSet" />
</main>
</div>
`,
data() {
const parsed = queryString.parse(location.search)
return assign({}, data, {
selectedMenu: parsed.name,
error: null,
})
},
created() {
// 設定画面が開かれてるときにメニューバーから指定された設定項目を開く
ipcRenderer.on(types.CHANGE_PREFERENCE_MENU, (e, {preferenceName, error}) => {
this.selectedMenu = preferenceName
this.error = error
console.log(this.error)
})
ipcRenderer.on(types.TOKEN_VERIFIED, (e, {apiToken}) => {
this.tokenVerified = true
this.apiToken = apiToken
this.setCustomEmojis(apiToken)
this.error = null
})
this.$nextTick(() => {
ipcRenderer.send('readyToShow')
})
ipcRenderer.on(types.SET_INFO, (e, {team, user}) => {
this.team = team
this.user = user
})
},
methods: {
update(payload) {
ipcRenderer.send(types.UPDATE_PREFERENCE, payload)
},
isSelectedMenu(type) {
return this.selectedMenu === type
},
signOut() {
axios({
url: 'https://slack.com/api/auth.revoke',
params: {
token: this.apiToken
}
})
.then(res => res.data)
.then(res => {
if (res.ok && res.revoked) {
this.apiToken = null
this.update({apiToken: null})
}
})
},
setCustomEmojis(apiToken) {
const url = 'https://slack.com/api/emoji.list'
return axios({
url,
method: 'POST',
params: {
token: apiToken,
}
})
.then(res => res.data)
.then(body => {
if (!body.ok) return
const customEmojis = Object.keys(body.emoji).map((name) => {
const imageUrl = body.emoji[name]
return {
name,
short_names: [name],
text: '',
emoticons: [],
keywords: ['reacji'],
imageUrl,
custom: true
}
})
this.$set(this.team, 'customEmojis', customEmojis)
})
}
},
components: assign({}, components)
})
}