From 25728e356426ff0b7c78b21a946eeeccfd254a1b Mon Sep 17 00:00:00 2001 From: Vanessa Date: Thu, 13 Jul 2023 23:48:05 +0800 Subject: [PATCH] :sparkles: https://github.com/siyuan-note/siyuan/issues/8664 --- app/src/protyle/render/av/cell.ts | 49 ++++++++++++++++++++++++ app/src/protyle/render/av/col.ts | 5 ++- app/src/protyle/render/av/filter.ts | 58 ++++++----------------------- app/src/protyle/render/av/select.ts | 2 +- 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts index 7a24a246a..935d6627e 100644 --- a/app/src/protyle/render/av/cell.ts +++ b/app/src/protyle/render/av/cell.ts @@ -2,6 +2,55 @@ import {transaction} from "../../wysiwyg/transaction"; import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest"; import {openMenuPanel} from "./openMenuPanel"; +export const genCellValue = (colType: TAVCol, value: string | { + content: string, + color: string +}[]) => { + 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 + } + }; + } + } else if (colType === "text") { + cellValue = { + type: colType, + text: { + content: value + } + }; + } else if (colType === "mSelect" || colType === "select") { + return cellValue = { + type: colType, + mSelect: [{ + content: value, + color: "" + }] + }; + } + return cellValue; + } + if (colType === "mSelect" || colType === "select") { + return cellValue = { + type: colType, + mSelect: value + }; + } +}; + export const popTextCell = (protyle: IProtyle, cellElement: HTMLElement) => { const type = cellElement.parentElement.parentElement.firstElementChild.querySelector(`[data-col-id="${cellElement.getAttribute("data-col-id")}"]`).getAttribute("data-dtype") as TAVCol; const cellRect = cellElement.getBoundingClientRect(); diff --git a/app/src/protyle/render/av/col.ts b/app/src/protyle/render/av/col.ts index e14b1a058..9c1426e0e 100644 --- a/app/src/protyle/render/av/col.ts +++ b/app/src/protyle/render/av/col.ts @@ -2,7 +2,8 @@ import {hasClosestBlock} from "../../util/hasClosest"; import {Menu} from "../../../plugin/Menu"; import {transaction} from "../../wysiwyg/transaction"; import {fetchPost} from "../../../util/fetch"; -import {genCellValue, setFilter} from "./filter"; +import {getDefaultOperatorByType, setFilter} from "./filter"; +import {genCellValue} from "./cell"; export const getColIconByType = (type: TAVCol) => { switch (type) { @@ -155,7 +156,7 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl if (!filter) { filter = { column: colId, - operator: "Contains", + operator: getDefaultOperatorByType(type), value: genCellValue(type, "") }; avData.view.filters.push(filter); diff --git a/app/src/protyle/render/av/filter.ts b/app/src/protyle/render/av/filter.ts index be0e6352e..4d8ab8b63 100644 --- a/app/src/protyle/render/av/filter.ts +++ b/app/src/protyle/render/av/filter.ts @@ -4,53 +4,14 @@ import {hasClosestByClassName} from "../../util/hasClosest"; import {getColIconByType} from "./col"; import {setPosition} from "../../../util/setPosition"; import {objEquals} from "../../../util/functions"; +import {genCellValue} from "./cell"; -export const genCellValue = (colType: TAVCol, value: string | { - content: string, - color: string -}[]) => { - 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 - } - }; - } - } else if (colType === "text") { - cellValue = { - type: colType, - text: { - content: value - } - }; - } else if (colType === "mSelect" || colType === "select") { - return cellValue = { - type: colType, - mSelect: [{ - content: value, - color: "" - }] - }; - } - return cellValue; +export const getDefaultOperatorByType = (type: TAVCol) => { + if (type === "number" || type === "select") { + return "="; } - if (colType === "mSelect" || colType === "select") { - return cellValue = { - type: colType, - mSelect: value - }; + if (type === "text" || type === "mSelect") { + return "Contains"; } }; @@ -94,6 +55,9 @@ export const setFilter = (options: { }); } }); + if (mSelect.length === 0) { + mSelect.push({color: "", content: ""}); + } cellValue = genCellValue(options.filter.value.type, mSelect); } const newFilter: IAVFilter = { @@ -298,7 +262,7 @@ export const addFilter = (options: { const cellValue = genCellValue(column.type, ""); options.data.view.filters.push({ column: column.id, - operator: "Contains", + operator: getDefaultOperatorByType(column.type), value: cellValue, }); transaction(options.protyle, [{ @@ -315,7 +279,7 @@ export const addFilter = (options: { const filterElement = options.menuElement.querySelector(`[data-id="${column.id}"] .b3-chip`) as HTMLElement; setFilter({ filter: { - operator: "Contains", + operator: getDefaultOperatorByType(column.type), column: column.id, value: cellValue }, diff --git a/app/src/protyle/render/av/select.ts b/app/src/protyle/render/av/select.ts index 3d7511bfe..3df4a65e8 100644 --- a/app/src/protyle/render/av/select.ts +++ b/app/src/protyle/render/av/select.ts @@ -3,7 +3,7 @@ import {transaction} from "../../wysiwyg/transaction"; import {hasClosestByClassName} from "../../util/hasClosest"; import {confirmDialog} from "../../../dialog/confirmDialog"; import {upDownHint} from "../../../util/upDownHint"; -import {genCellValue} from "./filter"; +import {genCellValue} from "./cell"; const filterSelectHTML = (key: string, options: { name: string, color: string }[]) => { let html = "";