An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support copying files from the image viewer https://github.com/siyuan-note/siyuan/issues/19151
Daniel9bf5e830e90a
diff --git a/app/src/assets/scss/viewerjs/_viewer.scss b/app/src/assets/scss/viewerjs/_viewer.scssindex 02b75bbd6f..3b042cebff 100644--- a/app/src/assets/scss/viewerjs/_viewer.scss+++ b/app/src/assets/scss/viewerjs/_viewer.scss@@ -13,6 +13,7 @@&-fullscreen,&-fullscreen-exit,&-copy,+ &-copy-file,&-close {svg {height: 24px;diff --git a/app/src/protyle/preview/image.test.ts b/app/src/protyle/preview/image.test.tsindex e411949d24..cd3f83f484 100644--- a/app/src/protyle/preview/image.test.ts+++ b/app/src/protyle/preview/image.test.ts@@ -6,17 +6,28 @@ import {ModuleKind, ScriptTarget, transpileModule} from "typescript";test("image preview initializes its toolbar when ready is dispatched on the source element", async () => {const actions = ["zoom-in", "zoom-out", "one-to-one", "reset", "prev", "play", "next",- "rotate-left", "rotate-right", "flip-horizontal", "flip-vertical", "copy", "close"];+ "rotate-left", "rotate-right", "flip-horizontal", "flip-vertical", "copy", "copy-file", "close"];+ class Classes extends Set<string> {+ public toggle(name: string, force: boolean) {+ if (force) {+ this.add(name);+ } else {+ this.delete(name);+ }+ }+ }const buttons = new Map(actions.map(action => {const attributes = new Map<string, string>();return [`.viewer-${action}`, {innerHTML: "",attributes,- classList: new Set<string>(),+ classList: new Classes(),setAttribute: (name: string, value: string) => attributes.set(name, value),}];}));const copied: string[] = [];+ const copiedFiles: string[] = [];+ let supportsFiles = true;const sourceElement = new EventTarget();class TestViewer {public viewed = true;@@ -30,7 +41,9 @@ test("image preview initializes its toolbar when ready is dispatched on the sourconstructor(element: EventTarget, public options: {ready: EventListener,- toolbar: {copy: () => void},+ view: () => void,+ viewed: () => void,+ toolbar: {copy: () => void, copyFile: () => void},}) {element.addEventListener("ready", options.ready);}@@ -46,22 +59,29 @@ test("image preview initializes its toolbar when ready is dispatched on the sour}).outputText;const exports = {} as {previewImages: (sources: string[]) => void};const languages = JSON.parse(readFileSync("appearance/langs/en.json", "utf8"));- const previewWindow = {siyuan: {languages, viewer: undefined as TestViewer}};+ const previewWindow = {+ siyuan: {languages, viewer: undefined as TestViewer},+ location: {href: "http://localhost:6806/stage/build/app/", origin: "http://localhost:6806"},+ };runInNewContext(compiled, {exports,require: () => ({Constants: {PROTYLE_CDN: ""},addScript: () => Promise.resolve(),copyPNGByLink: (src: string) => copied.push(src),+ writeAssetToClipboard: (src: string) => ({ignore: !supportsFiles, click: () => copiedFiles.push(src)}),+ isEncryptedBox: (box: string) => box === "encrypted",}),document: {createElement: () => sourceElement},window: previewWindow,Viewer: TestViewer,+ URL,});exports.previewImages(["blob:first", "blob:second"]);await Promise.resolve();const instance = previewWindow.siyuan.viewer;- assert.match(buttons.get(".viewer-copy").innerHTML, /#iconCopy/);+ assert.match(buttons.get(".viewer-copy").innerHTML, /#iconImage/);+ assert.match(buttons.get(".viewer-copy-file").innerHTML, /#iconFile/);assert.equal(buttons.get(".viewer-copy").attributes.get("aria-label"), "Copy as PNG");buttons.forEach(button => {assert.ok(button.classList.has("ariaLabel"));@@ -73,4 +93,25 @@ test("image preview initializes its toolbar when ready is dispatched on the sourinstance.image.src = "blob:second";instance.options.toolbar.copy();assert.deepEqual(copied, ["blob:first", "blob:second"]);+ const fileButton = buttons.get(".viewer-copy-file");+ for (const src of ["blob:test", "data:image/png;base64,test", "https://example.com/assets/test.png",+ "http://localhost:6806/assets/test.png?box=encrypted", "http://localhost:6806/emojis/test.png"]) {+ instance.image.src = src;+ instance.options.viewed();+ assert.ok(fileButton.classList.has("fn__none"));+ instance.options.toolbar.copyFile();+ }+ assert.deepEqual(copiedFiles, []);+ instance.image.src = "http://localhost:6806/assets/a%20b.gif?box=plain&style=thumb#image";+ instance.options.viewed();+ assert.equal(fileButton.classList.has("fn__none"), false);+ instance.options.toolbar.copyFile();+ assert.deepEqual(copiedFiles, ["assets/a b.gif?box=plain"]);+ instance.options.view();+ assert.ok(fileButton.classList.has("fn__none"));+ supportsFiles = false;+ instance.options.viewed();+ assert.ok(fileButton.classList.has("fn__none"));+ instance.options.toolbar.copyFile();+ assert.equal(copiedFiles.length, 1);});diff --git a/app/src/protyle/preview/image.ts b/app/src/protyle/preview/image.tsindex 341d667a78..c8081992c5 100644--- a/app/src/protyle/preview/image.ts+++ b/app/src/protyle/preview/image.ts@@ -2,7 +2,25 @@ import {Constants} from "../../constants";import {addScript} from "../util/addScript";import {fetchPost} from "../../util/fetch";import {isBrowserRenderableImagePath} from "../../util/imageURL";-import {copyPNGByLink} from "../../menus/util";+import {copyPNGByLink, writeAssetToClipboard} from "../../menus/util";+import {isEncryptedBox} from "../../util/pathName";++const getCopyFileMenu = (src: string) => {+ try {+ const url = new URL(src, window.location.href);+ const path = decodeURIComponent(url.pathname);+ const box = url.searchParams.get("box");+ if (url.origin !== window.location.origin || !["http:", "https:"].includes(url.protocol) ||+ !path.startsWith("/assets/") || path.includes("\\") || path.split("/").includes("..") ||+ isEncryptedBox(box)) {+ return;+ }+ const menu = writeAssetToClipboard(path.substring(1) + (box ? `?box=${encodeURIComponent(box)}` : ""));+ return menu.ignore ? undefined : menu;+ } catch {+ return;+ }+};export const previewImages = (srcList: string[], currentSrc?: string, onHidden?: () => void) => {addScript(`${Constants.PROTYLE_CDN}/js/viewerjs/viewer.js?v=1.11.8`, "protyleViewerScript").then(() => {@@ -40,7 +58,10 @@ export const previewImages = (srcList: string[], currentSrc?: string, onHidden?:transition: false,ready: () => {const copyElement = viewer.toolbar.querySelector(".viewer-copy");- copyElement.innerHTML = '<svg><use xlink:href="#iconCopy"></use></svg>';+ copyElement.innerHTML = '<svg><use xlink:href="#iconImage"></use></svg>';+ const copyFileElement = viewer.toolbar.querySelector(".viewer-copy-file");+ copyFileElement.innerHTML = '<svg><use xlink:href="#iconFile"></use></svg>';+ copyFileElement.classList.add("fn__none");const languages = window.siyuan.languages;const labels: Record<string, string> = {"zoom-in": languages.zoomIn,@@ -55,6 +76,7 @@ export const previewImages = (srcList: string[], currentSrc?: string, onHidden?:"flip-horizontal": languages.imageFlipHorizontal,"flip-vertical": languages.imageFlipVertical,copy: languages.copyAsPNG,+ "copy-file": languages.copyFile,close: languages.close,};Object.entries(labels).forEach(([action, label]) => {@@ -65,6 +87,12 @@ export const previewImages = (srcList: string[], currentSrc?: string, onHidden?:});},hidden: close,+ view: () => {+ viewer.toolbar.querySelector(".viewer-copy-file").classList.add("fn__none");+ },+ viewed: () => {+ viewer.toolbar.querySelector(".viewer-copy-file").classList.toggle("fn__none", !getCopyFileMenu(viewer.image.src));+ },toolbar: {zoomIn: true,zoomOut: true,@@ -82,6 +110,11 @@ export const previewImages = (srcList: string[], currentSrc?: string, onHidden?:copyPNGByLink(viewer.image.src);}},+ copyFile: () => {+ if (viewer.viewed && viewer.image) {+ getCopyFileMenu(viewer.image.src)?.click();+ }+ },close,},});diff --git a/app/src/types/protyle.d.ts b/app/src/types/protyle.d.tsindex 978195a4d3..2ff8ea3750 100644--- a/app/src/types/protyle.d.ts+++ b/app/src/types/protyle.d.ts@@ -153,6 +153,8 @@ declare class Viewer {transition: boolean,hidden: () => void,ready?: (this: HTMLElement, event: CustomEvent) => void,+ view?: (this: HTMLElement, event: CustomEvent) => void,+ viewed?: (this: HTMLElement, event: CustomEvent) => void,toolbar: {zoomIn: boolean,zoomOut: boolean,@@ -166,6 +168,7 @@ declare class Viewer {flipHorizontal: boolean,flipVertical: boolean,copy?: () => void,+ copyFile?: () => void,close: () => void}})