2023-06-10 16:27:52 +08:00
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
2023-06-30 11:11:45 +08:00
|
|
|
import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest";
|
2023-07-09 23:54:32 +08:00
|
|
|
import {openMenuPanel} from "./openMenuPanel";
|
2023-07-15 22:24:54 +08:00
|
|
|
import {Menu} from "../../../plugin/Menu";
|
2023-09-21 10:54:17 +08:00
|
|
|
import {updateAttrViewCellAnimation} from "./action";
|
2023-09-28 17:25:18 +08:00
|
|
|
import {isCtrl} from "../../util/compatibility";
|
2023-10-06 10:56:30 +08:00
|
|
|
import {objEquals} from "../../../util/functions";
|
2023-10-13 11:30:44 +08:00
|
|
|
import {fetchPost} from "../../../util/fetch";
|
2023-10-13 11:55:51 +08:00
|
|
|
import {focusBlock} from "../../util/selection";
|
2023-06-10 12:22:10 +08:00
|
|
|
|
2023-07-15 23:18:35 +08:00
|
|
|
export const getCalcValue = (column: IAVColumn) => {
|
|
|
|
|
if (!column.calc || !column.calc.result) {
|
2023-07-15 23:19:40 +08:00
|
|
|
return "";
|
2023-07-15 23:18:35 +08:00
|
|
|
}
|
2023-07-23 23:03:44 +08:00
|
|
|
let resultCalc: any = column.calc.result.number;
|
|
|
|
|
if (column.calc.operator === "Earliest" || column.calc.operator === "Latest" ||
|
2023-10-09 11:33:29 +08:00
|
|
|
(column.calc.operator === "Range" && ["date", "created", "updated"].includes(column.type))) {
|
|
|
|
|
resultCalc = column.calc.result[column.type as "date"];
|
2023-07-23 23:03:44 +08:00
|
|
|
}
|
2023-07-15 23:18:35 +08:00
|
|
|
let value = "";
|
|
|
|
|
switch (column.calc.operator) {
|
|
|
|
|
case "Count all":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultCountAll}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Count values":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultCountValues}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Count unique values":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultCountUniqueValues}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Count empty":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultCountEmpty}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Count not empty":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultCountNotEmpty}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Percent empty":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultPercentEmpty}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Percent not empty":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultPercentNotEmpty}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Sum":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultSum}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Average":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultAverage}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Median":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultMedian}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Min":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultMin}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Max":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultMax}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
|
|
|
|
case "Range":
|
2023-07-16 22:03:39 +08:00
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcResultRange}`;
|
2023-07-15 23:18:35 +08:00
|
|
|
break;
|
2023-07-23 18:57:11 +08:00
|
|
|
case "Earliest":
|
|
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcOperatorEarliest}`;
|
|
|
|
|
break;
|
|
|
|
|
case "Latest":
|
|
|
|
|
value = `<span>${resultCalc.formattedContent}</span>${window.siyuan.languages.calcOperatorLatest}`;
|
|
|
|
|
break;
|
2023-07-15 23:18:35 +08:00
|
|
|
}
|
|
|
|
|
return value;
|
2023-07-15 23:19:40 +08:00
|
|
|
};
|
2023-07-15 23:18:35 +08:00
|
|
|
|
2023-09-22 20:24:00 +08:00
|
|
|
export const genCellValue = (colType: TAVCol, value: string | any) => {
|
2023-07-13 23:48:05 +08:00
|
|
|
let cellValue: IAVCellValue;
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
|
if (colType === "number") {
|
|
|
|
|
if (value) {
|
|
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
|
|
|
|
number: {
|
|
|
|
|
content: parseFloat(value),
|
|
|
|
|
isNotEmpty: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
|
|
|
|
number: {
|
|
|
|
|
isNotEmpty: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-10-09 09:52:40 +08:00
|
|
|
} else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) {
|
2023-07-13 23:48:05 +08:00
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
2023-08-02 14:27:49 +08:00
|
|
|
[colType]: {
|
2023-07-13 23:48:05 +08:00
|
|
|
content: value
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} else if (colType === "mSelect" || colType === "select") {
|
2023-07-27 00:47:14 +08:00
|
|
|
cellValue = {
|
2023-07-13 23:48:05 +08:00
|
|
|
type: colType,
|
|
|
|
|
mSelect: [{
|
|
|
|
|
content: value,
|
|
|
|
|
color: ""
|
|
|
|
|
}]
|
|
|
|
|
};
|
2023-10-09 11:33:29 +08:00
|
|
|
} else if (["date", "created", "updated"].includes(colType) && value === "") {
|
2023-07-27 13:50:59 +08:00
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
2023-10-09 11:33:29 +08:00
|
|
|
[colType]: {
|
2023-07-27 13:50:59 +08:00
|
|
|
content: null,
|
2023-08-02 00:14:05 +08:00
|
|
|
isNotEmpty: false,
|
2023-07-27 13:50:59 +08:00
|
|
|
content2: null,
|
2023-08-02 00:14:05 +08:00
|
|
|
isNotEmpty2: false,
|
2023-07-27 13:50:59 +08:00
|
|
|
hasEndDate: false,
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-07-13 23:48:05 +08:00
|
|
|
}
|
2023-07-27 00:47:14 +08:00
|
|
|
} else {
|
|
|
|
|
if (colType === "mSelect" || colType === "select") {
|
|
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
2023-09-22 20:24:00 +08:00
|
|
|
mSelect: value as IAVCellSelectValue[]
|
2023-07-27 00:47:14 +08:00
|
|
|
};
|
2023-10-09 11:33:29 +08:00
|
|
|
} else if (["date", "created", "updated"].includes(colType)) {
|
2023-07-27 00:47:14 +08:00
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
2023-10-09 11:33:29 +08:00
|
|
|
[colType]: value as IAVCellDateValue
|
2023-07-27 00:47:14 +08:00
|
|
|
};
|
2023-09-22 20:24:00 +08:00
|
|
|
} else if (colType === "mAsset") {
|
|
|
|
|
cellValue = {
|
|
|
|
|
type: colType,
|
|
|
|
|
mAsset: value as IAVCellAssetValue[]
|
|
|
|
|
};
|
2023-07-27 00:47:14 +08:00
|
|
|
}
|
2023-07-13 23:48:05 +08:00
|
|
|
}
|
2023-07-27 00:47:14 +08:00
|
|
|
return cellValue;
|
2023-07-13 23:48:05 +08:00
|
|
|
};
|
|
|
|
|
|
2023-07-15 22:24:54 +08:00
|
|
|
const calcItem = (options: {
|
|
|
|
|
menu: Menu,
|
|
|
|
|
protyle: IProtyle,
|
|
|
|
|
label: string,
|
|
|
|
|
operator: string,
|
|
|
|
|
oldOperator: string,
|
|
|
|
|
colId: string,
|
|
|
|
|
avId: string
|
|
|
|
|
}) => {
|
|
|
|
|
options.menu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: options.label,
|
|
|
|
|
click() {
|
|
|
|
|
transaction(options.protyle, [{
|
|
|
|
|
action: "setAttrViewColCalc",
|
|
|
|
|
avID: options.avId,
|
|
|
|
|
id: options.colId,
|
|
|
|
|
data: {
|
|
|
|
|
operator: options.operator
|
|
|
|
|
}
|
|
|
|
|
}], [{
|
|
|
|
|
action: "setAttrViewColCalc",
|
|
|
|
|
avID: options.avId,
|
|
|
|
|
id: options.colId,
|
|
|
|
|
data: {
|
|
|
|
|
operator: options.oldOperator
|
|
|
|
|
}
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-15 23:19:40 +08:00
|
|
|
};
|
2023-10-04 20:11:33 +08:00
|
|
|
|
2023-07-15 22:24:54 +08:00
|
|
|
export const openCalcMenu = (protyle: IProtyle, calcElement: HTMLElement) => {
|
|
|
|
|
const blockElement = hasClosestBlock(calcElement);
|
|
|
|
|
if (!blockElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
calcElement.parentElement.classList.add("av__row--show");
|
|
|
|
|
const menu = new Menu("av-calc", () => {
|
|
|
|
|
calcElement.parentElement.classList.remove("av__row--show");
|
|
|
|
|
});
|
|
|
|
|
if (menu.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const type = calcElement.dataset.dtype as TAVCol;
|
2023-07-15 22:31:51 +08:00
|
|
|
const colId = calcElement.dataset.colId;
|
2023-07-15 22:24:54 +08:00
|
|
|
const avId = blockElement.dataset.avId;
|
|
|
|
|
const oldOperator = calcElement.dataset.operator;
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorNone
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Count all",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorCountAll
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Count values",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorCountValues
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Count unique values",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorCountUniqueValues
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Count empty",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorCountEmpty
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Count not empty",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorCountNotEmpty
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Percent empty",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorPercentEmpty
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Percent not empty",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorPercentNotEmpty
|
|
|
|
|
});
|
2023-10-12 12:01:53 +08:00
|
|
|
if (["number", "template"].includes(type)) {
|
2023-07-15 22:24:54 +08:00
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Sum",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorSum
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Average",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorAverage
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Median",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorMedian
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Min",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorMin
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Max",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorMax
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Range",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorRange
|
|
|
|
|
});
|
2023-10-09 11:33:29 +08:00
|
|
|
} else if (["date", "created", "updated"].includes(type)) {
|
2023-07-23 18:57:11 +08:00
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Earliest",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorEarliest
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Latest",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorLatest
|
|
|
|
|
});
|
|
|
|
|
calcItem({
|
|
|
|
|
menu,
|
|
|
|
|
protyle,
|
|
|
|
|
colId,
|
|
|
|
|
avId,
|
|
|
|
|
oldOperator,
|
|
|
|
|
operator: "Range",
|
|
|
|
|
label: window.siyuan.languages.calcOperatorRange
|
|
|
|
|
});
|
2023-07-15 22:24:54 +08:00
|
|
|
}
|
|
|
|
|
const calcRect = calcElement.getBoundingClientRect();
|
|
|
|
|
menu.open({x: calcRect.left, y: calcRect.bottom, h: calcRect.height});
|
2023-07-15 23:19:40 +08:00
|
|
|
};
|
2023-07-15 22:24:54 +08:00
|
|
|
|
2023-10-13 21:50:14 +08:00
|
|
|
export const cellScrollIntoView = (blockElement: HTMLElement, cellRect: DOMRect, onlyHeight = true) => {
|
|
|
|
|
if (!onlyHeight) {
|
|
|
|
|
const avScrollElement = blockElement.querySelector(".av__scroll");
|
|
|
|
|
const avScrollRect = avScrollElement.getBoundingClientRect();
|
|
|
|
|
if (avScrollRect.left > cellRect.left) {
|
|
|
|
|
avScrollElement.scrollLeft = avScrollElement.scrollLeft + cellRect.left - avScrollRect.left;
|
|
|
|
|
} else if (avScrollRect.right < cellRect.right) {
|
|
|
|
|
avScrollElement.scrollLeft = avScrollElement.scrollLeft + cellRect.right - avScrollRect.right;
|
|
|
|
|
}
|
2023-10-13 12:33:29 +08:00
|
|
|
}
|
|
|
|
|
const avHeaderRect = blockElement.querySelector(".av__header").getBoundingClientRect()
|
|
|
|
|
if (avHeaderRect.bottom > cellRect.top) {
|
2023-10-13 21:50:14 +08:00
|
|
|
const contentElement = hasClosestByClassName(blockElement, "protyle-content", true);
|
2023-10-13 12:33:29 +08:00
|
|
|
if (contentElement) {
|
|
|
|
|
contentElement.scrollTop = contentElement.scrollTop + cellRect.top - avHeaderRect.bottom;
|
|
|
|
|
}
|
2023-10-13 21:50:14 +08:00
|
|
|
} else {
|
|
|
|
|
const avFooterRect = blockElement.querySelector(".av__row--footer").getBoundingClientRect();
|
|
|
|
|
if (avFooterRect.top < cellRect.bottom) {
|
|
|
|
|
const contentElement = hasClosestByClassName(blockElement, "protyle-content", true);
|
|
|
|
|
if (contentElement) {
|
|
|
|
|
contentElement.scrollTop = contentElement.scrollTop + cellRect.bottom - avFooterRect.top;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-13 12:33:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 17:53:28 +08:00
|
|
|
export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type?: TAVCol) => {
|
|
|
|
|
if (!type) {
|
|
|
|
|
type = cellElements[0].parentElement.parentElement.firstElementChild.querySelector(`[data-col-id="${cellElements[0].getAttribute("data-col-id")}"]`).getAttribute("data-dtype") as TAVCol;
|
|
|
|
|
}
|
2023-10-13 11:30:44 +08:00
|
|
|
if (type === "updated" || type === "created") {
|
2023-10-01 17:41:00 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-09-27 23:58:48 +08:00
|
|
|
if (type === "block" && (cellElements.length > 1 || !cellElements[0].getAttribute("data-detached"))) {
|
2023-08-02 22:48:36 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-10-13 09:37:24 +08:00
|
|
|
const blockElement = hasClosestBlock(cellElements[0]);
|
|
|
|
|
let cellRect = cellElements[0].getBoundingClientRect();
|
|
|
|
|
if (blockElement) {
|
2023-10-13 12:33:29 +08:00
|
|
|
cellScrollIntoView(blockElement, cellRect)
|
2023-10-13 09:37:24 +08:00
|
|
|
}
|
|
|
|
|
cellRect = cellElements[0].getBoundingClientRect();
|
2023-06-10 17:45:04 +08:00
|
|
|
let html = "";
|
2023-10-08 09:30:23 +08:00
|
|
|
const style = `style="position:absolute;left: ${cellRect.left}px;top: ${cellRect.top}px;width:${Math.max(cellRect.width, 100)}px;height: ${cellRect.height}px"`;
|
2023-10-13 11:30:44 +08:00
|
|
|
if (["text", "url", "email", "phone", "block", "template"].includes(type)) {
|
2023-07-28 15:20:34 +08:00
|
|
|
html = `<textarea ${style} class="b3-text-field">${cellElements[0].firstElementChild.textContent}</textarea>`;
|
2023-10-01 17:41:00 +08:00
|
|
|
} else if (type === "number") {
|
2023-08-03 23:23:28 +08:00
|
|
|
html = `<input type="number" value="${cellElements[0].firstElementChild.getAttribute("data-content")}" ${style} class="b3-text-field">`;
|
2023-07-13 23:16:07 +08:00
|
|
|
} else if (["select", "mSelect"].includes(type) && blockElement) {
|
2023-07-18 01:03:35 +08:00
|
|
|
openMenuPanel({protyle, blockElement, type: "select", cellElements});
|
2023-07-22 10:18:11 +08:00
|
|
|
return;
|
2023-09-22 12:11:56 +08:00
|
|
|
} else if (type === "mAsset" && blockElement) {
|
|
|
|
|
openMenuPanel({protyle, blockElement, type: "asset", cellElements});
|
|
|
|
|
return;
|
2023-07-22 10:18:11 +08:00
|
|
|
} else if (type === "date" && blockElement) {
|
|
|
|
|
openMenuPanel({protyle, blockElement, type: "date", cellElements});
|
2023-07-09 23:54:32 +08:00
|
|
|
return;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
2023-07-13 09:43:10 +08:00
|
|
|
window.siyuan.menus.menu.remove();
|
2023-09-02 11:13:47 +08:00
|
|
|
document.body.insertAdjacentHTML("beforeend", `<div class="av__mask" style="z-index: ${++window.siyuan.zIndex}">
|
2023-06-10 16:27:52 +08:00
|
|
|
${html}
|
|
|
|
|
</div>`);
|
|
|
|
|
const avMaskElement = document.querySelector(".av__mask");
|
|
|
|
|
const inputElement = avMaskElement.querySelector(".b3-text-field") as HTMLInputElement;
|
|
|
|
|
if (inputElement) {
|
|
|
|
|
inputElement.select();
|
2023-06-30 17:05:54 +08:00
|
|
|
inputElement.focus();
|
2023-10-13 11:30:44 +08:00
|
|
|
if (blockElement && type === "template") {
|
|
|
|
|
fetchPost("/api/av/renderAttributeView", {id: blockElement.dataset.avId}, (response) => {
|
|
|
|
|
response.data.view.columns.find((item: IAVColumn) => {
|
|
|
|
|
if (item.id === cellElements[0].dataset.colId) {
|
|
|
|
|
inputElement.value = item.template;
|
|
|
|
|
inputElement.dataset.template = item.template;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
inputElement.addEventListener("blur", () => {
|
2023-07-17 12:54:24 +08:00
|
|
|
updateCellValue(protyle, type, cellElements);
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-06-10 17:45:04 +08:00
|
|
|
return;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
2023-09-28 17:25:18 +08:00
|
|
|
if (event.key === "Escape" ||
|
|
|
|
|
(event.key === "Enter" && !event.shiftKey && !isCtrl(event))) {
|
2023-07-17 12:54:24 +08:00
|
|
|
updateCellValue(protyle, type, cellElements);
|
2023-06-10 16:27:52 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
|
|
|
|
avMaskElement.addEventListener("click", (event) => {
|
|
|
|
|
if ((event.target as HTMLElement).classList.contains("av__mask")) {
|
|
|
|
|
avMaskElement?.remove();
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
};
|
|
|
|
|
|
2023-07-17 12:54:24 +08:00
|
|
|
const updateCellValue = (protyle: IProtyle, type: TAVCol, cellElements: HTMLElement[]) => {
|
2023-10-12 20:40:09 +08:00
|
|
|
if (!document.contains(cellElements[0]) && cellElements.length === 1) {
|
|
|
|
|
// 原始 cell 已被更新
|
2023-09-28 00:33:00 +08:00
|
|
|
const avid = cellElements[0].parentElement.dataset.avid;
|
2023-10-12 20:40:09 +08:00
|
|
|
if (avid) {
|
|
|
|
|
// 新增行后弹出的输入框
|
|
|
|
|
cellElements[0] = protyle.wysiwyg.element.querySelector(`[data-av-id="${avid}"] .av__row--add`).previousElementSibling.querySelector('[data-detached="true"]');
|
|
|
|
|
} else {
|
|
|
|
|
// 修改单元格后立即修改其他单元格
|
|
|
|
|
cellElements[0] = protyle.wysiwyg.element.querySelector(`.av__cell[data-id="${cellElements[0].dataset.id}"]`);
|
|
|
|
|
if (!cellElements[0]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-28 00:33:00 +08:00
|
|
|
}
|
2023-09-28 17:25:18 +08:00
|
|
|
if (cellElements.length === 1 && cellElements[0].dataset.detached === "true" && !cellElements[0].parentElement.dataset.id) {
|
2023-09-28 00:33:00 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-17 12:54:24 +08:00
|
|
|
const blockElement = hasClosestBlock(cellElements[0]);
|
2023-06-10 16:27:52 +08:00
|
|
|
if (!blockElement) {
|
2023-06-10 17:45:04 +08:00
|
|
|
return;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
2023-07-17 12:54:24 +08:00
|
|
|
|
2023-07-17 19:56:52 +08:00
|
|
|
const avMaskElement = document.querySelector(".av__mask");
|
|
|
|
|
const doOperations: IOperation[] = [];
|
|
|
|
|
const undoOperations: IOperation[] = [];
|
2023-07-13 00:07:33 +08:00
|
|
|
const avID = blockElement.getAttribute("data-av-id");
|
2023-10-13 11:30:44 +08:00
|
|
|
if (type === "template") {
|
|
|
|
|
const colId = cellElements[0].getAttribute("data-col-id");
|
|
|
|
|
const textElement = avMaskElement.querySelector(".b3-text-field") as HTMLInputElement;
|
2023-10-13 11:34:19 +08:00
|
|
|
if (textElement.value !== textElement.dataset.template) {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColTemplate",
|
|
|
|
|
id: colId,
|
|
|
|
|
avID,
|
|
|
|
|
data: textElement.value,
|
|
|
|
|
type: "template",
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColTemplate",
|
|
|
|
|
id: colId,
|
|
|
|
|
avID,
|
|
|
|
|
data: textElement.dataset.template,
|
|
|
|
|
type: "template",
|
|
|
|
|
}]);
|
|
|
|
|
}
|
2023-10-13 11:30:44 +08:00
|
|
|
} else {
|
|
|
|
|
cellElements.forEach((item) => {
|
|
|
|
|
const rowElement = hasClosestByClassName(item, "av__row");
|
|
|
|
|
if (!rowElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const rowID = rowElement.getAttribute("data-id");
|
|
|
|
|
const cellId = item.getAttribute("data-id");
|
|
|
|
|
const colId = item.getAttribute("data-col-id");
|
|
|
|
|
const inputValue: { content: string | number, isNotEmpty?: boolean } = {
|
|
|
|
|
content: (avMaskElement.querySelector(".b3-text-field") as HTMLInputElement).value
|
|
|
|
|
};
|
|
|
|
|
const oldValue: { content: string | number, isNotEmpty?: boolean } = {
|
|
|
|
|
content: type === "block" ? item.firstElementChild.textContent.trim() : item.textContent.trim()
|
|
|
|
|
};
|
|
|
|
|
if (type === "number") {
|
|
|
|
|
oldValue.content = parseFloat(oldValue.content as string);
|
|
|
|
|
oldValue.isNotEmpty = typeof oldValue.content === "number" && !isNaN(oldValue.content);
|
|
|
|
|
inputValue.content = parseFloat(inputValue.content as string);
|
|
|
|
|
inputValue.isNotEmpty = typeof inputValue.content === "number" && !isNaN(inputValue.content);
|
2023-07-17 12:54:24 +08:00
|
|
|
}
|
2023-10-13 11:30:44 +08:00
|
|
|
if (objEquals(inputValue, oldValue)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
doOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
avID,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
data: {
|
|
|
|
|
[type]: inputValue
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
undoOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
avID,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
data: {
|
|
|
|
|
[type]: oldValue
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
updateAttrViewCellAnimation(item);
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-10-13 11:30:44 +08:00
|
|
|
}
|
2023-10-06 10:56:30 +08:00
|
|
|
if (doOperations.length > 0) {
|
|
|
|
|
transaction(protyle, doOperations, undoOperations);
|
|
|
|
|
}
|
2023-10-13 10:14:11 +08:00
|
|
|
cellElements[0].classList.add("av__cell--select");
|
2023-10-13 11:55:51 +08:00
|
|
|
if (blockElement) {
|
|
|
|
|
focusBlock(blockElement);
|
|
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
avMaskElement.remove();
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
|
|
|
|
};
|