diff --git a/app/[lng]/login/page.tsx b/app/[lng]/login/page.tsx index 26cb57f..4d7811f 100644 --- a/app/[lng]/login/page.tsx +++ b/app/[lng]/login/page.tsx @@ -35,7 +35,7 @@ export default async function Login({ }); //sentry const user = data?.user; - if (user) { + if (user && process.env.NODE_ENV === "production") { Sentry.setUser({ email: user.email, id: user.id, diff --git a/components/QuillEditor.tsx b/components/QuillEditor.tsx index 33f6967..e98fbcc 100644 --- a/components/QuillEditor.tsx +++ b/components/QuillEditor.tsx @@ -366,7 +366,8 @@ const QEditor = ({ lng }) => { selectedModel!, apiKey, upsreamUrl, - systemPrompt + systemPrompt, + cursorPosition! ); setUserInput(""); // 重新获取更新后的内容并更新 Redux store diff --git a/components/chatAI.tsx b/components/chatAI.tsx index c6e1411..a30ea67 100644 --- a/components/chatAI.tsx +++ b/components/chatAI.tsx @@ -6,6 +6,7 @@ import { extractText } from "@/utils/others/slateutils"; import { updateBracketNumbersInDeltaKeepSelection, convertToSuperscript, + deleteSameBracketNumber, } from "@/utils/others/quillutils"; //redux不能在普通函数使用 @@ -26,7 +27,8 @@ const sendMessageToOpenAI = async ( selectedModel: string, apiKey: string, upsreamUrl: string, - prompt?: string + prompt: string, + cursorPosition?: number ) => { //识别应该使用的模型 let model = selectedModel; @@ -87,8 +89,9 @@ const sendMessageToOpenAI = async ( // editor.focus(); editor.insertText(editor.getSelection(true).index, "\n"); await processResult(reader, decoder, editor); - + //搜索是否有相同的括号编号,如果有相同的则删除到只剩一个 convertToSuperscript(editor); + deleteSameBracketNumber(editor, cursorPosition); updateBracketNumbersInDeltaKeepSelection(editor); } catch (error) { console.error("Error:", error); diff --git a/utils/others/quillutils.ts b/utils/others/quillutils.ts index 1dcb9b7..556ea8c 100644 --- a/utils/others/quillutils.ts +++ b/utils/others/quillutils.ts @@ -183,6 +183,67 @@ function convertToSuperscript(quill: Quill) { } } +function removeDuplicateBracketNumbersInDelta(delta: any) { + let seenNumbers = new Set(); // 用于记录已经看到的编号 + + const updatedOps = delta.ops.map((op: any) => { + if (typeof op.insert === "string") { + // 使用正则表达式和replace方法来找到并处理[数字] + return { + ...op, + insert: op.insert.replace(/\[\d+\]/g, (match) => { + const number = match.slice(1, -1); // 提取括号中的数字 + if (seenNumbers.has(number)) { + // 如果这个编号已经处理过,就删除它(用空字符串替换) + return ""; + } else { + // 如果是首次见到这个编号,就记录并保留它 + seenNumbers.add(number); + console.log("seenNumbers", seenNumbers); + return match; + } + }), + }; + } + return op; + }); + + return { ops: updatedOps }; +} + +export function deleteSameBracketNumber( + quill: Quill, + cursorOldPosition: number +) { + //搜索是否有相同的括号编号,如果有相同的则删除到只剩一个 + const selection = quill.getSelection(true); + if (selection) { + // 获取整个文档的内容 + const delta = quill.getContents(); + + // 仅获取选区中的Delta片段 + const selectionDelta = delta.slice(cursorOldPosition, selection.index); + console.log("cursorOldPosition", cursorOldPosition); + console.log("selection.index", selection.index); + console.log("selectionDelta", selectionDelta); + // 对选区中的Delta片段进行处理,移除重复的括号编号 + const updatedSelectionDelta = + removeDuplicateBracketNumbersInDelta(selectionDelta); + + // 构建一个新的Delta,包括未修改的选区前部分和处理后的选区后部分 + const updatedDelta = delta + .slice(0, cursorOldPosition) + .concat(updatedSelectionDelta) + .concat(delta.slice(selection.index)); + + // 设置更新后的内容 + quill.setContents(updatedDelta); + + // 恢复选区 + quill.setSelection(selection.index, selection.length); + } +} + function getRandomOffset(max: number) { return Math.floor(Math.random() * max); }