Skip to content

Commit 7010ca4

Browse files
authored
Merge pull request #1343 from ant-design/doc_demo
docs: updae docs demo
2 parents eb42d28 + be967ac commit 7010ca4

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

packages/x/components/bubble/interface.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ export type Info = {
7575
extraInfo?: AnyObject;
7676
};
7777
export interface BubbleProps<ContentType extends BubbleContentType = string>
78-
extends Omit<
79-
React.HTMLAttributes<HTMLDivElement>,
80-
'content' | 'onAnimationStart' | 'onAnimationEnd'
81-
> {
78+
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'content'> {
8279
prefixCls?: string;
8380
styles?: Partial<Record<SemanticType, React.CSSProperties>>;
8481
rootClassName?: string;

packages/x/components/file-card/FileCard.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export type PresetIcons =
4040
| 'java'
4141
| 'javascript'
4242
| 'python';
43-
export interface FileCardProps {
43+
export interface FileCardProps
44+
extends Omit<
45+
React.HTMLAttributes<HTMLDivElement>,
46+
'content' | 'onAnimationStart' | 'onAnimationEnd'
47+
> {
4448
prefixCls?: string;
4549
style?: React.CSSProperties;
4650
styles?: Partial<Record<SemanticType, React.CSSProperties>>;

packages/x/docs/x-markdown/demo/codeDemo/link.tsx

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,70 @@ const LOCALE_MARKDOWN = {
1717
};
1818

1919
const findFirstForbiddenCharIndex = (() => {
20-
let _markdownSegmenter: any;
20+
// 预定义常量,避免重复创建
21+
const FORBIDDEN_CHARS = new Set([
22+
'(',
23+
')',
24+
'[',
25+
']',
26+
'{',
27+
'}',
28+
'(',
29+
')',
30+
'「',
31+
'」',
32+
'。',
33+
',',
34+
]);
35+
const CHINESE_REGEX = /[\u4e00-\u9fa5]/;
2136

22-
return (str: string): number => {
23-
if (typeof str !== 'string' || str.length === 0) {
24-
return -1;
25-
}
37+
let segmenter: any = null;
38+
39+
// 检查是否支持 Intl.Segmenter
40+
const isSegmenterSupported = (): boolean => {
41+
return typeof window !== 'undefined' && 'Intl' in window && 'Segmenter' in (Intl as any);
42+
};
2643

27-
const forbiddenChars = new Set(['(', ')', '[', ']', '{', '}', '(', ')', '「', '」']);
44+
// 获取或初始化 segmenter
45+
const getSegmenter = (): any => {
46+
if (segmenter !== null) return segmenter;
2847

29-
if ('Intl' in window && 'Segmenter' in Intl) {
48+
if (isSegmenterSupported()) {
3049
try {
31-
if (!_markdownSegmenter) {
32-
_markdownSegmenter = new (Intl as any).Segmenter('zh', { granularity: 'grapheme' });
33-
}
34-
const segmenter = _markdownSegmenter;
35-
36-
let index = 0;
37-
for (const segment of segmenter.segment(str)) {
38-
const char = segment.segment;
39-
if (forbiddenChars.has(char)) {
40-
return index;
41-
}
42-
index += char.length;
43-
}
44-
return -1;
50+
segmenter = new (Intl as any).Segmenter('zh', { granularity: 'grapheme' });
4551
} catch {
46-
// 降级到字符遍历方法
52+
segmenter = null;
4753
}
4854
}
55+
return segmenter;
56+
};
4957

50-
// 优化的字符遍历方法,避免正则表达式开销
51-
for (let i = 0; i < str.length; i++) {
52-
const char = str[i];
53-
if (forbiddenChars.has(char)) {
54-
return i;
58+
// 检查字符是否为禁止字符
59+
const isForbiddenChar = (char: string): boolean => {
60+
return FORBIDDEN_CHARS.has(char) || CHINESE_REGEX.test(char);
61+
};
62+
63+
return (str: string): number => {
64+
// 简化的空值检查
65+
if (!str) return -1;
66+
67+
const seg = getSegmenter();
68+
69+
// 使用 Intl.Segmenter 进行 Unicode 感知处理
70+
if (seg) {
71+
let index = 0;
72+
for (const segment of seg.segment(str)) {
73+
const char = segment.segment;
74+
if (isForbiddenChar(char)) return index;
75+
index += char.length;
76+
}
77+
} else {
78+
// 降级到直接字符遍历
79+
for (let i = 0; i < str.length; i++) {
80+
if (isForbiddenChar(str[i])) return i;
5581
}
5682
}
83+
5784
return -1;
5885
};
5986
})();

0 commit comments

Comments
 (0)