+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+```
+
+### 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