-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTerm.razor.js
More file actions
93 lines (79 loc) · 2.12 KB
/
Term.razor.js
File metadata and controls
93 lines (79 loc) · 2.12 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
import "../lib/xterm.js";
import "../lib/xterm-addon-fit.js";
import { addLink } from '../../BootstrapBlazor/modules/utility.js'
import Data from '../../BootstrapBlazor/modules/data.js'
export async function init(id, invoke, options) {
await addLink('./_content/BootstrapBlazor.Term/lib/xterm.css');
const el = document.getElementById(id);
if (el === null) {
return;
}
options = {
... {
fontFamily: "Consolas, 'Courier New', monospace",
fontSize: 14,
lineHeight: 1.0,
},
...options
};
const term = new Terminal(options);
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
term.open(el);
fitAddon.fit();
const encoder = new TextEncoder();
term.onData(data => {
invoke.invokeMethodAsync("TriggerReceiveDataAsync", encoder.encode(data));
});
term.onResize(size => {
invoke.invokeMethodAsync("TriggerResizeAsync", size.rows, size.cols);
});
const resizeHandler = () => {
try {
fitAddon.fit();
const dims = fitAddon.proposeDimensions();
if (dims) {
invoke.invokeMethodAsync("TriggerResizeAsync", dims.rows, dims.cols);
}
} catch (e) {
console.warn(e);
}
};
window.addEventListener('resize', resizeHandler);
Data.set(id, {
term,
resizeHandler
});
}
export function write(id, data) {
const terminal = Data.get(id);
const { term } = terminal;
if (term) {
term.write(data);
}
}
export function writeln(id, data) {
const terminal = Data.get(id);
const { term } = terminal;
if (term) {
term.writeln(data);
}
}
export function clear(id) {
const terminal = Data.get(id);
const { term } = terminal;
if (term) {
term.clear();
}
}
export function dispose(id) {
const terminal = Data.get(id);
Data.remove(id);
const { term, resizeHandler } = terminal;
if (resizeHandler) {
window.removeEventListener('resize', resizeHandler);
}
if (term) {
term.dispose();
}
}