-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTerm.razor.js
More file actions
96 lines (83 loc) · 2.38 KB
/
Term.razor.js
File metadata and controls
96 lines (83 loc) · 2.38 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
import "../../lib/xterm/xterm.js";
import "../../lib/xterm/xterm-addon-fit.js";
export function init(id, invoke, options) {
const el = document.getElementById(id);
if (el === null) {
return;
}
// Load CSS
const linkId = "bb-term-css";
if (!document.getElementById(linkId)) {
const link = document.createElement('link');
link.id = linkId;
link.rel = 'stylesheet';
link.href = './_content/BootstrapBlazor.Term/lib/xterm/xterm.css';
document.head.appendChild(link);
}
const term = new Terminal({
fontFamily: options.fontFamily || "Consolas, 'Courier New', monospace",
fontSize: options.fontSize || 14,
cursorBlink: options.cursorBlink,
lineHeight: options.lineHeight || 1.0,
theme: options.theme || {}
});
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
term.open(el);
fitAddon.fit();
term.onData(data => {
invoke.invokeMethodAsync("OnDataAsync", data);
});
term.onResize(size => {
invoke.invokeMethodAsync("OnResizeAsync", size.rows, size.cols);
});
// Store instance
el.term = term;
el.fitAddon = fitAddon;
el.invoke = invoke;
// Window resize handling
const resizeHandler = () => {
try {
fitAddon.fit();
const dims = fitAddon.proposeDimensions();
if (dims) {
invoke.invokeMethodAsync("OnResizeAsync", dims.rows, dims.cols);
}
} catch (e) { }
};
window.addEventListener('resize', resizeHandler);
el.resizeHandler = resizeHandler;
}
export function write(id, data) {
const el = document.getElementById(id);
if (el && el.term) {
el.term.write(data);
}
}
export function writeln(id, data) {
const el = document.getElementById(id);
if (el && el.term) {
el.term.writeln(data);
}
}
export function clear(id) {
const el = document.getElementById(id);
if (el && el.term) {
el.term.clear();
}
}
export function dispose(id) {
const el = document.getElementById(id);
if (el) {
if (el.resizeHandler) {
window.removeEventListener('resize', el.resizeHandler);
}
if (el.term) {
el.term.dispose();
delete el.term;
}
if (el.fitAddon) {
delete el.fitAddon;
}
}
}