Skip to content

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

:sparkles: Support canceling newly added database entries with Esc or Ctrl+Z https://github.com/siyuan-note/siyuan/issues/19308

Danielf94293f700f8
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index f978d8bc2e..f07c947c07 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -18,7 +18,8 @@ import {mergeAddOption} from "./select";
import {escapeAriaLabel, escapeAttr, escapeHtml} from "../../../util/escape";
import {electronUndo} from "../../undo";
import {formatDateDisplay, formatDateValue, parseDateValue} from "./dateFormat";
-import {getFieldIdByCellElement} from "./row";
+import {cancelAddedRow, getFieldIdByCellElement} from "./row";
+import {matchHotKey} from "../../util/hotKey";
import {getFieldsByData} from "./view";
import {getCompressURL, removeCompressURL} from "../../../util/image";
import {isBrowserRenderableImagePath} from "../../../util/imageURL";
@@ -492,6 +493,7 @@ 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?.();
@@ -726,6 +728,19 @@ 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/render.ts b/app/src/protyle/render/av/render.ts
index 72d0e4e3ac..c1fdd98679 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -1144,6 +1144,11 @@ 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"]`);
@@ -1157,7 +1162,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");
+ popTextCell(protyle, [popCellElement], "block", {addedItem});
}
} else {
isAddingFocusPending = true;
@@ -1178,7 +1183,7 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
genCellValueByElement("block", result.cellElement).block?.content) {
return;
}
- popTextCell(protyle, [result.cellElement], "block", {scrollIntoView: false});
+ popTextCell(protyle, [result.cellElement], "block", {scrollIntoView: false, addedItem});
});
}
}
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index 71de50983d..7f98644427 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -922,6 +922,46 @@ 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,