Skip to content

Commit

Permalink
feat(tips): 闲置消息的wordTheDay支持传入格式化函数, 现在您可以通过传入格式化函数定制您自己的消息模板
Browse files Browse the repository at this point in the history
  • Loading branch information
loclink committed Mar 4, 2024
1 parent e37abbb commit 7f43e4c
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 294 deletions.
668 changes: 397 additions & 271 deletions docs/documentation.json

Large diffs are not rendered by default.

Empty file removed docs/options/ImportType.md
Empty file.
26 changes: 22 additions & 4 deletions docs/options/TipsOptions.md
Expand Up @@ -70,9 +70,13 @@

### idleTips.message

- 类型: `string[]`
- 类型: `string[] | (() => Promise<string>)`

- 默认值: `[]`

播放的消息内容, 需要是一个字符串组成的数组, 播放时会从中随机取出一条进行提示, 空数组则不播放, 默认为空数组
闲置时播放的消息内容, 可以是字符串数组也可以是一个返回类型为 string 的异步函数.
当为 message 为数组时会从中随机取出一条进行提示, 当 message 为异步函数时会在定时器同步执行这个函数并使用这个函数的返回值进行提示, 空数组则不播放, 默认为空数组.
但当 wordTheDay 开启时, 将接管 message 的值

---

Expand All @@ -88,11 +92,25 @@

### idleTips.wordTheDay

- 类型: `boolean`
- 类型: `boolean | ((wordTheDayData: WordTheDayData) => string)`

- 默认值: `false`

是否开启每日一言, 默认为关闭状态, 开启后将从这个地址请求 <https://v1.hitokoto.cn> 随机信息作为闲置提示内容
是否开启每日一言, 默认为关闭状态, 开启后将从 <https://v1.hitokoto.cn> 发送网络请求, 并将请求到的随机信息作为闲置提示内容, 除了 boolean 类型之外, 它还支持您传入一个格式化函数, 这在您需要自定义消息格式时非常有用, 格式化函数的返回值必须是一个 string 类型, 其接收一个 wordTheDayData, 该参数包含了这次网络请求成功后的所有响应数据, 方便您处理格式化.

以下是一个示例:

```ts
{
tips: {
idleTips: {
wordTheDay(wordTheDayData) {
return `${wordTheDayData.hitokoto} by.${wordTheDayData.from}`;
}
}
}
}
```

::: tip

