@@ -14,11 +14,9 @@ import {
1414 BEFORE_INIT_EVENT_NAME ,
1515 type BeforeInitEvent ,
1616} from "@/live2d/events/before-init" ;
17+ import { MODEL_READY_EVENT_NAME } from "@/live2d/events/model-ready" ;
1718import { dataWithinRange } from "@/live2d/helpers/dateWithinRange" ;
18- import { getPluginTips } from "@/live2d/helpers/getPluginTips" ;
19- import { loadFullTipsResource } from "@/live2d/helpers/loadFullTipsResource" ;
20- import { loadTipsResource } from "@/live2d/helpers/loadTipsResource" ;
21- import { mergeTips } from "@/live2d/helpers/mergeTips" ;
19+ import { loadMergedTips } from "@/live2d/helpers/loadMergedTips" ;
2220import { sendMessage } from "@/live2d/helpers/sendMessage" ;
2321import { timeWithinRange } from "@/live2d/helpers/timeWithinRange" ;
2422import { isString } from "@/live2d/utils/isString" ;
@@ -29,6 +27,7 @@ import {
2927 hasWebsiteHome ,
3028} from "@/live2d/utils/util" ;
3129let activeTipEvents : TipEventController | undefined ;
30+ let isInitialModelReady = false ;
3231
3332const _getWelComeMessage = ( times : TipTime [ ] ) => {
3433 if ( hasWebsiteHome ) {
@@ -216,16 +215,24 @@ const _userActionEvent = (
216215class TipEventController {
217216 private readonly abortController = new AbortController ( ) ;
218217 private readonly disposers : Array < ( ) => void > = [ ] ;
218+ private hasShownWelcome = false ;
219219
220220 constructor (
221221 private readonly config : Live2dConfig ,
222222 private readonly tips : TipConfig ,
223223 ) { }
224224
225- start ( ) : void {
225+ start ( modelReady : boolean ) : void {
226226 const { signal } = this . abortController ;
227227 if ( this . config . firstOpenSite ) {
228- _welcomeEvent ( this . tips . time ) ;
228+ if ( modelReady ) {
229+ this . showWelcomeMessage ( ) ;
230+ } else {
231+ window . addEventListener ( MODEL_READY_EVENT_NAME , this . handleModelReady , {
232+ signal,
233+ once : true ,
234+ } ) ;
235+ }
229236 }
230237
231238 const userActionState = _userActionEvent ( this . tips . message , signal ) ;
@@ -266,52 +273,38 @@ class TipEventController {
266273 }
267274 this . disposers . length = 0 ;
268275 }
269- }
270276
271- const _loadTips = async ( config : Live2dConfig ) => {
272- if ( ! config ) {
273- return ;
274- }
275- // 后台配置 tips,其中包含 mouseover 及 click 两种配置,以及单独配置的 message
276- const pluginTips = getPluginTips ( config ) ;
277- // 主题设置 tips,只会包含 mouseover 及 click 两种配置(会过滤掉其他配置)
278- const themeTipsResult = await loadTipsResource ( config . themeTipsPath ) ;
279- const themeTips : TipConfig = {
280- click : themeTipsResult ?. click || [ ] ,
281- mouseover : themeTipsResult ?. mouseover || [ ] ,
282- seasons : [ ] ,
283- time : [ ] ,
284- message : { } ,
277+ private readonly handleModelReady = ( ) => {
278+ this . showWelcomeMessage ( ) ;
285279 } ;
286- const fullOrDefaultTips = await _getFullOrDefaultTips ( config ) ;
287- // 合并三种 tips
288- return mergeTips ( {
289- pluginTips,
290- themeTips,
291- fullOrDefaultTips,
292- } ) ;
293- } ;
294280
295- export const _getFullOrDefaultTips = async (
296- config : Live2dConfig ,
297- ) : Promise < TipConfig > => {
298- return loadFullTipsResource ( config ?. tipsPath , async ( ) => {
299- return ( await import ( "../libs/live2d-tips.json" ) ) . default ;
300- } ) ;
301- } ;
281+ private showWelcomeMessage ( ) : void {
282+ if ( this . hasShownWelcome ) {
283+ return ;
284+ }
285+
286+ this . hasShownWelcome = true ;
287+ _welcomeEvent ( this . tips . time ) ;
288+ }
289+ }
302290
303291window . addEventListener ( BEFORE_INIT_EVENT_NAME , async ( event ) => {
292+ isInitialModelReady = false ;
304293 const config = ( event as BeforeInitEvent ) . detail . config ;
305294 if ( ! config ) {
306295 return ;
307296 }
308297
309- const tips = await _loadTips ( config ) ;
298+ const tips = await loadMergedTips ( config ) ;
310299 if ( ! tips ) {
311300 return ;
312301 }
313302
314303 activeTipEvents ?. dispose ( ) ;
315304 activeTipEvents = new TipEventController ( config , tips ) ;
316- activeTipEvents . start ( ) ;
305+ activeTipEvents . start ( isInitialModelReady ) ;
306+ } ) ;
307+
308+ window . addEventListener ( MODEL_READY_EVENT_NAME , ( ) => {
309+ isInitialModelReady = true ;
317310} ) ;
0 commit comments