forked from Aerlinger/editor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
227 lines (195 loc) · 6.47 KB
/
renderer.js
File metadata and controls
227 lines (195 loc) · 6.47 KB
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
105
106
107
108
109
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict';
(() => {
//
try {
const Electron = require('electron');
const Path = require('fire-path');
let EditorR;
// init EditorR
window.onerror = function ( message, filename, lineno, colno, err ) {
if ( EditorR && EditorR.Ipc.sendToMain ) {
EditorR.Ipc.sendToMain('editor:renderer-console-error', err.stack || err);
} else {
console.error(err.stack || err);
}
// Just let default handler run.
return false;
};
// init document events
// prevent default drag
document.addEventListener( 'dragstart', event => {
event.preventDefault();
event.stopPropagation();
});
document.addEventListener( 'drop', event => {
event.preventDefault();
event.stopPropagation();
});
document.addEventListener( 'dragover', event => {
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = 'none';
});
// prevent contextmenu
document.addEventListener( 'contextmenu', event => {
event.preventDefault();
event.stopPropagation();
});
// TODO: should we use webContents.clearHistory() instead?
// prevent go back
document.addEventListener( 'keydown', event => {
if ( event.keyCode === 8 ) {
if ( event.target === document.body ) {
event.preventDefault();
event.stopPropagation();
}
}
});
window.addEventListener('copy', event => {
// the element can handle the copy event
if ( event.target !== document.body ) {
return;
}
// get current focused panel
let focusedPanelFrame = EditorR.UI.focusedPanelFrame;
if ( focusedPanelFrame ) {
event.preventDefault();
event.stopPropagation();
EditorR.UI.fire(focusedPanelFrame, 'panel-copy', {
bubbles: false,
detail: {
clipboardData: event.clipboardData,
}
});
}
});
window.addEventListener('cut', event => {
// the element can handle the copy event
if ( event.target !== document.body ) {
return;
}
// get current focused panel
let focusedPanelFrame = EditorR.UI.focusedPanelFrame;
if ( focusedPanelFrame ) {
event.preventDefault();
event.stopPropagation();
EditorR.UI.fire(focusedPanelFrame, 'panel-cut', {
bubbles: false,
detail: {
clipboardData: event.clipboardData,
}
});
}
});
window.addEventListener('paste', event => {
// the element can handle the copy event
if ( event.target !== document.body ) {
return;
}
// get current focused panel
let focusedPanelFrame = EditorR.UI.focusedPanelFrame;
if ( focusedPanelFrame ) {
event.preventDefault();
event.stopPropagation();
EditorR.UI.fire(focusedPanelFrame, 'panel-paste', {
bubbles: false,
detail: {
clipboardData: event.clipboardData,
}
});
}
});
window.addEventListener('beforeunload', event => {
let frameELs = EditorR.Panel.panels;
let stopUnload = false;
frameELs.forEach(el => {
let result = true;
if ( el.close ) {
result = el.close();
}
if ( result === false ) {
stopUnload = true;
}
});
if ( stopUnload ) {
event.returnValue = true;
}
});
// DISABLE: looks like setting the `body: { overflow: hidden; }` will solve the problem
// window.onload = function () {
// // NOTE: this will prevent mac touchpad scroll the body
// document.body.onscroll = function ( event ) {
// document.body.scrollLeft = 0;
// document.body.scrollTop = 0;
// };
// };
// DISABLE: I disable this because developer may debug during initialize,
// and when he refresh at that time, the layout will be saved and
// reloaded layout will not be the expected one
// window.onunload = function () {
// if ( EditorR && EditorR.Panel ) {
// // NOTE: do not use DockUtils.saveLayout() which will be invoked in requestAnimationFrame.
// // It will not be called in window.onunload
// EditorR.Ipc.sendToMain(
// 'editor:window-save-layout',
// EditorR.Panel.dumpLayout()
// );
// }
// else {
// EditorR.Ipc.sendToMain(
// 'editor:window-save-layout',
// null
// );
// }
// };
// limit zooming
Electron.webFrame.setZoomLevelLimits(1,1);
// init & cache remote
let _remoteEditor = Electron.remote.getGlobal('Editor');
let _appPath = _remoteEditor.url('app://');
let _frameworkPath = _remoteEditor.url('editor-framework://');
// add builtin node_modules search path for page-level
require('module').globalPaths.push(Path.join(_appPath,'node_modules'));
// load editor-init.js
EditorR = require(`${_frameworkPath}/lib/renderer`);
EditorR.remote = _remoteEditor;
// DISABLE: use hash instead
// // init argument list sending from core by url?queries
// // format: '?foo=bar&hell=world'
// // skip '?'
// let queryString = decodeURIComponent(location.search.substr(1));
// let queryList = queryString.split('&');
// let queries = {};
// for ( let i = 0; i < queryList.length; ++i ) {
// let pair = queryList[i].split('=');
// if ( pair.length === 2) {
// queries[pair[0]] = pair[1];
// }
// }
// NOTE: hash is better than query from semantic, it means this is client data.
if ( window.location.hash ) {
let hash = window.location.hash.slice(1);
EditorR.argv = Object.freeze(JSON.parse(decodeURIComponent(hash)));
} else {
EditorR.argv = {};
}
EditorR.dev = _remoteEditor.dev;
EditorR.lang = _remoteEditor.lang;
EditorR.appPath = _appPath;
EditorR.frameworkPath = _frameworkPath;
// config submodules
EditorR.Ipc.debug = _remoteEditor.dev;
// register protocol
EditorR.Protocol.init(EditorR);
} catch ( err ) {
window.onload = function () {
const Electron = require('electron');
let currentWindow = Electron.remote.getCurrentWindow();
currentWindow.setSize(800, 600);
currentWindow.center();
currentWindow.show();
currentWindow.openDevTools();
console.error(err.stack || err);
};
}
})();