aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/lib
diff options
context:
space:
mode:
authorLibravatar Markus Hatvan <markus_hatvan@aon.at>2021-11-18 17:37:45 +0100
committerLibravatar GitHub <noreply@github.com>2021-11-18 22:07:45 +0530
commitb37a6b07b39c8c7827052dc6fb97f490f1e0f514 (patch)
tree0276e7c51f5ebfa14c566def7aac39f014c2291d /src/webview/lib
parentUpdate github issues template [skip ci] (diff)
downloadferdium-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')
-rw-r--r--src/webview/lib/RecipeWebview.ts (renamed from src/webview/lib/RecipeWebview.js)15
-rw-r--r--src/webview/lib/Userscript.ts (renamed from src/webview/lib/Userscript.js)77
2 files changed, 36 insertions, 56 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.ts
index ebe88ed85..09dc462ed 100644
--- a/src/webview/lib/RecipeWebview.js
+++ b/src/webview/lib/RecipeWebview.ts
@@ -5,6 +5,14 @@ import { pathExistsSync, readFileSync, existsSync } from 'fs-extra';
5const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); 5const debug = require('debug')('Ferdi:Plugin:RecipeWebview');
6 6
7class RecipeWebview { 7class RecipeWebview {
8 badgeHandler: any;
9
10 dialogTitleHandler: any;
11
12 notificationsHandler: any;
13
14 sessionHandler: any;
15
8 constructor( 16 constructor(
9 badgeHandler, 17 badgeHandler,
10 dialogTitleHandler, 18 dialogTitleHandler,
@@ -97,9 +105,12 @@ class RecipeWebview {
97 const styles = document.createElement('style'); 105 const styles = document.createElement('style');
98 styles.innerHTML = readFileSync(file, 'utf8'); 106 styles.innerHTML = readFileSync(file, 'utf8');
99 107
100 document.querySelector('head').append(styles); 108 const head = document.querySelector('head');
101 109
102 debug('Append styles', styles); 110 if (head) {
111 head.append(styles);
112 debug('Append styles', styles);
113 }
103 } 114 }
104 }); 115 });
105 } 116 }
diff --git a/src/webview/lib/Userscript.js b/src/webview/lib/Userscript.ts
index f7bb99206..c50941dc7 100644
--- a/src/webview/lib/Userscript.js
+++ b/src/webview/lib/Userscript.ts
@@ -1,8 +1,12 @@
1import { ipcRenderer } from 'electron'; 1type Recipe = {
2 setBadge: (direct: number, indirect: number) => void;
3 setDialogTitle: (title: string) => void;
4 injectCSS: (css: string | string[]) => void;
5};
2 6
3export default class Userscript { 7export default class Userscript {
4 // Current ./lib/RecipeWebview instance 8 // Current ./lib/RecipeWebview instance
5 recipe = null; 9 recipe: Recipe | null = null;
6 10
7 // Current ./recipe.js instance 11 // Current ./recipe.js instance
8 controller = null; 12 controller = null;
@@ -13,8 +17,6 @@ export default class Userscript {
13 // Ferdi and service settings 17 // Ferdi and service settings
14 settings = {}; 18 settings = {};
15 19
16 settingsUpdateHandler = null;
17
18 constructor(recipe, controller, config) { 20 constructor(recipe, controller, config) {
19 this.recipe = recipe; 21 this.recipe = recipe;
20 this.controller = controller; 22 this.controller = controller;
@@ -29,31 +31,18 @@ export default class Userscript {
29 * @param {*} settings 31 * @param {*} settings
30 */ 32 */
31 // eslint-disable-next-line camelcase 33 // eslint-disable-next-line camelcase
32 internal_setSettings(settings) { 34 internal_setSettings(settings: any) {
33 // This is needed to get a clean JS object from the settings itself to provide better accessibility 35 // This is needed to get a clean JS object from the settings itself to provide better accessibility
34 // Otherwise this will be a mobX instance 36 // Otherwise this will be a mobX instance
35 this.settings = JSON.parse(JSON.stringify(settings)); 37 this.settings = JSON.parse(JSON.stringify(settings));
36
37 if (typeof this.settingsUpdateHandler === 'function') {
38 this.settingsUpdateHandler();
39 }
40 }
41
42 /**
43 * Register a settings handler to be executed when the settings change
44 *
45 * @param {function} handler
46 */
47 onSettingsUpdate(handler) {
48 this.settingsUpdateHandler = handler;
49 } 38 }
50 39
51 /** 40 /**
52 * Set badge count for the current service 41 * Set badge count for the current service
53 * @param {*} direct Direct messages 42 * @param {number} direct Direct messages
54 * @param {*} indirect Indirect messages 43 * @param {number} indirect Indirect messages
55 */ 44 */
56 setBadge(direct = 0, indirect = 0) { 45 setBadge(direct: number = 0, indirect: number = 0) {
57 if (this.recipe && this.recipe.setBadge) { 46 if (this.recipe && this.recipe.setBadge) {
58 this.recipe.setBadge(direct, indirect); 47 this.recipe.setBadge(direct, indirect);
59 } 48 }
@@ -63,7 +52,7 @@ export default class Userscript {
63 * Set active dialog title to the app title 52 * Set active dialog title to the app title
64 * @param {*} title Dialog title 53 * @param {*} title Dialog title
65 */ 54 */
66 setDialogTitle(title) { 55 setDialogTitle(title: string) {
67 if (this.recipe && this.recipe.setDialogTitle) { 56 if (this.recipe && this.recipe.setDialogTitle) {
68 this.recipe.setDialogTitle(title); 57 this.recipe.setDialogTitle(title);
69 } 58 }
@@ -74,8 +63,9 @@ export default class Userscript {
74 * 63 *
75 * @param {...string} files 64 * @param {...string} files
76 */ 65 */
77 injectCSSFiles(...files) { 66 injectCSSFiles(...files: string[]) {
78 if (this.recipe && this.recipe.injectCSS) { 67 if (this.recipe && this.recipe.injectCSS) {
68 // @ts-expect-error A spread argument must either have a tuple type or be passed to a rest parameter.
79 this.recipe.injectCSS(...files); 69 this.recipe.injectCSS(...files);
80 } 70 }
81 } 71 }
@@ -85,54 +75,33 @@ export default class Userscript {
85 * 75 *
86 * @param {string} css 76 * @param {string} css
87 */ 77 */
88 injectCSS(css) { 78 injectCSS(css: string) {
89 const style = document.createElement('style'); 79 const style = document.createElement('style');
90 style.textContent = css; 80 style.textContent = css;
91 document.head.append(style); 81 document.head.append(style);
92 } 82 }
93 83
94 /** 84 /**
95 * Open "Find in Page" popup
96 */
97 openFindInPage() {
98 this.controller.openFindInPage();
99 }
100
101 /**
102 * Set or update value in storage 85 * Set or update value in storage
103 * 86 *
104 * @param {*} key 87 * @param {string} key
105 * @param {*} value 88 * @param {any} value
106 */ 89 */
107 set(key, value) { 90 set(key: string, value: string) {
108 window.localStorage.setItem(`ferdi-user-${key}`, JSON.stringify(value)); 91 window.localStorage.setItem(`ferdi-user-${key}`, JSON.stringify(value));
109 } 92 }
110 93
111 /** 94 /**
112 * Get value from storage 95 * Get value from storage
113 * 96 *
114 * @param {*} key 97 * @param {string} key
115 * @return Value of the key 98 * @return Value of the key
116 */ 99 */
117 get(key) { 100 get(key: string) {
118 return JSON.parse(window.localStorage.getItem(`ferdi-user-${key}`)); 101 const ferdiUserKey = window.localStorage.getItem(`ferdi-user-${key}`);
119 }
120 102
121 /** 103 if (ferdiUserKey) {
122 * Open a URL in an external browser 104 return JSON.parse(ferdiUserKey);
123 * 105 }
124 * @param {*} url
125 */
126 externalOpen(url) {
127 ipcRenderer.sendToHost('new-window', url);
128 }
129
130 /**
131 * Open a URL in the current service
132 *
133 * @param {*} url
134 */
135 internalOpen(url) {
136 window.location.href = url;
137 } 106 }
138} 107}