Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18779

Vanessa48d1e34b2431
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index e339a0524c..7257053002 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -49,6 +49,7 @@ import {
import * as dayjs from "dayjs";
import {dropEvent} from "../util/editorCommonEvent";
import {beforeBlockquoteInput, input} from "./input";
+import {getMultilineInputText} from "./multilineInput";
import {
getContenteditableElement,
getEmbedGutterOperationContext,
@@ -3719,6 +3720,8 @@ export class WYSIWYG {
// 输入法测试点 https://github.com/siyuan-note/siyuan/issues/3027
let isComposition = false; // for iPhone
+ // 多行组合输入已按 Markdown 插入时,忽略其后到达的 compositionend,避免重复生成事务。
+ let multilineCompositionHandled = false;
// 原生软换行在 input 触发前已经修改 DOM,需预存选区供撤销恢复。
let lineBreakUndoContext: Record<string, string>;
// 仅矫正从数据库外进入的占位光标,避免重置数据库内部的方向键导航。
@@ -3854,6 +3857,11 @@ export class WYSIWYG {
return;
}
isComposition = false;
+ if (multilineCompositionHandled) {
+ multilineCompositionHandled = false;
+ compositionRange = undefined;
+ return;
+ }
const range = getEditorRange(this.element);
const blockElement = hasClosestBlock(range.startContainer);
if (!blockElement) {
@@ -3919,6 +3927,39 @@ export class WYSIWYG {
return;
}
}
+ const multilineText = isMac() && event.cancelable ?
+ getMultilineInputText(event.inputType, event.data) : undefined;
+ if (multilineText !== undefined) {
+ const selection = getSelection();
+ if (selection.rangeCount > 0) {
+ let range = selection.getRangeAt(0);
+ const targetRange = event.getTargetRanges()[0];
+ if (targetRange && this.element.contains(targetRange.startContainer) &&
+ this.element.contains(targetRange.endContainer)) {
+ range = document.createRange();
+ range.setStart(targetRange.startContainer, targetRange.startOffset);
+ range.setEnd(targetRange.endContainer, targetRange.endOffset);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ }
+ const blockElement = hasClosestBlock(range.startContainer) as HTMLElement;
+ if (blockElement && getContenteditableElement(blockElement)) {
+ event.preventDefault();
+ event.stopPropagation();
+ lineBreakUndoContext = undefined;
+ if (event.isComposing || isComposition) {
+ multilineCompositionHandled = true;
+ }
+ beforePaste(protyle, blockElement);
+ await paste(protyle, {
+ textHTML: "",
+ textPlain: multilineText,
+ target: event.target as HTMLElement,
+ });
+ return;
+ }
+ }
+ }
if (!isComposition) {
beforeBlockquoteInput(protyle, event);
}
diff --git a/app/src/protyle/wysiwyg/multilineInput.test.ts b/app/src/protyle/wysiwyg/multilineInput.test.ts
new file mode 100644
index 0000000000..b8f3222580
--- /dev/null
+++ b/app/src/protyle/wysiwyg/multilineInput.test.ts
@@ -0,0 +1,21 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getMultilineInputText} from "./multilineInput";
+
+describe("multiline input", () => {
+ it("accepts text, replacement, and composition input", () => {
+ ["insertText", "insertReplacementText", "insertCompositionText"].forEach(inputType => {
+ assert.equal(getMultilineInputText(inputType, "aaa\nbbb"), "aaa\nbbb");
+ });
+ });
+
+ it("normalizes line separators", () => {
+ assert.equal(getMultilineInputText("insertText", "a\r\nb\rc\u2028d\u2029e"), "a\nb\nc\nd\ne");
+ });
+
+ it("ignores ordinary and line break input", () => {
+ assert.equal(getMultilineInputText("insertText", "aaa"), undefined);
+ assert.equal(getMultilineInputText("insertLineBreak", "aaa\nbbb"), undefined);
+ assert.equal(getMultilineInputText("insertText", null), undefined);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/multilineInput.ts b/app/src/protyle/wysiwyg/multilineInput.ts
new file mode 100644
index 0000000000..42d9946abd
--- /dev/null
+++ b/app/src/protyle/wysiwyg/multilineInput.ts
@@ -0,0 +1,12 @@
+const multilineInputTypes = new Set([
+ "insertText",
+ "insertReplacementText",
+ "insertCompositionText",
+]);
+
+export const getMultilineInputText = (inputType: string, data: string | null) => {
+ if (!data || !multilineInputTypes.has(inputType) || !/[\r\n\u2028\u2029]/.test(data)) {
+ return;
+ }
+ return data.replace(/\r\n|\r|\u2028|\u2029/g, "\n");
+};