clipboard reccursion fix

This commit is contained in:
Nystik
2026-06-02 16:58:01 +02:00
parent 35118ca190
commit 3833ef2668
3 changed files with 70 additions and 10 deletions

View File

@@ -1,10 +1,18 @@
import { getClipboard } from "./native-clipboard.js";
export const clipboardShim = {
readText() {
return "";
},
writeText(text) {
navigator.clipboard.writeText(text).catch((e) => {
const clip = getClipboard();
if (!clip) {
return;
}
clip.writeText(text).catch((e) => {
console.warn("[shim:clipboard] writeText failed:", e);
});
},
@@ -14,7 +22,13 @@ export const clipboardShim = {
},
writeHTML(html) {
navigator.clipboard
const clip = getClipboard();
if (!clip) {
return;
}
clip
.write([
new ClipboardItem({
"text/html": new Blob([html], { type: "text/html" }),
@@ -35,6 +49,12 @@ export const clipboardShim = {
return;
}
const clip = getClipboard();
if (!clip) {
return;
}
const pngData = image.toPNG();
if (!pngData || pngData.length === 0) {
@@ -43,11 +63,9 @@ export const clipboardShim = {
const blob = new Blob([pngData], { type: "image/png" });
navigator.clipboard
.write([new ClipboardItem({ "image/png": blob })])
.catch((e) => {
console.warn("[shim:clipboard] writeImage failed:", e);
});
clip.write([new ClipboardItem({ "image/png": blob })]).catch((e) => {
console.warn("[shim:clipboard] writeImage failed:", e);
});
},
has(format) {
@@ -59,6 +77,12 @@ export const clipboardShim = {
},
clear() {
navigator.clipboard.writeText("").catch(() => {});
const clip = getClipboard();
if (!clip) {
return;
}
clip.writeText("").catch(() => {});
},
};

View File

@@ -0,0 +1,22 @@
// Obsidian points navigator.clipboard.writeText at electron.clipboard, which already points at this shim.
// To avoid recursion, use the untouched native prototype methods.
const proto = typeof Clipboard !== "undefined" ? Clipboard.prototype : null;
// Returns a native-backed clipboard facade, or null in insecure (non-localhost http) contexts.
export function getClipboard() {
const clip =
typeof navigator !== "undefined" ? navigator.clipboard : undefined;
if (!proto || !clip) {
console.warn(
"[shim:clipboard] clipboard API unavailable (insecure context?)",
);
return null;
}
return {
writeText: (text) => proto.writeText.call(clip, text),
write: (items) => proto.write.call(clip, items),
read: () => proto.read.call(clip),
};
}

View File

@@ -1,3 +1,5 @@
import { getClipboard } from "./native-clipboard.js";
const currentWindowState = {
title: "Obsidian",
isMaximized: false,
@@ -196,7 +198,13 @@ const currentWebContents = {
document.execCommand("copy");
},
paste() {
navigator.clipboard
const clip = getClipboard();
if (!clip) {
return;
}
clip
.read()
.then(async (items) => {
const dt = new DataTransfer();
@@ -233,7 +241,13 @@ const currentWebContents = {
});
},
pasteAndMatchStyle() {
navigator.clipboard
const clip = getClipboard();
if (!clip) {
return;
}
clip
.read()
.then(async (items) => {
for (const item of items) {