Skip to content

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

:sparkles: Support rich clipboard copying in export preview https://github.com/siyuan-note/siyuan/issues/18377

Danield5c9b96bec20
diff --git a/app/src/protyle/preview/index.ts b/app/src/protyle/preview/index.ts
index ab9b377a0f..e4b1cde16e 100644
--- a/app/src/protyle/preview/index.ts
+++ b/app/src/protyle/preview/index.ts
@@ -11,6 +11,7 @@ import {Constants} from "../../constants";
import {getSearch, isMobile} from "../../util/functions";
/// #if !BROWSER
import {shell} from "electron";
+import {enhanceRichClipboard, hasRichClipboardImages} from "../util/richClipboard";
/// #endif
/// #if !MOBILE
import {openAsset, openBy} from "../../editor/util";
@@ -29,6 +30,8 @@ export class Preview {
public element: HTMLElement;
public previewElement: HTMLElement;
private mdTimeoutId: number;
+ private copyingToX = false;
+ private copyEventHandler?: (event: ClipboardEvent) => void;
constructor(protyle: IProtyle) {
this.element = document.createElement("div");
@@ -74,6 +77,42 @@ export class Preview {
this.element.appendChild(actionElement);
this.element.appendChild(previewElement);
+ /// #if !BROWSER
+ this.copyEventHandler = (event: ClipboardEvent) => {
+ if (this.copyingToX || !event.clipboardData) {
+ return;
+ }
+
+ const selection = window.getSelection();
+ if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
+ return;
+ }
+ const range = selection.getRangeAt(0);
+ if (!previewElement.contains(range.startContainer) || !previewElement.contains(range.endContainer)) {
+ return;
+ }
+
+ const copyElement = document.createElement("div");
+ copyElement.appendChild(range.cloneContents());
+ const copiedHTML = copyElement.innerHTML;
+ if (!hasRichClipboardImages(copiedHTML)) {
+ return;
+ }
+
+ const marker = `<!--siyuan-rich-clipboard='${Lute.NewNodeID()}'-->`;
+ const text = selection.toString();
+ const html = marker + copiedHTML;
+ event.preventDefault();
+ event.clipboardData.setData("text/plain", text);
+ event.clipboardData.setData("text/html", html);
+ enhanceRichClipboard(text, html, protyle.notebookId, {
+ marker,
+ removeMarker: true,
+ });
+ };
+ document.addEventListener("copy", this.copyEventHandler);
+ /// #endif
+
this.element.addEventListener("click", (event) => {
let target = event.target as HTMLElement;
while (target && !target.isEqualNode(this.element)) {
@@ -183,6 +222,16 @@ export class Preview {
this.previewElement = previewElement;
}
+ public destroy() {
+ window.clearTimeout(this.mdTimeoutId);
+ /// #if !BROWSER
+ if (this.copyEventHandler) {
+ document.removeEventListener("copy", this.copyEventHandler);
+ this.copyEventHandler = undefined;
+ }
+ /// #endif
+ }
+
public updatePadding(padding: { left: number, right: number, bottom: number, top: number }) {
if (!this.element.classList.contains("fn__none") &&
this.element.querySelector('.protyle-preview__action [data-type="desktop"]')?.classList.contains("protyle-preview__action--current")) {
@@ -345,7 +394,12 @@ export class Preview {
range.setEndAfter(copyElement.lastElementChild);
}
focusByRange(range);
- document.execCommand("copy");
+ this.copyingToX = true;
+ try {
+ document.execCommand("copy");
+ } finally {
+ this.copyingToX = false;
+ }
this.element.lastElementChild.remove();
focusByRange(cloneRange);
if (type) {
diff --git a/app/src/protyle/util/destroy.ts b/app/src/protyle/util/destroy.ts
index a635e3ab75..60ab103ef3 100644
--- a/app/src/protyle/util/destroy.ts
+++ b/app/src/protyle/util/destroy.ts
@@ -6,6 +6,7 @@ export const destroy = (protyle: IProtyle) => {
return;
}
hideElements(["util"], protyle);
+ protyle.preview?.destroy();
if (isSupportCSSHL()) {
protyle.highlight.markHL.clear();
protyle.highlight.mark.clear();
diff --git a/app/src/protyle/util/richClipboard.ts b/app/src/protyle/util/richClipboard.ts
index 536587e211..09b61af1a3 100644
--- a/app/src/protyle/util/richClipboard.ts
+++ b/app/src/protyle/util/richClipboard.ts
@@ -19,6 +19,11 @@ interface IRichClipboardPrepared {
}[];
}
+interface IRichClipboardOptions {
+ marker?: string;
+ removeMarker?: boolean;
+}
+
const richClipboardImageExts = new Set([
"apng",
"avif",
@@ -105,7 +110,13 @@ const getRichClipboardSources = (template: HTMLTemplateElement, notebookID: stri
return sources;
};
-export const enhanceRichClipboard = (text: string, html: string, notebookID: string) => {
+export const hasRichClipboardImages = (html: string) => {
+ const template = document.createElement("template");
+ template.innerHTML = html;
+ return getRichClipboardSources(template, "").length > 0;
+};
+
+export const enhanceRichClipboard = (text: string, html: string, notebookID: string, options: IRichClipboardOptions = {}) => {
/// #if !BROWSER
window.setTimeout(async () => {
const template = document.createElement("template");
@@ -115,7 +126,7 @@ export const enhanceRichClipboard = (text: string, html: string, notebookID: str
return;
}
- const marker = html.match(/<!--data-siyuan='[^']+'-->/)?.[0];
+ const marker = options.marker || html.match(/<!--data-siyuan='[^']+'-->/)?.[0];
if (!marker) {
return;
}
@@ -191,11 +202,15 @@ export const enhanceRichClipboard = (text: string, html: string, notebookID: str
}
try {
+ let clipboardHTML = template.innerHTML;
+ if (options.removeMarker) {
+ clipboardHTML = clipboardHTML.replace(marker, "");
+ }
const written = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
cmd: "completeRichClipboard",
token,
text,
- html: template.innerHTML,
+ html: clipboardHTML,
replacements,
});
if (!written) {