Skip to content

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

:bug: Restore normal undo for newly added database entries https://github.com/siyuan-note/siyuan/issues/19308

Daniele15b7632c102
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
index fcb07a9bac..1c8510ca9f 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
@@ -7,7 +7,7 @@
"id": "20240208172514-9dsv6na",
"title": "Database",
"type": "doc",
- "updated": "20260904121106"
+ "updated": "20260911164949"
},
"Children": [
{
@@ -3127,6 +3127,62 @@
"id": "20260613194213-n2rytff",
"updated": "20260613194213"
}
+ },
+ {
+ "ID": "20260911164949-hmuujgx",
+ "Type": "NodeHeading",
+ "Properties": {
+ "id": "20260911164949-hmuujgx",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Undo adding an entry"
+ }
+ ],
+ "HeadingLevel": 2
+ },
+ {
+ "ID": "20260911164949-3rfp2zf",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260911164949-3rfp2zf",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "If you accidentally add an entry and have not edited it, press "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Esc"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " to close the primary cell editor, then press "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " ( "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "⌘Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " on macOS) to undo the addition. Inside the editor, undo only affects text editing, and Esc closes the editor while keeping the entry. After leaving the editor, undo affects the most recent database operation; redo restores an entry whose addition was undone."
+ }
+ ]
}
]
}
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
index 9029de7598..e2a667954a 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
@@ -7,7 +7,7 @@
"id": "20240208113259-nykkvaq",
"title": "数据库",
"type": "doc",
- "updated": "20260904121106"
+ "updated": "20260911164949"
},
"Children": [
{
@@ -3127,6 +3127,62 @@
"id": "20260613194228-iha8ygq",
"updated": "20260613194228"
}
+ },
+ {
+ "ID": "20260911164949-0wyw08b",
+ "Type": "NodeHeading",
+ "Properties": {
+ "id": "20260911164949-0wyw08b",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "撤销新增条目"
+ }
+ ],
+ "HeadingLevel": 2
+ },
+ {
+ "ID": "20260911164949-5xo72fb",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260911164949-5xo72fb",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "误新增条目且尚未编辑时,先按 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Esc"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 关闭主键输入框,再按撤销快捷键 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "(macOS 为 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "⌘Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": ")撤销新增。输入框内的撤销快捷键仅撤销文本编辑,Esc 关闭输入框并保留条目。退出输入框后,撤销作用于最近的数据库操作;重做可恢复被撤销的新增条目。"
+ }
+ ]
}
]
}
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
index fc2f4b2efe..3c4db791bc 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
@@ -7,7 +7,7 @@
"id": "20240208171522-y7dxcno",
"title": "資料庫",
"type": "doc",
- "updated": "20260904121106"
+ "updated": "20260911164949"
},
"Children": [
{
@@ -3119,6 +3119,62 @@
"Data": "​。"
}
]
+ },
+ {
+ "ID": "20260911164949-25ia9x8",
+ "Type": "NodeHeading",
+ "Properties": {
+ "id": "20260911164949-25ia9x8",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "復原新增條目"
+ }
+ ],
+ "HeadingLevel": 2
+ },
+ {
+ "ID": "20260911164949-u1ojzfi",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260911164949-u1ojzfi",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "誤新增條目且尚未編輯時,先按 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Esc"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 關閉主鍵輸入框,再按復原快捷鍵 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "(macOS 為 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "⌘Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": ")復原新增。輸入框內的復原快捷鍵僅復原文字編輯,Esc 關閉輸入框並保留條目。離開輸入框後,復原作用於最近的資料庫操作;重做可恢復被復原的新增條目。"
+ }
+ ]
}
]
}
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
index ae52e115f7..c7e0e55c0d 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
@@ -8,7 +8,7 @@
"id": "20240530101000-3eaevtp",
"title": "データベース",
"type": "doc",
- "updated": "20260904121106"
+ "updated": "20260911164949"
},
"Children": [
{
@@ -3129,6 +3129,62 @@
"Data": "​ を使用してください。"
}
]
+ },
+ {
+ "ID": "20260911164949-bl0hpyi",
+ "Type": "NodeHeading",
+ "Properties": {
+ "id": "20260911164949-bl0hpyi",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "エントリの追加を取り消す"
+ }
+ ],
+ "HeadingLevel": 2
+ },
+ {
+ "ID": "20260911164949-zxvbk8s",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260911164949-zxvbk8s",
+ "updated": "20260911164949"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "誤ってエントリを追加し、まだ編集していない場合は、"
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Esc"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " を押して主キーの入力欄を閉じ、"
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Ctrl+Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": "(macOS では "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "⌘Z"
+ },
+ {
+ "Type": "NodeText",
+ "Data": ")で追加を取り消します。入力欄内の取り消し操作はテキスト編集のみが対象で、Esc はエントリを残して入力欄を閉じます。入力欄を閉じた後は、直前のデータベース操作が取り消されます。やり直し操作で、追加を取り消したエントリを復元できます。"
+ }
+ ]
}
]
}
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index f07c947c07..f978d8bc2e 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -18,8 +18,7 @@ import {mergeAddOption} from "./select";
import {escapeAriaLabel, escapeAttr, escapeHtml} from "../../../util/escape";
import {electronUndo} from "../../undo";
import {formatDateDisplay, formatDateValue, parseDateValue} from "./dateFormat";
-import {cancelAddedRow, getFieldIdByCellElement} from "./row";
-import {matchHotKey} from "../../util/hotKey";
+import {getFieldIdByCellElement} from "./row";
import {getFieldsByData} from "./view";
import {getCompressURL, removeCompressURL} from "../../../util/image";
import {isBrowserRenderableImagePath} from "../../../util/imageURL";
@@ -493,7 +492,6 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type
keepMenuOpen?: boolean;
positionByMenu?: boolean;
requireExplicitChange?: boolean;
- addedItem?: {itemID: string, previousID?: string, groupID?: string};
}) => {
if (cellElements.length === 0 || (cellElements.length === 1 && !cellElements[0])) {
options?.destroyCallback?.();
@@ -728,19 +726,6 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type
if (event.isComposing) {
return;
}
- if (options?.addedItem && !inputElement.value.trim() &&
- (event.key === "Escape" ||
- matchHotKey(window.siyuan.config.keymap.editor.general.undo, event))) {
- // 新建条目尚未填写数据时,Esc 或 Ctrl+Z 取消该条目
- cancelAddedRow(protyle, blockElement, options.addedItem);
- avMaskElement?.remove();
- if (!document.querySelector(".b3-dialog")) {
- focusBlock(blockElement);
- }
- event.preventDefault();
- event.stopPropagation();
- return;
- }
if (electronUndo(event)) {
return;
}
diff --git a/app/src/protyle/render/av/insertRows.test.ts b/app/src/protyle/render/av/insertRows.test.ts
new file mode 100644
index 0000000000..cf3057d92e
--- /dev/null
+++ b/app/src/protyle/render/av/insertRows.test.ts
@@ -0,0 +1,62 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const source = readFileSync("src/protyle/render/av/row.ts", "utf8");
+const compiled = transpileModule(source.slice(source.indexOf("export const insertRows ="),
+ source.indexOf("export const duplicateRows =")), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+for (const viewType of ["table", "gallery", "kanban"]) {
+ for (const count of [1, 3]) {
+ test(`${viewType}: undo and redo ${count} added entries using their item IDs`, () => {
+ let nextID = 0;
+ let operations: any[];
+ let inverse: any[];
+ const exports: any = {};
+ const blockElement = {
+ dataset: {nodeId: "database-block"},
+ getAttribute: (name: string) => ({
+ "data-av-id": "database",
+ "data-av-type": viewType,
+ "custom-av-view": "view",
+ updated: "20260911000000",
+ })[name],
+ setAttribute: () => {},
+ };
+ runInNewContext(compiled, {
+ exports,
+ Lute: {NewNodeID: () => `generated-${++nextID}`},
+ dayjs: () => ({format: () => "20260911010000"}),
+ Constants: {CUSTOM_SY_AV_VIEW: "custom-av-view"},
+ getAVFilteredTipContext: () => ({}),
+ transaction: (_protyle: any, doOperations: any[], undoOperations: any[]) => {
+ operations = doOperations;
+ inverse = undoOperations;
+ },
+ insertGalleryItemAnimation: () => {},
+ insertAttrViewBlockAnimation: () => {},
+ });
+ exports.insertRows({blockElement, protyle: {}, count, previousID: "previous", groupID: "group"});
+ const rows = new Set(["existing"]);
+ const apply = (items: any[]) => items.forEach(operation => {
+ if (operation.action === "insertAttrViewBlock") {
+ operation.srcs.forEach((src: any) => rows.add(src.itemID));
+ } else if (operation.action === "removeAttrViewBlock") {
+ operation.srcIDs.forEach((id: string) => rows.delete(id));
+ }
+ });
+ for (let cycle = 0; cycle < 2; cycle++) {
+ apply(operations);
+ assert.equal(rows.size, count + 1);
+ apply(inverse);
+ assert.deepEqual([...rows], ["existing"]);
+ }
+ assert.equal(operations[0].previousID, "previous");
+ assert.equal(operations[0].groupID, "group");
+ });
+ }
+}
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index c1fdd98679..72d0e4e3ac 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -1144,11 +1144,6 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
}
let isAddingFocusPending = false;
if (operation.srcs.length === 1) {
- const addedItem = {
- itemID: operation.srcs[0].itemID,
- previousID: operation.previousID,
- groupID: operation.groupID,
- };
let popCellElement = item.querySelector(`.av__body${groupQuery} [data-id="${operation.srcs[0].itemID}"] .av__cell[data-dtype="block"]`) as HTMLElement;
if (!popCellElement) {
const popCellElements = item.querySelectorAll(`.av__body [data-id="${operation.srcs[0].itemID}"] .av__cell[data-dtype="block"]`);
@@ -1162,7 +1157,7 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
if (item.getAttribute("data-av-type") !== "table") {
if (addingFocusTokens.get(addingFocusKey) === addingFocusToken) {
addingFocusTokens.delete(addingFocusKey);
- popTextCell(protyle, [popCellElement], "block", {addedItem});
+ popTextCell(protyle, [popCellElement], "block");
}
} else {
isAddingFocusPending = true;
@@ -1183,7 +1178,7 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
genCellValueByElement("block", result.cellElement).block?.content) {
return;
}
- popTextCell(protyle, [result.cellElement], "block", {scrollIntoView: false, addedItem});
+ popTextCell(protyle, [result.cellElement], "block", {scrollIntoView: false});
});
}
}
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index 7f98644427..0d36d58f47 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -922,46 +922,6 @@ export const deleteRow = (blockElement: HTMLElement, protyle: IProtyle) => {
blockElement.setAttribute("updated", newUpdated);
};
-export const cancelAddedRow = (protyle: IProtyle, blockElement: HTMLElement, options: {
- itemID: string,
- previousID?: string,
- groupID?: string
-}) => {
- const avID = blockElement.getAttribute("data-av-id");
- const newUpdated = dayjs().format("YYYYMMDDHHmmss");
- // 取消新建条目,同时提供逆操作,误触取消后仍可撤销恢复
- transaction(protyle, [{
- action: "removeAttrViewBlock",
- srcIDs: [options.itemID],
- avID,
- }, {
- action: "doUpdateUpdated",
- id: blockElement.dataset.nodeId,
- data: newUpdated,
- }], [{
- action: "insertAttrViewBlock",
- avID,
- previousID: options.previousID,
- srcs: [{
- itemID: Lute.NewNodeID(),
- id: options.itemID,
- isDetached: true,
- content: "",
- }],
- blockID: blockElement.dataset.nodeId,
- groupID: options.groupID,
- }, {
- action: "doUpdateUpdated",
- id: blockElement.dataset.nodeId,
- data: blockElement.getAttribute("updated"),
- }]);
- blockElement.setAttribute("updated", newUpdated);
- blockElement.querySelector(`.av__row[data-id="${options.itemID}"], .av__gallery-item[data-id="${options.itemID}"]`)?.remove();
- clearSelect(["cell"], blockElement);
- stickyRow(blockElement, protyle.contentElement, "all");
- updateHeader(blockElement.querySelector(".av__row"));
-};
-
export const insertRows = (options: {
blockElement: HTMLElement,
protyle: IProtyle,
@@ -974,9 +934,10 @@ export const insertRows = (options: {
const srcs: IOperationSrcs[] = [];
new Array(options.count).fill(0).forEach(() => {
const newNodeID = Lute.NewNodeID();
- srcIDs.push(newNodeID);
+ const itemID = Lute.NewNodeID();
+ srcIDs.push(itemID);
srcs.push({
- itemID: Lute.NewNodeID(),
+ itemID,
id: newNodeID,
isDetached: true,
content: "",