Skip to content

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

:bug: Allow plugins to await pending editor transactions https://github.com/siyuan-note/siyuan/issues/16453

Danieldd08975c765e
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 1ada651cbf..0ca645c435 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -51,6 +51,7 @@ import {isSupportCSSHL} from "./render/searchMarkRender";
import {renderAVAttribute} from "./render/av/blockAttr";
import {setFoldById, zoomOut} from "../menus/protyle";
import {setEditMode} from "./util/setEditMode";
+import {waitForPendingTransactions} from "./util/transactionQueue";
export class Protyle {
@@ -542,6 +543,11 @@ export class Protyle {
insertHTML(html, this.protyle, isBlock, useProtyleRange);
}
+ public async flushPendingTransactions() {
+ await this.protyle.wysiwyg.flushPendingInput();
+ await waitForPendingTransactions(this.protyle);
+ }
+
public transaction(doOperations: IOperation[], undoOperations?: IOperation[]) {
transaction(this.protyle, doOperations, undoOperations);
}
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index be57b13906..73b955f601 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -307,10 +307,10 @@ export class WYSIWYG {
private preventInput: boolean;
private copyAsRichText = false;
private inputTimeout: number;
- private pendingInputTimeouts = new Map<number, () => void>();
+ private pendingInputTimeouts = new Map<number, () => void | Promise<void>>();
public tableControl: TableControl;
- private scheduleInput(callback: () => void, delay = 0, replace = true) {
+ private scheduleInput(callback: () => void | Promise<void>, delay = 0, replace = true) {
if (replace && this.inputTimeout) {
clearTimeout(this.inputTimeout);
this.pendingInputTimeouts.delete(this.inputTimeout);
@@ -320,7 +320,7 @@ export class WYSIWYG {
if (this.inputTimeout === timeout) {
this.inputTimeout = undefined;
}
- callback();
+ void callback();
}, delay);
this.pendingInputTimeouts.set(timeout, callback);
if (replace) {
@@ -328,12 +328,12 @@ export class WYSIWYG {
}
}
- public flushPendingInput() {
+ public async flushPendingInput() {
const callbacks = Array.from(this.pendingInputTimeouts.values());
this.pendingInputTimeouts.forEach((callback, timeout) => clearTimeout(timeout));
this.pendingInputTimeouts.clear();
this.inputTimeout = undefined;
- callbacks.forEach(callback => callback());
+ await Promise.all(callbacks.map(callback => callback()));
}
public copyRichText() {
@@ -3930,19 +3930,15 @@ export class WYSIWYG {
// 百度输入法中文反双引号 https://github.com/siyuan-note/siyuan/issues/9686
event.data === "”" ||
event.data === "「")) {
- this.scheduleInput(() => {
- // 搜狗拼音数字后面句号变为点;Mac 反向双引号无法输入
- input(protyle, blockElement, range, true);
- });
+ // 搜狗拼音数字后面句号变为点;Mac 反向双引号无法输入
+ this.scheduleInput(() => input(protyle, blockElement, range, true));
} else {
if (isMac() && event.data === "【】") {
- this.scheduleInput(() => {
- input(protyle, blockElement, range, true, event);
- }, Constants.TIMEOUT_INPUT, false);
+ this.scheduleInput(() => input(protyle, blockElement, range, true, event),
+ Constants.TIMEOUT_INPUT, false);
} else {
- this.scheduleInput(() => {
- input(protyle, blockElement, range, true, event, lineBreakInputOperations);
- });
+ this.scheduleInput(() => input(protyle, blockElement, range, true, event,
+ lineBreakInputOperations));
}
}
event.stopPropagation();