Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import BaseComponent from './common/BaseComponent.js';
export default class WebviewComponent extends BaseComponent {
init() {
this.state = {
partition: '',
preload: '',
startPage: '',
isDebugMode: false
};
this._isActivated = false;
this._webview = null;
this._viewHandler_webview_activate = this._viewHandler_webview_activate.bind(this);
this._viewHandler_webview_config = this._viewHandler_webview_config.bind(this);
}
componentConnectedCallback() {
this.getStateManager().viewHandler
.add('webview_activate', this._viewHandler_webview_activate)
.add('webview_config', this._viewHandler_webview_config);
}
componentDisconnectedCallback() {
this.getStateManager().viewHandler
.remove('webview_activate', this._viewHandler_webview_activate)
.remove('webview_config', this._viewHandler_webview_config);
}
render() {
return `
<style>
${this.sharedStyle}
</style>
<style>
:host {
display: flex;
flex-flow: column nowrap;
flex: 1 1 auto;
}
webview {
flex: 1 1 auto;
}
</style>
`;
}
componentUpdatedCallback() {
if (this._isActivated) {
this._createWebview();
}
else {
this.dispatch('webview_activate', {component: this});
}
}
loadUrl(url) {
this._webview.setAttribute('src', url);
}
getUrl() {
return this._webview.getURL();
}
getTitle() {
return this._webview.getTitle();
}
goBack() {
this._webview.goBack();
}
goForward() {
this._webview.goForward();
}
reload() {
this._webview.reload();
}
stop() {
this._webview.stop();
}
executeJavaScript(...args) {
this._webview.executeJavaScript(...args);
}
_createWebview() {
this._webview = document.createElement('webview');
this._webview.setAttribute('partition', this.state.partition);
this._webview.setAttribute('preload', this.state.preload);
this._webview.setAttribute('src', this.state.startPage);
this._webview.addEventListener('did-start-loading', () => {
this.dispatch('webview_loading', {isLoading: true});
});
this._webview.addEventListener('did-stop-loading', () => {
this.dispatch('webview_loading', {isLoading: false});
// workaround for Input cursor invisible after navigation in webview
// details at https://github.com/electron/electron/issues/14474
this._webview.blur();
this._webview.focus();
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
});
this._webview.addEventListener('dom-ready', () => {
this.dispatch('webview_page', {
url: this._webview.getURL(),
title: this._webview.getTitle(),
canGoBack: this._webview.canGoBack(),
canGoForward: this._webview.canGoForward()
});
if (this.state.isDebugMode) {
this._webview.openDevTools();
}
this._webview.send('ipc-message');
});
this._webview.addEventListener('new-window', (event) => {
if (event.url.startsWith('http://') || event.url.startsWith('https://')) {
this.dispatch('ocsManager_openUrl', {url: event.url});
}
});
this._webview.addEventListener('will-navigate', (event) => {
// See also "will-navigate" event handling in main.js
if (event.url.startsWith('ocs://') || event.url.startsWith('ocss://')) {
const info = this._detectOcsApiInfo(this._webview.getURL());
this.dispatch('ocsManager_getItemByOcsUrl', {url: event.url, ...info});
}
});
//this._webview.addEventListener('ipc-message', (event) => {});
this.contentRoot.appendChild(this._webview);
}
_detectOcsApiInfo(url) {
// Detect provider key and content id from page url
// https://www.opendesktop.org/s/Gnome/p/123456789/?key=val#hash
//
// providerKey = https://www.opendesktop.org/ocs/v1/
// contentId = 123456789
const info = {
providerKey: '',
contentId: ''
};
const matches = url.match(/(https?:\/\/[^/]+).*\/p\/([^/?#]+)/);
if (matches) {
info.providerKey = `${matches[1]}/ocs/v1/`;
info.contentId = matches[2];
}
return info;
}
_viewHandler_webview_activate(state) {
this._isActivated = state.isActivated;
this.dispatch('webview_config', {});
}
_viewHandler_webview_config(state) {
this.update({...this.state, ...state});
}
}