@@ -5,8 +5,6 @@ import { formatTime } from '@app/utils/common';
55import { HttpService } from '@app/services' ;
66import { filter } from 'rxjs' ;
77
8- declare const AsciinemaPlayer : any ;
9-
108@Component ( {
119 standalone : false ,
1210 selector : 'elements-replay-asciicast' ,
@@ -29,6 +27,7 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
2927 startTimeStamp = null ;
3028 commands : Command [ ] ;
3129 page = 0 ;
30+ private AsciinemaPlayer : any ;
3231
3332 constructor (
3433 private route : ActivatedRoute ,
@@ -37,7 +36,10 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
3736 this . startAt = 0 ;
3837 }
3938
40- ngOnInit ( ) {
39+ async ngOnInit ( ) {
40+ // 动态加载 asciinema-player
41+ await this . loadAsciinemaPlayer ( ) ;
42+
4143 this . commands = new Array < Command > ( ) ;
4244 const start = new Date ( this . replay . date_start ) ;
4345 const end = new Date ( this . replay . date_end ) ;
@@ -62,38 +64,60 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
6264 this . getCommands ( this . page ) ;
6365 }
6466
67+ private async loadAsciinemaPlayer ( ) {
68+ try {
69+ // 动态导入 asciinema-player
70+ const module = await import ( 'asciinema-player' ) ;
71+ this . AsciinemaPlayer = module . default || module ;
72+ } catch ( error ) {
73+ console . error ( 'Failed to load asciinema-player:' , error ) ;
74+ // 如果动态导入失败,尝试从全局变量获取(兼容性)
75+ this . AsciinemaPlayer = ( window as any ) . AsciinemaPlayer ;
76+ }
77+ }
78+
6579 ngAfterViewInit ( ) {
66- this . player = this . createPlayer ( ) ;
67- this . player . play ( ) ;
68- this . isPlaying = true ;
69- this . createTimer ( ) ;
80+ if ( this . AsciinemaPlayer ) {
81+ this . player = this . createPlayer ( ) ;
82+ this . player . play ( ) ;
83+ this . isPlaying = true ;
84+ this . createTimer ( ) ;
85+ }
7086 }
7187
7288 @HostListener ( 'window:resize' , [ '$event' ] )
7389 onResize ( event : Event ) {
7490 this . rows = Math . min ( 25 , Math . floor ( ( window . innerHeight - 120 ) / 16 ) ) ;
75- this . currentTime = this . player . getCurrentTime ( ) ;
76- this . resetPlayer ( ) ;
91+ if ( this . player ) {
92+ this . currentTime = this . player . getCurrentTime ( ) ;
93+ this . resetPlayer ( ) ;
94+ }
7795 }
7896
7997 speedDown ( ) {
8098 if ( this . speed <= 1 ) {
8199 return ;
82100 }
83101 this . speed -- ;
84- this . currentTime = this . player . getCurrentTime ( ) ;
85- this . resetPlayer ( ) ;
86- this . player . seek ( this . currentTime ) ;
102+ if ( this . player ) {
103+ this . currentTime = this . player . getCurrentTime ( ) ;
104+ this . resetPlayer ( ) ;
105+ this . player . seek ( this . currentTime ) ;
106+ }
87107 }
88108
89109 speedUp ( ) {
90110 this . speed ++ ;
91- this . currentTime = this . player . getCurrentTime ( ) ;
92- this . resetPlayer ( ) ;
93- this . player . seek ( this . currentTime ) ;
111+ if ( this . player ) {
112+ this . currentTime = this . player . getCurrentTime ( ) ;
113+ this . resetPlayer ( ) ;
114+ this . player . seek ( this . currentTime ) ;
115+ }
94116 }
95117
96118 toggle ( ) {
119+ if ( ! this . player ) return ;
120+
97121 clearInterval ( this . timer ) ;
98122 if ( this . isPlaying ) {
99123 this . player . pause ( ) ;
@@ -126,7 +150,7 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
126150
127151 resetPlayer ( ) {
128152 clearInterval ( this . timer ) ;
129- if ( ! this . player ) {
153+ if ( ! this . player || ! this . AsciinemaPlayer ) {
130154 return ;
131155 }
132156 this . player . pause ( ) ;
@@ -148,9 +172,13 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
148172 }
149173
150174 createPlayer ( ) {
175+ if ( ! this . AsciinemaPlayer ) {
176+ console . error ( 'AsciinemaPlayer not loaded' ) ;
177+ return null ;
178+ }
151179 const el = document . getElementById ( 'screen' ) ;
152180 const opt = this . getPlayerOptions ( ) ;
153- return AsciinemaPlayer . create ( this . replay . src , el , opt ) ;
181+ return this . AsciinemaPlayer . create ( this . replay . src , el , opt ) ;
154182 }
155183
156184 getUserLang ( ) {
@@ -191,6 +219,8 @@ export class ElementReplayAsciicastComponent implements OnInit, AfterViewInit {
191219 }
192220
193221 commandClick ( item : Command ) {
222+ if ( ! this . player ) return ;
223+
194224 const startPlayTime = new Date ( this . replay . date_start ) . getTime ( ) / 1000 ;
195225 const instructStartTime = item . timestamp - 5 - startPlayTime ;
196226 const time = instructStartTime > 0 ? instructStartTime : 0 ;
0 commit comments