Skip to content

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

:art: Improve editing selected options in database select fields https://github.com/siyuan-note/siyuan/issues/13777

Daniel3d6da9179c33
diff --git a/app/src/protyle/render/av/openMenuPanel.ts b/app/src/protyle/render/av/openMenuPanel.ts
index 6a2871669c..6bd08fabf1 100644
--- a/app/src/protyle/render/av/openMenuPanel.ts
+++ b/app/src/protyle/render/av/openMenuPanel.ts
@@ -640,6 +640,7 @@ export const openMenuPanel = (options: {
setPosition(menuElement, tabRect.right - menuElement.clientWidth, tabRect.bottom, tabRect.height, 0, true);
event.stopPropagation();
});
+ let suppressSelectClick = false;
// 多选排序
avPanelElement.addEventListener("mousedown", (event: MouseEvent & { target: HTMLElement }) => {
if (event.button === 1 && !hasClosestByClassName(event.target, "b3-menu")) {
@@ -647,19 +648,31 @@ export const openMenuPanel = (options: {
}
if (event.button !== 0 || options.type !== "select") return;
const selectedElement = event.target.closest(".b3-chip--middle") as HTMLElement;
- if (!selectedElement) {
+ if (!selectedElement || !selectedElement.parentElement.classList.contains("b3-chips") ||
+ event.target.closest('[data-type="removeCellOption"]')) {
+ return;
+ }
+ const colId = getColId(options.cellElements[0], data.viewType);
+ const colData = fields.find((item) => item.id === colId);
+ if (colData?.type !== "mSelect" ||
+ selectedElement.parentElement.querySelectorAll(".b3-chip--middle").length < 2) {
return;
}
event.preventDefault();
- document.body.style.cursor = "grabbing";
const documentSelf = document;
documentSelf.ondragstart = () => false;
let ghostElement: HTMLElement;
const diffPosition = {x: 0, y: 0};
+ const startPosition = {x: event.clientX, y: event.clientY};
+ const oldValue = Array.from(selectedElement.parentElement.querySelectorAll(".b3-chip--middle"))
+ .map((item: HTMLElement) => item.dataset.content);
documentSelf.onmousemove = (moveEvent: MouseEvent & { target: HTMLElement }) => {
moveEvent.preventDefault();
moveEvent.stopPropagation();
if (!ghostElement) {
+ if (Math.hypot(moveEvent.clientX - startPosition.x, moveEvent.clientY - startPosition.y) < 5) {
+ return;
+ }
ghostElement = selectedElement.cloneNode(true) as HTMLElement;
document.body.append(ghostElement);
ghostElement.setAttribute("id", "dragGhost");
@@ -667,12 +680,13 @@ export const openMenuPanel = (options: {
ghostElement.style.position = "fixed";
ghostElement.style.zIndex = (window.siyuan.zIndex++).toString();
selectedElement.style.opacity = ".38";
+ document.body.style.cursor = "grabbing";
const selectedRect = selectedElement.getBoundingClientRect();
diffPosition.x = moveEvent.clientX - selectedRect.left;
diffPosition.y = moveEvent.clientY - selectedRect.top;
}
- ghostElement.style.top = (moveEvent.clientY - diffPosition.x) + "px";
- ghostElement.style.left = (moveEvent.clientX - diffPosition.y) + "px";
+ ghostElement.style.top = (moveEvent.clientY - diffPosition.y) + "px";
+ ghostElement.style.left = (moveEvent.clientX - diffPosition.x) + "px";
const targetElement = moveEvent.target.closest(".b3-chip--middle") as HTMLElement;
if (targetElement && targetElement !== selectedElement) {
const nodeRect = targetElement.getBoundingClientRect();
@@ -695,11 +709,20 @@ export const openMenuPanel = (options: {
ghostElement?.remove();
selectedElement.style.opacity = "";
document.body.style.cursor = "";
+ if (!ghostElement) {
+ return;
+ }
const newValue: IAVCellSelectValue[] = [];
selectedElement.parentElement.querySelectorAll(".b3-chip--middle").forEach((item: HTMLElement) => {
- newValue.push({content: item.dataset.content, color: item.style.color.match(/color(\d+)/)[1]});
+ newValue.push({content: item.dataset.content, color: item.dataset.valueColor});
+ });
+ suppressSelectClick = true;
+ setTimeout(() => {
+ suppressSelectClick = false;
});
- updateCellsValue(options.protyle, options.blockElement as HTMLElement, newValue, options.cellElements);
+ if (newValue.some((item, index) => item.content !== oldValue[index])) {
+ updateCellsValue(options.protyle, options.blockElement as HTMLElement, newValue, options.cellElements);
+ }
};
});
avPanelElement.addEventListener("click", async (event: MouseEvent) => {
@@ -711,6 +734,17 @@ export const openMenuPanel = (options: {
type = (event.detail as { type: string }).type;
target = (event.detail as { target: HTMLElement }).target;
}
+ const selectedElement = target?.closest(".b3-chip--middle") as HTMLElement;
+ if (options.type === "select" && selectedElement?.parentElement.classList.contains("b3-chips") &&
+ !target.closest('[data-type="removeCellOption"]')) {
+ if (!suppressSelectClick) {
+ setColOption(options.protyle, data, selectedElement, options.blockElement, isCustomAttr, options.cellElements);
+ }
+ suppressSelectClick = false;
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
while (target && target !== avPanelElement || type) {
type = target?.dataset.type || type;
// toggleCombination 由 change 事件处理,click 直接跳过避免空跑
diff --git a/app/src/protyle/render/av/select.ts b/app/src/protyle/render/av/select.ts
index 05096443bf..5bb0b83159 100644
--- a/app/src/protyle/render/av/select.ts
+++ b/app/src/protyle/render/av/select.ts
@@ -152,15 +152,16 @@ export const removeCellOption = (protyle: IProtyle, cellElements: HTMLElement[],
export const setColOption = (protyle: IProtyle, data: IAV, target: HTMLElement, blockElement: Element, isCustomAttr: boolean, cellElements?: HTMLElement[]) => {
const menuElement = hasClosestByClassName(target, "b3-menu");
- if (!menuElement) {
+ const optionElement = target.closest("[data-name]") as HTMLElement;
+ if (!menuElement || !optionElement) {
return;
}
const blockID = blockElement.getAttribute("data-node-id");
const viewType = blockElement.getAttribute("data-av-type") as TAVView;
const colId = (cellElements && cellElements[0]) ? getColId(cellElements[0], viewType) : menuElement.querySelector(".b3-menu__item").getAttribute("data-col-id");
- let name = target.parentElement.dataset.name;
- let desc = target.parentElement.dataset.desc;
- let color = target.parentElement.dataset.color;
+ let name = optionElement.dataset.name;
+ let desc = optionElement.dataset.desc;
+ let color = optionElement.dataset.color;
const fields = getFieldsByData(data);
const menu = new Menu(Constants.MENU_AV_COL_OPTION, () => {
if ((name === inputElement.value && desc === descElement.value) || !inputElement.value) {
@@ -690,9 +691,11 @@ export const getSelectHTML = (fields: IAVColumn[], cellElements: HTMLElement[],
});
let selectedHTML = "";
const selected: string[] = [];
+ const canSort = colData.type === "mSelect" && cellValues[0].mSelect?.length > 1;
cellValues[0].mSelect?.forEach((item) => {
+ const option = colData.options?.find((colOption) => colOption.name === item.content);
selected.push(item.content);
- selectedHTML += `<div class="b3-chip b3-chip--middle fn__grab" data-content="${escapeAttr(item.content)}" style="white-space: nowrap;max-width:100%;background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})"><span class="fn__ellipsis">${escapeHtml(item.content)}</span><svg class="b3-chip__close" data-type="removeCellOption"><use xlink:href="#iconClose"></use></svg></div>`;
+ selectedHTML += `<div class="b3-chip b3-chip--middle${canSort ? " fn__grab" : " b3-chip--pointer"}" data-content="${escapeAttr(item.content)}" data-name="${escapeAttr(item.content)}" data-desc="${escapeAttr(option?.desc || "")}" data-color="${escapeAttr(option?.color || item.color)}" data-value-color="${escapeAttr(item.color)}" style="white-space: nowrap;max-width:100%;background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})"><span class="fn__ellipsis">${escapeHtml(item.content)}</span><svg class="b3-chip__close" data-type="removeCellOption"><use xlink:href="#iconClose"></use></svg></div>`;
});
return `<div class="b3-menu__items" style="display: flex;flex-direction: column;flex: 1;">