Expand Down
3 changes: 1 addition & 2 deletions docs/sideBarData.json
@@ -1,6 +1,5 @@
[
{ "text": "配置选项", "link": "/options/Options" },
{ "text": "模型选项", "link": "/options/ModelOptions" },
{ "text": "提示框选项", "link": "/options/TipsOptions" },
{ "text": "ImportType", "link": "/options/ImportType" }
{ "text": "提示框选项", "link": "/options/TipsOptions" }
]
6 changes: 5 additions & 1 deletion packages/oh-my-live2d/src/modules/tips.ts
Expand Up @@ -159,7 +159,11 @@ export class Tips {
const timer = setIntervalAsync(async () => {
// 是否开启每日一言
if (this.tipsOptions.idleTips.wordTheDay) {
message = await getWordTheDay();
if (isFunction(this.tipsOptions.idleTips.wordTheDay)) {
message = await getWordTheDay(this.tipsOptions.idleTips.wordTheDay);
} else {
message = await getWordTheDay();
}
} else {
if (isFunction(messages)) {
message = await messages();
Expand Down
23 changes: 23 additions & 0 deletions packages/oh-my-live2d/src/types/common.ts
Expand Up @@ -2,3 +2,26 @@ export type IdleTimer = {
start: () => Promise<void>;
stop: () => void;
};

export type WordTheDayData = {
id: number;
uuid: string;
hitokoto: string;
type: string;
from: string;

// eslint-disable-next-line @typescript-eslint/naming-convention
from_who: string;
creator: string;

// eslint-disable-next-line @typescript-eslint/naming-convention
creator_uid: number;
reviewer: number;

// eslint-disable-next-line @typescript-eslint/naming-convention
commit_from: string;

// eslint-disable-next-line @typescript-eslint/naming-convention
created_at: string;
length: number;
};
27 changes: 22 additions & 5 deletions packages/oh-my-live2d/src/types/tips.ts
@@ -1,3 +1,5 @@
import type { WordTheDayData } from 'src/types/common.js';

/**
* # 提示框选项
*
Expand Down Expand Up @@ -47,16 +49,30 @@ export interface TipsOptions {
*/
idleTips?: {
/**
* 是否开启每日一言, 默认为关闭状态, 开启后将从 <https://v1.hitokoto.cn> 发送网络请求, 并将请求到的随机信息作为闲置提示内容
* 是否开启每日一言, 默认为关闭状态, 开启后将从 <https://v1.hitokoto.cn> 发送网络请求, 并将请求到的随机信息作为闲置提示内容, 除了 boolean 类型之外, 它还支持您传入一个格式化函数, 这在您需要自定义消息格式时非常有用, 格式化函数的返回值必须是一个 string 类型, 其接收一个 wordTheDayData, 该参数包含了这次网络请求成功后的所有响应数据, 方便您处理格式化.
*
* 以下是一个示例:
* ```ts
*{
* tips: {
* idleTips: {
* wordTheDay(wordTheDayData) {
* return `${wordTheDayData.hitokoto} by.${wordTheDayData.from}`;
* }
* }
* }
*}
* ```
*
* ::: tip
*
* 开启后将接管闲置状态下 message 选项设定的值
*
* :::
* @default false
* @valueType boolean | ((wordTheDayData: WordTheDayData) => string)
*/
wordTheDay?: boolean;
wordTheDay?: boolean | ((wordTheDayData: WordTheDayData) => string);

/**
* 提示框持续时间, 单位 ms
Expand All @@ -79,11 +95,12 @@ export interface TipsOptions {
interval?: number;

/**
* 闲置时播放的消息内容, 可以是字符串数组也可以是一个返回类型为Promise<string>的异步函数
*
* 当为 message 为数组时会从中随机取出一条进行提示, 当 message 为异步函数时会在定时器同步执行这个函数并使用这个函数的返回值进行提示, 空数组则不播放, 默认为空数组
* 闲置时播放的消息内容, 可以是字符串数组也可以是一个返回类型为 string 的异步函数.
* 当为 message 为数组时会从中随机取出一条进行提示, 当 message 为异步函数时会在定时器同步执行这个函数并使用这个函数的返回值进行提示, 空数组则不播放, 默认为空数组.
* 但当 wordTheDay 开启时, 将接管 message 的值
*
* @valueType string[] | (() => Promise<string>)
* @default []
*/
message?: string[] | (() => Promise<string>);
};
Expand Down
7 changes: 6 additions & 1 deletion packages/oh-my-live2d/src/utils/index.ts
@@ -1,3 +1,4 @@
import type { WordTheDayData } from 'src/types/common.js';
import { isNumber } from 'tianjie';

import { SDK } from '../config/index.js';
Expand Down Expand Up @@ -128,9 +129,13 @@ export const checkVersion = async (): Promise<void> => {
};

// 获取每日一言
export const getWordTheDay = async (): Promise<string> => {
export const getWordTheDay = async (format?: (wordTheDayData: WordTheDayData) => string): Promise<string> => {
const fetchResult = await fetch('https://v1.hitokoto.cn/');
const data = <{ hitokoto: string; from: string }>await fetchResult.json();

if (format) {
return format(data as WordTheDayData);
}

return `${data.hitokoto} -- ${data.from}`;
};
5 changes: 3 additions & 2 deletions scripts/typedoc.js
Expand Up @@ -137,8 +137,9 @@ async function main() {

// 判断是否为监听模式
if (process.argv.includes('-w') || process.argv.includes('--watch')) {
app.convertAndWatch((project) => {
generateDocs(app, project);
// @ts-ignore
app.convertAndWatch(async (project) => {
await generateDocs(app, project);
});
} else {
const project = await app.convert();
Expand Down

0 comments on commit 7f43e4c

Please sign in to comment.