fix: 未选中焦点的时候会报错

This commit is contained in:
liuweiqing 2024-02-13 13:15:38 +08:00
parent 67212d6a85
commit 342d78bd88
3 changed files with 15 additions and 10 deletions

View File

@ -143,7 +143,11 @@ const QEditor = ({ lng }) => {
const textWithoutSpaces = leaf.text.replace(/\s+/g, ""); // 去掉所有空格
if (/^\[\d+\]$/.test(textWithoutSpaces)) {
console.log("点击了引用", textWithoutSpaces);
document.getElementById(textWithoutSpaces)!.scrollIntoView();
try {
document.getElementById(textWithoutSpaces)!.scrollIntoView();
} catch (e) {
console.log("没有找到对应的引用");
}
}
}
} else {

View File

@ -81,14 +81,15 @@ const sendMessageToOpenAI = async (
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
//开始结束前先进行换行
editor.insertText(editor.getSelection().index, "\n");
//开始前先进行换行
// editor.focus();
editor.insertText(editor.getSelection(true).index, "\n");
await processResult(reader, decoder, editor);
convertToSuperscript(editor);
updateBracketNumbersInDeltaKeepSelection(editor);
} catch (error) {
// console.error("Error:", error);
console.error("Error:", error);
// 如果有响应,返回响应的原始内容
if (response) {
const rawResponse = await response.text();
@ -177,8 +178,8 @@ async function processResult(reader, decoder, editor) {
dataObject.choices[0].delta?.content;
if (content) {
// 在当前光标位置插入文本
editor.focus();
editor.insertText(editor.getSelection().index, content);
// editor.focus();
editor.insertText(editor.getSelection(true).index, content);
// console.log("成功插入:", content);
}
}

View File

@ -3,13 +3,13 @@ import Quill from "quill";
import { animated, useSpring } from "@react-spring/web";
function getTextBeforeCursor(quill: Quill, length = 500) {
const cursorPosition = quill.getSelection()!.index;
const cursorPosition = quill.getSelection(true)!.index;
const start = Math.max(0, cursorPosition - length); // 确保开始位置不是负数
return quill.getText(start, cursorPosition - start);
}
function getNumberBeforeCursor(quill: Quill, length = 3000) {
const cursorPosition = quill.getSelection()!.index;
const cursorPosition = quill.getSelection(true)!.index;
const start = Math.max(0, cursorPosition - length); // 确保开始位置不是负数
const textBeforeCursor = quill.getText(start, cursorPosition - start);
@ -133,7 +133,7 @@ function removeParagraphWithReference(
}
function updateBracketNumbersInDeltaKeepSelection(quill: Quill) {
const selection = quill.getSelection();
const selection = quill.getSelection(true);
const delta = quill.getContents();
const updatedDelta = updateBracketNumbersInDelta(delta);
quill.setContents(updatedDelta);
@ -147,7 +147,7 @@ export function delteIndexUpdateBracketNumbersInDeltaKeepSelection(
index: number,
rmPg: boolean
) {
const selection = quill.getSelection();
const selection = quill.getSelection(true);
const delta = quill.getContents();
let updatedDelta = deleteReferenceNumberOrParagraph(
delta,