Skip to content

Commit

Permalink
feat: 新增闲置状态下message支持异步函数动态提供提示的选项, 现在你可以给idleTips.message传入一个异步函数, …
Browse files Browse the repository at this point in the history
…并返回一个string类型
  • Loading branch information
loclink committed Mar 4, 2024
1 parent 43b297f commit e37abbb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
18 changes: 10 additions & 8 deletions packages/oh-my-live2d/src/modules/tips.ts
@@ -1,16 +1,14 @@
import { getRandomElement, mergeDeep, setIntervalAsync } from 'tianjie';
import { getRandomElement, isFunction, mergeDeep, setIntervalAsync } from 'tianjie';

import { CONFIG } from '../config/index.js';
import type { IdleTimer } from '../types/common.js';
import type { CSSProperties, DeepRequired, TipsOptions } from '../types/index.js';
import { createElement, getWelcomeMessage, getWordTheDay, setStyleForElement, sleep } from '../utils/index.js';

export class Tips {
private element: HTMLElement;
// private status: Status = Status.Hidden;
private idlePlayer?: {
start: () => Promise<void>;
stop: () => void;
};
private idlePlayer?: IdleTimer;

private closeTimer = 0;
private transitionTime = 1000; // 默认的消息过渡动画持续时长
Expand Down Expand Up @@ -80,7 +78,7 @@ export class Tips {
this.element.innerHTML = message;
}

showMessage(message: string, duration = 3000, priority = 0) {
showMessage(message: string, duration = 3000, priority = 0): void {
if (priority < this.priority) {
return;
}
Expand Down Expand Up @@ -152,7 +150,7 @@ export class Tips {
* 创建闲置消息播放器
* @returns
*/
private createIdleMessagePlayer() {
private createIdleMessagePlayer(): undefined | IdleTimer {
if (!this.tipsOptions) {
return;
}
Expand All @@ -163,7 +161,11 @@ export class Tips {
if (this.tipsOptions.idleTips.wordTheDay) {
message = await getWordTheDay();
} else {
message = getRandomElement(messages || []) || '';
if (isFunction(messages)) {
message = await messages();
} else {
message = getRandomElement(messages || []) || '';
}
}

if (message) {
Expand Down
4 changes: 4 additions & 0 deletions packages/oh-my-live2d/src/types/common.ts
@@ -0,0 +1,4 @@
export type IdleTimer = {
start: () => Promise<void>;
stop: () => void;
};
3 changes: 2 additions & 1 deletion packages/oh-my-live2d/src/types/index.ts
Expand Up @@ -4,12 +4,13 @@ import type { Application } from 'pixi.js';
import type { DEFAULT_OPTIONS } from 'src/config/config.js';

import type { ModelOptions } from './model.js';
import type { ImportType, Options } from './options.js';
import type { Options } from './options.js';
import type { DeepRequired } from './utils.js';

export * from './options.js';
export * from './utils.js';

export type ImportType = 'complete' | 'cubism2' | 'cubism5';
export type PIXI_LIVE2D_DISPLAY_MODULE = typeof import('pixi-live2d-display');
export type PIXI_MODULE = typeof import('pixi.js');

Expand Down
2 changes: 1 addition & 1 deletion packages/oh-my-live2d/src/types/options.ts
@@ -1,7 +1,7 @@
import type { ModelOptions } from './model.js';
import type { TipsOptions } from './tips.js';

export type ImportType = 'complete' | 'cubism2' | 'cubism5';
import type { ImportType } from './index.js';

/**
* # 配置选项
Expand Down
10 changes: 6 additions & 4 deletions packages/oh-my-live2d/src/types/tips.ts
Expand Up @@ -47,7 +47,7 @@ export interface TipsOptions {
*/
idleTips?: {
/**
* 是否开启每日一言, 默认为关闭状态, 开启后将从这个地址请求 <https://v1.hitokoto.cn> 随机信息作为闲置提示内容
* 是否开启每日一言, 默认为关闭状态, 开启后将从 <https://v1.hitokoto.cn> 发送网络请求, 并将请求到的随机信息作为闲置提示内容
*
* ::: tip
*
Expand Down Expand Up @@ -79,11 +79,13 @@ export interface TipsOptions {
interval?: number;

/**
* 播放的消息内容, 需要是一个字符串组成的数组, 播放时会从中随机取出一条进行提示, 空数组则不播放, 默认为空数组
* 闲置时播放的消息内容, 可以是字符串数组也可以是一个返回类型为Promise<string>的异步函数
*
* @valueType string[]
* 当为 message 为数组时会从中随机取出一条进行提示, 当 message 为异步函数时会在定时器同步执行这个函数并使用这个函数的返回值进行提示, 空数组则不播放, 默认为空数组
*
* @valueType string[] | (() => Promise<string>)
*/
message?: string[];
message?: string[] | (() => Promise<string>);
};
/**
* 模型入场后的欢迎提示
Expand Down
10 changes: 5 additions & 5 deletions tests/vite-app/public/index.min.js

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions tests/vite-app/src/main.ts
Expand Up @@ -17,23 +17,25 @@ void loadOml2d({
// width: 320
// }
// }
// {
// path: 'https://cdn.jsdelivr.net/gh/Eikanya/Live2d-model/galgame%20live2d/Fox%20Hime%20Zero/mori_miko/mori_miko.model3.json',
// position: [50, 100],
// stageStyle: {
// width: 300,
// height: 450
// }
// },
{
path: 'https://cdn.jsdelivr.net/gh/guansss/pixi-live2d-display/test/assets/shizuku/shizuku.model.json',
scale: 0.3
path: 'https://cdn.jsdelivr.net/gh/Eikanya/Live2d-model/galgame%20live2d/Fox%20Hime%20Zero/mori_miko/mori_miko.model3.json',
position: [50, 100],
stageStyle: {
width: 300,
height: 450
}
}
// {
// path: 'https://cdn.jsdelivr.net/gh/guansss/pixi-live2d-display/test/assets/shizuku/shizuku.model.json',
// scale: 0.3
// }
],
tips: {
idleTips: {
// message: []
wordTheDay: true
message: async () => {
return 'asdasd';
}
}
}
});
Expand Down

0 comments on commit e37abbb

Please sign in to comment.