From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/webview/lib/RecipeWebview.js | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/webview/lib/RecipeWebview.js (limited to 'src/webview/lib') 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 @@ +// @flow +const { ipcRenderer } = require('electron'); +const fs = require('fs-extra'); + +class RecipeWebview { + constructor() { + this.countCache = { + direct: 0, + indirect: 0, + }; + + ipcRenderer.on('poll', () => { + this.loopFunc(); + }); + } + + loopFunc = () => null; + + /** + * Initialize the loop + * + * @param {Function} Function that will be executed + */ + loop(fn) { + this.loopFunc = fn; + } + + /** + * Set the unread message badge + * + * @param {int} direct Set the count of direct messages + * eg. Slack direct mentions, or a + * message to @channel + * @param {int} indirect Set a badge that defines there are + * new messages but they do not involve + * me directly to me eg. in a channel + */ + setBadge(direct = 0, indirect = 0) { + if (this.countCache.direct === direct + && this.countCache.indirect === indirect) return; + + const count = { + direct, + indirect, + }; + + ipcRenderer.sendToHost('messages', count); + Object.assign(this.countCache, count); + } + + /** + * Injects the contents of a CSS file into the current webview + * + * @param {Array} files CSS files that should be injected. This must + * be an absolute path to the file + */ + injectCSS(...files) { + files.forEach((file) => { + const data = fs.readFileSync(file); + const styles = document.createElement('style'); + styles.innerHTML = data.toString(); + + document.querySelector('head').appendChild(styles); + }); + } + + initialize(fn) { + if (typeof fn === 'function') { + fn(); + } + } +} + +module.exports = RecipeWebview; -- cgit v1.2.3-70-g09d2