@@ -11,15 +11,16 @@ export async function init(id, invoke, options) {
1111 return ;
1212 }
1313
14- const term = new Terminal ( {
15- fontFamily : options . fontFamily || "Consolas, 'Courier New', monospace" ,
16- fontSize : options . fontSize || 14 ,
17- cursorBlink : options . cursorBlink ,
18- lineHeight : options . lineHeight || 1.0 ,
19- theme : options . theme || { } ,
20- convertEol : options . convertEol
21- } ) ;
14+ options = {
15+ ... {
16+ fontFamily : "Consolas, 'Courier New', monospace" ,
17+ fontSize : 14 ,
18+ lineHeight : 1.0 ,
19+ } ,
20+ ... options
21+ } ;
2222
23+ const term = new Terminal ( options ) ;
2324 const fitAddon = new FitAddon . FitAddon ( ) ;
2425 term . loadAddon ( fitAddon ) ;
2526
@@ -28,65 +29,65 @@ export async function init(id, invoke, options) {
2829
2930 const encoder = new TextEncoder ( ) ;
3031 term . onData ( data => {
31- el . term . write ( data ) ;
32- console . log ( data ) ;
33- //invoke.invokeMethodAsync("TriggerReceiveDataAsync", encoder.encode(data));
32+ invoke . invokeMethodAsync ( "TriggerReceiveDataAsync" , encoder . encode ( data ) ) ;
3433 } ) ;
3534
3635 term . onResize ( size => {
3736 invoke . invokeMethodAsync ( "TriggerResizeAsync" , size . rows , size . cols ) ;
3837 } ) ;
3938
40- el . term = term ;
41- el . fitAddon = fitAddon ;
42- el . invoke = invoke ;
43-
4439 const resizeHandler = ( ) => {
4540 try {
4641 fitAddon . fit ( ) ;
4742 const dims = fitAddon . proposeDimensions ( ) ;
4843 if ( dims ) {
4944 invoke . invokeMethodAsync ( "TriggerResizeAsync" , dims . rows , dims . cols ) ;
5045 }
51- } catch ( e ) { }
46+ } catch ( e ) {
47+ console . warn ( e ) ;
48+ }
5249 } ;
5350 window . addEventListener ( 'resize' , resizeHandler ) ;
54- el . resizeHandler = resizeHandler ;
51+
52+ Data . set ( id , {
53+ term,
54+ resizeHandler
55+ } ) ;
5556}
5657
5758export function write ( id , data ) {
58- const el = document . getElementById ( id ) ;
59- if ( el && el . term ) {
60- el . term . write ( data ) ;
59+ const terminal = Data . get ( id ) ;
60+ const { term } = terminal ;
61+ if ( term ) {
62+ term . write ( data ) ;
6163 }
6264}
6365
6466export function writeln ( id , data ) {
65- const el = document . getElementById ( id ) ;
66- if ( el && el . term ) {
67- el . term . writeln ( data ) ;
67+ const terminal = Data . get ( id ) ;
68+ const { term } = terminal ;
69+ if ( term ) {
70+ term . writeln ( data ) ;
6871 }
6972}
7073
7174export function clear ( id ) {
72- const el = document . getElementById ( id ) ;
73- if ( el && el . term ) {
74- el . term . clear ( ) ;
75+ const terminal = Data . get ( id ) ;
76+ const { term } = terminal ;
77+ if ( term ) {
78+ term . clear ( ) ;
7579 }
7680}
7781
7882export function dispose ( id ) {
79- const el = document . getElementById ( id ) ;
80- if ( el ) {
81- if ( el . resizeHandler ) {
82- window . removeEventListener ( 'resize' , el . resizeHandler ) ;
83- }
84- if ( el . term ) {
85- el . term . dispose ( ) ;
86- delete el . term ;
87- }
88- if ( el . fitAddon ) {
89- delete el . fitAddon ;
90- }
83+ const terminal = Data . get ( id ) ;
84+ Data . remove ( id ) ;
85+
86+ const { term, resizeHandler } = terminal ;
87+ if ( resizeHandler ) {
88+ window . removeEventListener ( 'resize' , resizeHandler ) ;
89+ }
90+ if ( term ) {
91+ term . dispose ( ) ;
9192 }
9293}
0 commit comments