-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.js
61 lines (54 loc) · 1.54 KB
/
index.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
import { VimWasm } from './node_modules/vim-wasm/vimwasm.js';
const screenCanvasElement = document.getElementById('vim-screen');
const vim = new VimWasm({
workerScriptPath: './node_modules/vim-wasm/vim.js',
canvas: screenCanvasElement,
input: document.getElementById('vim-input'),
});
// Handle drag and drop
function cancel(e) {
e.stopPropagation();
e.preventDefault();
}
screenCanvasElement.addEventListener(
'dragover',
e => {
cancel(e);
if (e.dataTransfer) {
e.dataTransfer.dropEffect = 'copy';
}
},
false,
);
screenCanvasElement.addEventListener(
'drop',
e => {
cancel(e);
if (e.dataTransfer) {
vim.dropFiles(e.dataTransfer.files).catch(console.error);
}
},
false,
);
vim.onVimExit = status => {
alert(`Vim exited with status ${status}`);
};
vim.onFileExport = (fullpath, contents) => {
const slashIdx = fullpath.lastIndexOf('/');
const filename = slashIdx !== -1 ? fullpath.slice(slashIdx + 1) : fullpath;
const blob = new Blob([contents], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.rel = 'noopener';
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
vim.readClipboard = navigator.clipboard.readText;
vim.onWriteClipboard = navigator.clipboard.writeText;
vim.onError = console.error;
vim.start();