cwd-widget 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/dev.js ADDED
@@ -0,0 +1,164 @@
1
+ /**
2
+ * 开发调试脚本
3
+ */
4
+
5
+ import { CWDComments } from './index.js';
6
+
7
+ // 本地存储的 key
8
+ const STORAGE_KEY = 'cwd-dev-config';
9
+
10
+ // 默认配置
11
+ const DEFAULT_CONFIG = {
12
+ el: '#comments',
13
+ apiBaseUrl: 'http://localhost:8788',
14
+ theme: 'light', // 默认 light / dark
15
+ };
16
+
17
+ let widgetInstance = null;
18
+
19
+ /**
20
+ * 从本地存储加载配置
21
+ */
22
+ function loadConfigFromStorage() {
23
+ try {
24
+ const saved = localStorage.getItem(STORAGE_KEY);
25
+ if (saved) {
26
+ return { ...DEFAULT_CONFIG, ...JSON.parse(saved) };
27
+ }
28
+ } catch (e) {
29
+ }
30
+ return DEFAULT_CONFIG;
31
+ }
32
+
33
+ /**
34
+ * 保存配置到本地存储
35
+ */
36
+ function saveConfigToStorage(config) {
37
+ try {
38
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
39
+ } catch (e) {
40
+ }
41
+ }
42
+
43
+ /**
44
+ * 将配置填充到输入框
45
+ */
46
+ function populateInputs(config) {
47
+ const apiBaseUrlInput = document.getElementById('apiBaseUrl');
48
+ const themeSelect = document.getElementById('theme');
49
+ const avatarPrefixInput = document.getElementById('avatarPrefix');
50
+
51
+ if (apiBaseUrlInput) apiBaseUrlInput.value = config.apiBaseUrl || DEFAULT_CONFIG.apiBaseUrl;
52
+ if (themeSelect) themeSelect.value = config.theme || DEFAULT_CONFIG.theme;
53
+ if (avatarPrefixInput) avatarPrefixInput.value = config.avatarPrefix || DEFAULT_CONFIG.avatarPrefix;
54
+ }
55
+
56
+ /**
57
+ * 从输入框获取当前配置
58
+ */
59
+ function getConfigFromInputs() {
60
+ const apiBaseUrl = document.getElementById('apiBaseUrl')?.value || DEFAULT_CONFIG.apiBaseUrl;
61
+ const theme = document.getElementById('theme')?.value || DEFAULT_CONFIG.theme;
62
+ return { apiBaseUrl, theme };
63
+ }
64
+
65
+ /**
66
+ * 初始化 widget
67
+ */
68
+ async function initWidget() {
69
+ const config = getConfigFromInputs();
70
+
71
+ // 保存到本地存储
72
+ saveConfigToStorage(config);
73
+
74
+ // 如果已存在实例,先卸载
75
+ if (widgetInstance) {
76
+ widgetInstance.unmount();
77
+ widgetInstance = null;
78
+ }
79
+
80
+ // 清空容器
81
+ const container = document.getElementById('comments');
82
+ if (container) {
83
+ container.innerHTML = '';
84
+ }
85
+
86
+ // 创建新实例
87
+ try {
88
+ widgetInstance = new CWDComments({
89
+ el: '#comments',
90
+ apiBaseUrl: config.apiBaseUrl,
91
+ });
92
+ widgetInstance.mount();
93
+ } catch (error) {}
94
+ }
95
+
96
+ /**
97
+ * 切换主题
98
+ */
99
+ function toggleTheme() {
100
+ if (!widgetInstance) {
101
+ return;
102
+ }
103
+
104
+ const currentConfig = widgetInstance.getConfig();
105
+ const newTheme = currentConfig.theme === 'light' ? 'dark' : 'light';
106
+ widgetInstance.updateConfig({ theme: newTheme });
107
+
108
+ // 更新下拉框
109
+ const themeSelect = document.getElementById('theme');
110
+ if (themeSelect) {
111
+ themeSelect.value = newTheme;
112
+ }
113
+
114
+ // 保存到本地存储
115
+ const config = getConfigFromInputs();
116
+ config.theme = newTheme;
117
+ saveConfigToStorage(config);
118
+
119
+ }
120
+
121
+ /**
122
+ * 清除本地存储的配置
123
+ */
124
+ function clearConfig() {
125
+ try {
126
+ localStorage.removeItem(STORAGE_KEY);
127
+ populateInputs(DEFAULT_CONFIG);
128
+ } catch (e) {
129
+ }
130
+ }
131
+
132
+ // 将函数挂载到 window 对象,使其在 HTML 中可访问
133
+ window.initWidget = initWidget;
134
+ window.toggleTheme = toggleTheme;
135
+ window.clearConfig = clearConfig;
136
+
137
+ // 页面加载完成后自动初始化
138
+ document.addEventListener('DOMContentLoaded', () => {
139
+
140
+
141
+ // 从本地存储加载配置并填充到输入框
142
+ const savedConfig = loadConfigFromStorage();
143
+ populateInputs(savedConfig);
144
+
145
+ // 初始化 widget
146
+ initWidget();
147
+ });
148
+
149
+ // 监听输入框变化,实时保存
150
+ document.addEventListener('DOMContentLoaded', () => {
151
+ const inputs = ['apiBaseUrl', 'theme'];
152
+ inputs.forEach((id) => {
153
+ const element = document.getElementById(id);
154
+ if (element) {
155
+ element.addEventListener('change', () => {
156
+ const config = getConfigFromInputs();
157
+ saveConfigToStorage(config);
158
+ });
159
+ }
160
+ });
161
+ });
162
+
163
+ // 导出类型(用于调试)
164
+ window.CWDComments = CWDComments;
package/src/index.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Momo Comments Widget 入口文件
3
+ *
4
+ * 使用方法:
5
+ * ```html
6
+ * <div id="comments"></div>
7
+ * <script src="cwd.js"></script>
8
+ * <script>
9
+ * new CWDComments({
10
+ * el: '#comments',
11
+ * apiBaseUrl: 'https://api.example.com'
12
+ * }).mount();
13
+ * </script>
14
+ * ```
15
+ */
16
+
17
+ import { CWDComments } from './core/CWDComments.js';
18
+
19
+ // 导出为全局变量(用于 UMD 构建)
20
+ if (typeof window !== 'undefined') {
21
+ window.CWDComments = CWDComments;
22
+ }
23
+
24
+ // ES Module 默认导出
25
+ export default CWDComments;
26
+ export { CWDComments };