fix(editor): 斜杠命令文本残留进新节点(/code 带入 codeBlock)
Suggestion 传给 command 的 range 在某些输入路径下过时——停在首次
输入 '/' 的位置 {from:1,to:2},不随后续字符更新。deleteRange 只删 '/',
命令文本(如 'code')残留,被 toggleCodeBlock 包进新节点。
该 bug 同时导致衍生症状:残留文本让 codeBlock 非空,Backspace 的
clearNodes 条件(!textContent.length)不成立,空块退格看似无效。
修复:command 转发处防御性重算 range——基于当前 selection 在同一段落
内往前找触发字符 '/',删到光标位置。对所有命令统一生效。
经 Playwright 真实浏览器复现验证(happy-dom 无法复现此 bug):
- /code 回车:codeBlock 现为空(原残留 'code' 文本)
- 空 codeBlock 退格:正常删除变段落(原无反应)
This commit is contained in:
parent
7fbaae7622
commit
df8f7397b2
@ -277,7 +277,17 @@ export const SlashCommand = Extension.create<SlashCommandOptions>({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
command: ({ editor, range, props: item }) => {
|
command: ({ editor, range, props: item }) => {
|
||||||
item.command({ editor, range });
|
// 防御性重算 range:Suggestion 的 range 在某些输入路径下会过时
|
||||||
|
//(停在首次输入 '/' 的位置,不随后续字符更新),导致 deleteRange 只删 '/',
|
||||||
|
// 命令文本(如 'code')残留进新节点。基于当前 selection 重算:
|
||||||
|
// 从光标往前在同一段落内找触发字符 '/',删到光标。
|
||||||
|
const { from } = editor.state.selection;
|
||||||
|
const $from = editor.state.doc.resolve(from);
|
||||||
|
const text = $from.parent.textBetween(0, $from.parentOffset, '', '');
|
||||||
|
const slashIdx = text.lastIndexOf('/');
|
||||||
|
const effectiveRange =
|
||||||
|
slashIdx >= 0 ? { from: $from.start() + slashIdx, to: from } : range;
|
||||||
|
item.command({ editor, range: effectiveRange });
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user