Skip to content

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

:bug: Allow undoing pasted database asset values https://github.com/siyuan-note/siyuan/issues/18624

Daniel1fdbf41dc0f5
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index 59d1543d0d..a7f0a2d2ba 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -23,6 +23,7 @@ import {getCompressURL, removeCompressURL} from "../../../util/image";
import {callMobileAppShowKeyboard} from "../../../mobile/util/mobileAppUtil";
import {
cellValueIsEmpty,
+ cloneAVCellValueSnapshot,
genEmptyAVCellValue,
genRelationAVCellValue,
getConvertedEmptyAVCellValue,
@@ -802,10 +803,11 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLEleme
const colId = source.selectedCell?.colID || (item ? getColId(item, viewType) : "");
const renderedOldValue = source.selectedCell?.cell.value ||
(item ? genCellValueByElement(type, item) : undefined);
- const oldValue = item && renderedOldValue ? getAVBatchSourceValue(item, renderedOldValue) : renderedOldValue;
- if (!colId || !oldValue) {
+ const sourceOldValue = item && renderedOldValue ? getAVBatchSourceValue(item, renderedOldValue) : renderedOldValue;
+ if (!colId || !sourceOldValue) {
continue;
}
+ const oldValue = cloneAVCellValueSnapshot(sourceOldValue);
const batchMode = getAVBatchEditMode(item);
if (elementIndex === 0 && item && batchMode !== "replace") {
batchDisplayValue = getAVBatchDisplayValue(item, renderedOldValue);
diff --git a/app/src/protyle/render/av/cellValue.test.ts b/app/src/protyle/render/av/cellValue.test.ts
index f4e24b59e5..83e0c9ae14 100644
--- a/app/src/protyle/render/av/cellValue.test.ts
+++ b/app/src/protyle/render/av/cellValue.test.ts
@@ -2,10 +2,44 @@ import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
cellValueIsEmpty,
+ cloneAVCellValueSnapshot,
genRelationAVCellValue,
getConvertedEmptyAVCellValue,
} from "./cellValue";
+describe("cloneAVCellValueSnapshot", () => {
+ it("preserves empty collection values in transaction snapshots", () => {
+ const assetValue = {type: "mAsset"} as IAVCellValue;
+ const selectValue = {type: "select"} as IAVCellValue;
+
+ assert.deepEqual(cloneAVCellValueSnapshot(assetValue), {
+ type: "mAsset",
+ mAsset: [],
+ });
+ assert.deepEqual(cloneAVCellValueSnapshot(selectValue), {
+ type: "select",
+ mSelect: [],
+ });
+ assert.equal(assetValue.mAsset, undefined);
+ assert.equal(selectValue.mSelect, undefined);
+ });
+
+ it("creates an independent snapshot of populated collection values", () => {
+ const value = {
+ type: "mAsset",
+ mAsset: [{
+ name: "asset.png",
+ content: "assets/asset.png",
+ type: "file",
+ }],
+ } as IAVCellValue;
+ const snapshot = cloneAVCellValueSnapshot(value);
+
+ snapshot.mAsset[0].name = "changed.png";
+ assert.equal(value.mAsset[0].name, "asset.png");
+ });
+});
+
describe("getConvertedEmptyAVCellValue", () => {
const emptyBlockValue: IAVCellValue = {
type: "block",
diff --git a/app/src/protyle/render/av/cellValue.ts b/app/src/protyle/render/av/cellValue.ts
index d24d5d7b2e..10fd47eee9 100644
--- a/app/src/protyle/render/av/cellValue.ts
+++ b/app/src/protyle/render/av/cellValue.ts
@@ -78,6 +78,16 @@ export const genEmptyAVCellValue = (colType: TAVCol): IAVCellValue => {
return cellValue;
};
+export const cloneAVCellValueSnapshot = (value: IAVCellValue): IAVCellValue => {
+ const snapshot = JSON.parse(JSON.stringify(value)) as IAVCellValue;
+ if ((snapshot.type === "mSelect" || snapshot.type === "select") && !snapshot.mSelect) {
+ snapshot.mSelect = [];
+ } else if (snapshot.type === "mAsset" && !snapshot.mAsset) {
+ snapshot.mAsset = [];
+ }
+ return snapshot;
+};
+
export const getConvertedEmptyAVCellValue = (colType: TAVCol, value: IAVCellValue) => {
if (colType === value.type || !cellValueIsEmpty(value)) {
return;
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index 1a13f4ab50..857b6a2026 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -50,6 +50,7 @@ import {
showAVPasteSkeleton,
} from "../render/av/paste";
import {getAVColumnFitWidth, getAVColumnTextMeasurer} from "../render/av/columnWidth";
+import {cloneAVCellValueSnapshot} from "../render/av/cellValue";
import {Dialog} from "../../dialog";
import {isMobile} from "../../util/functions";
import {getCrossBlockMergeRemoveElement} from "../wysiwyg/removeRange";
@@ -327,7 +328,7 @@ const pasteAVMatrix = async (options: {
row.cells.forEach((cell, index) => {
const column = originalColumns[index];
if (column) {
- originalCellValues.set(`${row.id}:${column.id}`, JSON.parse(JSON.stringify(cell.value)));
+ originalCellValues.set(`${row.id}:${column.id}`, cloneAVCellValueSnapshot(cell.value));
}
});
});