@alicloud/appflow-chat 0.0.3 → 0.0.4-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alicloud/appflow-chat",
3
- "version": "0.0.3",
3
+ "version": "0.0.4-beta.1",
4
4
  "description": "Appflow-Chat AI聊天机器人组件库,提供聊天服务和UI组件",
5
5
  "type": "module",
6
6
  "main": "./dist/appflow-chat.cjs.js",
@@ -58,9 +58,19 @@
58
58
  "vite-plugin-dts": "^4.3.0"
59
59
  },
60
60
  "peerDependencies": {
61
- "antd": "^5.0.0",
61
+ "antd": "^4.0.0 || ^5.0.0",
62
62
  "react": "^18.0.0",
63
- "react-dom": "^18.0.0"
63
+ "react-dom": "^18.0.0",
64
+ "dayjs": "^1.11.0",
65
+ "moment": "^2.29.0"
66
+ },
67
+ "peerDependenciesMeta": {
68
+ "dayjs": {
69
+ "optional": true
70
+ },
71
+ "moment": {
72
+ "optional": true
73
+ }
64
74
  },
65
75
  "files": [
66
76
  "dist",
@@ -1,6 +1,5 @@
1
1
  import React, { useCallback, useMemo } from 'react';
2
- import { DatePicker } from 'antd';
3
- import dayjs, { Dayjs } from 'dayjs';
2
+ import { DatePicker, version } from 'antd';
4
3
  import { TimeFieldProps, TimeSubType } from './types';
5
4
  import styled from 'styled-components';
6
5
 
@@ -15,6 +14,50 @@ const TimeFieldContainer = styled.div`
15
14
  }
16
15
  `;
17
16
 
17
+ // ==================== 版本检测 ====================
18
+
19
+ /**
20
+ * 检测 antd 版本是否为 5.x 或更高
21
+ * antd 5.x 使用 dayjs,antd 4.x 使用 moment
22
+ */
23
+ const getAntdMajorVersion = (): number => {
24
+ try {
25
+ return parseInt(version.split('.')[0], 10);
26
+ } catch {
27
+ return 5; // 默认假设为 5.x
28
+ }
29
+ };
30
+
31
+ const isAntd5OrAbove = getAntdMajorVersion() >= 5;
32
+
33
+ // ==================== 动态导入时间库 ====================
34
+
35
+ // 根据 antd 版本动态选择时间库
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ let dayjs: any = null;
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ let moment: any = null;
40
+
41
+ if (isAntd5OrAbove) {
42
+ // antd 5.x 使用 dayjs
43
+ try {
44
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
45
+ dayjs = require('dayjs');
46
+ } catch {
47
+ console.warn('dayjs not found, TimeField may not work correctly with antd 5.x');
48
+ }
49
+ } else {
50
+ // antd 4.x 使用 moment
51
+ try {
52
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
53
+ moment = require('moment');
54
+ } catch {
55
+ console.warn('moment not found, TimeField may not work correctly with antd 4.x');
56
+ }
57
+ }
58
+
59
+ // ==================== 工具函数 ====================
60
+
18
61
  /**
19
62
  * 根据时间子类型获取日期格式
20
63
  */
@@ -45,10 +88,45 @@ const getPickerType = (subType?: TimeSubType): 'date' | 'month' | undefined => {
45
88
  }
46
89
  };
47
90
 
91
+ /**
92
+ * 将字符串值转换为时间对象(根据 antd 版本使用 dayjs 或 moment)
93
+ */
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ const parseValue = (value: string | null | undefined, format: string): any => {
96
+ if (!value) return null;
97
+
98
+ if (isAntd5OrAbove && dayjs) {
99
+ return dayjs(value, format);
100
+ } else if (moment) {
101
+ return moment(value, format);
102
+ }
103
+
104
+ return null;
105
+ };
106
+
107
+ /**
108
+ * 将时间对象格式化为字符串
109
+ */
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ const formatValue = (date: any, format: string): string | null => {
112
+ if (!date) return null;
113
+
114
+ // dayjs 和 moment 都有 format 方法
115
+ if (typeof date.format === 'function') {
116
+ return date.format(format);
117
+ }
118
+
119
+ return null;
120
+ };
121
+
48
122
  /**
49
123
  * 时间字段组件
50
124
  * 根据 SubType 渲染不同的时间选择器
51
125
  * 优先从 AssociationPropertyMetadata.SubType 读取,兼容旧的 TimeSubType 字段
126
+ *
127
+ * 兼容性说明:
128
+ * - antd 5.x: 使用 dayjs
129
+ * - antd 4.x: 使用 moment
52
130
  */
53
131
  export const TimeField: React.FC<TimeFieldProps> = ({
54
132
  schema,
@@ -72,23 +150,21 @@ export const TimeField: React.FC<TimeFieldProps> = ({
72
150
 
73
151
  // 处理值变化
74
152
  const handleChange = useCallback(
75
- (date: Dayjs | null) => {
76
- if (date) {
77
- onChange?.(date.format(format));
78
- } else {
79
- onChange?.(null);
80
- }
153
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
+ (date: any) => {
155
+ const formattedValue = formatValue(date, format);
156
+ onChange?.(formattedValue);
81
157
  },
82
158
  [onChange, format]
83
159
  );
84
160
 
85
- // 将字符串值转换为 dayjs 对象
86
- const dayjsValue = value ? dayjs(value, format) : null;
161
+ // 将字符串值转换为时间对象
162
+ const dateValue = parseValue(value, format);
87
163
 
88
164
  return (
89
165
  <TimeFieldContainer>
90
166
  <DatePicker
91
- value={dayjsValue}
167
+ value={dateValue}
92
168
  onChange={handleChange}
93
169
  format={format}
94
170
  picker={picker}
@@ -53,9 +53,28 @@ export const MarkdownView: React.FC<MarkdownViewProps> = ({
53
53
  />
54
54
  );
55
55
  },
56
- code: ({ node, className, ...props }: any) => {
56
+ code: ({ node, inline, className, ...props }: any) => {
57
+ // 判断是否为行内代码
58
+ // 行内代码没有 className,且 inline 为 true
59
+ const isInline = inline || (!className && !node?.properties?.className);
60
+
61
+ if (isInline) {
62
+ // 行内代码 - 使用简单的 <code> 标签样式
63
+ return (
64
+ <code style={{
65
+ backgroundColor: 'rgba(175, 184, 193, 0.2)',
66
+ padding: '0.2em 0.4em',
67
+ borderRadius: '3px',
68
+ fontSize: '85%',
69
+ fontFamily: 'Consolas, Monaco, "Andale Mono", monospace',
70
+ }}>
71
+ {props.children}
72
+ </code>
73
+ );
74
+ }
75
+
57
76
  // 元信息中解析语言类型
58
- const languageType = /language-(\w+)/.exec(node?.properties?.className || '')?.[0];
77
+ const languageType = /language-(\w+)/.exec(node?.properties?.className || className || '')?.[0];
59
78
  const langFromMeta = node?.position?.start?.line === 2 ? 'language-file' : null;
60
79
  const finalLang = languageType || langFromMeta;
61
80