Skip to content

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

:lipstick: Unify proportional image resize handles https://github.com/siyuan-note/siyuan/issues/20017

Daniel8b03c52fd29d
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy
index b29b7b9a92..914c969158 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy
@@ -8,7 +8,7 @@
"title": "Formatting elements",
"title-img": "background-color:#269;background-image: linear-gradient(white 2px, transparent 2px),linear-gradient(90deg, white 2px, transparent 2px),linear-gradient(rgba(255,255,255,.3) 1px, transparent 1px),linear-gradient(90deg, rgba(255,255,255,.3) 1px, transparent 1px);background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;background-position:-2px -2px, -2px -2px, -1px -1px, -1px -1px;",
"type": "doc",
- "updated": "20261018200107"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -17,7 +17,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20210104091509-sg4pt12",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -32,7 +32,7 @@
"ListData": {},
"Properties": {
"id": "20210104091509-8csaq4j",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -105,7 +105,7 @@
},
"Properties": {
"id": "20220916200111-8pnrqr2",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -127,12 +127,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20210604225055-4pq8g38",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
"Type": "NodeText",
- "Data": "Support direct copy and paste or drag and upload. After uploading, you can adjust the size by dragging the sidebar"
+ "Data": "Paste or drag images to upload them. Hover over an image to reveal the small handles outside its border, then drag the right, bottom, or bottom-right handle to resize it proportionally. On touchscreens, drag a handle directly"
}
]
},
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy
index 5528a9e997..f695f6f77c 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy
@@ -8,7 +8,7 @@
"title": "排版元素",
"title-img": "background-color: hsl(2, 57%, 40%);background-image: repeating-linear-gradient(transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(270deg, transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(125deg, transparent, transparent 2px, rgba(0,0,0,.2) 2px, rgba(0,0,0,.2) 3px, transparent 3px, transparent 5px, rgba(0,0,0,.2) 5px);",
"type": "doc",
- "updated": "20261018200107"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -17,7 +17,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20210604182832-nm3hapx",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -32,7 +32,7 @@
"ListData": {},
"Properties": {
"id": "20210104091228-ttcj9nm",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -105,7 +105,7 @@
},
"Properties": {
"id": "20220916195046-bdzv6fy",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -127,12 +127,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20210604222221-9zkgkky",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
"Type": "NodeText",
- "Data": "可通过复制粘贴或拖拽来上传图片;上传后的图片可通过拖拽进行大小调整"
+ "Data": "可通过复制粘贴或拖拽来上传图片。将鼠标移到图片上可显示边框外的小手柄,拖动右侧、底部或右下角的手柄可等比调整大小;触屏可直接拖动手柄"
}
]
},
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy
index d583f3d97d..71cd549c94 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy
@@ -8,7 +8,7 @@
"title": "排版元素",
"title-img": "background-color: hsl(2, 57%, 40%);background-image: repeating-linear-gradient(transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(270deg, transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(125deg, transparent, transparent 2px, rgba(0,0,0,.2) 2px, rgba(0,0,0,.2) 3px, transparent 3px, transparent 5px, rgba(0,0,0,.2) 5px);",
"type": "doc",
- "updated": "20261018200107"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -17,7 +17,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20211226122008-i45akvs",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -32,7 +32,7 @@
"ListData": {},
"Properties": {
"id": "20211226122008-1e014nh",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -105,7 +105,7 @@
},
"Properties": {
"id": "20220916195409-g57vfyf",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -127,12 +127,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20211226122008-pei037e",
- "updated": "20261016202635"
+ "updated": "20261018210230"
},
"Children": [
{
"Type": "NodeText",
- "Data": "可通過複制貼上或拖拽來上傳圖片;上傳後的圖片可通過拖拽進行大小調整"
+ "Data": "可透過複製貼上或拖曳來上傳圖片。將滑鼠移到圖片上可顯示邊框外的小把手,拖曳右側、底部或右下角的把手可等比例調整大小;觸控螢幕可直接拖曳把手"
}
]
},
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy
index e35dc7ba62..0364ee2a69 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy
@@ -9,7 +9,7 @@
"title": "書式要素",
"title-img": "background-color:#269;background-image: linear-gradient(white 2px, transparent 2px),linear-gradient(90deg, white 2px, transparent 2px),linear-gradient(rgba(255,255,255,.3) 1px, transparent 1px),linear-gradient(90deg, rgba(255,255,255,.3) 1px, transparent 1px);background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;background-position:-2px -2px, -2px -2px, -1px -1px, -1px -1px;",
"type": "doc",
- "updated": "20261018200107"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -19,7 +19,7 @@
"Properties": {
"ID": "20240530101000-tfcp9du",
"id": "20240530101000-e1tndfu",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -35,7 +35,7 @@
"Properties": {
"ID": "20240530101000-8o1a9my",
"id": "20240530101000-wlqefeq",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -113,7 +113,7 @@
"Properties": {
"ID": "20240530101000-gjgldj9",
"id": "20240530101000-gm1p50s",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
@@ -137,12 +137,12 @@
"Properties": {
"ID": "20240530101000-h8nud0n",
"id": "20240530101000-r0imp45",
- "updated": "20261016202856"
+ "updated": "20261018210230"
},
"Children": [
{
"Type": "NodeText",
- "Data": "コピー&ペースト、またはドラッグ&ドロップによるアップロードに対応しています。サイドバーをドラッグすることでサイズを調整できます"
+ "Data": "コピー&ペースト、またはドラッグ&ドロップで画像をアップロードできます。画像にマウスを合わせると枠の外側に小さなハンドルが表示され、右側、下側、右下のハンドルをドラッグして縦横比を保ちながらサイズを変更できます。タッチスクリーンではハンドルを直接ドラッグできます"
}
]
},
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index d3be534627..9d5f0b293a 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -662,40 +662,22 @@ $block-rtl-selector: ".protyle-block--rtl" !default;
height: 100%;
}
- .protyle-action__drag {
- height: 30%;
- width: 4px;
- background: var(--b3-theme-surface);
- display: none;
- border-radius: var(--b3-border-radius);
- cursor: col-resize;
- transition: var(--b3-transition);
- touch-action: none;
+ > .protyle-image-container {
+ position: relative;
+ }
+
+ .protyle-image-resize {
position: absolute;
- top: 35%;
- margin-left: -8px;
- box-shadow: 0 0 1px 1px var(--b3-theme-on-surface);
- box-sizing: border-box;
- z-index: 1;
+ pointer-events: none;
+ z-index: 6;
- &:hover {
- background-color: var(--b3-theme-background);
- box-shadow: 0 0 1px 1px var(--b3-theme-on-background);
- }
-
- &::before,
- &::after {
- position: absolute;
- width: 4px;
- content: "";
- height: 100%;
- left: -4px;
+ > .protyle-block-resize {
+ pointer-events: auto;
}
+ }
- &::after {
- left: 4px;
- width: 8px
- }
+ &:hover .protyle-block-resize {
+ opacity: 1;
}
&__net {
@@ -986,6 +968,22 @@ body[data-format-painter] .protyle-wysiwyg[data-readonly="false"] {
display: none;
}
+.protyle-wysiwyg[data-readonly="true"] .protyle-image-resize,
+.protyle-wysiwyg__embed .protyle-image-resize,
+.mindmap-view__preview-block .protyle-image-resize {
+ display: none;
+}
+
+.protyle-wysiwyg[data-readonly="false"] .img .protyle-image-caption {
+ margin-top: 12px;
+}
+
+@media (any-pointer: coarse) {
+ .protyle-wysiwyg[data-readonly="false"] .img .protyle-image-caption {
+ margin-top: 20px;
+ }
+}
+
.protyle-format-painter__cursor {
position: fixed;
z-index: 9;
diff --git a/app/src/protyle/render/iframeResize.test.ts b/app/src/protyle/render/iframeResize.test.ts
index f45b49bb11..61af5dc250 100644
--- a/app/src/protyle/render/iframeResize.test.ts
+++ b/app/src/protyle/render/iframeResize.test.ts
@@ -621,7 +621,9 @@ test("external resize handles preserve source, independent axes, historical dime
};
visit(source);
assert.ok(resize);
- const resizeSource = transpileModule(`function resize() { ${resize} }`, {compilerOptions: {target: ScriptTarget.ES2021}})
+ const imageResizeSource = transpileModule(readFileSync("src/protyle/render/imageResize.ts", "utf8").replace(/^export /gm, ""),
+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const resizeSource = imageResizeSource + transpileModule(`function resize() { ${resize} }`, {compilerOptions: {target: ScriptTarget.ES2021}})
.outputText + "\nreturn resize.call(this);";
const renderSource = transpileModule(readFileSync("src/protyle/render/iframeResize.ts", "utf8").replace(/^export /gm, ""),
{compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
diff --git a/app/src/protyle/render/imageResize.test.ts b/app/src/protyle/render/imageResize.test.ts
new file mode 100644
index 0000000000..b11456c4eb
--- /dev/null
+++ b/app/src/protyle/render/imageResize.test.ts
@@ -0,0 +1,301 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import {compileString} from "sass";
+import {createSourceFile, isIfStatement, ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const browserCases = async (source: string, resizeSource: string, luteSource: string, css: string,
+ bridgeSource: string, coreSource: string) => {
+ const check: typeof assert = require("node:assert/strict");
+ new Function(luteSource)();
+ const lute = Lute.New();
+ const api = new Function(source + "\nreturn {bindImageResize, getImageResizeWidth};")() as typeof import("./imageResize");
+ const style = document.createElement("style");
+ style.textContent = css + `
+ body { margin: 24px; }
+ :root { --b3-theme-on-surface-light: #888; --b3-theme-on-surface: #555; --b3-theme-primary: #4285f4; }
+ .protyle-wysiwyg { width: min(600px, calc(100vw - 48px)); font-size: 24px; }
+ `;
+ document.head.appendChild(style);
+ const root = document.createElement("div");
+ root.className = "protyle-wysiwyg";
+ root.dataset.readonly = "false";
+ document.body.appendChild(root);
+ const dispose = api.bindImageResize(root);
+ const canvas = document.createElement("canvas");
+ canvas.width = 200;
+ canvas.height = 100;
+ const context = canvas.getContext("2d");
+ context.fillStyle = "#6495ed";
+ context.fillRect(0, 0, 200, 100);
+ const url = canvas.toDataURL();
+ const settle = () => new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
+ const create = async (legacy = false) => {
+ root.style.zoom = "";
+ root.innerHTML = lute.Md2BlockDOM('![description](assets/sample.png "caption")');
+ const image = root.querySelector<HTMLImageElement>("img");
+ image.loading = "eager";
+ image.src = url;
+ if (legacy) {
+ image.style.width = "200px";
+ image.style.height = "80px";
+ image.setAttribute("width", "200");
+ image.setAttribute("height", "80");
+ image.closest<HTMLElement>(".img").style.width = "200px";
+ }
+ const original = lute.BlockDOM2StdMd(root.innerHTML);
+ await image.decode();
+ await settle();
+ check.equal(lute.BlockDOM2StdMd(root.innerHTML), original, "initializing controls does not alter image data");
+ return image;
+ };
+ const updates: {before: string, after: string}[] = [];
+ const start = new Function("getImageResizeWidth", "protyle", "target", "nodeElement", "event", "documentSelf",
+ "mostRight", "mostBottom", "y", "focusBlock", "updateTransaction", "img3115", resizeSource);
+ const begin = (image: HTMLImageElement, axis: string, event: MouseEvent) => {
+ const block = root.firstElementChild as HTMLElement;
+ const handle = image.parentElement.querySelector<HTMLElement>(`.protyle-block-resize[data-resize-axis="${axis}"]`);
+ start(api.getImageResizeWidth, {disabled: false}, handle, block, event, document,
+ root.getBoundingClientRect().right, 1000, event.clientY, () => {},
+ (_owner: unknown, node: HTMLElement, old: string) => updates.push({before: old, after: node.outerHTML}),
+ (container: HTMLElement) => {
+ if (container.style.minWidth) container.style.width = "";
+ else container.removeAttribute("style");
+ });
+ };
+ for (const legacy of [false, true]) {
+ for (const axis of ["width", "height", "both"]) {
+ const image = await create(legacy);
+ const block = root.firstElementChild as HTMLElement;
+ const before = block.outerHTML;
+ const frame = image.parentElement.querySelector<HTMLElement>(".protyle-image-resize");
+ check.equal(frame.querySelectorAll(".protyle-block-resize").length, 3);
+ const handle = frame.querySelector<HTMLElement>(`[data-resize-axis="${axis}"]`);
+ const width = image.clientWidth;
+ const height = width / 2;
+ const imageRect = image.getBoundingClientRect();
+ const frameRect = frame.getBoundingClientRect();
+ check.equal(frameRect.top, imageRect.top);
+ check.equal(frameRect.left, imageRect.left);
+ check.equal(frameRect.width, imageRect.width);
+ check.equal(frameRect.height, imageRect.height, "captions do not displace image handles");
+ const handleRect = handle.getBoundingClientRect();
+ check.equal(document.elementFromPoint(handleRect.left + handleRect.width / 2,
+ handleRect.top + handleRect.height / 2), handle, "external handles remain reachable");
+ check.equal(handle.dataset.preventSwipe, "true");
+ const caption = image.parentElement.querySelector<HTMLElement>(".protyle-action__title");
+ const captionRect = caption.getBoundingClientRect();
+ check.equal(document.elementFromPoint(captionRect.left + captionRect.width / 2,
+ captionRect.top + captionRect.height / 2).closest(".protyle-action__title"), caption,
+ "the bottom resize handle does not intercept caption editing");
+ const sourceWithControls = lute.BlockDOM2StdMd(block.outerHTML);
+ frame.remove();
+ check.equal(lute.BlockDOM2StdMd(block.outerHTML), sourceWithControls, "derived controls preserve serialized image source");
+ await settle();
+ const currentHandle = image.parentElement.querySelector<HTMLElement>(`.protyle-block-resize[data-resize-axis="${axis}"]`);
+ const dx = axis === "height" ? 0 : 20;
+ const dy = axis === "width" ? 0 : 10;
+ updates.length = 0;
+ start(api.getImageResizeWidth, {disabled: false}, currentHandle, block, {clientX: 300}, document,
+ root.getBoundingClientRect().right, 1000, 200, () => {},
+ (_owner: unknown, node: HTMLElement, old: string) => updates.push({before: old, after: node.outerHTML}),
+ (container: HTMLElement) => {
+ if (container.style.minWidth) container.style.width = "";
+ else container.removeAttribute("style");
+ });
+ document.onmousemove(new MouseEvent("mousemove", {clientX: 300 + dx, clientY: 200 + dy}));
+ document.onmouseup(new MouseEvent("mouseup"));
+ await settle();
+ check.equal(image.clientWidth, width + 20, `${axis} resizes the newly inserted image`);
+ check.equal(image.clientHeight, height + 10, `${axis} retains the natural aspect ratio`);
+ check.equal(updates.length, 1);
+ check.equal(lute.BlockDOM2StdMd(updates[0].before), lute.BlockDOM2StdMd(before), "undo preserves historical image styles");
+ check.equal(image.style.height, "");
+ check.equal(image.getAttribute("height"), null);
+ check.equal(image.closest(".img").classList.contains("img--drag"), false);
+ check.equal(currentHandle.classList.contains("touch-resize-active"), false);
+ const after = lute.BlockDOM2StdMd(block.outerHTML);
+ block.outerHTML = updates[0].before;
+ await settle();
+ check.equal(lute.BlockDOM2StdMd(root.innerHTML), sourceWithControls, "undo restores the image source");
+ root.innerHTML = updates[0].after;
+ await settle();
+ check.equal(lute.BlockDOM2StdMd(root.innerHTML), after, "redo restores the new width");
+ check.equal(root.querySelectorAll(".protyle-block-resize").length, 3, "replay does not duplicate controls");
+ }
+ }
+ const bridgeCore = {};
+ new Function("exports", coreSource)(bridgeCore);
+ const bridge = {} as {initTouchDragBridge: () => void};
+ new Function("exports", "require", bridgeSource)(bridge, (id: string) => id === "./touchDragBridgeCore" ? bridgeCore : {
+ Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 460, TIMEOUT_MOUSE_DRAG_DELAY: 150},
+ isInAndroid: () => false, isInHarmony: () => false, isMobile: () => false,
+ ipcRenderer: {on() {}}, stopScrollAnimation() {},
+ });
+ Object.assign(window, {siyuan: {touchDragActive: false}});
+ bridge.initTouchDragBridge();
+ for (const axis of ["width", "height", "both"]) {
+ const image = await create();
+ const width = image.clientWidth;
+ const handle = image.parentElement.querySelector<HTMLElement>(`.protyle-block-resize[data-resize-axis="${axis}"]`);
+ handle.addEventListener("mousedown", event => begin(image, axis, event));
+ const rect = handle.getBoundingClientRect();
+ const dx = axis === "height" ? 0 : 20;
+ const dy = axis === "width" ? 0 : 10;
+ const dispatch = (type: string, moved: boolean) => {
+ const touch = new Touch({identifier: 1, target: handle, clientX: rect.left + rect.width / 2 + (moved ? dx : 0),
+ clientY: rect.top + rect.height / 2 + (moved ? dy : 0), radiusX: 1, radiusY: 1});
+ const event = new TouchEvent(type, {bubbles: true, cancelable: true,
+ touches: type === "touchend" ? [] : [touch], changedTouches: [touch]});
+ handle.dispatchEvent(event);
+ return event;
+ };
+ updates.length = 0;
+ dispatch("touchstart", false);
+ check.equal(dispatch("touchmove", true).defaultPrevented, true);
+ dispatch("touchend", true);
+ check.equal(image.clientWidth, width + 20, `${axis} touch drag resizes the image`);
+ check.equal(image.clientHeight * 2, image.clientWidth);
+ check.equal(updates.length, 1);
+ check.equal(document.onmousemove, null);
+ }
+ const centered = await create();
+ root.firstElementChild.setAttribute("style", "text-align: center");
+ begin(centered, "width", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ document.onmousemove(new MouseEvent("mousemove", {clientX: 310, clientY: 200}));
+ document.onmouseup(new MouseEvent("mouseup"));
+ check.equal(centered.clientWidth, 220, "centered images expand symmetrically");
+ const zoomed = await create();
+ root.style.zoom = "1.5";
+ await settle();
+ begin(zoomed, "height", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ document.onmousemove(new MouseEvent("mousemove", {clientX: 300, clientY: 215}));
+ document.onmouseup(new MouseEvent("mouseup"));
+ check.equal(zoomed.clientWidth, 220, "vertical dragging accounts for editor scale");
+ const bounded = await create();
+ begin(bounded, "height", new MouseEvent("mousedown", {clientX: 300, clientY: 200}));
+ document.onmousemove(new MouseEvent("mousemove", {clientX: 300, clientY: 2000}));
+ document.onmouseup(new MouseEvent("mouseup"));
+ check.ok(bounded.getBoundingClientRect().right <= root.getBoundingClientRect().right + 1,
+ "vertical resizing respects the editor's right boundary");
+ const image = await create();
+ const frame = image.parentElement.querySelector<HTMLElement>(".protyle-image-resize");
+ const move = (x: number, y: number) => require("electron").ipcRenderer.invoke("image-resize-pointer", x, y);
+ await move(1, 1);
+ if (!matchMedia("(pointer: coarse)").matches) {
+ const opacity = () => Array.from(frame.querySelectorAll(".protyle-block-resize")).map(handle => getComputedStyle(handle).opacity);
+ check.deepEqual(opacity(), ["0", "0", "0"]);
+ const rect = image.getBoundingClientRect();
+ await move(rect.left + rect.width / 2, rect.top + rect.height / 2);
+ check.deepEqual(opacity(), ["1", "1", "1"], "mouse hover reveals the handles");
+ await move(1, 1);
+ check.deepEqual(opacity(), ["0", "0", "0"], "leaving the image hides the handles");
+ }
+ if (process.env.SIYUAN_IMAGE_RESIZE_PREVIEW) {
+ frame.querySelectorAll(".protyle-block-resize").forEach(handle => handle.classList.add("touch-resize-active"));
+ await settle();
+ const mode = matchMedia("(pointer: coarse)").matches ? "touch" : "desktop";
+ await require("electron").ipcRenderer.invoke("image-resize-preview", mode);
+ document.body.style.background = "#202124";
+ document.documentElement.style.setProperty("--b3-theme-on-surface", "#ddd");
+ await settle();
+ await require("electron").ipcRenderer.invoke("image-resize-preview", mode + "-dark");
+ document.body.style.background = "";
+ document.documentElement.style.setProperty("--b3-theme-on-surface", "#555");
+ frame.querySelectorAll(".protyle-block-resize").forEach(handle => handle.classList.remove("touch-resize-active"));
+ }
+ root.dataset.readonly = "true";
+ check.equal(getComputedStyle(frame).display, "none");
+ root.dataset.readonly = "false";
+ const preview = document.createElement("div");
+ preview.className = "protyle-wysiwyg__embed";
+ preview.innerHTML = lute.Md2BlockDOM("![preview](assets/sample.png)");
+ root.appendChild(preview);
+ await settle();
+ check.equal(preview.querySelector(".protyle-image-resize"), null);
+ root.style.zoom = "1.5";
+ await settle();
+ const rect = image.getBoundingClientRect();
+ const overlay = frame.getBoundingClientRect();
+ check.equal(overlay.width, rect.width);
+ check.equal(overlay.height, rect.height);
+ root.style.fontSize = "80px";
+ root.dir = "rtl";
+ await settle();
+ check.equal(frame.getBoundingClientRect().top, image.getBoundingClientRect().top, "large fonts retain vertical alignment");
+ check.equal(frame.getBoundingClientRect().left, image.getBoundingClientRect().left, "RTL retains image-edge alignment");
+ dispose();
+ root.innerHTML = lute.Md2BlockDOM("![after](assets/sample.png)");
+ await settle();
+ check.equal(root.querySelector(".protyle-image-resize"), null, "destroy disconnects image initialization");
+ root.remove();
+ return "Image resize cases passed";
+};
+
+test("image handles initialize on insertion and retain source, aspect ratio and replay compatibility", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 60000,
+}, async () => {
+ const transpile = (file: string) => transpileModule(readFileSync(file, "utf8").replace(/^export /gm, ""),
+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const source = createSourceFile("index.ts", readFileSync("src/protyle/wysiwyg/index.ts", "utf8"), ScriptTarget.ES2021, true);
+ let resize = "";
+ const visit = (node: import("typescript").Node) => {
+ if (isIfStatement(node) && node.expression.getText(source) === '!protyle.disabled && target.classList.contains("protyle-action__drag")') {
+ resize = node.getText(source);
+ }
+ node.forEachChild(visit);
+ };
+ visit(source);
+ assert.ok(resize);
+ const resizeSource = transpileModule(`function resize() { ${resize} }`, {compilerOptions: {target: ScriptTarget.ES2021}})
+ .outputText + "\nreturn resize.call(this);";
+ const css = compileString('@use "protyle/protyle"; @use "component/typography"; @use "business/resize";',
+ {loadPaths: ["src/assets/scss"], logger: {warn() {}, debug() {}}}).css;
+ const mobileCSS = compileString('@use "main/mobile";',
+ {loadPaths: ["src/assets/scss"], logger: {warn() {}, debug() {}}}).css;
+ const bridgeSources = ["src/util/touchDragBridge.ts", "src/util/touchDragBridgeCore.ts"].map(file =>
+ transpileModule(readFileSync(file, "utf8"), {compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021}}).outputText);
+ const makeCode = (styles: string) => "const __name = value => value; (" + browserCases.toString() + ")(" +
+ [transpile("src/protyle/render/imageResize.ts"), resizeSource,
+ readFileSync("stage/protyle/js/lute/lute.min.js", "utf8"), styles, ...bridgeSources]
+ .map(value => JSON.stringify(value)).join(",") + ")";
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-image-resize-test-"));
+ const script = path.join(temporary, "run.cjs");
+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");
+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
+app.whenReady().then(async () => {
+ const win = new BrowserWindow({show: false, width: 900, height: 700,
+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});
+ win.webContents.setBackgroundThrottling(false);
+ win.webContents.debugger.attach("1.3");
+ require("electron").ipcMain.handle("image-resize-pointer", (_event, x, y) =>
+ win.webContents.debugger.sendCommand("Input.dispatchMouseEvent", {type: "mouseMoved", x, y}));
+ require("electron").ipcMain.handle("image-resize-preview", async (_event, mode) => {
+ require("fs").writeFileSync(process.env.SIYUAN_IMAGE_RESIZE_PREVIEW + "-" + mode + ".png", (await win.webContents.capturePage()).toPNG());
+ });
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(makeCode(css))}));
+ await win.webContents.debugger.sendCommand("Emulation.setTouchEmulationEnabled", {enabled: true});
+ win.setSize(390, 700);
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(makeCode(css + mobileCSS))}));
+ app.exit(0);
+ } catch (error) { console.error(error); app.exit(1); }
+});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron") as unknown as string, [script], {env, windowsHide: true, timeout: 55000});
+ assert.match(result.stdout, /Image resize cases passed/);
+ } finally {
+ if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) && path.basename(temporary).startsWith("siyuan-image-resize-test-")) {
+ rmSync(temporary, {recursive: true, force: true, maxRetries: 20, retryDelay: 100});
+ }
+ }
+});
diff --git a/app/src/protyle/render/imageResize.ts b/app/src/protyle/render/imageResize.ts
new file mode 100644
index 0000000000..25ff83e596
--- /dev/null
+++ b/app/src/protyle/render/imageResize.ts
@@ -0,0 +1,120 @@
+export const getImageResizeWidth = (width: number, height: number, deltaX: number, deltaY: number,
+ axis: string, multiple: number, maxWidth: number) => {
+ const ratio = width > 0 && height > 0 ? width / height : 1;
+ let delta = deltaX * multiple;
+ if (axis === "height") {
+ delta = deltaY * ratio;
+ } else if (axis === "both") {
+ // 将拖动位置投影到等比缩放方向,横向与纵向移动都能调整右下角。
+ delta = (deltaX / multiple + deltaY / ratio) / (1 / (multiple * multiple) + 1 / (ratio * ratio));
+ }
+ return Math.max(17, Math.min(maxWidth, width + delta));
+};
+
+export const bindImageResize = (root: HTMLElement) => {
+ const images = new Map<HTMLImageElement, {frame: HTMLElement, container: HTMLElement}>();
+ const position = (image: HTMLImageElement, frame: HTMLElement) => {
+ frame.style.left = image.offsetLeft + "px";
+ frame.style.top = image.offsetTop + "px";
+ frame.style.width = image.clientWidth + "px";
+ frame.style.height = image.clientHeight + "px";
+ };
+ const resizeObserver = new ResizeObserver(entries => {
+ entries.forEach(entry => {
+ const image = entry.target instanceof HTMLImageElement ? entry.target :
+ entry.target.querySelector<HTMLImageElement>(":scope > img");
+ const state = images.get(image);
+ if (state) {
+ position(image, state.frame);
+ }
+ });
+ });
+ const render = (image: HTMLImageElement) => {
+ if (image.closest(".protyle-wysiwyg") !== root ||
+ image.closest(".protyle-wysiwyg__embed, .mindmap-view__preview-block")) {
+ return;
+ }
+ const container = image.parentElement;
+ if (container.parentElement?.getAttribute("data-type") !== "img") {
+ return;
+ }
+ container.classList.add("protyle-image-container");
+ let firstCaption = true;
+ container.querySelectorAll<HTMLElement>(":scope > .protyle-action__title, :scope > .img__alt").forEach(caption => {
+ const visible = !!caption.textContent;
+ caption.classList.toggle("protyle-image-caption", visible && firstCaption);
+ if (visible) {
+ firstCaption = false;
+ }
+ });
+ let frame = container.querySelector<HTMLElement>(":scope > .protyle-action__drag:not(.protyle-block-resize)");
+ if (!frame) {
+ frame = image.ownerDocument.createElement("span");
+ container.appendChild(frame);
+ }
+ frame.className = "protyle-action__drag protyle-image-resize";
+ frame.contentEditable = "false";
+ for (const axis of ["width", "height", "both"]) {
+ if (frame.querySelector(`[data-resize-axis="${axis}"]`)) {
+ continue;
+ }
+ const handle = image.ownerDocument.createElement("span");
+ handle.className = "protyle-action__drag protyle-block-resize";
+ handle.dataset.resizeAxis = axis;
+ handle.setAttribute("data-prevent-swipe", "true");
+ frame.appendChild(handle);
+ }
+ const previous = images.get(image);
+ if (!previous) {
+ resizeObserver.observe(image);
+ }
+ if (previous?.container !== container) {
+ if (previous) {
+ resizeObserver.unobserve(previous.container);
+ }
+ resizeObserver.observe(container);
+ }
+ images.set(image, {frame, container});
+ position(image, frame);
+ };
+ const renderAdded = (element: Element) => {
+ if (element.matches(".img img")) {
+ render(element as HTMLImageElement);
+ }
+ element.querySelectorAll<HTMLImageElement>(".img img").forEach(render);
+ };
+ const observer = new MutationObserver(records => {
+ const remove = (image: HTMLImageElement) => {
+ const state = images.get(image);
+ if (state && !root.contains(image)) {
+ resizeObserver.unobserve(image);
+ resizeObserver.unobserve(state.container);
+ images.delete(image);
+ }
+ };
+ records.forEach(record => {
+ const parent = record.target instanceof Element ? record.target.closest(".img") : null;
+ parent?.querySelectorAll<HTMLImageElement>("img").forEach(render);
+ record.addedNodes.forEach(node => {
+ if (node instanceof Element) {
+ renderAdded(node);
+ }
+ });
+ record.removedNodes.forEach(node => {
+ if (node instanceof HTMLImageElement) {
+ remove(node);
+ } else if (node instanceof Element) {
+ node.querySelectorAll<HTMLImageElement>("img").forEach(remove);
+ }
+ });
+ });
+ });
+ // 在图片插入、替换和撤销后初始化派生控件,不依赖图片的具体插入入口。
+ observer.observe(root, {childList: true, subtree: true});
+ renderAdded(root);
+ return () => {
+ observer.disconnect();
+ resizeObserver.disconnect();
+ images.clear();
+ };
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 2f89e4969a..4f4df50160 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -247,6 +247,7 @@ import {forEachPluginSubscriber} from "../../plugin/EventBusCore";
import {areProtylePluginExtensionsEnabled} from "../runtimeCapabilities";
import {syncRootAttributes} from "../util/syncRootAttributes";
import {bindImageTitleEditor} from "../render/imageTitleEditor";
+import {bindImageResize, getImageResizeWidth} from "../render/imageResize";
import {isDirectCalloutStructureClick} from "./calloutClick";
import {
BLOCK_SELECTION_CLASS,
@@ -422,6 +423,7 @@ export class WYSIWYG {
private disposeTouchCaret?: () => void;
private disposeListMarkerCaret?: () => void;
private imageTitleEditor: ReturnType<typeof bindImageTitleEditor>;
+ private disposeImageResize?: () => void;
private scheduleInput(callback: () => void | Promise<void>, delay = 0, replace = true) {
if (replace && this.inputTimeout) {
@@ -552,6 +554,7 @@ export class WYSIWYG {
}
this.bindCommonEvent(protyle);
this.imageTitleEditor = bindImageTitleEditor(protyle, this.element);
+ this.disposeImageResize = bindImageResize(this.element);
this.bindEvent(protyle);
this.disposeListMarkerCaret = bindListMarkerCaret(this.element, () => !protyle.disabled &&
!protyle.toolbar.isMultiSelectMode() && !window.siyuan.touchDragActive &&
@@ -603,6 +606,7 @@ export class WYSIWYG {
public destroy() {
this.disposeFocusMemory?.();
this.imageTitleEditor?.destroy();
+ this.disposeImageResize?.();
this.persistComposition?.();
this.persistComposition = undefined;
this.disposeHeadingFoldIndicators?.();
@@ -1992,7 +1996,6 @@ export class WYSIWYG {
}
const html = nodeElement.outerHTML;
const isIFrame = ["NodeIFrame", "NodeWidget"].includes(nodeElement.dataset.type);
- const resizeAxis = isIFrame ? target.dataset.resizeAxis || "both" : "both";
let isCenter = true;
if ("NodeVideo" === nodeElement.dataset.type) {
nodeElement.classList.add("iframe--drag");
@@ -2004,36 +2007,53 @@ export class WYSIWYG {
if (!nodeElement.style.margin) {
isCenter = false;
}
- } else if (target.parentElement.parentElement.getAttribute("data-type") === "img") {
- target.parentElement.parentElement.classList.add("img--drag");
}
const x = event.clientX;
+ const imageFrame = target.closest(".protyle-image-resize");
const dragElement = isIFrame ? nodeElement.querySelector<HTMLElement>(":scope > .iframe-content > iframe") :
- target.previousElementSibling as HTMLElement;
+ imageFrame ? imageFrame.parentElement.querySelector<HTMLImageElement>(":scope > img") :
+ target.previousElementSibling as HTMLElement;
if (!dragElement) {
nodeElement.classList.remove("iframe--drag");
return;
}
const dragWidth = dragElement.clientWidth;
const dragHeight = dragElement.clientHeight;
+ const resizeAxis = isIFrame || dragElement.tagName === "IMG" ?
+ target.dataset.resizeAxis || (isIFrame ? "both" : "width") : "both";
if (target.classList.contains("protyle-block-resize")) {
target.classList.add("touch-resize-active");
}
const imgElement = dragElement.parentElement.parentElement;
+ const imageRect = dragElement.getBoundingClientRect();
+ const imageScale = imageRect.width / dragWidth || 1;
+ const imageMultiple = !imgElement.style.minWidth && nodeElement.style.textAlign !== "center" ? 1 : 2;
+ const imageMaxWidth = Math.max(17, (mostRight - imageRect.left -
+ (imageMultiple === 2 ? imageRect.width / 2 : 0)) * imageMultiple / imageScale);
if (dragElement.tagName === "IMG") {
+ imgElement.classList.add("img--drag");
img3115(imgElement);
}
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
if (dragElement.tagName === "IMG") {
+ const image = dragElement as HTMLImageElement;
+ const height = image.naturalWidth && image.naturalHeight ?
+ dragWidth * image.naturalHeight / image.naturalWidth : dragHeight;
+ const width = getImageResizeWidth(dragWidth, height,
+ (moveEvent.clientX - x) / imageScale, (moveEvent.clientY - y) / imageScale,
+ resizeAxis, imageMultiple, imageMaxWidth);
+ dragElement.style.width = "";
dragElement.style.height = "";
+ dragElement.removeAttribute("width");
+ dragElement.removeAttribute("height");
+ dragElement.parentElement.style.width = width + "px";
+ return;
}
if (resizeAxis !== "height" && moveEvent.clientX > x - dragWidth + 8 && moveEvent.clientX < mostRight) {
- const multiple = ((dragElement.tagName === "IMG" && !imgElement.style.minWidth && nodeElement.style.textAlign !== "center") || !isCenter) ? 1 : 2;
- if (dragElement.tagName === "IMG") {
- dragElement.parentElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x) * multiple) + "px";
- } else if (dragElement.tagName === "IFRAME") {
+ const multiple = isCenter ? 2 : 1;
+ if (dragElement.tagName === "IFRAME") {
// 只清理当前轴的历史尺寸,另一轴继续沿用原数据。
dragElement.style.width = "";
dragElement.removeAttribute("width");
@@ -2042,7 +2062,7 @@ export class WYSIWYG {
dragElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x) * multiple) + "px";
}
}
- if (resizeAxis !== "width" && dragElement.tagName !== "IMG") {
+ if (resizeAxis !== "width") {
if (moveEvent.clientY > y - dragHeight + 8 && moveEvent.clientY < mostBottom) {
if (dragElement.tagName === "IFRAME") {
dragElement.style.height = "";
@@ -2063,7 +2083,7 @@ export class WYSIWYG {
documentSelf.onselect = null;
nodeElement.classList.remove("iframe--drag");
target.classList.remove("touch-resize-active");
- target.parentElement.parentElement.classList.remove("img--drag");
+ imgElement.classList.remove("img--drag");
if (target.classList.contains("protyle-action__drag") && nodeElement) {
focusBlock(nodeElement);
updateTransaction(protyle, nodeElement, html);