Skip to content

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

:bug: Reset empty query embed rendering state https://github.com/siyuan-note/siyuan/issues/18917

Daniel48acf5225c94
diff --git a/app/src/protyle/render/blockRender.ts b/app/src/protyle/render/blockRender.ts
index 9e81614e85..b9c6806846 100644
--- a/app/src/protyle/render/blockRender.ts
+++ b/app/src/protyle/render/blockRender.ts
@@ -8,7 +8,7 @@ import {genRenderFrame} from "./util";
import {isEncryptedBox} from "../../util/pathName";
import {disabledWYSIWYG} from "../util/disabledWYSIWYG";
import {normalizeHTMLAssetIFrameBlockDOM} from "../../asset/html";
-import {finishCustomEmbedRender, IEmbedRenderLoadingState} from "./embedRenderState";
+import {finishCustomEmbedRender, finishEmptyEmbedRender, IEmbedRenderLoadingState} from "./embedRenderState";
/**
* 渲染嵌入块
@@ -27,6 +27,8 @@ export const blockRender = (protyle: IProtyle, element: Element, top?: number, o
blockElements.forEach((item: HTMLElement) => {
const content = Lute.UnEscapeHTMLStr(item.getAttribute("data-content"));
if (!content.trim()) {
+ genRenderFrame(item);
+ finishEmptyEmbedRender(item, onEmbedRender);
return;
}
// 需置于请求返回前,否则快速滚动会导致重复加载 https://ld246.com/article/1666857862494?r=88250
diff --git a/app/src/protyle/render/embedRenderState.test.ts b/app/src/protyle/render/embedRenderState.test.ts
index ffdef6e064..9990a79cd8 100644
--- a/app/src/protyle/render/embedRenderState.test.ts
+++ b/app/src/protyle/render/embedRenderState.test.ts
@@ -1,6 +1,45 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {finishCustomEmbedRender} from "./embedRenderState";
+import {finishCustomEmbedRender, finishEmptyEmbedRender} from "./embedRenderState";
+
+const createClassList = (...names: string[]) => {
+ const classes = new Set(names);
+ return {
+ classes,
+ classList: {
+ contains: (name: string) => classes.has(name),
+ remove: (name: string) => classes.delete(name),
+ },
+ };
+};
+
+describe("finishEmptyEmbedRender", () => {
+ it("clears rendered content and loading state while preserving the frame", () => {
+ const rotateElement = createClassList("fn__rotate");
+ const icons = createClassList("protyle-icons");
+ const cursor = createClassList("protyle-cursor");
+ const result = {...createClassList("protyle-wysiwyg__embed"), remove() {
+ children.splice(children.indexOf(result), 1);
+ }};
+ const attr = createClassList("protyle-attr");
+ const children = [icons, result, cursor, attr];
+ const item = {
+ children,
+ querySelector: () => rotateElement,
+ style: {height: "120px"},
+ } as unknown as HTMLElement;
+ let callbackCount = 0;
+
+ finishEmptyEmbedRender(item, () => {
+ callbackCount++;
+ });
+
+ assert.equal(rotateElement.classes.has("fn__rotate"), false);
+ assert.deepEqual(children, [icons, cursor, attr]);
+ assert.equal(item.style.height, "");
+ assert.equal(callbackCount, 1);
+ });
+});
describe("finishCustomEmbedRender", () => {
it("clears the loading state and invokes the completion callback", () => {
diff --git a/app/src/protyle/render/embedRenderState.ts b/app/src/protyle/render/embedRenderState.ts
index 76e2108c29..7c7b01ed6b 100644
--- a/app/src/protyle/render/embedRenderState.ts
+++ b/app/src/protyle/render/embedRenderState.ts
@@ -3,6 +3,18 @@ export interface IEmbedRenderLoadingState {
height?: string;
}
+export const finishEmptyEmbedRender = (item: HTMLElement, onEmbedRender?: () => void) => {
+ item.querySelector(":scope > .protyle-icons .protyle-action__reload .fn__rotate")?.classList.remove("fn__rotate");
+ Array.from(item.children).forEach(child => {
+ if (!child.classList.contains("protyle-icons") && !child.classList.contains("protyle-cursor") &&
+ !child.classList.contains("protyle-attr")) {
+ child.remove();
+ }
+ });
+ item.style.height = "";
+ onEmbedRender?.();
+};
+
export const finishCustomEmbedRender = (item: HTMLElement, loadingState: IEmbedRenderLoadingState,
onEmbedRender?: () => void) => {
loadingState.rotateElement?.classList.remove("fn__rotate");
diff --git a/app/src/protyle/render/util.ts b/app/src/protyle/render/util.ts
index 2c124ac4dc..e6ac96bc96 100644
--- a/app/src/protyle/render/util.ts
+++ b/app/src/protyle/render/util.ts
@@ -32,8 +32,9 @@ export const genRenderFrame = (renderElement: Element) => {
}
const type = renderElement.getAttribute("data-type");
if (type === "NodeBlockQueryEmbed") {
+ const rotateClass = renderElement.getAttribute("data-content")?.trim() ? " class=\"fn__rotate\"" : "";
renderElement.insertAdjacentHTML("afterbegin", `<div class="protyle-icons${isInEmbedBlock(renderElement) ? " fn__none" : ""}">
- <span aria-label="${window.siyuan.languages.refresh}" data-position="4north" class="ariaLabel protyle-icon protyle-action__reload protyle-icon--first"><svg class="fn__rotate"><use xlink:href="#iconRefresh"></use></svg></span>
+ <span aria-label="${window.siyuan.languages.refresh}" data-position="4north" class="ariaLabel protyle-icon protyle-action__reload protyle-icon--first"><svg${rotateClass}><use xlink:href="#iconRefresh"></use></svg></span>
<span aria-label="${window.siyuan.languages.update} SQL" data-position="4north" class="ariaLabel protyle-icon protyle-action__edit"><svg><use xlink:href="#iconEdit"></use></svg></span>
<span aria-label="${window.siyuan.languages.refPopover}" data-position="4north" data-action="openFloat" class="ariaLabel protyle-icon"><svg><use xlink:href="#iconPictureInPicture"></use></svg></span>
<span aria-label="${window.siyuan.languages.more}" data-position="4north" class="ariaLabel protyle-icon protyle-action__menu protyle-icon--last"><svg><use xlink:href="#iconMore"></use></svg></span>