Skip to content

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

:zap: Improve animated image rendering performance https://github.com/siyuan-note/siyuan/issues/18686

Daniel89c014f4c80d
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index d7929395a2..5df80d2042 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -7,6 +7,11 @@
user-select: auto;
overflow-x: clip;
+ &.protyle--image-animation-paused,
+ .protyle--image-animation-paused & {
+ image-animation: paused;
+ }
+
&--empty:empty::before {
color: var(--b3-empty-color);
content: attr(placeholder);
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.ts
index b26de21c71..8c87b7a635 100644
--- a/app/src/boot/globalEvent/event.ts
+++ b/app/src/boot/globalEvent/event.ts
@@ -17,6 +17,7 @@ import {fetchPost} from "../../util/fetch";
import {initHarmonyTextSelectionMenu} from "../../util/harmonyTextSelectionMenu";
import {clearDragTipGhost, hideDragTip} from "../../protyle/util/dragTip";
import {formatPainter} from "../../protyle/toolbar/FormatPainter";
+import {pauseImageAnimation, resumeImageAnimation} from "../../protyle/util/imageAnimation";
const KANBAN_GROUP_DRAG_TYPE = `${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}Group${Constants.ZWSP}`.toLowerCase();
@@ -247,6 +248,7 @@ export const initWindowEvent = (app: App) => {
});
window.addEventListener("blur", () => {
+ pauseImageAnimation(document.body);
window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false;
window.siyuan.altIsPressed = false;
@@ -256,6 +258,10 @@ export const initWindowEvent = (app: App) => {
/// #endif
});
+ window.addEventListener("focus", () => {
+ resumeImageAnimation(document.body, Constants.TIMEOUT_INPUT);
+ });
+
window.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
globalClick(event);
});
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index 1098071a38..cbc8f863e5 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -43,6 +43,7 @@ import {hideAllElements} from "../protyle/ui/hideElements";
import {focusByOffset, getSelectionOffset} from "../protyle/util/selection";
import {Custom} from "./dock/Custom";
import type {App} from "../index";
+import {pauseImageAnimation, resumeImageAnimation} from "../protyle/util/imageAnimation";
import {unicode2Emoji} from "../emoji";
import {closeWindow} from "../window/closeWin";
import {newCenterEmptyTab, resizeTabs, setTabPosition} from "./tabUtil";
@@ -540,10 +541,12 @@ export class Wnd {
}
}
item.panelElement.classList.remove("fn__none");
+ resumeImageAnimation(item.panelElement, Constants.TIMEOUT_INPUT);
}
currentTab = item;
} else {
item.headElement?.classList.remove("item--focus");
+ pauseImageAnimation(item.panelElement);
if (!item.panelElement.classList.contains("fn__none")) {
// 必须现判断,否则会触发 observer.observe(this.element, {attributeFilter: ["class"]}); 导致 https://ld246.com/article/1641198819303
item.panelElement.classList.add("fn__none");
diff --git a/app/src/protyle/scroll/event.ts b/app/src/protyle/scroll/event.ts
index 22fe786006..a08589246c 100644
--- a/app/src/protyle/scroll/event.ts
+++ b/app/src/protyle/scroll/event.ts
@@ -4,11 +4,13 @@ import {isMobile} from "../../util/functions";
import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
import {stickyRow} from "../render/av/row";
import {trimAVRowsSync} from "../render/av/virtualScroll";
+import {pauseImageAnimationTemporarily} from "../util/imageAnimation";
let getIndexTimeout: number;
const avScrollPending = new WeakSet<HTMLElement>();
export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
element.addEventListener("scroll", () => {
+ pauseImageAnimationTemporarily(protyle.wysiwyg.element, Constants.TIMEOUT_INPUT);
const elementRect = element.getBoundingClientRect();
if (!protyle.toolbar.element.classList.contains("fn__none")) {
const initY = protyle.toolbar.element.getAttribute("data-inity").split(Constants.ZWSP);
diff --git a/app/src/protyle/util/imageAnimation.test.ts b/app/src/protyle/util/imageAnimation.test.ts
new file mode 100644
index 0000000000..e97e45406d
--- /dev/null
+++ b/app/src/protyle/util/imageAnimation.test.ts
@@ -0,0 +1,75 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ createImageAnimationController,
+ IMAGE_ANIMATION_PAUSED_CLASS,
+} from "./imageAnimation";
+
+const createElement = () => {
+ const classes = new Set<string>();
+ return {
+ element: {
+ classList: {
+ add: (name: string) => classes.add(name),
+ contains: (name: string) => classes.has(name),
+ remove: (name: string) => classes.delete(name),
+ },
+ } as unknown as HTMLElement,
+ hasClass: (name: string) => classes.has(name),
+ };
+};
+
+const createScheduler = () => {
+ let nextID = 0;
+ const callbacks = new Map<number, () => void>();
+ return {
+ clearTimer: (id: number) => callbacks.delete(id),
+ runAll: () => {
+ const pendingCallbacks = Array.from(callbacks.values());
+ callbacks.clear();
+ pendingCallbacks.forEach(callback => callback());
+ },
+ setTimer: (callback: () => void) => {
+ nextID++;
+ callbacks.set(nextID, callback);
+ return nextID;
+ },
+ };
+};
+
+describe("imageAnimationController", () => {
+ it("pauses immediately and resumes after the scheduled delay", () => {
+ const scheduler = createScheduler();
+ const controller = createImageAnimationController(scheduler.setTimer, scheduler.clearTimer);
+ const target = createElement();
+
+ controller.pauseTemporarily(target.element, 256);
+
+ assert.equal(target.hasClass(IMAGE_ANIMATION_PAUSED_CLASS), true);
+ scheduler.runAll();
+ assert.equal(target.hasClass(IMAGE_ANIMATION_PAUSED_CLASS), false);
+ });
+
+ it("cancels a scheduled resume when the element pauses again", () => {
+ const scheduler = createScheduler();
+ const controller = createImageAnimationController(scheduler.setTimer, scheduler.clearTimer);
+ const target = createElement();
+
+ controller.pauseTemporarily(target.element, 256);
+ controller.pause(target.element);
+ scheduler.runAll();
+
+ assert.equal(target.hasClass(IMAGE_ANIMATION_PAUSED_CLASS), true);
+ });
+
+ it("resumes immediately when no delay is requested", () => {
+ const scheduler = createScheduler();
+ const controller = createImageAnimationController(scheduler.setTimer, scheduler.clearTimer);
+ const target = createElement();
+
+ controller.pause(target.element);
+ controller.resume(target.element, 0);
+
+ assert.equal(target.hasClass(IMAGE_ANIMATION_PAUSED_CLASS), false);
+ });
+});
diff --git a/app/src/protyle/util/imageAnimation.ts b/app/src/protyle/util/imageAnimation.ts
new file mode 100644
index 0000000000..a8b81da77c
--- /dev/null
+++ b/app/src/protyle/util/imageAnimation.ts
@@ -0,0 +1,58 @@
+export const IMAGE_ANIMATION_PAUSED_CLASS = "protyle--image-animation-paused";
+
+export const createImageAnimationController = <T>(
+ setTimer: (callback: () => void, delay: number) => T,
+ clearTimer: (timer: T) => void,
+) => {
+ const resumeTimers = new WeakMap<HTMLElement, T>();
+
+ const cancelResume = (element: HTMLElement) => {
+ const timer = resumeTimers.get(element);
+ if (typeof timer === "undefined") {
+ return;
+ }
+ clearTimer(timer);
+ resumeTimers.delete(element);
+ };
+
+ const pause = (element: HTMLElement) => {
+ cancelResume(element);
+ element.classList.add(IMAGE_ANIMATION_PAUSED_CLASS);
+ };
+
+ const resume = (element: HTMLElement, delay: number) => {
+ cancelResume(element);
+ if (!element.classList.contains(IMAGE_ANIMATION_PAUSED_CLASS)) {
+ return;
+ }
+ if (delay <= 0) {
+ element.classList.remove(IMAGE_ANIMATION_PAUSED_CLASS);
+ return;
+ }
+ const timer = setTimer(() => {
+ resumeTimers.delete(element);
+ element.classList.remove(IMAGE_ANIMATION_PAUSED_CLASS);
+ }, delay);
+ resumeTimers.set(element, timer);
+ };
+
+ const pauseTemporarily = (element: HTMLElement, delay: number) => {
+ pause(element);
+ resume(element, delay);
+ };
+
+ return {
+ pause,
+ pauseTemporarily,
+ resume,
+ };
+};
+
+const imageAnimationController = createImageAnimationController(
+ (callback, delay) => globalThis.setTimeout(callback, delay),
+ (timer) => globalThis.clearTimeout(timer),
+);
+
+export const pauseImageAnimation = imageAnimationController.pause;
+export const pauseImageAnimationTemporarily = imageAnimationController.pauseTemporarily;
+export const resumeImageAnimation = imageAnimationController.resume;