Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Restore all blocks after replacing cross-block text https://github.com/siyuan-note/siyuan/issues/8554

Daniel61cf9db4fe8a
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index ca6a52632e..4933c10fbf 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -49,7 +49,7 @@ import {
import * as dayjs from "dayjs";
import {dropEvent} from "../util/editorCommonEvent";
import {beforeBlockquoteInput, input} from "./input";
-import {getMultilineInputText} from "./multilineInput";
+import {getMultilineInputText, isTextInputType} from "./multilineInput";
import {
getContenteditableElement,
getEmbedGutterOperationContext,
@@ -3752,8 +3752,8 @@ export class WYSIWYG {
// 输入法测试点 https://github.com/siyuan-note/siyuan/issues/3027
let isComposition = false; // for iPhone
- // 多行组合输入已按 Markdown 插入时,忽略其后到达的 compositionend,避免重复生成事务。
- let multilineCompositionHandled = false;
+ // 组合输入已由 beforeinput 处理时,忽略其后到达的 compositionend,避免重复生成事务。
+ let beforeInputCompositionHandled = false;
// 原生软换行在 input 触发前已经修改 DOM,需预存选区供撤销恢复。
let lineBreakUndoContext: Record<string, string>;
// 仅矫正从数据库外进入的占位光标,避免重置数据库内部的方向键导航。
@@ -3889,8 +3889,8 @@ export class WYSIWYG {
return;
}
isComposition = false;
- if (multilineCompositionHandled) {
- multilineCompositionHandled = false;
+ if (beforeInputCompositionHandled) {
+ beforeInputCompositionHandled = false;
compositionRange = undefined;
return;
}
@@ -3991,7 +3991,7 @@ export class WYSIWYG {
event.stopPropagation();
lineBreakUndoContext = undefined;
if (event.isComposing || isComposition) {
- multilineCompositionHandled = true;
+ beforeInputCompositionHandled = true;
}
beforePaste(protyle, blockElement);
await paste(protyle, {
@@ -4010,7 +4010,7 @@ export class WYSIWYG {
lineBreakUndoContext = !event.defaultPrevented && event.inputType === "insertLineBreak" &&
selection.rangeCount > 0 ?
getUndoFocusContext(protyle.wysiwyg.element, selection.getRangeAt(0)) : undefined;
- if (event.defaultPrevented || event.inputType !== "insertText" || !event.data ||
+ if (event.defaultPrevented || !isTextInputType(event.inputType, event.data) ||
selection.rangeCount === 0) {
return;
}
@@ -4026,6 +4026,9 @@ export class WYSIWYG {
}
event.preventDefault();
event.stopPropagation();
+ if (event.isComposing || isComposition) {
+ beforeInputCompositionHandled = true;
+ }
await removeCrossBlockRange(protyle, range, startElement, endElement, false, {
event: /^\d{1}$/.test(event.data) ? undefined : event,
text: event.data,
diff --git a/app/src/protyle/wysiwyg/multilineInput.test.ts b/app/src/protyle/wysiwyg/multilineInput.test.ts
index b8f3222580..860b95ee07 100644
--- a/app/src/protyle/wysiwyg/multilineInput.test.ts
+++ b/app/src/protyle/wysiwyg/multilineInput.test.ts
@@ -1,6 +1,20 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getMultilineInputText} from "./multilineInput";
+import {getMultilineInputText, isTextInputType} from "./multilineInput";
+
+describe("text input type", () => {
+ it("accepts text, replacement, and composition input", () => {
+ ["insertText", "insertReplacementText", "insertCompositionText"].forEach(inputType => {
+ assert.equal(isTextInputType(inputType, "`"), true);
+ });
+ });
+
+ it("ignores empty and non-text input", () => {
+ assert.equal(isTextInputType("insertText", null), false);
+ assert.equal(isTextInputType("insertLineBreak", "\n"), false);
+ assert.equal(isTextInputType("insertFromPaste", "text"), false);
+ });
+});
describe("multiline input", () => {
it("accepts text, replacement, and composition input", () => {
diff --git a/app/src/protyle/wysiwyg/multilineInput.ts b/app/src/protyle/wysiwyg/multilineInput.ts
index 42d9946abd..05b9e01d14 100644
--- a/app/src/protyle/wysiwyg/multilineInput.ts
+++ b/app/src/protyle/wysiwyg/multilineInput.ts
@@ -1,11 +1,15 @@
-const multilineInputTypes = new Set([
+const textInputTypes = new Set([
"insertText",
"insertReplacementText",
"insertCompositionText",
]);
+export const isTextInputType = (inputType: string, data: string | null): data is string => {
+ return !!data && textInputTypes.has(inputType);
+};
+
export const getMultilineInputText = (inputType: string, data: string | null) => {
- if (!data || !multilineInputTypes.has(inputType) || !/[\r\n\u2028\u2029]/.test(data)) {
+ if (!isTextInputType(inputType, data) || !/[\r\n\u2028\u2029]/.test(data)) {
return;
}
return data.replace(/\r\n|\r|\u2028|\u2029/g, "\n");