优化:useAICommand.ts

This commit is contained in:
jingrow 2026-06-14 17:27:22 +08:00
parent 6aa685b584
commit 1aa8acd9e8

View File

@ -536,6 +536,8 @@ async function callInlineAI(options: InlineAICallOptions) {
// Step 2: 结构化内容解析(标题/列表/引用 → 对应 ProseMirror 节点)
// reviewFrom 跟踪审阅范围起始Step 2 块对齐后可能变化)
let reviewFrom = originalInsertAt
// 审阅起始是否在块边界(空格键场景 = trueBubbleMenu 选中 = false
let reviewStartIsBlockAligned = false
if (accumulated && originalInsertAt !== null) {
// ── 计算块对齐的删除范围 ──
// 流式阶段的 splitBlock 创建了块结构heading/paragraph
@ -564,6 +566,7 @@ async function callInlineAI(options: InlineAICallOptions) {
.run()
// Step 2 从块边界插入,审阅范围起始也要同步
reviewFrom = startPos
reviewStartIsBlockAligned = (startPos !== originalInsertAt)
} catch (e) {
console.warn('[AI Command] 结构化解析失败,保留段落格式', e)
}
@ -578,7 +581,16 @@ async function callInlineAI(options: InlineAICallOptions) {
// 审阅模式:通知调用方插入范围 + diff 数据
if (options.reviewMode && originalInsertAt !== null) {
const reviewTo = editor.state.selection.to
// reviewTo 必须对齐块边界,否则 rejectAll 的 deleteRange 无法删除完整块结构
// (留下空 heading/paragraph 壳 → 用户看到空白 Heading 1
let reviewTo = editor.state.selection.to
if (reviewStartIsBlockAligned) {
// reviewFrom 在块起始(空格键场景)→ reviewTo 也扩展到块之后
const $selEnd = editor.state.doc.resolve(editor.state.selection.to)
if ($selEnd.depth > 0) {
reviewTo = Math.min($selEnd.after($selEnd.depth), editor.state.doc.content.size)
}
}
const modifiedText = editor.state.doc.textBetween(reviewFrom, reviewTo, '\n', '\n')
// 捕获修改后内容的 PM 结构化节点,用于块级 diff
const modifiedContent = editor.state.doc