@opensumi/ide-ai-native 3.8.3-next-1741752385.0 → 3.8.3-next-1741763229.0
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/lib/browser/chat/chat-agent.service.d.ts +1 -1
- package/lib/browser/chat/chat-agent.service.d.ts.map +1 -1
- package/lib/browser/chat/chat-agent.service.js +7 -7
- package/lib/browser/chat/chat-agent.service.js.map +1 -1
- package/lib/browser/chat/chat-model.d.ts +2 -2
- package/lib/browser/chat/chat-model.d.ts.map +1 -1
- package/lib/browser/chat/chat-model.js +18 -1
- package/lib/browser/chat/chat-model.js.map +1 -1
- package/lib/browser/chat/chat.view.d.ts.map +1 -1
- package/lib/browser/chat/chat.view.js +49 -18
- package/lib/browser/chat/chat.view.js.map +1 -1
- package/lib/browser/components/ChatEditor.d.ts +5 -2
- package/lib/browser/components/ChatEditor.d.ts.map +1 -1
- package/lib/browser/components/ChatEditor.js +45 -6
- package/lib/browser/components/ChatEditor.js.map +1 -1
- package/lib/browser/components/ChatMentionInput.d.ts +25 -0
- package/lib/browser/components/ChatMentionInput.d.ts.map +1 -0
- package/lib/browser/components/ChatMentionInput.js +221 -0
- package/lib/browser/components/ChatMentionInput.js.map +1 -0
- package/lib/browser/components/ChatReply.d.ts.map +1 -1
- package/lib/browser/components/ChatReply.js +35 -17
- package/lib/browser/components/ChatReply.js.map +1 -1
- package/lib/browser/components/ChatThinking.js +1 -1
- package/lib/browser/components/ChatThinking.js.map +1 -1
- package/lib/browser/components/WelcomeMsg.js +1 -1
- package/lib/browser/components/WelcomeMsg.js.map +1 -1
- package/lib/browser/components/{ChatContext/ContextSelector.d.ts → chat-context/context-selector.d.ts} +1 -1
- package/lib/browser/components/chat-context/context-selector.d.ts.map +1 -0
- package/lib/browser/components/{ChatContext/ContextSelector.js → chat-context/context-selector.js} +1 -1
- package/lib/browser/components/chat-context/context-selector.js.map +1 -0
- package/lib/browser/components/chat-context/index.d.ts.map +1 -0
- package/lib/browser/components/{ChatContext → chat-context}/index.js +2 -2
- package/lib/browser/components/chat-context/index.js.map +1 -0
- package/lib/browser/components/components.module.less +43 -0
- package/lib/browser/components/mention-input/mention-input.d.ts +5 -0
- package/lib/browser/components/mention-input/mention-input.d.ts.map +1 -0
- package/lib/browser/components/mention-input/mention-input.js +753 -0
- package/lib/browser/components/mention-input/mention-input.js.map +1 -0
- package/lib/browser/components/mention-input/mention-input.module.less +327 -0
- package/lib/browser/components/mention-input/mention-item.d.ts +10 -0
- package/lib/browser/components/mention-input/mention-item.d.ts.map +1 -0
- package/lib/browser/components/mention-input/mention-item.js +16 -0
- package/lib/browser/components/mention-input/mention-item.js.map +1 -0
- package/lib/browser/components/mention-input/mention-panel.d.ts +15 -0
- package/lib/browser/components/mention-input/mention-panel.d.ts.map +1 -0
- package/lib/browser/components/mention-input/mention-panel.js +49 -0
- package/lib/browser/components/mention-input/mention-panel.js.map +1 -0
- package/lib/browser/components/mention-input/types.d.ts +76 -0
- package/lib/browser/components/mention-input/types.d.ts.map +1 -0
- package/lib/browser/components/mention-input/types.js +16 -0
- package/lib/browser/components/mention-input/types.js.map +1 -0
- package/lib/browser/context/llm-context.service.d.ts +10 -2
- package/lib/browser/context/llm-context.service.d.ts.map +1 -1
- package/lib/browser/context/llm-context.service.js +71 -2
- package/lib/browser/context/llm-context.service.js.map +1 -1
- package/lib/browser/contrib/inline-completions/prompt/matcher.js +2 -2
- package/lib/browser/contrib/inline-completions/prompt/similarSnippets.d.ts +1 -1
- package/lib/browser/contrib/inline-completions/prompt/similarSnippets.js +2 -2
- package/lib/browser/contrib/intelligent-completions/view/default.d.ts.map +1 -1
- package/lib/browser/contrib/intelligent-completions/view/default.js.map +1 -1
- package/lib/browser/mcp/config/components/mcp-config.view.d.ts.map +1 -1
- package/lib/browser/mcp/config/components/mcp-config.view.js +28 -18
- package/lib/browser/mcp/config/components/mcp-config.view.js.map +1 -1
- package/lib/browser/mcp/config/components/mcp-server-form.d.ts.map +1 -1
- package/lib/browser/mcp/config/components/mcp-server-form.js +33 -25
- package/lib/browser/mcp/config/components/mcp-server-form.js.map +1 -1
- package/lib/browser/mcp/mcp-server.feature.registry.js +1 -1
- package/lib/browser/mcp/mcp-server.feature.registry.js.map +1 -1
- package/lib/browser/mcp/tools/components/ExpandableFileList.d.ts.map +1 -1
- package/lib/browser/mcp/tools/components/ExpandableFileList.js +3 -1
- package/lib/browser/mcp/tools/components/ExpandableFileList.js.map +1 -1
- package/lib/browser/mcp/tools/components/Terminal.d.ts.map +1 -1
- package/lib/browser/mcp/tools/components/Terminal.js +6 -5
- package/lib/browser/mcp/tools/components/Terminal.js.map +1 -1
- package/lib/browser/mcp/tools/components/computeAnsiLogString.d.ts +4 -0
- package/lib/browser/mcp/tools/components/computeAnsiLogString.d.ts.map +1 -0
- package/lib/browser/mcp/tools/components/computeAnsiLogString.js +22 -0
- package/lib/browser/mcp/tools/components/computeAnsiLogString.js.map +1 -0
- package/lib/browser/mcp/tools/components/filterEraseMultipleLine.d.ts +18 -0
- package/lib/browser/mcp/tools/components/filterEraseMultipleLine.d.ts.map +1 -0
- package/lib/browser/mcp/tools/components/filterEraseMultipleLine.js +69 -0
- package/lib/browser/mcp/tools/components/filterEraseMultipleLine.js.map +1 -0
- package/lib/browser/mcp/tools/components/index.module.less +8 -5
- package/lib/browser/mcp/tools/createNewFileWithText.d.ts.map +1 -1
- package/lib/browser/mcp/tools/createNewFileWithText.js +1 -0
- package/lib/browser/mcp/tools/createNewFileWithText.js.map +1 -1
- package/lib/browser/mcp/tools/editFile.d.ts.map +1 -1
- package/lib/browser/mcp/tools/editFile.js +1 -0
- package/lib/browser/mcp/tools/editFile.js.map +1 -1
- package/lib/browser/mcp/tools/fileSearch.d.ts.map +1 -1
- package/lib/browser/mcp/tools/fileSearch.js +1 -0
- package/lib/browser/mcp/tools/fileSearch.js.map +1 -1
- package/lib/browser/mcp/tools/getDiagnosticsByPath.d.ts.map +1 -1
- package/lib/browser/mcp/tools/getDiagnosticsByPath.js +2 -1
- package/lib/browser/mcp/tools/getDiagnosticsByPath.js.map +1 -1
- package/lib/browser/mcp/tools/getOpenEditorFileDiagnostics.d.ts.map +1 -1
- package/lib/browser/mcp/tools/getOpenEditorFileDiagnostics.js +2 -0
- package/lib/browser/mcp/tools/getOpenEditorFileDiagnostics.js.map +1 -1
- package/lib/browser/mcp/tools/grepSearch.d.ts.map +1 -1
- package/lib/browser/mcp/tools/grepSearch.js +1 -0
- package/lib/browser/mcp/tools/grepSearch.js.map +1 -1
- package/lib/browser/mcp/tools/listDir.d.ts.map +1 -1
- package/lib/browser/mcp/tools/listDir.js +1 -0
- package/lib/browser/mcp/tools/listDir.js.map +1 -1
- package/lib/browser/mcp/tools/readFile.d.ts.map +1 -1
- package/lib/browser/mcp/tools/readFile.js +1 -0
- package/lib/browser/mcp/tools/readFile.js.map +1 -1
- package/lib/browser/mcp/tools/runTerminalCmd.d.ts.map +1 -1
- package/lib/browser/mcp/tools/runTerminalCmd.js +1 -0
- package/lib/browser/mcp/tools/runTerminalCmd.js.map +1 -1
- package/lib/browser/types.d.ts +1 -0
- package/lib/browser/types.d.ts.map +1 -1
- package/lib/browser/widget/inline-diff/inline-diff-manager.d.ts.map +1 -1
- package/lib/browser/widget/inline-diff/inline-diff-manager.js.map +1 -1
- package/lib/browser/widget/inline-stream-diff/live-preview.decoration.d.ts.map +1 -1
- package/lib/browser/widget/inline-stream-diff/live-preview.decoration.js +10 -5
- package/lib/browser/widget/inline-stream-diff/live-preview.decoration.js.map +1 -1
- package/lib/common/llm-context.d.ts +15 -1
- package/lib/common/llm-context.d.ts.map +1 -1
- package/lib/common/llm-context.js.map +1 -1
- package/lib/common/prompts/context-prompt-provider.d.ts +12 -2
- package/lib/common/prompts/context-prompt-provider.d.ts.map +1 -1
- package/lib/common/prompts/context-prompt-provider.js +94 -28
- package/lib/common/prompts/context-prompt-provider.js.map +1 -1
- package/lib/node/base-language-model.d.ts.map +1 -1
- package/lib/node/base-language-model.js +6 -0
- package/lib/node/base-language-model.js.map +1 -1
- package/package.json +24 -23
- package/src/browser/chat/chat-agent.service.ts +7 -7
- package/src/browser/chat/chat-model.ts +19 -2
- package/src/browser/chat/chat.view.tsx +63 -20
- package/src/browser/components/ChatEditor.tsx +72 -9
- package/src/browser/components/ChatMentionInput.tsx +268 -0
- package/src/browser/components/ChatReply.tsx +61 -18
- package/src/browser/components/ChatThinking.tsx +1 -1
- package/src/browser/components/WelcomeMsg.tsx +1 -1
- package/src/browser/components/{ChatContext → chat-context}/index.tsx +1 -1
- package/src/browser/components/components.module.less +43 -0
- package/src/browser/components/mention-input/mention-input.module.less +327 -0
- package/src/browser/components/mention-input/mention-input.tsx +943 -0
- package/src/browser/components/mention-input/mention-item.tsx +24 -0
- package/src/browser/components/mention-input/mention-panel.tsx +89 -0
- package/src/browser/components/mention-input/types.ts +82 -0
- package/src/browser/context/llm-context.service.ts +81 -3
- package/src/browser/contrib/inline-completions/prompt/matcher.ts +2 -2
- package/src/browser/contrib/inline-completions/prompt/similarSnippets.ts +2 -2
- package/src/browser/contrib/intelligent-completions/view/default.ts +0 -1
- package/src/browser/mcp/config/components/mcp-config.view.tsx +23 -12
- package/src/browser/mcp/config/components/mcp-server-form.tsx +68 -54
- package/src/browser/mcp/mcp-server.feature.registry.ts +1 -1
- package/src/browser/mcp/tools/components/ExpandableFileList.tsx +4 -1
- package/src/browser/mcp/tools/components/Terminal.tsx +4 -6
- package/src/browser/mcp/tools/components/computeAnsiLogString.ts +24 -0
- package/src/browser/mcp/tools/components/filterEraseMultipleLine.ts +71 -0
- package/src/browser/mcp/tools/components/index.module.less +8 -5
- package/src/browser/mcp/tools/createNewFileWithText.ts +1 -0
- package/src/browser/mcp/tools/editFile.ts +1 -0
- package/src/browser/mcp/tools/fileSearch.ts +1 -0
- package/src/browser/mcp/tools/getDiagnosticsByPath.ts +2 -1
- package/src/browser/mcp/tools/getOpenEditorFileDiagnostics.ts +2 -0
- package/src/browser/mcp/tools/grepSearch.ts +1 -0
- package/src/browser/mcp/tools/listDir.ts +1 -0
- package/src/browser/mcp/tools/readFile.ts +1 -0
- package/src/browser/mcp/tools/runTerminalCmd.ts +1 -0
- package/src/browser/types.ts +1 -0
- package/src/browser/widget/inline-diff/inline-diff-manager.tsx +0 -1
- package/src/browser/widget/inline-stream-diff/live-preview.decoration.tsx +9 -5
- package/src/common/llm-context.ts +16 -1
- package/src/common/prompts/context-prompt-provider.ts +126 -32
- package/src/node/base-language-model.ts +5 -0
- package/lib/browser/components/ChatContext/ContextSelector.d.ts.map +0 -1
- package/lib/browser/components/ChatContext/ContextSelector.js.map +0 -1
- package/lib/browser/components/ChatContext/index.d.ts.map +0 -1
- package/lib/browser/components/ChatContext/index.js.map +0 -1
- /package/lib/browser/components/{ChatContext → chat-context}/index.d.ts +0 -0
- /package/lib/browser/components/{ChatContext → chat-context}/style.module.less +0 -0
- /package/src/browser/components/{ChatContext/ContextSelector.tsx → chat-context/context-selector.tsx} +0 -0
- /package/src/browser/components/{ChatContext → chat-context}/style.module.less +0 -0
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MentionInput = exports.WHITE_SPACE_TEXT = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const classnames_1 = tslib_1.__importDefault(require("classnames"));
|
|
6
|
+
const React = tslib_1.__importStar(require("react"));
|
|
7
|
+
const components_1 = require("@opensumi/ide-core-browser/lib/components");
|
|
8
|
+
const ai_native_1 = require("@opensumi/ide-core-browser/lib/components/ai-native");
|
|
9
|
+
const ide_utils_1 = require("@opensumi/ide-utils");
|
|
10
|
+
const mention_input_module_less_1 = tslib_1.__importDefault(require("./mention-input.module.less"));
|
|
11
|
+
const mention_panel_1 = require("./mention-panel");
|
|
12
|
+
const types_1 = require("./types");
|
|
13
|
+
exports.WHITE_SPACE_TEXT = ' ';
|
|
14
|
+
const MentionInput = ({ mentionItems = [], onSend, onStop, loading = false, mentionKeyword = types_1.MENTION_KEYWORD, onSelectionChange, labelService, placeholder = 'Ask anything, @ to mention', footerConfig = {
|
|
15
|
+
buttons: [],
|
|
16
|
+
showModelSelector: false,
|
|
17
|
+
}, }) => {
|
|
18
|
+
const editorRef = React.useRef(null);
|
|
19
|
+
const [mentionState, setMentionState] = React.useState({
|
|
20
|
+
active: false,
|
|
21
|
+
startPos: null,
|
|
22
|
+
filter: '',
|
|
23
|
+
position: { top: 0, left: 0 },
|
|
24
|
+
activeIndex: 0,
|
|
25
|
+
level: 0, // 0: 一级菜单, 1: 二级菜单
|
|
26
|
+
parentType: null, // 二级菜单的父类型
|
|
27
|
+
secondLevelFilter: '', // 二级菜单的筛选文本
|
|
28
|
+
inlineSearchActive: false, // 是否在输入框中进行二级搜索
|
|
29
|
+
inlineSearchStartPos: null, // 内联搜索的起始位置
|
|
30
|
+
loading: false, // 添加加载状态
|
|
31
|
+
});
|
|
32
|
+
// 添加模型选择状态
|
|
33
|
+
const [selectedModel, setSelectedModel] = React.useState(footerConfig.defaultModel || '');
|
|
34
|
+
// 添加缓存状态,用于存储二级菜单项
|
|
35
|
+
const [secondLevelCache, setSecondLevelCache] = React.useState({});
|
|
36
|
+
// 添加历史记录状态
|
|
37
|
+
const [history, setHistory] = React.useState([]);
|
|
38
|
+
const [historyIndex, setHistoryIndex] = React.useState(-1);
|
|
39
|
+
const [currentInput, setCurrentInput] = React.useState('');
|
|
40
|
+
const [isNavigatingHistory, setIsNavigatingHistory] = React.useState(false);
|
|
41
|
+
// 获取当前菜单项
|
|
42
|
+
const getCurrentItems = () => {
|
|
43
|
+
if (mentionState.level === 0) {
|
|
44
|
+
return mentionItems;
|
|
45
|
+
}
|
|
46
|
+
else if (mentionState.parentType) {
|
|
47
|
+
// 如果正在加载,返回缓存的项目
|
|
48
|
+
if (mentionState.loading) {
|
|
49
|
+
return secondLevelCache[mentionState.parentType] || [];
|
|
50
|
+
}
|
|
51
|
+
// 返回缓存的项目
|
|
52
|
+
return secondLevelCache[mentionState.parentType] || [];
|
|
53
|
+
}
|
|
54
|
+
return [];
|
|
55
|
+
};
|
|
56
|
+
// 添加防抖函数
|
|
57
|
+
const useDebounce = (value, delay) => {
|
|
58
|
+
const [debouncedValue, setDebouncedValue] = React.useState(value);
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
const handler = setTimeout(() => {
|
|
61
|
+
setDebouncedValue(value);
|
|
62
|
+
}, delay);
|
|
63
|
+
return () => {
|
|
64
|
+
clearTimeout(handler);
|
|
65
|
+
};
|
|
66
|
+
}, [value, delay]);
|
|
67
|
+
return debouncedValue;
|
|
68
|
+
};
|
|
69
|
+
// 使用防抖处理搜索文本
|
|
70
|
+
const debouncedSecondLevelFilter = useDebounce(mentionState.secondLevelFilter, 300);
|
|
71
|
+
// 监听搜索文本变化,实时更新二级菜单
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
if (mentionState.level === 1 && mentionState.parentType && debouncedSecondLevelFilter !== undefined) {
|
|
74
|
+
// 查找父级菜单项
|
|
75
|
+
const parentItem = mentionItems.find((item) => item.id === mentionState.parentType);
|
|
76
|
+
if (!parentItem) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// 设置加载状态
|
|
80
|
+
setMentionState((prev) => ({ ...prev, loading: true }));
|
|
81
|
+
// 异步加载
|
|
82
|
+
const fetchItems = async () => {
|
|
83
|
+
try {
|
|
84
|
+
// 首先显示高优先级项目(如果有)
|
|
85
|
+
const items = [];
|
|
86
|
+
if (parentItem.getHighestLevelItems) {
|
|
87
|
+
const highestLevelItems = parentItem.getHighestLevelItems();
|
|
88
|
+
for (const item of highestLevelItems) {
|
|
89
|
+
if (!items.some((i) => i.id === item.id)) {
|
|
90
|
+
items.push(item);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// 立即更新缓存,显示高优先级项目
|
|
94
|
+
setSecondLevelCache((prev) => ({
|
|
95
|
+
...prev,
|
|
96
|
+
[mentionState.parentType]: highestLevelItems,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
// 然后异步加载更多项目
|
|
100
|
+
if (parentItem.getItems) {
|
|
101
|
+
try {
|
|
102
|
+
// 获取子菜单项
|
|
103
|
+
const newItems = await parentItem.getItems(debouncedSecondLevelFilter);
|
|
104
|
+
// 去重合并
|
|
105
|
+
const combinedItems = [...items];
|
|
106
|
+
for (const item of newItems) {
|
|
107
|
+
if (!combinedItems.some((i) => i.id === item.id)) {
|
|
108
|
+
combinedItems.push(item);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// 更新缓存
|
|
112
|
+
setSecondLevelCache((prev) => ({
|
|
113
|
+
...prev,
|
|
114
|
+
[mentionState.parentType]: combinedItems,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
// 如果异步加载失败,至少保留高优先级项目
|
|
119
|
+
setMentionState((prev) => ({ ...prev, loading: false }));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// 最后清除加载状态
|
|
123
|
+
setMentionState((prev) => ({ ...prev, loading: false }));
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
setMentionState((prev) => ({ ...prev, loading: false }));
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
fetchItems();
|
|
130
|
+
}
|
|
131
|
+
}, [debouncedSecondLevelFilter, mentionState.level, mentionState.parentType]);
|
|
132
|
+
// 获取光标位置
|
|
133
|
+
const getCursorPosition = (element) => {
|
|
134
|
+
const selection = window.getSelection();
|
|
135
|
+
if (!selection || !selection.rangeCount) {
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
const range = selection.getRangeAt(0);
|
|
139
|
+
const preCaretRange = range.cloneRange();
|
|
140
|
+
preCaretRange.selectNodeContents(element);
|
|
141
|
+
preCaretRange.setEnd(range.endContainer, range.endOffset);
|
|
142
|
+
return preCaretRange.toString().length;
|
|
143
|
+
};
|
|
144
|
+
// 处理输入事件
|
|
145
|
+
const handleInput = () => {
|
|
146
|
+
// 如果用户开始输入,退出历史导航模式
|
|
147
|
+
if (isNavigatingHistory) {
|
|
148
|
+
setIsNavigatingHistory(false);
|
|
149
|
+
setHistoryIndex(-1);
|
|
150
|
+
}
|
|
151
|
+
const selection = window.getSelection();
|
|
152
|
+
if (!selection || !selection.rangeCount || !editorRef.current) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const text = editorRef.current.textContent || '';
|
|
156
|
+
const cursorPos = getCursorPosition(editorRef.current);
|
|
157
|
+
// 判断是否刚输入了 @
|
|
158
|
+
if (text[cursorPos - 1] === mentionKeyword && !mentionState.active && !mentionState.inlineSearchActive) {
|
|
159
|
+
setMentionState({
|
|
160
|
+
active: true,
|
|
161
|
+
startPos: cursorPos,
|
|
162
|
+
filter: mentionKeyword,
|
|
163
|
+
position: { top: 0, left: 0 }, // 固定位置,不再需要动态计算
|
|
164
|
+
activeIndex: 0,
|
|
165
|
+
level: 0,
|
|
166
|
+
parentType: null,
|
|
167
|
+
secondLevelFilter: '',
|
|
168
|
+
inlineSearchActive: false,
|
|
169
|
+
inlineSearchStartPos: null,
|
|
170
|
+
loading: false,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
// 如果已激活提及面板且在一级菜单,更新过滤内容
|
|
174
|
+
if (mentionState.active && mentionState.level === 0 && mentionState.startPos !== null) {
|
|
175
|
+
if (cursorPos < mentionState.startPos) {
|
|
176
|
+
// 如果光标移到了 @ 之前,关闭面板
|
|
177
|
+
setMentionState((prev) => ({ ...prev, active: false }));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const newFilter = text.substring(mentionState.startPos - 1, cursorPos);
|
|
181
|
+
setMentionState((prev) => ({
|
|
182
|
+
...prev,
|
|
183
|
+
filter: newFilter,
|
|
184
|
+
activeIndex: 0,
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// 如果在输入框中进行二级搜索
|
|
189
|
+
if (mentionState.inlineSearchActive && mentionState.inlineSearchStartPos !== null && mentionState.parentType) {
|
|
190
|
+
// 获取父级类型
|
|
191
|
+
const parentItem = mentionItems.find((i) => i.id === mentionState.parentType);
|
|
192
|
+
if (!parentItem) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
// 检查光标是否在 @type: 之后
|
|
196
|
+
const typePrefix = `@${parentItem.type}:`;
|
|
197
|
+
const prefixPos = mentionState.inlineSearchStartPos - typePrefix.length;
|
|
198
|
+
if (prefixPos >= 0 && cursorPos > prefixPos + typePrefix.length) {
|
|
199
|
+
// 提取搜索文本
|
|
200
|
+
const searchText = text.substring(prefixPos + typePrefix.length, cursorPos);
|
|
201
|
+
// 只有当搜索文本变化时才更新状态
|
|
202
|
+
if (searchText !== mentionState.secondLevelFilter) {
|
|
203
|
+
setMentionState((prev) => ({
|
|
204
|
+
...prev,
|
|
205
|
+
secondLevelFilter: searchText,
|
|
206
|
+
active: true,
|
|
207
|
+
activeIndex: 0,
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else if (cursorPos <= prefixPos) {
|
|
212
|
+
// 如果光标移到了 @type: 之前,关闭内联搜索
|
|
213
|
+
setMentionState((prev) => ({
|
|
214
|
+
...prev,
|
|
215
|
+
inlineSearchActive: false,
|
|
216
|
+
active: false,
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// 检查输入框高度,如果超过最大高度则添加滚动条
|
|
221
|
+
if (editorRef.current) {
|
|
222
|
+
const editorHeight = editorRef.current.scrollHeight;
|
|
223
|
+
if (editorHeight > 120) {
|
|
224
|
+
editorRef.current.style.overflowY = 'auto';
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
editorRef.current.style.overflowY = 'hidden';
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// 检查编辑器内容,处理只有 <br> 标签的情况
|
|
231
|
+
if (editorRef.current) {
|
|
232
|
+
const content = editorRef.current.innerHTML;
|
|
233
|
+
// 如果内容为空或只有 <br> 标签
|
|
234
|
+
if (content === '' || content === '<br>' || content === '<br/>') {
|
|
235
|
+
// 清空编辑器内容
|
|
236
|
+
editorRef.current.innerHTML = '';
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
// 处理键盘事件
|
|
241
|
+
const handleKeyDown = (e) => {
|
|
242
|
+
// 如果按下ESC键且提及面板处于活动状态或内联搜索处于活动状态
|
|
243
|
+
if (e.key === 'Escape' && (mentionState.active || mentionState.inlineSearchActive)) {
|
|
244
|
+
// 如果在二级菜单,返回一级菜单
|
|
245
|
+
if (mentionState.level > 0) {
|
|
246
|
+
setMentionState((prev) => ({
|
|
247
|
+
...prev,
|
|
248
|
+
level: 0,
|
|
249
|
+
activeIndex: 0,
|
|
250
|
+
secondLevelFilter: '',
|
|
251
|
+
inlineSearchActive: false,
|
|
252
|
+
}));
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
// 如果在一级菜单,完全关闭面板
|
|
256
|
+
setMentionState((prev) => ({
|
|
257
|
+
...prev,
|
|
258
|
+
active: false,
|
|
259
|
+
inlineSearchActive: false,
|
|
260
|
+
}));
|
|
261
|
+
}
|
|
262
|
+
e.preventDefault();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
// 添加对 @ 键的监听,支持在任意位置触发菜单
|
|
266
|
+
if (e.key === types_1.MENTION_KEYWORD && !mentionState.active && !mentionState.inlineSearchActive && editorRef.current) {
|
|
267
|
+
const cursorPos = getCursorPosition(editorRef.current);
|
|
268
|
+
// 立即设置菜单状态,不等待 handleInput
|
|
269
|
+
setMentionState({
|
|
270
|
+
active: true,
|
|
271
|
+
startPos: cursorPos + 1, // +1 因为 @ 还没有被插入
|
|
272
|
+
filter: mentionKeyword,
|
|
273
|
+
position: { top: 0, left: 0 }, // 固定位置
|
|
274
|
+
activeIndex: 0,
|
|
275
|
+
level: 0,
|
|
276
|
+
parentType: null,
|
|
277
|
+
secondLevelFilter: '',
|
|
278
|
+
inlineSearchActive: false,
|
|
279
|
+
inlineSearchStartPos: null,
|
|
280
|
+
loading: false,
|
|
281
|
+
});
|
|
282
|
+
// 不要阻止默认行为,让 @ 正常输入到编辑器中
|
|
283
|
+
}
|
|
284
|
+
// 添加对 Enter 键的处理,只有在按下 Shift+Enter 时才允许换行
|
|
285
|
+
if (e.key === 'Enter') {
|
|
286
|
+
// 检查是否是输入法的回车键
|
|
287
|
+
// isComposing 属性表示是否正在进行输入法组合输入
|
|
288
|
+
if (e.nativeEvent.isComposing) {
|
|
289
|
+
return; // 如果是输入法组合输入过程中的回车,不做任何处理
|
|
290
|
+
}
|
|
291
|
+
if (!e.shiftKey) {
|
|
292
|
+
e.preventDefault();
|
|
293
|
+
if (!mentionState.active) {
|
|
294
|
+
handleSend();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// 处理上下方向键导航历史记录
|
|
300
|
+
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
|
|
301
|
+
// 只有在非提及面板激活状态下才处理历史导航
|
|
302
|
+
if (!mentionState.active && !mentionState.inlineSearchActive && editorRef.current) {
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
// 如果是第一次按上下键,保存当前输入
|
|
305
|
+
if (!isNavigatingHistory) {
|
|
306
|
+
setCurrentInput(editorRef.current.innerHTML);
|
|
307
|
+
setIsNavigatingHistory(true);
|
|
308
|
+
}
|
|
309
|
+
// 计算新的历史索引
|
|
310
|
+
let newIndex = historyIndex;
|
|
311
|
+
if (e.key === 'ArrowUp') {
|
|
312
|
+
// 向上导航到较早的历史记录
|
|
313
|
+
newIndex = Math.min(history.length - 1, historyIndex + 1);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
// 向下导航到较新的历史记录
|
|
317
|
+
newIndex = Math.max(-1, historyIndex - 1);
|
|
318
|
+
}
|
|
319
|
+
setHistoryIndex(newIndex);
|
|
320
|
+
// 更新编辑器内容
|
|
321
|
+
if (newIndex === -1) {
|
|
322
|
+
// 恢复到当前输入
|
|
323
|
+
editorRef.current.innerHTML = currentInput;
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
// 显示历史记录
|
|
327
|
+
editorRef.current.innerHTML = history[history.length - 1 - newIndex];
|
|
328
|
+
}
|
|
329
|
+
// 将光标移到末尾
|
|
330
|
+
const range = document.createRange();
|
|
331
|
+
range.selectNodeContents(editorRef.current);
|
|
332
|
+
range.collapse(false);
|
|
333
|
+
const selection = window.getSelection();
|
|
334
|
+
if (selection) {
|
|
335
|
+
selection.removeAllRanges();
|
|
336
|
+
selection.addRange(range);
|
|
337
|
+
}
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
else if (isNavigatingHistory && e.key !== 'ArrowUp' && e.key !== 'ArrowDown') {
|
|
342
|
+
// 如果用户在浏览历史记录后开始输入,退出历史导航模式
|
|
343
|
+
setIsNavigatingHistory(false);
|
|
344
|
+
setHistoryIndex(-1);
|
|
345
|
+
}
|
|
346
|
+
// 如果提及面板未激活,不处理其他键盘事件
|
|
347
|
+
if (!mentionState.active) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
// 获取当前过滤后的项目
|
|
351
|
+
let filteredItems = getCurrentItems();
|
|
352
|
+
// 一级菜单过滤
|
|
353
|
+
if (mentionState.level === 0 && mentionState.filter && mentionState.filter.length > 1) {
|
|
354
|
+
const searchText = mentionState.filter.substring(1).toLowerCase();
|
|
355
|
+
filteredItems = filteredItems.filter((item) => item.text.toLowerCase().includes(searchText));
|
|
356
|
+
}
|
|
357
|
+
// 二级菜单过滤已经在 getCurrentItems 中处理
|
|
358
|
+
if (filteredItems.length === 0) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (e.key === 'ArrowDown') {
|
|
362
|
+
// 向下导航
|
|
363
|
+
setMentionState((prev) => ({
|
|
364
|
+
...prev,
|
|
365
|
+
activeIndex: (prev.activeIndex + 1) % filteredItems.length,
|
|
366
|
+
}));
|
|
367
|
+
e.preventDefault();
|
|
368
|
+
}
|
|
369
|
+
else if (e.key === 'ArrowUp') {
|
|
370
|
+
// 向上导航
|
|
371
|
+
setMentionState((prev) => ({
|
|
372
|
+
...prev,
|
|
373
|
+
activeIndex: (prev.activeIndex - 1 + filteredItems.length) % filteredItems.length,
|
|
374
|
+
}));
|
|
375
|
+
e.preventDefault();
|
|
376
|
+
}
|
|
377
|
+
else if (e.key === 'Enter' || e.key === 'Tab') {
|
|
378
|
+
// 确认选择
|
|
379
|
+
if (filteredItems.length > 0) {
|
|
380
|
+
handleSelectItem(filteredItems[mentionState.activeIndex]);
|
|
381
|
+
e.preventDefault();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// 处理 Backspace 键,检查是否需要清空编辑器
|
|
385
|
+
if (e.key === 'Backspace' && editorRef.current) {
|
|
386
|
+
const content = editorRef.current.innerHTML;
|
|
387
|
+
if (content === '<br>' || content === '<br/>') {
|
|
388
|
+
editorRef.current.innerHTML = '';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
// 添加对输入法事件的处理
|
|
393
|
+
const handleCompositionEnd = () => {
|
|
394
|
+
// 输入法输入完成后的处理
|
|
395
|
+
// 这里可以添加额外的逻辑,如果需要的话
|
|
396
|
+
};
|
|
397
|
+
// 初始化编辑器
|
|
398
|
+
React.useEffect(() => {
|
|
399
|
+
if (editorRef.current) {
|
|
400
|
+
// 设置初始占位符
|
|
401
|
+
if (placeholder && !editorRef.current.textContent) {
|
|
402
|
+
editorRef.current.setAttribute('data-placeholder', placeholder);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}, [placeholder]);
|
|
406
|
+
// 处理点击事件
|
|
407
|
+
const handleDocumentClick = (e) => {
|
|
408
|
+
var _a;
|
|
409
|
+
if (mentionState.active && !((_a = document.querySelector(`.${mention_input_module_less_1.default.mention_panel}`)) === null || _a === void 0 ? void 0 : _a.contains(e.target))) {
|
|
410
|
+
setMentionState((prev) => ({
|
|
411
|
+
...prev,
|
|
412
|
+
active: false,
|
|
413
|
+
inlineSearchActive: false,
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
// 添加和移除全局点击事件监听器
|
|
418
|
+
React.useEffect(() => {
|
|
419
|
+
document.addEventListener('click', handleDocumentClick, true);
|
|
420
|
+
return () => {
|
|
421
|
+
document.removeEventListener('click', handleDocumentClick, true);
|
|
422
|
+
};
|
|
423
|
+
}, [mentionState.active]);
|
|
424
|
+
// 选择提及项目
|
|
425
|
+
const handleSelectItem = (item, isTriggerByClick = false) => {
|
|
426
|
+
var _a;
|
|
427
|
+
if (!editorRef.current) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
// 如果项目有子菜单,进入二级菜单
|
|
431
|
+
if (item.getItems) {
|
|
432
|
+
const selection = window.getSelection();
|
|
433
|
+
if (!selection || !selection.rangeCount) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
// 如果是从一级菜单选择了带子菜单的项目
|
|
437
|
+
if (mentionState.level === 0 && mentionState.startPos !== null) {
|
|
438
|
+
// 更安全地处理文本替换
|
|
439
|
+
let textNode;
|
|
440
|
+
let startOffset;
|
|
441
|
+
let endOffset;
|
|
442
|
+
// 找到包含 @ 符号的文本节点
|
|
443
|
+
const walker = document.createTreeWalker(editorRef.current, NodeFilter.SHOW_TEXT);
|
|
444
|
+
let charCount = 0;
|
|
445
|
+
let node;
|
|
446
|
+
while ((node = walker.nextNode())) {
|
|
447
|
+
const nodeLength = ((_a = node.textContent) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
448
|
+
// 检查 @ 符号是否在这个节点中
|
|
449
|
+
if (mentionState.startPos - 1 >= charCount && mentionState.startPos - 1 < charCount + nodeLength) {
|
|
450
|
+
textNode = node;
|
|
451
|
+
startOffset = mentionState.startPos - 1 - charCount;
|
|
452
|
+
// 确保不会超出节点范围
|
|
453
|
+
const cursorPos = isTriggerByClick
|
|
454
|
+
? mentionState.startPos + mentionState.filter.length - 1
|
|
455
|
+
: getCursorPosition(editorRef.current);
|
|
456
|
+
endOffset = Math.min(cursorPos - charCount, nodeLength);
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
charCount += nodeLength;
|
|
460
|
+
}
|
|
461
|
+
if (textNode) {
|
|
462
|
+
// 创建一个新的范围来替换文本
|
|
463
|
+
const tempRange = document.createRange();
|
|
464
|
+
tempRange.setStart(textNode, startOffset);
|
|
465
|
+
tempRange.setEnd(textNode, endOffset);
|
|
466
|
+
// 替换为 @type:
|
|
467
|
+
tempRange.deleteContents();
|
|
468
|
+
const typePrefix = document.createTextNode(`${mentionKeyword}${item.type}:`);
|
|
469
|
+
tempRange.insertNode(typePrefix);
|
|
470
|
+
// 将光标移到 @type: 后面
|
|
471
|
+
const newRange = document.createRange();
|
|
472
|
+
newRange.setStartAfter(typePrefix);
|
|
473
|
+
newRange.setEndAfter(typePrefix);
|
|
474
|
+
selection.removeAllRanges();
|
|
475
|
+
selection.addRange(newRange);
|
|
476
|
+
// 激活内联搜索模式
|
|
477
|
+
setMentionState((prev) => ({
|
|
478
|
+
...prev,
|
|
479
|
+
active: true,
|
|
480
|
+
level: 1,
|
|
481
|
+
parentType: item.id,
|
|
482
|
+
inlineSearchActive: true,
|
|
483
|
+
inlineSearchStartPos: getCursorPosition(editorRef.current),
|
|
484
|
+
secondLevelFilter: '',
|
|
485
|
+
activeIndex: 0,
|
|
486
|
+
}));
|
|
487
|
+
editorRef.current.focus();
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const selection = window.getSelection();
|
|
494
|
+
if (!selection || !selection.rangeCount) {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
// 如果是在内联搜索模式下选择项目
|
|
498
|
+
if (mentionState.inlineSearchActive && mentionState.parentType && mentionState.inlineSearchStartPos !== null) {
|
|
499
|
+
// 找到 @type: 的位置
|
|
500
|
+
const parentItem = mentionItems.find((i) => i.id === mentionState.parentType);
|
|
501
|
+
if (!parentItem) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const typePrefix = `${mentionKeyword}${parentItem.type}:`;
|
|
505
|
+
const prefixPos = mentionState.inlineSearchStartPos - typePrefix.length;
|
|
506
|
+
if (prefixPos >= 0) {
|
|
507
|
+
// 创建一个带样式的提及标签
|
|
508
|
+
const mentionTag = document.createElement('span');
|
|
509
|
+
mentionTag.className = mention_input_module_less_1.default.mention_tag;
|
|
510
|
+
mentionTag.dataset.id = item.id;
|
|
511
|
+
mentionTag.dataset.type = item.type;
|
|
512
|
+
mentionTag.dataset.contextId = item.contextId || '';
|
|
513
|
+
mentionTag.contentEditable = 'false';
|
|
514
|
+
// 为 file 和 folder 类型添加图标
|
|
515
|
+
if (item.type === 'file' || item.type === 'folder') {
|
|
516
|
+
// 创建图标容器
|
|
517
|
+
const iconSpan = document.createElement('span');
|
|
518
|
+
iconSpan.className = (0, classnames_1.default)(mention_input_module_less_1.default.mention_icon, item.type === 'file' ? labelService === null || labelService === void 0 ? void 0 : labelService.getIcon(new ide_utils_1.URI(item.text)) : (0, components_1.getIcon)('folder'));
|
|
519
|
+
mentionTag.appendChild(iconSpan);
|
|
520
|
+
}
|
|
521
|
+
// 创建文本内容容器
|
|
522
|
+
const textSpan = document.createTextNode(item.text);
|
|
523
|
+
mentionTag.appendChild(textSpan);
|
|
524
|
+
// 创建一个范围从 @type: 开始到当前光标
|
|
525
|
+
const tempRange = document.createRange();
|
|
526
|
+
// 定位到 @type: 的位置
|
|
527
|
+
let charIndex = 0;
|
|
528
|
+
let foundStart = false;
|
|
529
|
+
const textNodes = [];
|
|
530
|
+
function findPosition(node) {
|
|
531
|
+
if (node.nodeType === 3) {
|
|
532
|
+
// 文本节点
|
|
533
|
+
textNodes.push({
|
|
534
|
+
node,
|
|
535
|
+
start: charIndex,
|
|
536
|
+
end: charIndex + node.textContent.length,
|
|
537
|
+
});
|
|
538
|
+
charIndex += node.textContent.length;
|
|
539
|
+
}
|
|
540
|
+
else if (node.nodeType === 1) {
|
|
541
|
+
// 元素节点
|
|
542
|
+
const children = node.childNodes || [];
|
|
543
|
+
for (const child of Array.from(children)) {
|
|
544
|
+
findPosition(child);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
findPosition(editorRef.current);
|
|
549
|
+
for (const textNode of textNodes) {
|
|
550
|
+
if (prefixPos >= textNode.start && prefixPos <= textNode.end) {
|
|
551
|
+
const startOffset = prefixPos - textNode.start;
|
|
552
|
+
tempRange.setStart(textNode.node, startOffset);
|
|
553
|
+
foundStart = true;
|
|
554
|
+
}
|
|
555
|
+
if (foundStart) {
|
|
556
|
+
// 如果是点击触发,使用过滤文本的长度来确定结束位置
|
|
557
|
+
const cursorPos = isTriggerByClick
|
|
558
|
+
? prefixPos + typePrefix.length + mentionState.secondLevelFilter.length
|
|
559
|
+
: getCursorPosition(editorRef.current);
|
|
560
|
+
if (cursorPos >= textNode.start && cursorPos <= textNode.end) {
|
|
561
|
+
const endOffset = cursorPos - textNode.start;
|
|
562
|
+
tempRange.setEnd(textNode.node, endOffset);
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (foundStart) {
|
|
568
|
+
tempRange.deleteContents();
|
|
569
|
+
tempRange.insertNode(mentionTag);
|
|
570
|
+
// 将光标移到提及标签后面
|
|
571
|
+
const newRange = document.createRange();
|
|
572
|
+
newRange.setStartAfter(mentionTag);
|
|
573
|
+
newRange.setEndAfter(mentionTag);
|
|
574
|
+
selection.removeAllRanges();
|
|
575
|
+
selection.addRange(newRange);
|
|
576
|
+
// 添加一个空格,增加间隔
|
|
577
|
+
const spaceNode = document.createTextNode('\u00A0'); // 使用不间断空格
|
|
578
|
+
newRange.insertNode(spaceNode);
|
|
579
|
+
newRange.setStartAfter(spaceNode);
|
|
580
|
+
newRange.setEndAfter(spaceNode);
|
|
581
|
+
selection.removeAllRanges();
|
|
582
|
+
selection.addRange(newRange);
|
|
583
|
+
}
|
|
584
|
+
setMentionState((prev) => ({
|
|
585
|
+
...prev,
|
|
586
|
+
active: false,
|
|
587
|
+
inlineSearchActive: false,
|
|
588
|
+
}));
|
|
589
|
+
editorRef.current.focus();
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
// 原有的处理逻辑(用于非内联搜索情况)
|
|
594
|
+
// 创建一个带样式的提及标签
|
|
595
|
+
const mentionTag = document.createElement('span');
|
|
596
|
+
mentionTag.className = mention_input_module_less_1.default.mention_tag;
|
|
597
|
+
mentionTag.dataset.id = item.id;
|
|
598
|
+
mentionTag.dataset.type = item.type;
|
|
599
|
+
mentionTag.dataset.contextId = item.contextId || '';
|
|
600
|
+
mentionTag.contentEditable = 'false';
|
|
601
|
+
// 为 file 和 folder 类型添加图标
|
|
602
|
+
if (item.type === 'file' || item.type === 'folder') {
|
|
603
|
+
// 创建图标容器
|
|
604
|
+
const iconSpan = document.createElement('span');
|
|
605
|
+
iconSpan.className = (0, classnames_1.default)(mention_input_module_less_1.default.mention_icon, item.type === 'file' ? labelService === null || labelService === void 0 ? void 0 : labelService.getIcon(new ide_utils_1.URI(item.text)) : (0, components_1.getIcon)('folder'));
|
|
606
|
+
mentionTag.appendChild(iconSpan);
|
|
607
|
+
}
|
|
608
|
+
// 创建文本内容容器
|
|
609
|
+
const textSpan = document.createTextNode(item.text);
|
|
610
|
+
mentionTag.appendChild(textSpan);
|
|
611
|
+
// 定位到 @ 符号的位置
|
|
612
|
+
let charIndex = 0;
|
|
613
|
+
let foundStart = false;
|
|
614
|
+
const textNodes = [];
|
|
615
|
+
function findPosition(node) {
|
|
616
|
+
if (node.nodeType === 3) {
|
|
617
|
+
// 文本节点
|
|
618
|
+
textNodes.push({
|
|
619
|
+
node,
|
|
620
|
+
start: charIndex,
|
|
621
|
+
end: charIndex + node.textContent.length,
|
|
622
|
+
});
|
|
623
|
+
charIndex += node.textContent.length;
|
|
624
|
+
}
|
|
625
|
+
else if (node.nodeType === 1) {
|
|
626
|
+
// 元素节点
|
|
627
|
+
const children = node.childNodes;
|
|
628
|
+
for (const child of Array.from(children)) {
|
|
629
|
+
findPosition(child);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
findPosition(editorRef.current);
|
|
634
|
+
const tempRange = document.createRange();
|
|
635
|
+
if (mentionState.startPos !== null) {
|
|
636
|
+
for (const textNode of textNodes) {
|
|
637
|
+
if (mentionState.startPos - 1 >= textNode.start && mentionState.startPos - 1 <= textNode.end) {
|
|
638
|
+
const startOffset = mentionState.startPos - 1 - textNode.start;
|
|
639
|
+
tempRange.setStart(textNode.node, startOffset);
|
|
640
|
+
foundStart = true;
|
|
641
|
+
}
|
|
642
|
+
if (foundStart) {
|
|
643
|
+
// 如果是点击触发,使用过滤文本的长度来确定结束位置
|
|
644
|
+
const cursorPos = isTriggerByClick
|
|
645
|
+
? mentionState.startPos + mentionState.filter.length - 1
|
|
646
|
+
: getCursorPosition(editorRef.current);
|
|
647
|
+
if (cursorPos >= textNode.start && cursorPos <= textNode.end) {
|
|
648
|
+
const endOffset = cursorPos - textNode.start;
|
|
649
|
+
tempRange.setEnd(textNode.node, endOffset);
|
|
650
|
+
break;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
if (foundStart) {
|
|
656
|
+
tempRange.deleteContents();
|
|
657
|
+
tempRange.insertNode(mentionTag);
|
|
658
|
+
// 将光标移到提及标签后面
|
|
659
|
+
const newRange = document.createRange();
|
|
660
|
+
newRange.setStartAfter(mentionTag);
|
|
661
|
+
newRange.setEndAfter(mentionTag);
|
|
662
|
+
selection.removeAllRanges();
|
|
663
|
+
selection.addRange(newRange);
|
|
664
|
+
// 添加一个空格,增加间隔
|
|
665
|
+
const spaceNode = document.createTextNode('\u00A0'); // 使用不间断空格
|
|
666
|
+
newRange.insertNode(spaceNode);
|
|
667
|
+
newRange.setStartAfter(spaceNode);
|
|
668
|
+
newRange.setEndAfter(spaceNode);
|
|
669
|
+
selection.removeAllRanges();
|
|
670
|
+
selection.addRange(newRange);
|
|
671
|
+
}
|
|
672
|
+
setMentionState((prev) => ({ ...prev, active: false }));
|
|
673
|
+
editorRef.current.focus();
|
|
674
|
+
};
|
|
675
|
+
// 处理模型选择变更
|
|
676
|
+
const handleModelChange = React.useCallback((value) => {
|
|
677
|
+
setSelectedModel(value);
|
|
678
|
+
onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(value);
|
|
679
|
+
}, [selectedModel, onSelectionChange]);
|
|
680
|
+
// 修改 handleSend 函数
|
|
681
|
+
const handleSend = () => {
|
|
682
|
+
if (!editorRef.current) {
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
// 获取原始HTML内容
|
|
686
|
+
const rawContent = editorRef.current.innerHTML;
|
|
687
|
+
if (!rawContent) {
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
// 创建一个临时元素来处理内容
|
|
691
|
+
const tempDiv = document.createElement('div');
|
|
692
|
+
tempDiv.innerHTML = rawContent;
|
|
693
|
+
// 查找所有提及标签并替换为对应的contextId
|
|
694
|
+
const mentionTags = tempDiv.querySelectorAll(`.${mention_input_module_less_1.default.mention_tag}`);
|
|
695
|
+
mentionTags.forEach((tag) => {
|
|
696
|
+
var _a;
|
|
697
|
+
const contextId = tag.getAttribute('data-context-id');
|
|
698
|
+
if (contextId) {
|
|
699
|
+
// 替换为contextId
|
|
700
|
+
const replacement = document.createTextNode(`{{${mentionKeyword}${tag.getAttribute('data-type')}:${contextId}}}`);
|
|
701
|
+
// 替换内容
|
|
702
|
+
(_a = tag.parentNode) === null || _a === void 0 ? void 0 : _a.replaceChild(replacement, tag);
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
// 获取处理后的内容
|
|
706
|
+
let processedContent = tempDiv.innerHTML;
|
|
707
|
+
processedContent = processedContent.trim().replaceAll(exports.WHITE_SPACE_TEXT, ' ');
|
|
708
|
+
// 添加到历史记录
|
|
709
|
+
if (rawContent) {
|
|
710
|
+
setHistory((prev) => [...prev, rawContent]);
|
|
711
|
+
// 重置历史导航状态
|
|
712
|
+
setHistoryIndex(-1);
|
|
713
|
+
setIsNavigatingHistory(false);
|
|
714
|
+
}
|
|
715
|
+
if (onSend) {
|
|
716
|
+
// 传递当前选择的模型和其他配置信息
|
|
717
|
+
onSend(processedContent, {
|
|
718
|
+
model: selectedModel,
|
|
719
|
+
...footerConfig,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
editorRef.current.innerHTML = '';
|
|
723
|
+
// 重置编辑器高度和滚动条
|
|
724
|
+
if (editorRef.current) {
|
|
725
|
+
editorRef.current.style.overflowY = 'hidden';
|
|
726
|
+
editorRef.current.style.height = 'auto';
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
const handleStop = React.useCallback(() => {
|
|
730
|
+
if (onStop) {
|
|
731
|
+
onStop();
|
|
732
|
+
}
|
|
733
|
+
}, [onStop]);
|
|
734
|
+
// 渲染自定义按钮
|
|
735
|
+
const renderButtons = React.useCallback((position) => (footerConfig.buttons || [])
|
|
736
|
+
.filter((button) => button.position === position)
|
|
737
|
+
.map((button) => (React.createElement(components_1.Popover, { key: button.id, overlayClassName: mention_input_module_less_1.default.popover_icon, id: `ai-chat-${button.id}`, position: components_1.PopoverPosition.top, title: button.title },
|
|
738
|
+
React.createElement(ai_native_1.EnhanceIcon, { className: (0, classnames_1.default)((0, components_1.getIcon)(button.icon), mention_input_module_less_1.default[`${button.id}_logo`]), tabIndex: 0, role: 'button', ariaLabel: button.title, onClick: button.onClick })))), [footerConfig.buttons]);
|
|
739
|
+
return (React.createElement("div", { className: mention_input_module_less_1.default.input_container },
|
|
740
|
+
mentionState.active && (React.createElement("div", { className: mention_input_module_less_1.default.mention_panel_container },
|
|
741
|
+
React.createElement(mention_panel_1.MentionPanel, { items: getCurrentItems(), activeIndex: mentionState.activeIndex, onSelectItem: (item) => handleSelectItem(item, true), position: { top: 0, left: 0 }, filter: mentionState.level === 0 ? mentionState.filter : mentionState.secondLevelFilter, visible: true, level: mentionState.level, loading: mentionState.loading }))),
|
|
742
|
+
React.createElement("div", { className: mention_input_module_less_1.default.editor_area },
|
|
743
|
+
React.createElement("div", { ref: editorRef, className: mention_input_module_less_1.default.editor, contentEditable: true, onInput: handleInput, onKeyDown: handleKeyDown, onCompositionEnd: handleCompositionEnd })),
|
|
744
|
+
React.createElement("div", { className: mention_input_module_less_1.default.footer },
|
|
745
|
+
React.createElement("div", { className: mention_input_module_less_1.default.left_control },
|
|
746
|
+
footerConfig.showModelSelector && (React.createElement(components_1.Select, { options: footerConfig.modelOptions || [], value: selectedModel, onChange: handleModelChange, className: mention_input_module_less_1.default.model_selector, size: 'small' })),
|
|
747
|
+
renderButtons(types_1.FooterButtonPosition.LEFT)),
|
|
748
|
+
React.createElement("div", { className: mention_input_module_less_1.default.right_control },
|
|
749
|
+
renderButtons(types_1.FooterButtonPosition.RIGHT),
|
|
750
|
+
React.createElement(components_1.Popover, { overlayClassName: mention_input_module_less_1.default.popover_icon, id: 'ai-chat-send', position: components_1.PopoverPosition.top, title: !loading ? 'Send' : 'Stop' }, !loading ? (React.createElement(ai_native_1.EnhanceIcon, { wrapperClassName: mention_input_module_less_1.default.send_logo, className: (0, classnames_1.default)((0, components_1.getIcon)('send-outlined'), mention_input_module_less_1.default.send_logo_icon), tabIndex: 0, role: 'button', onClick: handleSend, ariaLabel: 'Send' })) : (React.createElement(ai_native_1.EnhanceIcon, { wrapperClassName: mention_input_module_less_1.default.stop_logo, className: (0, classnames_1.default)((0, components_1.getIcon)('stop'), mention_input_module_less_1.default.stop_logo_icon), tabIndex: 0, role: 'button', ariaLabel: 'Stop', onClick: handleStop })))))));
|
|
751
|
+
};
|
|
752
|
+
exports.MentionInput = MentionInput;
|
|
753
|
+
//# sourceMappingURL=mention-input.js.map
|