Skip to content

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

:bug: Improve database text cell selection and paste handling

Daniel9a91fc35d625
diff --git a/app/src/protyle/render/av/paste.test.ts b/app/src/protyle/render/av/paste.test.ts
index ae43eb92c4..020313518d 100644
--- a/app/src/protyle/render/av/paste.test.ts
+++ b/app/src/protyle/render/av/paste.test.ts
@@ -3,6 +3,7 @@ import * as assert from "node:assert/strict";
import {
compactAVCellOperations,
getAVPasteCellValue,
+ getAVPasteContentRowCount,
getAVPasteValueForType,
getAVPasteMatrixWidth,
getUniqueAVPasteColumnName,
@@ -128,6 +129,13 @@ describe("AV paste matrix helpers", () => {
assert.equal(getAVPasteMatrixWidth([["1", "2", "3"]], ["a"]), 3);
});
+ it("ignores trailing empty table rows without removing intentional empty rows", () => {
+ assert.equal(getAVPasteContentRowCount([["value"], [""]]), 1);
+ assert.equal(getAVPasteContentRowCount([["value"], [""], ["next"]]), 3);
+ assert.equal(getAVPasteContentRowCount([["value"], [" "]]), 2);
+ assert.equal(getAVPasteContentRowCount([[""]]), 1);
+ });
+
it("generates localized unique field names", () => {
const usedNames = new Set(["Text", "Text 2"]);
assert.equal(getUniqueAVPasteColumnName("Text", usedNames), "Text 3");
diff --git a/app/src/protyle/render/av/paste.ts b/app/src/protyle/render/av/paste.ts
index 3b63e96af3..ba2d3b8c12 100644
--- a/app/src/protyle/render/av/paste.ts
+++ b/app/src/protyle/render/av/paste.ts
@@ -124,6 +124,14 @@ export const getAVPasteMatrixWidth = (rows: unknown[][], header?: string[]) => {
return Math.max(header?.length || 0, ...rows.map(row => row.length), 0);
};
+export const getAVPasteContentRowCount = (rows: unknown[][]) => {
+ let rowCount = rows.length;
+ while (rowCount > 1 && rows[rowCount - 1].every(value => typeof value === "string" && value === "")) {
+ rowCount--;
+ }
+ return rowCount;
+};
+
export const shouldShowAVPasteSkeleton = (rows: unknown[][]) => {
return rows.reduce((count, row) => count + row.length, 0) >= 100;
};
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index 46fb435560..d8e6bc3deb 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -42,6 +42,7 @@ import {
AV_PASTE_READONLY_TYPES,
compactAVCellOperations,
getAVPasteCellValue,
+ getAVPasteContentRowCount,
getAVPasteValueForType,
getAVPasteMatrixWidth,
getUniqueAVPasteColumnName,
@@ -696,6 +697,9 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
cellHTML.push(rowHTML);
}
});
+ const contentRowCount = getAVPasteContentRowCount(values);
+ values.length = contentRowCount;
+ cellHTML.length = contentRowCount;
headerCandidate = isAVPasteHeaderCandidate(values, headerCandidate);
}
const avID = blockElement.dataset.avId;
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 16673d2b3a..1b842ad993 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -150,6 +150,7 @@ import {checkFold} from "../../util/noRelyPCFunction";
import {confirmBlockRef} from "../../util/checkBlockRef";
import {
addDragFill,
+ cellValueIsEmpty,
dragFillCellsValue,
getAVCellData,
getAVSelectedCellData,
@@ -1167,6 +1168,7 @@ export class WYSIWYG {
!event.altKey && !protyle.disabled) {
repairHiddenTabSelection(this.element, target);
}
+ const avCellElement = hasClosestByClassName(target, "av__cell");
const emptyCell = target.closest<HTMLTableCellElement>("td:empty, th:empty");
if (emptyCell && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey &&
!event.altKey && !protyle.disabled && emptyCell.closest(".protyle-wysiwyg") === this.element) {
@@ -1174,7 +1176,8 @@ export class WYSIWYG {
event.preventDefault();
}
const customElement = hasClosestByClassName(target, "protyle-custom");
- let nodeElement = hasClosestBlock(target) as HTMLElement;
+ // 富文本预览以 b3-typography 作为渲染边界,先回到单元格再查找数据库块。
+ let nodeElement = hasClosestBlock(avCellElement || target) as HTMLElement;
let clickedTableNode = !customElement && nodeElement && nodeElement.dataset.type === "NodeTable" ?
nodeElement : undefined;
if (!nodeElement && !customElement) {
@@ -1213,7 +1216,14 @@ export class WYSIWYG {
const hasSelectClassElement = this.element.querySelector(".protyle-wysiwyg--select");
const galleryItemElement = hasClosestByClassName(target, "av__gallery-item");
const rowElement = hasClosestByClassName(target, "av__row");
- const avCellElement = hasClosestByClassName(target, "av__cell");
+ const selectFilledTextCell = Boolean(!isMobile() && avCellElement &&
+ avCellElement.dataset.dtype === "text" &&
+ !avCellElement.classList.contains("av__cell--select") &&
+ !cellValueIsEmpty(genCellValueByElement("text", avCellElement)) &&
+ !target.closest(".av__cell-action, .av__celltext--rich [data-type~='block-ref'][data-id], " +
+ ".av__celltext--rich [data-type~='file-annotation-ref'][data-id], " +
+ ".av__celltext--rich [data-type~='tag'], .av__celltext--rich [data-type~='a'], " +
+ ".av__celltext--rich a[href]"));
const avElement = getAVSelectionRoot(target);
const wysiwygRect = protyle.wysiwyg.element.getBoundingClientRect();
const wysiwygStyle = window.getComputedStyle(protyle.wysiwyg.element);
@@ -1833,7 +1843,7 @@ export class WYSIWYG {
}
};
- documentSelf.onmouseup = () => {
+ documentSelf.onmouseup = (upEvent: MouseEvent) => {
documentSelf.onmousemove = null;
documentSelf.onmouseup = null;
documentSelf.ondragstart = null;
@@ -1844,6 +1854,10 @@ export class WYSIWYG {
focusBlock(nodeElement);
addDragFill(lastCellElement);
this.preventClick = true;
+ } else if (selectFilledTextCell &&
+ hasClosestByClassName(upEvent.target as HTMLElement, "av__cell") === avCellElement) {
+ focusBlock(nodeElement);
+ this.preventClick = true;
}
return false;
};