Skip to content

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

:sparkles: Scale image captions with displayed image widths https://github.com/siyuan-note/siyuan/issues/20719

Daniel90083f090fb6
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index ab794a7eb3..420ef7e280 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -669,6 +669,15 @@ $block-rtl-selector: ".protyle-block--rtl" !default;
> .protyle-image-container {
position: relative;
+
+ > .img__name {
+ font-size: min(100%, var(--protyle-image-font-size, 100%));
+ }
+
+ > .protyle-action__title,
+ > .img__alt {
+ font-size: min(85%, var(--protyle-image-font-size, 85%));
+ }
}
> .protyle-image-container--compact {
diff --git a/app/src/protyle/render/imageResize.test.ts b/app/src/protyle/render/imageResize.test.ts
index ddaa267a9d..f0ba44ac8f 100644
--- a/app/src/protyle/render/imageResize.test.ts
+++ b/app/src/protyle/render/imageResize.test.ts
@@ -275,6 +275,46 @@ const browserCases = async (source: string, resizeSource: string, luteSource: st
await settle();
assertFrame();
}
+ const captioned = await create();
+ const captionContainer = captioned.parentElement;
+ captionContainer.insertAdjacentHTML("afterbegin", '<span class="img__name"><span>sample.png</span></span>');
+ captionContainer.insertAdjacentHTML("beforeend", '<span class="img__alt"><span>description</span></span>');
+ const captions = Array.from(captionContainer.querySelectorAll<HTMLElement>(".img__name, .protyle-action__title, .img__alt"));
+ const markdown = lute.BlockDOM2StdMd(root.innerHTML);
+ const fontSizes = () => captions.map(caption => parseFloat(getComputedStyle(caption).fontSize));
+ for (const [width, expected] of [[160, 16], [80, 8], [24, 8], [120, 12]]) {
+ captionContainer.style.width = width + "px";
+ await settle();
+ check.equal(captioned.clientWidth, width, "caption styles preserve the image's percentage width rule");
+ fontSizes().forEach(size => check.equal(size, expected, "all three caption types follow the displayed image width"));
+ check.ok(captions.every(caption => getComputedStyle(caption).display !== "none"), "small images retain their captions");
+ }
+ begin(captioned, "width", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ document.onmousemove(new MouseEvent("mousemove", {clientX: 320, clientY: 200}));
+ document.onmouseup(new MouseEvent("mouseup"));
+ await settle();
+ check.equal(captioned.clientWidth, 140);
+ check.deepEqual(fontSizes(), [14, 14, 14], "drag resizing updates the caption fonts");
+ root.style.zoom = "1.5";
+ await settle();
+ check.deepEqual(fontSizes(), [14, 14, 14], "editor zoom scales images and captions together without applying the ratio twice");
+ root.style.zoom = "";
+ captionContainer.style.width = "400px";
+ await settle();
+ fontSizes().forEach((size, i) => check.ok(Math.abs(size - (i === 0 ? 24 : 20.4)) < 0.01,
+ "large images retain the original caption font sizes"));
+ captionContainer.style.width = "";
+ await settle();
+ check.equal(captioned.clientWidth, 200);
+ check.equal(lute.BlockDOM2StdMd(root.innerHTML), markdown, "caption font styles do not enter the saved image source");
+ const restored = lute.BlockDOM2StdMd(root.innerHTML);
+ root.innerHTML = lute.Md2BlockDOM(restored);
+ const restoredImage = root.querySelector<HTMLImageElement>("img");
+ restoredImage.src = url;
+ await restoredImage.decode();
+ await settle();
+ check.equal(parseFloat(getComputedStyle(restoredImage.parentElement.querySelector(".protyle-action__title")).fontSize), 20,
+ "reopening derives the same font size from the current image width");
const centered = await create();
root.firstElementChild.setAttribute("style", "text-align: center");
begin(centered, "width", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
diff --git a/app/src/protyle/render/imageResize.ts b/app/src/protyle/render/imageResize.ts
index 0efc8c9532..81773c43e6 100644
--- a/app/src/protyle/render/imageResize.ts
+++ b/app/src/protyle/render/imageResize.ts
@@ -15,6 +15,9 @@ export const bindImageResize = (root: HTMLElement) => {
const images = new Map<HTMLImageElement, {frame: HTMLElement, container: HTMLElement}>();
const position = (image: HTMLImageElement, frame: HTMLElement) => {
image.parentElement.classList.toggle("protyle-image-container--compact", image.clientWidth < 96 || image.clientHeight < 48);
+ // 说明文字按图片显示宽度缩放,派生样式只写入说明节点,避免影响图片容器的宽度规则。
+ image.parentElement.querySelectorAll<HTMLElement>(":scope > .img__name, :scope > .protyle-action__title, :scope > .img__alt")
+ .forEach(caption => caption.style.setProperty("--protyle-image-font-size", Math.max(8, image.clientWidth / 10) + "px"));
frame.style.left = image.offsetLeft + "px";
frame.style.top = image.offsetTop + "px";
frame.style.width = image.clientWidth + "px";