-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdom_polyfill.js
127 lines (106 loc) · 3.17 KB
/
dom_polyfill.js
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
const { EventEmitter } = require('events');
/*
* Minimal Polyfill for globals document, window
* mouse + keypress events
*/
module.exports = function(screen) {
// Polyfilling
document = new EventEmitter();
document.addEventListener = document.addListener;
document.removeEventListener = document.removeListener;
window = {
// used for controls mostyle
get innerWidth() {
return screen ? screen.width: width;
},
get innerHeight() {
return screen ? screen.height: height;
},
addEventListener(type, handler) {
// better make sure you emulate these events!
document.addEventListener(type, handler);
},
removeEventListener(type, handler) {
document.removeEventListener(type, handler);
},
emit(...args) {
document.emit(...args);
}
}
const noop = () => {};
const _convert_event = (e) => ({
pageX: e.x,
pageY: e.y,
preventDefault: noop,
stopPropagation: noop,
button: 0
});
let mouseDown = false;
screen.on('mousedown', function(e) {
if (mouseDown) {
document.emit('mousemove', _convert_event(e));
return;
}
mouseDown = true;
document.emit('mousedown', _convert_event(e));
})
screen.on('mousemove', function mousemoving(e) {
document.emit('mousemove', _convert_event(e));
});
screen.on('mouseup', function(e) {
mouseDown = false;
document.emit('mouseup', _convert_event(e));
});
const keysDown = {};
const keyToCode = {
'a': 65,
's': 83,
'd': 68
};
// emulate keydown and keyup, 300ms is the allowance
// screen.key(['a', 's', 'd'], function(e) {
screen.on('keypress', function(e, f) {
if (keysDown[e]) {
clearTimeout(keysDown[e]);
} else {
document.emit('keydown', {
keyCode: keyToCode[e]
})
}
keysDown[e] = setTimeout( () => {
keysDown[e] = null;
document.emit('keyup', {
keyCode: keyToCode[e]
});
}, 300);
});
/*
// This doesn't seem to work so well, so use screen.program
screen.on('resize', function(e) {
console.error('resizing', e);
});
*/
screen.program.on('resize', e => {
get_window_pixels();
});
// Secret Code to get Terminal's Window Pixel Size
const get_window_pixels = _ => screen.program.manipulateWindow(14, (e, res) => {
if (e) {
// This terminal don't support the p 1 4 (window pixel dimension command) :(
console.error('Terminal does not support pixel dimension');
window.emit('resize');
return;
}
window.emit('resize', res);
/*
{ event: 'window-manipulation',
code: '',
type: 'window-size-pixels',
size: { height: 830, width: 1375 },
height: 830,
width: 1375,
windowSizePixels: { height: 830, width: 1375 } }
*/
});
get_window_pixels();
}