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