An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve pasting empty database cells https://github.com/siyuan-note/siyuan/issues/15569
Daniel2514388fd64a
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.tsindex 31ecb268a0..1aed4c7c37 100644--- a/app/src/protyle/render/av/cell.ts+++ b/app/src/protyle/render/av/cell.ts@@ -20,6 +20,14 @@ import {getFieldIdByCellElement} from "./row";import {getFieldsByData} from "./view";import {getCompressURL, removeCompressURL} from "../../../util/image";import {callMobileAppShowKeyboard} from "../../../mobile/util/mobileAppUtil";+import {+ cellValueIsEmpty,+ genEmptyAVCellValue,+ genRelationAVCellValue,+ getConvertedEmptyAVCellValue,+} from "./cellValue";++export {cellValueIsEmpty} from "./cellValue";const renderCellURL = (urlContent: string) => {let host = urlContent;@@ -184,6 +192,10 @@ const transformCellValue = (colType: TAVCol, value: IAVCellValue): IAVCellValueif (colType === value.type) {return value;}+ const emptyValue = getConvertedEmptyAVCellValue(colType, value);+ if (emptyValue) {+ return emptyValue;+ }const newValue: IAVCellValue = {type: colType,};@@ -218,14 +230,7 @@ const transformCellValue = (colType: TAVCol, value: IAVCellValue): IAVCellValuechecked: true};} else if (colType === "relation") {- if (value.type === "block") {- newValue.relation = {- blockIDs: [value.blockID],- contents: [value]- };- } else {- newValue.relation = {blockIDs: [], contents: []};- }+ return genRelationAVCellValue(value);} else if (colType === "mAsset") {const content = getCellValueContent(value).toString();newValue.mAsset = [{@@ -344,56 +349,7 @@ export const genCellValue = (colType: TAVCol, value: string | any) => {};}} else if (typeof value === "undefined" || !value) {- if (colType === "number") {- cellValue = {- type: colType,- number: {- content: 0,- isNotEmpty: false- }- };- } else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) {- cellValue = {- type: colType,- [colType]: {- content: ""- }- };- } else if (colType === "mSelect" || colType === "select" || colType === "mAsset") {- cellValue = {- type: colType,- [colType === "select" ? "mSelect" : colType]: []- };- } else if (["date", "created", "updated"].includes(colType)) {- cellValue = {- type: colType,- [colType]: {- content: null,- isNotEmpty: false,- content2: null,- isNotEmpty2: false,- hasEndDate: false,- isNotTime: true,- }- };- } else if (colType === "checkbox") {- cellValue = {- type: colType,- checkbox: {- checked: false- }- };- } else if (colType === "relation") {- cellValue = {- type: colType,- relation: {blockIDs: [], contents: []}- };- } else if (colType === "rollup") {- cellValue = {- type: colType,- rollup: {contents: []}- };- }+ cellValue = genEmptyAVCellValue(colType);}if (colType === "block") {if (typeof value === "object" && value && value.id) {@@ -1373,37 +1329,3 @@ export const addDragFill = (cellElement: Element) => {cellElement.insertAdjacentHTML("beforeend", `<div aria-label="${window.siyuan.languages.dragFill}" class="av__drag-fill ariaLabel"></div>`);}};--export const cellValueIsEmpty = (value: IAVCellValue) => {- if (value.type === "checkbox") {- return false;- }- if (["text", "block", "url", "phone", "email", "template"].includes(value.type)) {- return !value[value.type as "text"]?.content;- }- if (value.type === "number") {- return value.number ? !value.number.isNotEmpty : true;- }- if (["mSelect", "mAsset", "select"].includes(value.type)) {- if (value[(value.type === "select" ? "mSelect" : value.type) as "mSelect"]?.length > 0) {- return false;- }- return true;- }- if (["date", "created", "updated"].includes(value.type)) {- return !value[value.type as "date"]?.isNotEmpty &&- !value[value.type as "date"]?.isNotEmpty2;- }- if (value.type === "relation") {- if (value.relation?.blockIDs && value.relation.blockIDs.length > 0) {- return false;- }- return true;- }- if (value.type === "rollup") {- if (value.rollup?.contents && value.rollup.contents.length > 0) {- return false;- }- return true;- }-};diff --git a/app/src/protyle/render/av/cellValue.test.ts b/app/src/protyle/render/av/cellValue.test.tsnew file mode 100644index 0000000000..f4e24b59e5--- /dev/null+++ b/app/src/protyle/render/av/cellValue.test.ts@@ -0,0 +1,98 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {+ cellValueIsEmpty,+ genRelationAVCellValue,+ getConvertedEmptyAVCellValue,+} from "./cellValue";++describe("getConvertedEmptyAVCellValue", () => {+ const emptyBlockValue: IAVCellValue = {+ type: "block",+ isDetached: true,+ block: {+ content: "",+ },+ };++ it("preserves empty values when converting database cells", () => {+ assert.deepEqual(getConvertedEmptyAVCellValue("number", emptyBlockValue), {+ type: "number",+ number: {+ content: 0,+ isNotEmpty: false,+ },+ });+ assert.deepEqual(getConvertedEmptyAVCellValue("mAsset", emptyBlockValue), {+ type: "mAsset",+ mAsset: [],+ });+ assert.deepEqual(getConvertedEmptyAVCellValue("checkbox", emptyBlockValue), {+ type: "checkbox",+ checkbox: {+ checked: false,+ },+ });+ assert.deepEqual(getConvertedEmptyAVCellValue("relation", emptyBlockValue), {+ type: "relation",+ relation: {+ blockIDs: [],+ contents: [],+ },+ });+ });++ it("leaves same-type and non-empty values to the normal conversion path", () => {+ assert.equal(getConvertedEmptyAVCellValue("block", emptyBlockValue), undefined);+ assert.equal(getConvertedEmptyAVCellValue("number", {+ type: "block",+ block: {+ content: "12",+ },+ }), undefined);+ });++ it("distinguishes an explicit zero from an empty number", () => {+ assert.equal(cellValueIsEmpty({+ type: "number",+ number: {+ content: 0,+ isNotEmpty: true,+ },+ }), false);+ });+});++describe("genRelationAVCellValue", () => {+ it("rejects block values without a row ID", () => {+ assert.deepEqual(genRelationAVCellValue({+ type: "block",+ block: {+ content: "Title",+ },+ }), {+ type: "relation",+ relation: {+ blockIDs: [],+ contents: [],+ },+ });+ });++ it("converts block values with a row ID", () => {+ const blockValue: IAVCellValue = {+ type: "block",+ blockID: "20260727120000-abcdefg",+ block: {+ content: "Title",+ },+ };+ assert.deepEqual(genRelationAVCellValue(blockValue), {+ type: "relation",+ relation: {+ blockIDs: ["20260727120000-abcdefg"],+ contents: [blockValue],+ },+ });+ });+});diff --git a/app/src/protyle/render/av/cellValue.ts b/app/src/protyle/render/av/cellValue.tsnew file mode 100644index 0000000000..d24d5d7b2e--- /dev/null+++ b/app/src/protyle/render/av/cellValue.ts@@ -0,0 +1,99 @@+export const cellValueIsEmpty = (value: IAVCellValue) => {+ if (value.type === "checkbox") {+ return false;+ }+ if (["text", "block", "url", "phone", "email", "template"].includes(value.type)) {+ return !value[value.type as "text"]?.content;+ }+ if (value.type === "number") {+ return value.number ? !value.number.isNotEmpty : true;+ }+ if (["mSelect", "mAsset", "select"].includes(value.type)) {+ if (value[(value.type === "select" ? "mSelect" : value.type) as "mSelect"]?.length > 0) {+ return false;+ }+ return true;+ }+ if (["date", "created", "updated"].includes(value.type)) {+ return !value[value.type as "date"]?.isNotEmpty &&+ !value[value.type as "date"]?.isNotEmpty2;+ }+ if (value.type === "relation") {+ if (value.relation?.blockIDs && value.relation.blockIDs.length > 0) {+ return false;+ }+ return true;+ }+ if (value.type === "rollup") {+ if (value.rollup?.contents && value.rollup.contents.length > 0) {+ return false;+ }+ return true;+ }+};++export const genEmptyAVCellValue = (colType: TAVCol): IAVCellValue => {+ const cellValue: IAVCellValue = {+ type: colType,+ };+ if (colType === "number") {+ cellValue.number = {+ content: 0,+ isNotEmpty: false,+ };+ } else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) {+ cellValue[colType as "text"] = {+ content: "",+ };+ } else if (colType === "mSelect" || colType === "select") {+ cellValue.mSelect = [];+ } else if (colType === "mAsset") {+ cellValue.mAsset = [];+ } else if (["date", "created", "updated"].includes(colType)) {+ cellValue[colType as "date"] = {+ content: null,+ isNotEmpty: false,+ content2: null,+ isNotEmpty2: false,+ hasEndDate: false,+ isNotTime: true,+ };+ } else if (colType === "checkbox") {+ cellValue.checkbox = {+ checked: false,+ };+ } else if (colType === "relation") {+ cellValue.relation = {+ blockIDs: [],+ contents: [],+ };+ } else if (colType === "rollup") {+ cellValue.rollup = {+ contents: [],+ };+ }+ if (colType === "block") {+ cellValue.isDetached = true;+ }+ return cellValue;+};++export const getConvertedEmptyAVCellValue = (colType: TAVCol, value: IAVCellValue) => {+ if (colType === value.type || !cellValueIsEmpty(value)) {+ return;+ }+ return genEmptyAVCellValue(colType);+};++export const genRelationAVCellValue = (value: IAVCellValue): IAVCellValue => {+ if (value.type === "block" && value.blockID) {+ return {+ type: "relation",+ relation: {+ blockIDs: [value.blockID],+ contents: [value],+ },+ };+ }+ return genEmptyAVCellValue("relation");+};diff --git a/app/src/protyle/render/av/paste.ts b/app/src/protyle/render/av/paste.tsindex b123c842fb..4080751bc4 100644--- a/app/src/protyle/render/av/paste.ts+++ b/app/src/protyle/render/av/paste.ts@@ -49,7 +49,7 @@ export const inferAVPasteColumnType = (values: string[]): TAVCol => {return "text";};-export const getAVPasteMatrixWidth = (rows: string[][], header?: string[]) => {+export const getAVPasteMatrixWidth = (rows: unknown[][], header?: string[]) => {return Math.max(header?.length || 0, ...rows.map(row => row.length), 0);};diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.tsindex 71fa75348e..c32d74ff0b 100644--- a/app/src/protyle/util/insertHTML.ts+++ b/app/src/protyle/util/insertHTML.ts@@ -162,6 +162,8 @@ interface IAVPasteTargetColumn {typeChanged: boolean;}+type TAVPasteValue = string | IAVCellValue;+const restoreAVPasteCellPlaceholders = (placeholders: IAVPasteCellPlaceholder[]) => {placeholders.reverse().forEach(item => {if (item.originalHTML) {@@ -266,7 +268,7 @@ const confirmAVPasteHeader = () => {};const pasteAVMatrix = async (options: {- values: string[][],+ values: TAVPasteValue[][],protyle: IProtyle,blockElement: HTMLElement,startCell: HTMLElement,@@ -329,7 +331,8 @@ const pasteAVMatrix = async (options: {for (let sourceIndex = 0; sourceIndex < sourceWidth; sourceIndex++) {const headerName = options.header?.[sourceIndex]?.trim() || "";- const sourceValues = options.values.flatMap(row => sourceIndex < row.length ? [row[sourceIndex]] : []);+ const sourceValues = options.values.flatMap(row =>+ sourceIndex < row.length && typeof row[sourceIndex] === "string" ? [row[sourceIndex] as string] : []);const inferredType = options.header ? inferAVPasteColumnType(sourceValues) : "text";const currentColumn = availableColumns[sourceIndex];if (currentColumn) {@@ -549,7 +552,7 @@ const pasteAVMatrix = async (options: {const processAV = (range: Range, html: string, protyle: IProtyle, blockElement: HTMLElement) => {const tempElement = document.createElement("template");tempElement.innerHTML = html;- let values: string[][] = [];+ let values: TAVPasteValue[][] = [];const cellHTML: string[][] = [];let headerCandidate = false;if (html.endsWith("]") && html.startsWith("[")) {@@ -600,6 +603,8 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:}if (cellElements[0]) {const useHeader = headerCandidate ? await confirmAVPasteHeader() : false;+ const header = useHeader ?+ values[0].map(value => typeof value === "string" ? value : "") : undefined;await pasteAVMatrix({values: useHeader ? values.slice(1) : values,protyle,@@ -608,7 +613,7 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:columns,html,cellHTML: useHeader ? cellHTML.slice(1) : cellHTML,- header: useHeader ? values[0] : undefined,+ header,});}return;