diff options
author | 2018-11-22 14:14:25 +0100 | |
---|---|---|
committer | 2018-11-22 14:14:25 +0100 | |
commit | 46b8c8c4b3a5b80e0187b284abc84566a7e784db (patch) | |
tree | 7fd378bcdd18e78c42dfeb61a15f89fd10106046 /src/webview | |
parent | ADD features loading spinner (diff) | |
parent | feat(App): Add option to enable dark mode for supported services (diff) | |
download | ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.tar.gz ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.tar.zst ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.zip |
Merge branch 'develop' into feature/features-api
Diffstat (limited to 'src/webview')
-rw-r--r-- | src/webview/darkmode.js | 28 | ||||
-rw-r--r-- | src/webview/plugin.js | 38 |
2 files changed, 62 insertions, 4 deletions
diff --git a/src/webview/darkmode.js b/src/webview/darkmode.js new file mode 100644 index 000000000..9830ef33c --- /dev/null +++ b/src/webview/darkmode.js | |||
@@ -0,0 +1,28 @@ | |||
1 | import path from 'path'; | ||
2 | import fs from 'fs-extra'; | ||
3 | |||
4 | const ID = 'franz-theme-dark-mode'; | ||
5 | |||
6 | export function injectDarkModeStyle(recipePath) { | ||
7 | const darkModeStyle = path.join(recipePath, 'darkmode.css'); | ||
8 | if (fs.pathExistsSync(darkModeStyle)) { | ||
9 | const data = fs.readFileSync(darkModeStyle); | ||
10 | const styles = document.createElement('style'); | ||
11 | styles.id = ID; | ||
12 | styles.innerHTML = data.toString(); | ||
13 | |||
14 | document.querySelector('head').appendChild(styles); | ||
15 | } | ||
16 | } | ||
17 | |||
18 | export function removeDarkModeStyle() { | ||
19 | const style = document.querySelector(`#${ID}`); | ||
20 | |||
21 | if (style) { | ||
22 | style.remove(); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | export function isDarkModeStyleInjected() { | ||
27 | return !!document.querySelector(`#${ID}`); | ||
28 | } | ||
diff --git a/src/webview/plugin.js b/src/webview/plugin.js index d9e021e6d..e6fdc4efd 100644 --- a/src/webview/plugin.js +++ b/src/webview/plugin.js | |||
@@ -6,8 +6,14 @@ import { isDevMode } from '../environment'; | |||
6 | import RecipeWebview from './lib/RecipeWebview'; | 6 | import RecipeWebview from './lib/RecipeWebview'; |
7 | 7 | ||
8 | import Spellchecker from './spellchecker'; | 8 | import Spellchecker from './spellchecker'; |
9 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | ||
9 | import './notifications'; | 10 | import './notifications'; |
10 | 11 | ||
12 | const debug = require('debug')('Franz:Plugin'); | ||
13 | |||
14 | window.franzSettings = {}; | ||
15 | let serviceData; | ||
16 | |||
11 | ipcRenderer.on('initializeRecipe', (e, data) => { | 17 | ipcRenderer.on('initializeRecipe', (e, data) => { |
12 | const modulePath = path.join(data.recipe.path, 'webview.js'); | 18 | const modulePath = path.join(data.recipe.path, 'webview.js'); |
13 | // Delete module from cache | 19 | // Delete module from cache |
@@ -15,8 +21,16 @@ ipcRenderer.on('initializeRecipe', (e, data) => { | |||
15 | try { | 21 | try { |
16 | // eslint-disable-next-line | 22 | // eslint-disable-next-line |
17 | require(modulePath)(new RecipeWebview(), data); | 23 | require(modulePath)(new RecipeWebview(), data); |
24 | debug('Initialize Recipe', data); | ||
25 | |||
26 | serviceData = data; | ||
27 | |||
28 | if (data.isDarkModeEnabled) { | ||
29 | injectDarkModeStyle(data.recipe.path); | ||
30 | debug('Add dark theme styles'); | ||
31 | } | ||
18 | } catch (err) { | 32 | } catch (err) { |
19 | console.error(err); | 33 | debug('Recipe initialization failed', err); |
20 | } | 34 | } |
21 | }); | 35 | }); |
22 | 36 | ||
@@ -30,11 +44,27 @@ new ContextMenuListener((info) => { // eslint-disable-line | |||
30 | }); | 44 | }); |
31 | 45 | ||
32 | ipcRenderer.on('settings-update', (e, data) => { | 46 | ipcRenderer.on('settings-update', (e, data) => { |
33 | console.log('settings-update', data); | 47 | debug('Settings update received', data); |
48 | |||
34 | spellchecker.toggleSpellchecker(data.enableSpellchecking); | 49 | spellchecker.toggleSpellchecker(data.enableSpellchecking); |
50 | window.franzSettings = data; | ||
51 | }); | ||
52 | |||
53 | ipcRenderer.on('service-settings-update', (e, data) => { | ||
54 | debug('Service settings update received', data); | ||
55 | |||
56 | if (data.isDarkModeEnabled && !isDarkModeStyleInjected()) { | ||
57 | injectDarkModeStyle(serviceData.recipe.path); | ||
58 | |||
59 | debug('Enable service dark mode'); | ||
60 | } else if (!data.isDarkModeEnabled && isDarkModeStyleInjected()) { | ||
61 | removeDarkModeStyle(); | ||
62 | |||
63 | debug('Disable service dark mode'); | ||
64 | } | ||
35 | }); | 65 | }); |
36 | 66 | ||
37 | // initSpellche | 67 | // initSpellchecker |
38 | 68 | ||
39 | document.addEventListener('DOMContentLoaded', () => { | 69 | document.addEventListener('DOMContentLoaded', () => { |
40 | ipcRenderer.sendToHost('hello'); | 70 | ipcRenderer.sendToHost('hello'); |
@@ -44,7 +74,7 @@ document.addEventListener('DOMContentLoaded', () => { | |||
44 | const originalWindowOpen = window.open; | 74 | const originalWindowOpen = window.open; |
45 | 75 | ||
46 | window.open = (url, frameName, features) => { | 76 | window.open = (url, frameName, features) => { |
47 | // We need to differentiate if the link should be opened in a popup or in the systems default browser | 77 | // We need to differentiate if the link should be opened in a popup or in the systems default browser |
48 | if (!frameName && !features) { | 78 | if (!frameName && !features) { |
49 | return ipcRenderer.sendToHost('new-window', url); | 79 | return ipcRenderer.sendToHost('new-window', url); |
50 | } | 80 | } |