feat: 移除utils无关代码
This commit is contained in:
177
utils/index.js
177
utils/index.js
@@ -3,179 +3,6 @@
|
||||
* 包含打字机效果、ID生成、回调安全调用等通用工具函数
|
||||
*/
|
||||
|
||||
/**
|
||||
* 打字机工具类
|
||||
* 提供打字机效果相关的工具函数
|
||||
*/
|
||||
export class TypewriterUtils {
|
||||
/**
|
||||
* 计算动态打字速度
|
||||
* @param {string} char - 当前字符
|
||||
* @param {number} baseSpeed - 基础速度(ms)
|
||||
* @returns {number} 动态调整后的速度
|
||||
*/
|
||||
static calculateDynamicSpeed(char, baseSpeed = 100) {
|
||||
const punctuationMarks = [
|
||||
"。",
|
||||
"!",
|
||||
"?",
|
||||
".",
|
||||
"!",
|
||||
"?",
|
||||
",",
|
||||
",",
|
||||
";",
|
||||
";",
|
||||
":",
|
||||
":",
|
||||
];
|
||||
const isSpace = char === " ";
|
||||
const hasPunctuation = punctuationMarks.includes(char);
|
||||
|
||||
if (hasPunctuation) {
|
||||
// 标点符号后停顿更久,模拟思考时间
|
||||
return baseSpeed * 2.5;
|
||||
} else if (isSpace) {
|
||||
// 空格稍快一些
|
||||
return baseSpeed * 0.6;
|
||||
} else {
|
||||
// 普通字符添加一些随机性,模拟真实打字的不均匀性
|
||||
const randomFactor = 0.7 + Math.random() * 0.6; // 0.7-1.3倍速度
|
||||
return baseSpeed * randomFactor;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符是否为标点符号
|
||||
* @param {string} char - 要检查的字符
|
||||
* @returns {boolean} 是否为标点符号
|
||||
*/
|
||||
static isPunctuation(char) {
|
||||
const punctuationMarks = [
|
||||
"。",
|
||||
"!",
|
||||
"?",
|
||||
".",
|
||||
"!",
|
||||
"?",
|
||||
",",
|
||||
",",
|
||||
";",
|
||||
";",
|
||||
":",
|
||||
":",
|
||||
];
|
||||
return punctuationMarks.includes(char);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符是否为空格
|
||||
* @param {string} char - 要检查的字符
|
||||
* @returns {boolean} 是否为空格
|
||||
*/
|
||||
static isSpace(char) {
|
||||
return char === " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成加载动画文本
|
||||
* @param {number} dotCount - 点的数量(1-3)
|
||||
* @param {string} baseText - 基础文本
|
||||
* @returns {string} 加载动画文本
|
||||
*/
|
||||
static generateLoadingText(dotCount = 1, baseText = "加载中") {
|
||||
const normalizedDotCount = ((dotCount - 1) % 3) + 1;
|
||||
return baseText + ".".repeat(normalizedDotCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动加载动画
|
||||
*/
|
||||
static startLoadingAnimation(
|
||||
onProgress,
|
||||
options = {},
|
||||
onTimerCreated = null
|
||||
) {
|
||||
const { speed = 500, text = "加载中" } = options;
|
||||
let dotCount = 1;
|
||||
|
||||
const timerId = setInterval(() => {
|
||||
dotCount = (dotCount % 3) + 1;
|
||||
const loadingText = text + ".".repeat(dotCount);
|
||||
onProgress(loadingText);
|
||||
}, speed);
|
||||
|
||||
if (onTimerCreated) {
|
||||
onTimerCreated(timerId);
|
||||
}
|
||||
|
||||
return timerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动打字机效果
|
||||
* @param {string} text - 要显示的文本
|
||||
* @param {Element} targetElement - 目标元素(可选)
|
||||
* @param {Object} options - 配置选项
|
||||
* @param {Function} onTimerCreated - 定时器创建回调(可选)
|
||||
*/
|
||||
static startTypewriter(
|
||||
text,
|
||||
targetElement = null,
|
||||
options = {},
|
||||
onTimerCreated = null
|
||||
) {
|
||||
const {
|
||||
speed = 100,
|
||||
onProgress = () => {},
|
||||
onComplete = () => {},
|
||||
} = options;
|
||||
|
||||
let index = 0;
|
||||
|
||||
const typeNextChar = () => {
|
||||
if (index < text.length) {
|
||||
const char = text[index];
|
||||
const currentText = text.substring(0, index + 1);
|
||||
|
||||
// 调用进度回调
|
||||
onProgress(currentText, index);
|
||||
|
||||
// 如果有目标元素,更新其内容
|
||||
if (targetElement) {
|
||||
targetElement.textContent = currentText;
|
||||
}
|
||||
|
||||
index++;
|
||||
|
||||
// 计算下一个字符的延时
|
||||
const delay = TypewriterUtils.calculateDynamicSpeed(
|
||||
char,
|
||||
speed
|
||||
);
|
||||
|
||||
const timerId = setTimeout(typeNextChar, delay);
|
||||
|
||||
if (onTimerCreated && index === 1) {
|
||||
onTimerCreated(timerId);
|
||||
}
|
||||
} else {
|
||||
// 打字完成
|
||||
onComplete(text);
|
||||
}
|
||||
};
|
||||
|
||||
// 开始打字
|
||||
const initialTimerId = setTimeout(typeNextChar, 0);
|
||||
|
||||
if (onTimerCreated) {
|
||||
onTimerCreated(initialTimerId);
|
||||
}
|
||||
|
||||
return initialTimerId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ID生成工具类
|
||||
* 提供各种ID生成功能
|
||||
@@ -273,8 +100,7 @@ export class MessageUtils {
|
||||
static isPongMessage(messageData) {
|
||||
if (typeof messageData === "string") {
|
||||
return (
|
||||
messageData === "pong" ||
|
||||
messageData.toLowerCase().includes("pong")
|
||||
messageData === "pong" || messageData.toLowerCase().includes("pong")
|
||||
);
|
||||
}
|
||||
if (typeof messageData === "object" && messageData !== null) {
|
||||
@@ -421,7 +247,6 @@ export class TimerUtils {
|
||||
|
||||
// 默认导出所有工具类
|
||||
export default {
|
||||
TypewriterUtils,
|
||||
IdUtils,
|
||||
CallbackUtils,
|
||||
MessageUtils,
|
||||
|
||||
Reference in New Issue
Block a user