diff options
Diffstat (limited to 'src/features/webControls/containers')
-rw-r--r-- | src/features/webControls/containers/WebControlsScreen.js | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/features/webControls/containers/WebControlsScreen.js b/src/features/webControls/containers/WebControlsScreen.js new file mode 100644 index 000000000..cada01a6f --- /dev/null +++ b/src/features/webControls/containers/WebControlsScreen.js | |||
@@ -0,0 +1,139 @@ | |||
1 | import React, { Component } from 'react'; | ||
2 | import { observer, inject } from 'mobx-react'; | ||
3 | import PropTypes from 'prop-types'; | ||
4 | |||
5 | import { autorun, observable } from 'mobx'; | ||
6 | import WebControls from '../components/WebControls'; | ||
7 | import ServicesStore from '../../../stores/ServicesStore'; | ||
8 | import Service from '../../../models/Service'; | ||
9 | |||
10 | const URL_EVENTS = [ | ||
11 | 'load-commit', | ||
12 | 'will-navigate', | ||
13 | 'did-navigate', | ||
14 | 'did-navigate-in-page', | ||
15 | ]; | ||
16 | |||
17 | @inject('stores', 'actions') @observer | ||
18 | class WebControlsScreen extends Component { | ||
19 | @observable url = ''; | ||
20 | |||
21 | @observable canGoBack = false; | ||
22 | |||
23 | @observable canGoForward = false; | ||
24 | |||
25 | webview = null; | ||
26 | |||
27 | autorunDisposer = null; | ||
28 | |||
29 | componentDidMount() { | ||
30 | const { service } = this.props; | ||
31 | |||
32 | this.autorunDisposer = autorun(() => { | ||
33 | if (service.isAttached) { | ||
34 | this.webview = service.webview; | ||
35 | |||
36 | URL_EVENTS.forEach((event) => { | ||
37 | this.webview.addEventListener(event, (e) => { | ||
38 | if (!e.isMainFrame) return; | ||
39 | |||
40 | this.url = e.url; | ||
41 | this.canGoBack = this.webview.canGoBack(); | ||
42 | this.canGoForward = this.webview.canGoForward(); | ||
43 | }); | ||
44 | }); | ||
45 | } | ||
46 | }); | ||
47 | } | ||
48 | |||
49 | componentWillUnmount() { | ||
50 | this.autorunDisposer(); | ||
51 | } | ||
52 | |||
53 | goHome() { | ||
54 | const { reloadActive } = this.props.actions.service; | ||
55 | |||
56 | if (!this.webview) return; | ||
57 | |||
58 | reloadActive(); | ||
59 | } | ||
60 | |||
61 | reload() { | ||
62 | if (!this.webview) return; | ||
63 | |||
64 | this.webview.reload(); | ||
65 | } | ||
66 | |||
67 | goBack() { | ||
68 | if (!this.webview) return; | ||
69 | |||
70 | this.webview.goBack(); | ||
71 | } | ||
72 | |||
73 | goForward() { | ||
74 | if (!this.webview) return; | ||
75 | |||
76 | this.webview.goForward(); | ||
77 | } | ||
78 | |||
79 | navigate(newUrl) { | ||
80 | if (!this.webview) return; | ||
81 | |||
82 | let url = newUrl; | ||
83 | |||
84 | try { | ||
85 | url = new URL(url).toString(); | ||
86 | } catch (err) { | ||
87 | // eslint-disable-next-line no-useless-escape | ||
88 | if (url.match(/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9\-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/)) { | ||
89 | url = `http://${url}`; | ||
90 | } else { | ||
91 | url = `https://www.google.com/search?query=${url}`; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | this.webview.loadURL(url); | ||
96 | this.url = url; | ||
97 | } | ||
98 | |||
99 | openInBrowser() { | ||
100 | const { openExternalUrl } = this.props.actions.app; | ||
101 | |||
102 | if (!this.webview) return; | ||
103 | |||
104 | openExternalUrl({ url: this.url }); | ||
105 | } | ||
106 | |||
107 | render() { | ||
108 | return ( | ||
109 | <WebControls | ||
110 | goHome={() => this.goHome()} | ||
111 | reload={() => this.reload()} | ||
112 | openInBrowser={() => this.openInBrowser()} | ||
113 | canGoBack={this.canGoBack} | ||
114 | goBack={() => this.goBack()} | ||
115 | canGoForward={this.canGoForward} | ||
116 | goForward={() => this.goForward()} | ||
117 | navigate={url => this.navigate(url)} | ||
118 | url={this.url} | ||
119 | /> | ||
120 | ); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | export default WebControlsScreen; | ||
125 | |||
126 | WebControlsScreen.wrappedComponent.propTypes = { | ||
127 | service: PropTypes.instanceOf(Service).isRequired, | ||
128 | stores: PropTypes.shape({ | ||
129 | services: PropTypes.instanceOf(ServicesStore).isRequired, | ||
130 | }).isRequired, | ||
131 | actions: PropTypes.shape({ | ||
132 | app: PropTypes.shape({ | ||
133 | openExternalUrl: PropTypes.func.isRequired, | ||
134 | }).isRequired, | ||
135 | service: PropTypes.shape({ | ||
136 | reloadActive: PropTypes.func.isRequired, | ||
137 | }).isRequired, | ||
138 | }).isRequired, | ||
139 | }; | ||