diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-11-18 17:37:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-18 22:07:45 +0530 |
commit | b37a6b07b39c8c7827052dc6fb97f490f1e0f514 (patch) | |
tree | 0276e7c51f5ebfa14c566def7aac39f014c2291d /src/webview/lib/RecipeWebview.js | |
parent | Update github issues template [skip ci] (diff) | |
download | ferdium-app-b37a6b07b39c8c7827052dc6fb97f490f1e0f514.tar.gz ferdium-app-b37a6b07b39c8c7827052dc6fb97f490f1e0f514.tar.zst ferdium-app-b37a6b07b39c8c7827052dc6fb97f490f1e0f514.zip |
chore: convert various files to TS (#2246)
* convert various files to TS
* removed outdated docs/example-feature folder
* turn off unicorn/no-empty-file
* update eslint config
Diffstat (limited to 'src/webview/lib/RecipeWebview.js')
-rw-r--r-- | src/webview/lib/RecipeWebview.js | 166 |
1 files changed, 0 insertions, 166 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js deleted file mode 100644 index ebe88ed85..000000000 --- a/src/webview/lib/RecipeWebview.js +++ /dev/null | |||
@@ -1,166 +0,0 @@ | |||
1 | import { ipcRenderer } from 'electron'; | ||
2 | import { BrowserWindow } from '@electron/remote'; | ||
3 | import { pathExistsSync, readFileSync, existsSync } from 'fs-extra'; | ||
4 | |||
5 | const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); | ||
6 | |||
7 | class RecipeWebview { | ||
8 | constructor( | ||
9 | badgeHandler, | ||
10 | dialogTitleHandler, | ||
11 | notificationsHandler, | ||
12 | sessionHandler, | ||
13 | ) { | ||
14 | this.badgeHandler = badgeHandler; | ||
15 | this.dialogTitleHandler = dialogTitleHandler; | ||
16 | this.notificationsHandler = notificationsHandler; | ||
17 | this.sessionHandler = sessionHandler; | ||
18 | |||
19 | ipcRenderer.on('poll', () => { | ||
20 | this.loopFunc(); | ||
21 | |||
22 | debug('Poll event'); | ||
23 | |||
24 | // This event is for checking if the service recipe is still actively | ||
25 | // communicating with the client | ||
26 | ipcRenderer.sendToHost('alive'); | ||
27 | }); | ||
28 | } | ||
29 | |||
30 | loopFunc = () => null; | ||
31 | |||
32 | darkModeHandler = false; | ||
33 | |||
34 | // TODO Remove this once we implement a proper wrapper. | ||
35 | get ipcRenderer() { | ||
36 | return ipcRenderer; | ||
37 | } | ||
38 | |||
39 | // TODO Remove this once we implement a proper wrapper. | ||
40 | get BrowserWindow() { | ||
41 | return BrowserWindow; | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Initialize the loop | ||
46 | * | ||
47 | * @param {Function} Function that will be executed | ||
48 | */ | ||
49 | loop(fn) { | ||
50 | this.loopFunc = fn; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Set the unread message badge | ||
55 | * | ||
56 | * @param {string | number | undefined | null} direct Set the count of direct messages | ||
57 | * eg. Slack direct mentions, or a | ||
58 | * message to @channel | ||
59 | * @param {string | number | undefined | null} indirect Set a badge that defines there are | ||
60 | * new messages but they do not involve | ||
61 | * me directly to me eg. in a channel | ||
62 | */ | ||
63 | setBadge(direct = 0, indirect = 0) { | ||
64 | this.badgeHandler.setBadge(direct, indirect); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Set the active dialog title to the app title | ||
69 | * | ||
70 | * @param {string | undefined | null} title Set the active dialog title | ||
71 | * to the app title | ||
72 | * eg. WhatsApp contact name | ||
73 | */ | ||
74 | setDialogTitle(title) { | ||
75 | this.dialogTitleHandler.setDialogTitle(title); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Safely parse the given text into an integer | ||
80 | * | ||
81 | * @param {string | number | undefined | null} text to be parsed | ||
82 | */ | ||
83 | safeParseInt(text) { | ||
84 | return this.badgeHandler.safeParseInt(text); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * Injects the contents of a CSS file into the current webview | ||
89 | * | ||
90 | * @param {Array} files CSS files that should be injected. This must | ||
91 | * be an absolute path to the file | ||
92 | */ | ||
93 | injectCSS(...files) { | ||
94 | // eslint-disable-next-line unicorn/no-array-for-each | ||
95 | files.forEach(file => { | ||
96 | if (pathExistsSync(file)) { | ||
97 | const styles = document.createElement('style'); | ||
98 | styles.innerHTML = readFileSync(file, 'utf8'); | ||
99 | |||
100 | document.querySelector('head').append(styles); | ||
101 | |||
102 | debug('Append styles', styles); | ||
103 | } | ||
104 | }); | ||
105 | } | ||
106 | |||
107 | injectJSUnsafe(...files) { | ||
108 | Promise.all( | ||
109 | files.map(file => { | ||
110 | if (existsSync(file)) { | ||
111 | return readFileSync(file, 'utf8'); | ||
112 | } | ||
113 | debug('Script not found', file); | ||
114 | return null; | ||
115 | }), | ||
116 | ).then(scripts => { | ||
117 | const scriptsFound = scripts.filter(script => script !== null); | ||
118 | if (scriptsFound.length > 0) { | ||
119 | debug('Inject scripts to main world', scriptsFound); | ||
120 | ipcRenderer.sendToHost('inject-js-unsafe', ...scriptsFound); | ||
121 | } | ||
122 | }); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * Set a custom handler for turning on and off dark mode | ||
127 | * | ||
128 | * @param {function} handler | ||
129 | */ | ||
130 | handleDarkMode(handler) { | ||
131 | this.darkModeHandler = handler; | ||
132 | } | ||
133 | |||
134 | onNotify(fn) { | ||
135 | if (typeof fn === 'function') { | ||
136 | this.notificationsHandler.onNotify = fn; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | initialize(fn) { | ||
141 | if (typeof fn === 'function') { | ||
142 | fn(); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | clearStorageData(serviceId, targetsToClear) { | ||
147 | ipcRenderer.send('clear-storage-data', { | ||
148 | serviceId, | ||
149 | targetsToClear, | ||
150 | }); | ||
151 | } | ||
152 | |||
153 | releaseServiceWorkers() { | ||
154 | this.sessionHandler.releaseServiceWorkers(); | ||
155 | } | ||
156 | |||
157 | setAvatarImage(avatarUrl) { | ||
158 | ipcRenderer.sendToHost('avatar', avatarUrl); | ||
159 | } | ||
160 | |||
161 | openNewWindow(url) { | ||
162 | ipcRenderer.sendToHost('new-window', url); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | export default RecipeWebview; | ||