diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 68e9f05..099771b 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -52,50 +52,14 @@ + + +``` + +### 4. 实现 Config.vue + +```vue + + + + + +``` + +### 5. 实现 index.ts + +```typescript +import Widget from './Widget.vue'; +import Config from './Config.vue'; +import { defaultConfig } from './types'; +import { createWidget } from '../createWidget'; + +export default createWidget({ + label: '你的小组件', + value: 'your-widget', + icon: '🎯', // 选择合适的emoji图标 + description: '这是你的小组件的描述', + component: Widget, + configComponent: Config, + defaultConfig +}); +``` + +### 6. 在 registry.ts 中注册 + +```typescript +// 在 src/widgets/registry.ts 中添加导入 +import YourWidget from './your-widget'; + +// 在小组件注册表中添加 +export const widgets: WidgetRegistration[] = [ + ClockWidget, + DateWidget, + TextWidget, + ImageWidget, + WeatherWidget, + YourWidget, // 添加你的小组件 +]; +``` + +## 完成! + +现在你的新小组件就会自动出现在: +- 首页的小组件列表中 +- 配置页面的小组件选择器中 +- 预览页面中可以显示 + +## 最佳实践 + +1. **命名规范**: 使用kebab-case命名目录和文件 +2. **图标选择**: 选择有意义的emoji作为图标 +3. **配置项**: 提供合理的默认值,让小组件开箱即用 +4. **样式**: 使用CSS变量和计算属性,让样式可配置 +5. **类型安全**: 充分利用TypeScript的类型检查 diff --git a/src/widgets/clock/index.ts b/src/widgets/clock/index.ts index ee5e6c6..8fe80ac 100644 --- a/src/widgets/clock/index.ts +++ b/src/widgets/clock/index.ts @@ -1,11 +1,14 @@ import Widget from './Widget.vue'; import Config from './Config.vue'; import { defaultConfig } from './types'; +import { createWidget } from '../createWidget'; -export default { +export default createWidget({ label: '时钟小组件', value: 'clock', + icon: '⏰', + description: '显示当前时间,可自定义格式、样式和特效', component: Widget, configComponent: Config, - getDefaultConfig: () => defaultConfig -}; + defaultConfig +}); diff --git a/src/widgets/createWidget.ts b/src/widgets/createWidget.ts new file mode 100644 index 0000000..5f8553b --- /dev/null +++ b/src/widgets/createWidget.ts @@ -0,0 +1,47 @@ +import type { Component } from 'vue'; +import type { WidgetRegistration, BaseWidgetConfig } from './types'; + +/** + * 创建小组件注册信息的辅助函数 + * + * @param options 小组件配置选项 + * @returns 小组件注册信息 + */ +export function createWidget(options: { + /** 小组件显示名称 */ + label: string; + /** 小组件唯一标识符 */ + value: string; + /** 小组件图标(emoji 或图标字符) */ + icon: string; + /** 小组件描述 */ + description: string; + /** 小组件组件 */ + component: Component; + /** 小组件配置组件 */ + configComponent: Component; + /** 默认配置 */ + defaultConfig: BaseWidgetConfig; +}): WidgetRegistration { + return { + label: options.label, + value: options.value, + icon: options.icon, + description: options.description, + component: options.component, + configComponent: options.configComponent, + getDefaultConfig: () => options.defaultConfig + }; +} + +/** + * 创建小组件类型定义的辅助函数 + * + * @param defaultConfig 默认配置对象 + * @returns 包含默认配置的对象 + */ +export function createWidgetConfig(defaultConfig: T) { + return { + defaultConfig + }; +} diff --git a/src/widgets/date/index.ts b/src/widgets/date/index.ts index d0e0291..00fcd8c 100644 --- a/src/widgets/date/index.ts +++ b/src/widgets/date/index.ts @@ -1,11 +1,14 @@ import Widget from './Widget.vue'; import Config from './Config.vue'; import { defaultConfig } from './types'; +import { createWidget } from '../createWidget'; -export default { +export default createWidget({ label: '日期小组件', value: 'date', + icon: '📅', + description: '显示当前日期,可自定义格式、样式和特效', component: Widget, configComponent: Config, - getDefaultConfig: () => defaultConfig -}; + defaultConfig +}); diff --git a/src/widgets/image/index.ts b/src/widgets/image/index.ts index cde3e0d..975c40c 100644 --- a/src/widgets/image/index.ts +++ b/src/widgets/image/index.ts @@ -1,11 +1,14 @@ import Widget from './Widget.vue'; import Config from './Config.vue'; import { defaultConfig } from './types'; +import { createWidget } from '../createWidget'; -export default { +export default createWidget({ label: '图片小组件', value: 'image', + icon: '🖼️', + description: '显示图片,可自定义大小、特效和位置', component: Widget, configComponent: Config, - getDefaultConfig: () => defaultConfig -}; + defaultConfig +}); diff --git a/src/widgets/registry.ts b/src/widgets/registry.ts index cb716b9..4463ef0 100644 --- a/src/widgets/registry.ts +++ b/src/widgets/registry.ts @@ -4,14 +4,28 @@ import DateWidget from './date'; import TextWidget from './text'; import ImageWidget from './image'; + +// 导入类型定义 +import type { WidgetRegistration, WidgetItem } from './types'; + // 小组件注册表 -export const widgets = [ +export const widgets: WidgetRegistration[] = [ ClockWidget, DateWidget, TextWidget, ImageWidget, ]; +// 获取小组件显示信息列表(用于 UI 展示) +export const getWidgetItems = (): WidgetItem[] => { + return widgets.map(widget => ({ + value: widget.value, + label: widget.label, + icon: widget.icon, + description: widget.description + })); +}; + // 获取小组件默认配置 export const getDefaultConfig = (widgetType: string) => { for (const widget of widgets) { @@ -21,3 +35,8 @@ export const getDefaultConfig = (widgetType: string) => { } return {}; }; + +// 根据类型获取小组件注册信息 +export const getWidget = (widgetType: string): WidgetRegistration | undefined => { + return widgets.find(widget => widget.value === widgetType); +}; diff --git a/src/widgets/text/index.ts b/src/widgets/text/index.ts index 7ccb65a..fc2de6e 100644 --- a/src/widgets/text/index.ts +++ b/src/widgets/text/index.ts @@ -1,11 +1,14 @@ import Widget from './Widget.vue'; import Config from './Config.vue'; import { defaultConfig } from './types'; +import { createWidget } from '../createWidget'; -export default { +export default createWidget({ label: '文本小组件', value: 'text', + icon: '📝', + description: '显示文本,支持渐变、阴影、字体等自定义样式', component: Widget, configComponent: Config, - getDefaultConfig: () => defaultConfig -}; + defaultConfig +}); diff --git a/src/widgets/types.ts b/src/widgets/types.ts index daa4731..48604f1 100644 --- a/src/widgets/types.ts +++ b/src/widgets/types.ts @@ -9,12 +9,17 @@ export interface BaseWidgetConfig { export interface WidgetRegistration { label: string; value: string; + icon: string; + description: string; component: Component; configComponent: Component; getDefaultConfig: () => BaseWidgetConfig; } -// 小组件模块导出接口 -export interface WidgetModule { - registration: WidgetRegistration; +// 小组件列表项接口(用于 UI 显示) +export interface WidgetItem { + value: string; + label: string; + icon: string; + description: string; }