Skip to content

Commit df65c2b

Browse files
authored
fix: [AIChatInput]Fixed an issue with the setContentWhileSaveTool function (#3041)
1 parent efe11d5 commit df65c2b

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

content/ai/aiChatInput/index-en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ render(<CustomRichTextExtension />);
15251525
| focusEditor | Focus the input box. By default, the focus is on the end of the input box. | (pos?: string) => void | - |
15261526
| getEditor | Get the current tiptap editor instance | () => Editor | - |
15271527
| setContent | Set input box content | (content: TiptapContent) => void | - |
1528-
| setContentWhileSaveTool | Set the input box content while retaining the skill item | (content: TiptapContent) => void | - |
1528+
| setContentWhileSaveTool | Set the input box content while retaining the skill item | (content: string) => void | - |
15291529

15301530
## Design Tokens
15311531

content/ai/aiChatInput/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ render(<CustomRichTextExtension />);
16031603
| focusEditor | 聚焦输入框,默认聚焦到输入框的末尾 | (pos?: string) => void | - |
16041604
| getEditor | 获取当前的 tiptap 的 editor 实例 | () => Editor | - |
16051605
| setContent | 设置输入框内容 | (content: TiptapContent) => void | - |
1606-
| setContentWhileSaveTool | 保留技能项的同时设置输入框内容 | (content: TiptapContent) => void | - |
1606+
| setContentWhileSaveTool | 保留技能项的同时设置输入框内容 | (content: string) => void | - |
16071607

16081608

16091609

packages/semi-foundation/aiChatInput/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function getCustomSlotAttribute() {
145145

146146
export function findSkillSlotInString(content: string) {
147147
const reg = /<skill-slot\s+([^>]*)><\/skill-slot>/i;
148-
const attrReg = /([\w-]+)=["']?([^"'\s>]+)["']?/g;
148+
const attrReg = /([\w-]+)=["']([^"']*)["']/g;
149149
const match = reg.exec(content);
150150
if (match) {
151151
const attrsStr = match[1];
@@ -175,8 +175,8 @@ function omitUndefinedFromObj(obj: { [key: string]: any }) {
175175

176176
export function getSkillSlotString(skill: BaseSkill) {
177177
let skillParams = '';
178-
skill.label && (skillParams += ` data-label=${skill.label}`);
179-
skill.value && (skillParams += ` data-value=${skill.value}`);
178+
skill.label && (skillParams += ` data-label="${skill.label}"`);
179+
skill.value && (skillParams += ` data-value="${skill.value}"`);
180180
(typeof skill.hasTemplate === 'boolean') && (skillParams += ` data-template=${skill.hasTemplate}`);
181-
return `<skill-slot ${skillParams}"></skill-slot>`;
181+
return `<skill-slot ${skillParams}></skill-slot>`;
182182
}

packages/semi-ui/aiChatInput/_story/aiChatInput.stories.jsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useState, useRef, useMemo } from 'react';
1+
import React, { useCallback, useState, useRef, useMemo, useEffect } from 'react';
22
import Button from '../../button';
33
import AIChatInput from '../index';
44
import Configure from '../configure';
@@ -639,4 +639,43 @@ export const AddPasteRule = () => {
639639
{/* <Button onClick={onButtonClick2}>点我设置结果</Button> */}
640640
</>
641641
);
642-
}
642+
}
643+
644+
export const SetContent = () => {
645+
const ref = useRef();
646+
647+
const onContentChange = useCallback((content) => {
648+
console.log('onContentChange', content);
649+
}, []);
650+
651+
const onSkillChange = useCallback((skill) => {
652+
console.log("skill", skill);
653+
})
654+
655+
useEffect(() => {
656+
// 先用 setContent 设置了内容,其中包含 skill-slot
657+
ref.current?.setContent('<skill-slot data-label="AI 写代码" data-value="AI coding" data-template=true></skill-slot>帮我完成...');
658+
setTimeout(() => {
659+
// 后用 setContentWhileSaveTool 实现在保留 skill 的基础上修改内容
660+
ref.current?.setContentWhileSaveTool('改变后的内容');
661+
}, 1000);
662+
}, []);
663+
664+
return (<>
665+
<AIChatInput
666+
ref={ref}
667+
placeholder={'输入内容或者上传内容'}
668+
uploadProps={uploadProps}
669+
onSkillChange={onSkillChange}
670+
onContentChange={onContentChange}
671+
style={outerStyle}
672+
/>
673+
<br/><br/>
674+
<Button
675+
onClick={() => {
676+
console.log('html', ref.current?.getEditor().getHTML());
677+
console.log('json', ref.current?.getEditor().getJSON());
678+
}}
679+
>点我查看富文本区域内容</Button>
680+
</>);
681+
}

packages/semi-ui/aiChatInput/extension/skillSlot/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const SkillSlot = Node.create({
5353
renderHTML: (attributes: Record<string, any>) => ({ 'data-label': attributes.label }),
5454
},
5555
hasTemplate: {
56-
parseHTML: (element: HTMLElement) => (element as HTMLElement).getAttribute('data-template'),
56+
parseHTML: (element: HTMLElement) => (element as HTMLElement).getAttribute('data-template') === 'true',
5757
renderHTML: (attributes: Record<string, any>) => ({ 'data-template': attributes.hasTemplate }),
5858
},
5959
isCustomSlot: getCustomSlotAttribute(),

packages/semi-ui/aiChatInput/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IconSendMsgStroked, IconFile, IconCode, IconCrossStroked,
1313
import '@douyinfe/semi-foundation/aiChatInput/aiChatInput.scss';
1414
import HorizontalScroller from './horizontalScroller';
1515
import cls from 'classnames';
16-
import { getAttachmentType, isImageType, getContentType, getCustomSlotAttribute } from '@douyinfe/semi-foundation/aiChatInput/utils';
16+
import { getAttachmentType, isImageType, getContentType, getCustomSlotAttribute, getSkillSlotString } from '@douyinfe/semi-foundation/aiChatInput/utils';
1717
import Configure from './configure';
1818
import RichTextInput from './richTextInput';
1919
import { Editor, FocusPosition } from '@tiptap/core';
@@ -252,7 +252,7 @@ class AIChatInput extends BaseComponent<AIChatInputProps, AIChatInputState> {
252252
if (!skill) {
253253
realContent = `<p>${content}</p>`;
254254
} else {
255-
realContent = `<p><skill-slot data-value=${skill.label ?? 'test'}></skill-slot>${content}</p>`;
255+
realContent = `<p>${getSkillSlotString(skill)}${content}</p>`;
256256
}
257257
this.setContent(realContent);
258258
}

0 commit comments

Comments
 (0)