Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/14780

Vanessa221cf13e1fb7
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index 3fa2d6f73e..a5ccce07a4 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -140,6 +140,102 @@ export const genCellValueByElement = (colType: TAVCol, cellElement: HTMLElement)
return cellValue;
};
+const getCellValueContent = (value: IAVCellValue): string => {
+ if (["number", "text", "block", "url", "phone", "email", "template", "mAsset"].includes(value.type)) {
+ return value[value.type as "text"].content;
+ }
+ if (["mSelect", "select"].includes(value.type)) {
+ return value.mSelect[0].content;
+ }
+ if (value.type === "rollup") {
+ return getCellValueContent(value.relation.contents[0]);
+ }
+ if (value.type === "checkbox") {
+ return value.checkbox.checked ? "true" : "false";
+ }
+ if (value.type === "relation") {
+ return getCellValueContent(value.relation.contents[0]);
+ }
+ if (["date", "created", "updated"].includes(value.type)) {
+ return dayjs(value[value.type as "date"].content).format("YYYY-MM-DD HH:mm");
+ }
+ if (value.type === "lineNumber") {
+ return "";
+ }
+};
+
+const transformCellValue = (colType: TAVCol, value: IAVCellValue): IAVCellValue => {
+ if (colType === value.type) {
+ return value;
+ }
+ const newValue: IAVCellValue = {
+ type: colType,
+ };
+ if (colType === "number") {
+ if (["date", "created", "updated"].includes(colType)) {
+ newValue.number = {
+ content: value[value.type as "date"].content,
+ isNotEmpty: value[value.type as "date"].isNotEmpty
+ };
+ } else {
+ newValue.number = {
+ content: parseFloat(getCellValueContent(value)) || 0,
+ isNotEmpty: true
+ };
+ }
+ } else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) {
+ newValue[colType as "text"] = {
+ content: getCellValueContent(value)
+ };
+ } else if (colType === "mSelect" || colType === "select") {
+ newValue.mSelect = [{
+ content: getCellValueContent(value),
+ color: "1"
+ }];
+ } else if (colType === "rollup") {
+ newValue.rollup = {contents: [value]};
+ } else if (colType === "checkbox") {
+ newValue.checkbox = {
+ checked: true
+ };
+ } else if (colType === "relation") {
+ if (value.type === "block") {
+ newValue.relation = {
+ blockIDs: [value.block.id],
+ contents: [value]
+ };
+ } else {
+ newValue.relation = {blockIDs: [], contents: []};
+ }
+ } else if (colType === "mAsset") {
+ const content = getCellValueContent(value);
+ newValue.mAsset = [{
+ type: Constants.SIYUAN_ASSETS_IMAGE.includes(pathPosix().extname(content).toLowerCase()) ? "image" : "file",
+ content,
+ name: "",
+ }];
+ } else if (["date", "created", "updated"].includes(colType)) {
+ if (["date", "created", "updated"].includes(value.type)) {
+ newValue[colType as "date"] = JSON.parse(JSON.stringify(value[value.type as "date"]));
+ } else {
+ newValue[colType as "date"] = {
+ content: null,
+ isNotEmpty: false,
+ content2: null,
+ isNotEmpty2: false,
+ hasEndDate: false,
+ isNotTime: true,
+ };
+ }
+ } else if (colType === "lineNumber") {
+ return {
+ type: "lineNumber"
+ };
+ }
+ return newValue;
+};
+
+
export const genCellValue = (colType: TAVCol, value: string | any) => {
let cellValue: IAVCellValue = {
type: colType,
@@ -575,8 +671,8 @@ const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: H
});
};
-export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, value?: any, cElements?: HTMLElement[],
- columns?: IAVColumn[], html?: string) => {
+export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, value?: any,
+ cElements?: HTMLElement[], columns?: IAVColumn[], html?: string, getOperations = false) => {
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
@@ -630,7 +726,7 @@ export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, va
if (type === "mAsset") {
if (Array.isArray(value)) {
newValue = oldValue.mAsset.concat(value);
- } else if (typeof value !== "undefined") { // 不传入为删除,传入字符串不进行处理
+ } else if (typeof value !== "undefined" && typeof value !== "object") { // 不传入为删除,传入字符串不进行处理
let link = protyle.lute.GetLinkDest(value);
let name = "";
let imgSrc = "";
@@ -713,7 +809,12 @@ export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, va
newValue.icon = oldValue.block.icon;
}
}
- const cellValue = genCellValue(type, newValue);
+ let cellValue: IAVCellValue;
+ if (typeof newValue === "object" && newValue.type) {
+ cellValue = transformCellValue(type, newValue);
+ } else {
+ cellValue = genCellValue(type, newValue);
+ }
cellValue.id = cellId;
if ((cellValue.type === "date" && typeof cellValue.date === "string") ||
(cellValue.type === "relation" && typeof cellValue.relation === "string")) {
@@ -751,6 +852,9 @@ export const updateCellsValue = (protyle: IProtyle, nodeElement: HTMLElement, va
updateAttrViewCellAnimation(item, cellValue);
}
});
+ if (getOperations) {
+ return {doOperations, undoOperations};
+ }
if (doOperations.length > 0) {
doOperations.push({
action: "doUpdateUpdated",
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index 5af080e0b9..287f427332 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -13,17 +13,15 @@ import {Constants} from "../../constants";
import {highlightRender} from "../render/highlightRender";
import {scrollCenter} from "../../util/highlightById";
import {updateAttrViewCellAnimation, updateAVName} from "../render/av/action";
-import {genCellValue, genCellValueByElement, getTypeByCellElement, updateCellsValue} from "../render/av/cell";
+import {updateCellsValue} from "../render/av/cell";
import {input} from "../wysiwyg/input";
-import {objEquals} from "../../util/functions";
import {fetchPost} from "../../util/fetch";
-import {mergeAddOption} from "../render/av/select";
import {isIncludeCell} from "./table";
const processAV = (range: Range, html: string, protyle: IProtyle, blockElement: HTMLElement) => {
const tempElement = document.createElement("template");
tempElement.innerHTML = html;
- let values: IAVCellValue[][] = [];
+ let values: string[][] = [];
if (html.endsWith("]") && html.startsWith("[")) {
try {
values = JSON.parse(html);
@@ -34,21 +32,18 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
tempElement.content.querySelectorAll("tr").forEach(item => {
values.push([]);
Array.from(item.children).forEach(cell => {
- values[values.length - 1].push({
- text: {content: cell.textContent},
- type: "text"
- });
+ values[values.length - 1].push(cell.textContent);
});
});
}
const avID = blockElement.dataset.avId;
fetchPost("/api/av/getAttributeViewKeysByAvID", {avID}, (response) => {
const columns: IAVColumn[] = response.data;
+ const cellElements: HTMLElement[] = Array.from(blockElement.querySelectorAll(".av__cell--active, .av__cell--select")) || [];
if (values && Array.isArray(values) && values.length > 0) {
- const cellElements: Element[] = Array.from(blockElement.querySelectorAll(".av__cell--active, .av__cell--select")) || [];
if (cellElements.length === 0) {
blockElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(rowElement => {
- rowElement.querySelectorAll(".av__cell").forEach(cellElement => {
+ rowElement.querySelectorAll(".av__cell").forEach((cellElement: HTMLElement) => {
cellElements.push(cellElement);
});
});
@@ -59,7 +54,6 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
- const avID = blockElement.dataset.avId;
const id = blockElement.dataset.nodeId;
let currentRowElement: Element;
const firstColIndex = cellElements[0].getAttribute("data-col-id");
@@ -82,64 +76,12 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
if (!cellElement.classList.contains("av__cell")) {
return true;
}
- const type = getTypeByCellElement(cellElement) || cellElement.dataset.type as TAVCol;
- if (["created", "updated", "template", "rollup"].includes(type) ||
- (type === "block" && !cellElement.dataset.detached)) {
- return;
- }
- const rowID = currentRowElement.getAttribute("data-id");
- const cellId = cellElement.getAttribute("data-id");
- const colId = cellElement.getAttribute("data-col-id");
-
- const oldValue = genCellValueByElement(type, cellElement);
- if (cellValue.type !== type &&
- !(["select", "mSelect"].includes(type) && ["select", "mSelect"].includes(cellValue.type))) {
- if (type === "date") {
- // 类型不能转换时就不进行替换
- return;
- }
- const content = cellValue[cellValue.type as "text"]?.content;
- if (!content) {
- return;
- }
- cellValue = genCellValue(type, cellValue[cellValue.type as "text"].content.toString());
- }
- if (cellValue.type === "block") {
- cellValue.isDetached = true;
- delete cellValue.block.id;
- } else if (type === "select" || type === "mSelect") {
- if (type === "select" && cellValue.type === "mSelect" && cellValue.mSelect.length > 0) {
- cellValue.mSelect.splice(1, cellValue.mSelect.length - 1);
- }
- const operations = mergeAddOption(columns.find(e => e.id === cellElement.dataset.colId), cellValue, avID);
+ const operations = updateCellsValue(protyle, blockElement as HTMLElement,
+ cellValue, [cellElement], columns, html, true);
+ if (operations.doOperations.length > 0) {
doOperations.push(...operations.doOperations);
undoOperations.push(...operations.undoOperations);
}
- cellValue.id = cellId;
- if ((cellValue.type === "date" && typeof cellValue.date === "string") ||
- (cellValue.type === "relation" && typeof cellValue.relation === "string")) {
- return;
- }
- if (objEquals(cellValue, oldValue)) {
- return;
- }
- doOperations.push({
- action: "updateAttrViewCell",
- id: cellId,
- avID,
- keyID: colId,
- rowID,
- data: cellValue
- });
- undoOperations.push({
- action: "updateAttrViewCell",
- id: cellId,
- avID,
- keyID: colId,
- rowID,
- data: oldValue
- });
- updateAttrViewCellAnimation(cellElement, cellValue);
});
});
if (doOperations.length > 0) {
@@ -187,7 +129,6 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
}
const text = protyle.lute.BlockDOM2Content(html);
- const cellElements: HTMLElement[] = Array.from(blockElement.querySelectorAll(".av__cell--active, .av__cell--select"));
const rowsElement = blockElement.querySelectorAll(".av__row--select");
const textJSON: string[][] = [];
@@ -210,6 +151,8 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
updateCellsValue(protyle, blockElement as HTMLElement, text, cellElements, columns, html);
} else {
let currentRowElement: Element;
+ const doOperations: IOperation[] = [];
+ const undoOperations: IOperation[] = [];
const firstColIndex = cellElements[0].getAttribute("data-col-id");
textJSON.forEach((rowValue) => {
if (!currentRowElement) {
@@ -230,9 +173,27 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
if (!cellElement.classList.contains("av__cell")) {
return true;
}
- updateCellsValue(protyle, blockElement as HTMLElement, cellValue, [cellElement], columns, html);
+ const operations = updateCellsValue(protyle, blockElement as HTMLElement, cellValue, [cellElement], columns, html, true);
+ if (operations.doOperations.length > 0) {
+ doOperations.push(...operations.doOperations);
+ undoOperations.push(...operations.undoOperations);
+ }
});
});
+ if (doOperations.length > 0) {
+ const id = blockElement.getAttribute("data-node-id");
+ doOperations.push({
+ action: "doUpdateUpdated",
+ id,
+ data: dayjs().format("YYYYMMDDHHmmss"),
+ });
+ undoOperations.push({
+ action: "doUpdateUpdated",
+ id,
+ data: blockElement.getAttribute("updated"),
+ });
+ transaction(protyle, doOperations, undoOperations);
+ }
}
document.querySelector(".av__panel")?.remove();
} else if (hasClosestByClassName(range.startContainer, "av__title")) {