Skip to content

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

:bug: Prefer table data over bitmap previews when pasting into databases https://github.com/siyuan-note/siyuan/issues/10767

FelixJI256b94d953b4
diff --git a/app/src/protyle/util/paste.test.ts b/app/src/protyle/util/paste.test.ts
index a79d3c6405..595e2e3d8c 100644
--- a/app/src/protyle/util/paste.test.ts
+++ b/app/src/protyle/util/paste.test.ts
@@ -164,6 +164,35 @@ describe("strip pasted IAL data attributes", () => {
});
});
+describe("shouldPasteClipboardImage", () => {
+ const harness = createHarness();
+ const excelTableWithImage = "<table border=0 cellpadding=0 cellspacing=0 width=120 style='border-collapse:collapse'>" +
+ "<tr height=18><td>Cell A</td><td><img src=\"file:///C:/Temp/msohtmlclip1/01/clip_image002.png\" width=48 height=18></td></tr></table>";
+
+ it("keeps table data for database grid pastes when a bitmap preview coexists", () => {
+ assert.equal(harness.api.shouldPasteClipboardImage(excelTableWithImage, true), false);
+ });
+
+ it("keeps table data for database grid pastes without embedded images", () => {
+ assert.equal(harness.api.shouldPasteClipboardImage("<table><tr><td>a\tb</td></tr></table>", true), false);
+ });
+
+ it("still prefers the image outside database targets", () => {
+ assert.equal(harness.api.shouldPasteClipboardImage(excelTableWithImage, false), true);
+ });
+
+ it("uploads single clipboard images even for database targets", () => {
+ assert.equal(harness.api.shouldPasteClipboardImage("<img src=\"data:image/png;base64,xxx\">", true), true);
+ assert.equal(harness.api.shouldPasteClipboardImage("<img src=\"data:image/png;base64,xxx\">", false), true);
+ });
+
+ it("does not treat non-image tables as clipboard images", () => {
+ assert.equal(harness.api.shouldPasteClipboardImage("<table><tr><td>plain</td></tr></table>", false), false);
+ assert.equal(harness.api.shouldPasteClipboardImage("<div><p>text</p></div>", false), false);
+ assert.equal(harness.api.shouldPasteClipboardImage("", true), false);
+ });
+});
+
describe("restricted cell selected text paste", () => {
if (typeof Lute === "undefined") {
require("../../../stage/protyle/js/lute/lute.min.js");
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index e5a9ba5093..0974b46f8a 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -695,6 +695,16 @@ const insertConvertedBlockDOM = (protyle: IProtyle, dom: string, range: Range) =
scrollCenter(protyle, undefined, "nearest", "smooth");
};
+// 位图预览文件与网页内容并存时判断是否按图片粘贴:浏览器复制的单图或含图表格优先上传图片;
+// 数据库网格粘贴目标携带表格数据时不降级为图片,Excel 单元格数据始终按表格解析写入
+export const shouldPasteClipboardImage = (textHTML: string, isAVTarget: boolean) => {
+ if (isAVTarget && /<table[\s>]/i.test(textHTML)) {
+ return false;
+ }
+ return textHTML.startsWith("<img") || // 浏览器上复制单个图片
+ (textHTML.startsWith("<table") && textHTML.indexOf("<img") > -1); // Excel 或者浏览器中复制带有图片的表格
+};
+
export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent | IClipboardData) & {
target: HTMLElement
}, uploadOptions?: IUploadInsertOptions) => {
@@ -1318,10 +1328,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
let isHTML = false;
if (textHTML.replace("<!--StartFragment--><!--EndFragment-->", "").trim() !== "") {
textHTML = textHTML.replace("<!--StartFragment-->", "").replace("<!--EndFragment-->", "").trim();
- if (!officeListConverted && files && files.length === 1 && (
- textHTML.startsWith("<img") || // 浏览器上复制单个图片
- (textHTML.startsWith("<table") && textHTML.indexOf("<img") > -1) // Excel 或者浏览器中复制带有图片的表格
- )) {
+ if (!officeListConverted && files && files.length === 1 &&
+ shouldPasteClipboardImage(textHTML, nodeElement.classList.contains("av"))) {
isHTML = false;
} else {
// 需注意 Edge 中的划选不应识别为图片 https://github.com/siyuan-note/siyuan/issues/7021