Skip to content

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

:bug: Prevent long text wrapping flicker during input https://github.com/siyuan-note/siyuan/issues/19679

Daniel43f91d7427bc
diff --git a/app/src/protyle/util/longTextWrap.ts b/app/src/protyle/util/longTextWrap.ts
index a6e9ca50cc..7832996922 100644
--- a/app/src/protyle/util/longTextWrap.ts
+++ b/app/src/protyle/util/longTextWrap.ts
@@ -41,7 +41,7 @@ export const unwrapLongTextRuns = (root: ParentNode) => {
parents.forEach(parent => parent.normalize());
};
-const preserveSelection = (root: Element, update: () => void) => {
+const preserveSelection = (root: Element, update: () => void, retainedRange?: Range) => {
const selection = root.ownerDocument.getSelection();
const capture = (node: Node, offset: number) => {
if (!node || !root.contains(node)) {
@@ -54,10 +54,19 @@ const preserveSelection = (root: Element, update: () => void) => {
};
const anchor = selection?.rangeCount ? capture(selection.anchorNode, selection.anchorOffset) : undefined;
const focus = selection?.rangeCount ? capture(selection.focusNode, selection.focusOffset) : undefined;
- update();
- if (!anchor || !focus || (anchor.textOffset === undefined && focus.textOffset === undefined)) {
- return;
+ const ranges = new Set<Range>();
+ if (selection?.rangeCount) {
+ ranges.add(selection.getRangeAt(0));
}
+ if (retainedRange) {
+ ranges.add(retainedRange);
+ }
+ const positions = Array.from(ranges, range => ({
+ range,
+ start: capture(range.startContainer, range.startOffset),
+ end: capture(range.endContainer, range.endOffset),
+ }));
+ update();
const resolve = (position: ReturnType<typeof capture>) => {
if (position.textOffset === undefined) {
return position;
@@ -73,6 +82,19 @@ const preserveSelection = (root: Element, update: () => void) => {
}
return {node: root, offset: root.childNodes.length};
};
+ // 延迟输入处理持有原 Range 对象,更新显示节点时同步恢复其边界。
+ positions.forEach(({range, start, end}) => {
+ if (start.textOffset === undefined && end.textOffset === undefined) {
+ return;
+ }
+ const from = resolve(start);
+ const to = resolve(end);
+ range.setStart(from.node, from.offset);
+ range.setEnd(to.node, to.offset);
+ });
+ if (!anchor || !focus || (anchor.textOffset === undefined && focus.textOffset === undefined)) {
+ return;
+ }
const start = resolve(anchor);
const end = resolve(focus);
selection.setBaseAndExtent(start.node, start.offset, end.node, end.offset);
@@ -171,8 +193,11 @@ const configuredEditors = new WeakSet<Element>();
const composingEditors = new WeakSet<Element>();
const inputDepth = new WeakMap<Element, number>();
-export const suspendLongTextRuns = (editor: Element) => {
+export const suspendLongTextRuns = (editor: Element, block: Element, range: Range) => {
inputDepth.set(editor, (inputDepth.get(editor) || 0) + 1);
+ if (block.querySelector(LONG_TEXT_SELECTOR)) {
+ preserveSelection(block, () => unwrapLongTextRuns(block), range);
+ }
return () => {
inputDepth.set(editor, inputDepth.get(editor) - 1);
renderLongTextRuns(editor);
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index ed43e4f571..39f1189d38 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -2,7 +2,8 @@ import {recordReplacementUndo} from "./replacementInput";
import {bindSpellcheckFocus} from "../util/spellcheckFocus";
import {isTableLikeView} from "../render/av/viewType";
import {visibleTabsSelectionHTML} from "../render/tabsVisibility";
-import {prepareInlineElementBoundaryMutation} from "../util/inlineElementBoundary";
+import {normalizeInlineElementBoundaries, prepareInlineElementBoundaryMutation} from "../util/inlineElementBoundary";
+import {renderLongTextRuns} from "../util/longTextWrap";
import {repairHiddenTabSelection} from "../util/tabsSelection";
import {isTabTextBoundary} from "./tabsBoundary";
import {captureCompositionText} from "./compositionCaret";
@@ -4360,6 +4361,9 @@ export class WYSIWYG {
}
this.escapeInline(protyle, range, event);
+ // 原生输入结束后立即恢复各段折行,避免延迟解析前绘制出临时的整段换行。
+ normalizeInlineElementBoundaries(this.element);
+ renderLongTextRuns(this.element);
if ((/^\d{1}$/.test(event.data) || event.data === "‘" || event.data === "“" ||
// 百度输入法中文反双引号 https://github.com/siyuan-note/siyuan/issues/9686
event.data === "”" ||
diff --git a/app/src/protyle/wysiwyg/input.ts b/app/src/protyle/wysiwyg/input.ts
index 192bac77e7..a5f752a293 100644
--- a/app/src/protyle/wysiwyg/input.ts
+++ b/app/src/protyle/wysiwyg/input.ts
@@ -567,7 +567,7 @@ const inputBlock = async (protyle: IProtyle, blockElement: HTMLElement, range: R
};
export const input = async (...args: Parameters<typeof inputBlock>) => {
- const resume = suspendLongTextRuns(args[0].wysiwyg.element);
+ const resume = suspendLongTextRuns(args[0].wysiwyg.element, args[1], args[2]);
try {
await inputBlock(...args);
} finally {