Skip to content

Commit a1acff8

Browse files
committed
Re-broadcast events to PIO Home's inner frame
1 parent b2e8936 commit a1acff8

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/home.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as pioNodeHelpers from 'platformio-node-helpers';
1010

1111
import { IS_OSX } from './constants';
12+
import crypto from 'crypto';
1213
import { extension } from './main';
1314
import { notifyError } from './utils';
1415
import path from 'path';
@@ -136,30 +137,33 @@ export default class PIOHome {
136137
},
137138
});
138139
const theme = this.getTheme();
140+
const iframeId =
141+
'pioHomeIFrame-' +
142+
crypto.createHash('sha1').update(crypto.randomBytes(512)).digest('hex');
139143
return `<!DOCTYPE html>
140144
<html lang="en">
141145
<head>
142146
<script>
147+
for (const command of ['selectAll', 'copy', 'paste', 'cut', 'undo', 'redo']) {
148+
document.addEventListener(command, (e) => {
149+
document.getElementById('${iframeId}').contentWindow.postMessage({'command': 'execCommand', 'data': command}, '*');
150+
});
151+
}
143152
window.addEventListener('message', (e) => {
144-
switch (e.data.command) {
145-
case 'kbd-event': {
146-
if (${IS_OSX}) {
147-
window.dispatchEvent(new KeyboardEvent('keydown', e.data.data));
148-
}
149-
break;
150-
}
153+
if (e.data.command === 'kbd-event' && ${IS_OSX}) {
154+
window.dispatchEvent(new KeyboardEvent('keydown', e.data.data));
151155
}
152-
}, false);
156+
});
153157
</script>
154158
</head>
155159
<body style="margin: 0; padding: 0; height: 100%; overflow: hidden; background-color: ${
156160
theme === 'light' ? '#FFF' : '#1E1E1E'
157161
}">
158-
<iframe src="${pioNodeHelpers.home.getFrontendUrl({
159-
start: startUrl,
160-
theme,
161-
workspace: extension.getEnterpriseSetting('defaultPIOHomeWorkspace'),
162-
})}"
162+
<iframe id="${iframeId}" src="${pioNodeHelpers.home.getFrontendUrl({
163+
start: startUrl,
164+
theme,
165+
workspace: extension.getEnterpriseSetting('defaultPIOHomeWorkspace'),
166+
})}"
163167
width="100%"
164168
height="100%"
165169
frameborder="0"

0 commit comments

Comments
 (0)