工具函数模块: - 实现 localStorage 存储和加载功能 - 提供默认快捷短语模板(写作助手、代码解释、优化建议等) - 添加数据验证和排序函数 Hook 模块: - 实现增删改查功能 - 支持拖拽排序 - 自动同步到 localStorage - 提供重置为默认值功能
119 lines
2.9 KiB
TypeScript
119 lines
2.9 KiB
TypeScript
import type { QuickPhrase } from '@/types';
|
|
|
|
/**
|
|
* 生成唯一的快捷短语 ID
|
|
*/
|
|
export function generatePhraseId(): string {
|
|
return `phrase-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
}
|
|
|
|
/**
|
|
* 默认的快捷短语列表
|
|
*/
|
|
export const DEFAULT_PHRASES: QuickPhrase[] = [
|
|
{
|
|
id: 'default-1',
|
|
title: '写作助手',
|
|
content: '请帮我写一篇关于',
|
|
icon: 'PenTool',
|
|
sortOrder: 1,
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
},
|
|
{
|
|
id: 'default-2',
|
|
title: '代码解释',
|
|
content: '请解释这段代码的功能和工作原理:\n\n',
|
|
icon: 'Code',
|
|
sortOrder: 2,
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
},
|
|
{
|
|
id: 'default-3',
|
|
title: '优化建议',
|
|
content: '请帮我优化以下内容,使其更加专业和准确:\n\n',
|
|
icon: 'Sparkles',
|
|
sortOrder: 3,
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
},
|
|
{
|
|
id: 'default-4',
|
|
title: '翻译助手',
|
|
content: '请帮我翻译以下内容:\n\n',
|
|
icon: 'Languages',
|
|
sortOrder: 4,
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
},
|
|
{
|
|
id: 'default-5',
|
|
title: '头脑风暴',
|
|
content: '请帮我进行头脑风暴,给我一些关于以下主题的创意想法:\n\n',
|
|
icon: 'Lightbulb',
|
|
sortOrder: 5,
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
},
|
|
];
|
|
|
|
/**
|
|
* localStorage 的 key
|
|
*/
|
|
export const STORAGE_KEY = 'cch_quick_phrases';
|
|
|
|
/**
|
|
* 从 localStorage 加载快捷短语
|
|
*/
|
|
export function loadPhrasesFromStorage(): QuickPhrase[] {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored) {
|
|
const phrases = JSON.parse(stored) as QuickPhrase[];
|
|
// 验证数据格式
|
|
if (Array.isArray(phrases) && phrases.length > 0) {
|
|
return phrases;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load quick phrases from storage:', error);
|
|
}
|
|
// 如果没有存储的数据或加载失败,返回默认短语
|
|
return DEFAULT_PHRASES;
|
|
}
|
|
|
|
/**
|
|
* 保存快捷短语到 localStorage
|
|
*/
|
|
export function savePhrasesToStorage(phrases: QuickPhrase[]): void {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(phrases));
|
|
} catch (error) {
|
|
console.error('Failed to save quick phrases to storage:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证快捷短语数据
|
|
*/
|
|
export function validatePhrase(phrase: Partial<QuickPhrase>): phrase is QuickPhrase {
|
|
return (
|
|
typeof phrase.id === 'string' &&
|
|
typeof phrase.title === 'string' &&
|
|
phrase.title.trim().length > 0 &&
|
|
typeof phrase.content === 'string' &&
|
|
phrase.content.trim().length > 0 &&
|
|
typeof phrase.sortOrder === 'number' &&
|
|
typeof phrase.createdAt === 'number' &&
|
|
typeof phrase.updatedAt === 'number'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 按排序顺序对短语进行排序
|
|
*/
|
|
export function sortPhrases(phrases: QuickPhrase[]): QuickPhrase[] {
|
|
return [...phrases].sort((a, b) => a.sortOrder - b.sortOrder);
|
|
}
|