diff options
author | 2019-03-08 17:34:27 +0100 | |
---|---|---|
committer | 2019-03-08 08:34:27 -0800 | |
commit | baa45d935540601c07977e4d786e0953165cf1fa (patch) | |
tree | eee0423829cd4b8fb6306620edd8e85bc6fdaed8 /src/components/services/content/ServiceWebview.js | |
parent | fix(Linux): Fix minimized window focusing (#1304) (@skoruppa) (diff) | |
download | ferdium-app-baa45d935540601c07977e4d786e0953165cf1fa.tar.gz ferdium-app-baa45d935540601c07977e4d786e0953165cf1fa.tar.zst ferdium-app-baa45d935540601c07977e4d786e0953165cf1fa.zip |
Fix/service webview unmounting (#1328)
* detach service when underlying webview unmounts
* disable no-param-reassign eslint rule
Diffstat (limited to 'src/components/services/content/ServiceWebview.js')
-rw-r--r-- | src/components/services/content/ServiceWebview.js | 145 |
1 files changed, 25 insertions, 120 deletions
diff --git a/src/components/services/content/ServiceWebview.js b/src/components/services/content/ServiceWebview.js index bb577e4cc..7252c695f 100644 --- a/src/components/services/content/ServiceWebview.js +++ b/src/components/services/content/ServiceWebview.js | |||
@@ -1,145 +1,50 @@ | |||
1 | import React, { Component, Fragment } from 'react'; | 1 | import React, { Component } from 'react'; |
2 | import PropTypes from 'prop-types'; | 2 | import PropTypes from 'prop-types'; |
3 | import { autorun } from 'mobx'; | ||
4 | import { observer } from 'mobx-react'; | 3 | import { observer } from 'mobx-react'; |
5 | import Webview from 'react-electron-web-view'; | 4 | import ElectronWebView from 'react-electron-web-view'; |
6 | import classnames from 'classnames'; | ||
7 | 5 | ||
8 | import ServiceModel from '../../../models/Service'; | 6 | import ServiceModel from '../../../models/Service'; |
9 | import StatusBarTargetUrl from '../../ui/StatusBarTargetUrl'; | ||
10 | import WebviewLoader from '../../ui/WebviewLoader'; | ||
11 | import WebviewCrashHandler from './WebviewCrashHandler'; | ||
12 | import WebviewErrorHandler from './ErrorHandlers/WebviewErrorHandler'; | ||
13 | import ServiceDisabled from './ServiceDisabled'; | ||
14 | 7 | ||
15 | export default @observer class ServiceWebview extends Component { | 8 | @observer |
9 | class ServiceWebview extends Component { | ||
16 | static propTypes = { | 10 | static propTypes = { |
17 | service: PropTypes.instanceOf(ServiceModel).isRequired, | 11 | service: PropTypes.instanceOf(ServiceModel).isRequired, |
18 | setWebviewReference: PropTypes.func.isRequired, | 12 | setWebviewReference: PropTypes.func.isRequired, |
19 | reload: PropTypes.func.isRequired, | 13 | detachService: PropTypes.func.isRequired, |
20 | edit: PropTypes.func.isRequired, | ||
21 | enable: PropTypes.func.isRequired, | ||
22 | isActive: PropTypes.bool, | ||
23 | }; | 14 | }; |
24 | 15 | ||
25 | static defaultProps = { | ||
26 | isActive: false, | ||
27 | }; | ||
28 | |||
29 | state = { | ||
30 | forceRepaint: false, | ||
31 | targetUrl: '', | ||
32 | statusBarVisible: false, | ||
33 | }; | ||
34 | |||
35 | autorunDisposer = null; | ||
36 | |||
37 | webview = null; | 16 | webview = null; |
38 | 17 | ||
39 | componentDidMount() { | ||
40 | this.autorunDisposer = autorun(() => { | ||
41 | if (this.props.service.isActive) { | ||
42 | this.setState({ forceRepaint: true }); | ||
43 | setTimeout(() => { | ||
44 | this.setState({ forceRepaint: false }); | ||
45 | }, 100); | ||
46 | } | ||
47 | }); | ||
48 | } | ||
49 | |||
50 | componentWillUnmount() { | 18 | componentWillUnmount() { |
51 | this.autorunDisposer(); | 19 | const { service, detachService } = this.props; |
52 | } | 20 | detachService({ service }); |
53 | |||
54 | updateTargetUrl = (event) => { | ||
55 | let visible = true; | ||
56 | if (event.url === '' || event.url === '#') { | ||
57 | visible = false; | ||
58 | } | ||
59 | this.setState({ | ||
60 | targetUrl: event.url, | ||
61 | statusBarVisible: visible, | ||
62 | }); | ||
63 | } | 21 | } |
64 | 22 | ||
65 | render() { | 23 | render() { |
66 | const { | 24 | const { |
67 | service, | 25 | service, |
68 | setWebviewReference, | 26 | setWebviewReference, |
69 | reload, | ||
70 | edit, | ||
71 | enable, | ||
72 | } = this.props; | 27 | } = this.props; |
73 | 28 | ||
74 | const webviewClasses = classnames({ | ||
75 | services__webview: true, | ||
76 | 'services__webview-wrapper': true, | ||
77 | 'is-active': service.isActive, | ||
78 | 'services__webview--force-repaint': this.state.forceRepaint, | ||
79 | }); | ||
80 | |||
81 | let statusBar = null; | ||
82 | if (this.state.statusBarVisible) { | ||
83 | statusBar = ( | ||
84 | <StatusBarTargetUrl text={this.state.targetUrl} /> | ||
85 | ); | ||
86 | } | ||
87 | |||
88 | return ( | 29 | return ( |
89 | <div className={webviewClasses}> | 30 | <ElectronWebView |
90 | {service.isActive && service.isEnabled && ( | 31 | ref={(webview) => { this.webview = webview; }} |
91 | <Fragment> | 32 | autosize |
92 | {service.hasCrashed && ( | 33 | src={service.url} |
93 | <WebviewCrashHandler | 34 | preload="./webview/recipe.js" |
94 | name={service.recipe.name} | 35 | partition={`persist:service-${service.id}`} |
95 | webview={service.webview} | 36 | onDidAttach={() => { |
96 | reload={reload} | 37 | setWebviewReference({ |
97 | /> | 38 | serviceId: service.id, |
98 | )} | 39 | webview: this.webview.view, |
99 | {service.isEnabled && service.isLoading && service.isFirstLoad && ( | 40 | }); |
100 | <WebviewLoader | 41 | }} |
101 | loaded={false} | 42 | onUpdateTargetUrl={this.updateTargetUrl} |
102 | name={service.name} | 43 | useragent={service.userAgent} |
103 | /> | 44 | allowpopups |
104 | )} | 45 | /> |
105 | {service.isError && ( | ||
106 | <WebviewErrorHandler | ||
107 | name={service.recipe.name} | ||
108 | errorMessage={service.errorMessage} | ||
109 | reload={reload} | ||
110 | edit={edit} | ||
111 | /> | ||
112 | )} | ||
113 | </Fragment> | ||
114 | )} | ||
115 | {!service.isEnabled ? ( | ||
116 | <Fragment> | ||
117 | {service.isActive && ( | ||
118 | <ServiceDisabled | ||
119 | name={service.recipe.name} | ||
120 | webview={service.webview} | ||
121 | enable={enable} | ||
122 | /> | ||
123 | )} | ||
124 | </Fragment> | ||
125 | ) : ( | ||
126 | <Webview | ||
127 | ref={(element) => { this.webview = element; }} | ||
128 | autosize | ||
129 | src={service.url} | ||
130 | preload="./webview/recipe.js" | ||
131 | partition={`persist:service-${service.id}`} | ||
132 | onDidAttach={() => setWebviewReference({ | ||
133 | serviceId: service.id, | ||
134 | webview: this.webview.view, | ||
135 | })} | ||
136 | onUpdateTargetUrl={this.updateTargetUrl} | ||
137 | useragent={service.userAgent} | ||
138 | allowpopups | ||
139 | /> | ||
140 | )} | ||
141 | {statusBar} | ||
142 | </div> | ||
143 | ); | 46 | ); |
144 | } | 47 | } |
145 | } | 48 | } |
49 | |||
50 | export default ServiceWebview; | ||