diff options
Diffstat (limited to 'src/webview/lib/RecipeWebview.js')
-rw-r--r-- | src/webview/lib/RecipeWebview.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js new file mode 100644 index 000000000..1787f85e2 --- /dev/null +++ b/src/webview/lib/RecipeWebview.js | |||
@@ -0,0 +1,74 @@ | |||
1 | // @flow | ||
2 | const { ipcRenderer } = require('electron'); | ||
3 | const fs = require('fs-extra'); | ||
4 | |||
5 | class RecipeWebview { | ||
6 | constructor() { | ||
7 | this.countCache = { | ||
8 | direct: 0, | ||
9 | indirect: 0, | ||
10 | }; | ||
11 | |||
12 | ipcRenderer.on('poll', () => { | ||
13 | this.loopFunc(); | ||
14 | }); | ||
15 | } | ||
16 | |||
17 | loopFunc = () => null; | ||
18 | |||
19 | /** | ||
20 | * Initialize the loop | ||
21 | * | ||
22 | * @param {Function} Function that will be executed | ||
23 | */ | ||
24 | loop(fn) { | ||
25 | this.loopFunc = fn; | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Set the unread message badge | ||
30 | * | ||
31 | * @param {int} direct Set the count of direct messages | ||
32 | * eg. Slack direct mentions, or a | ||
33 | * message to @channel | ||
34 | * @param {int} indirect Set a badge that defines there are | ||
35 | * new messages but they do not involve | ||
36 | * me directly to me eg. in a channel | ||
37 | */ | ||
38 | setBadge(direct = 0, indirect = 0) { | ||
39 | if (this.countCache.direct === direct | ||
40 | && this.countCache.indirect === indirect) return; | ||
41 | |||
42 | const count = { | ||
43 | direct, | ||
44 | indirect, | ||
45 | }; | ||
46 | |||
47 | ipcRenderer.sendToHost('messages', count); | ||
48 | Object.assign(this.countCache, count); | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Injects the contents of a CSS file into the current webview | ||
53 | * | ||
54 | * @param {Array} files CSS files that should be injected. This must | ||
55 | * be an absolute path to the file | ||
56 | */ | ||
57 | injectCSS(...files) { | ||
58 | files.forEach((file) => { | ||
59 | const data = fs.readFileSync(file); | ||
60 | const styles = document.createElement('style'); | ||
61 | styles.innerHTML = data.toString(); | ||
62 | |||
63 | document.querySelector('head').appendChild(styles); | ||
64 | }); | ||
65 | } | ||
66 | |||
67 | initialize(fn) { | ||
68 | if (typeof fn === 'function') { | ||
69 | fn(); | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | module.exports = RecipeWebview; | ||