Skip to content

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

:sparkles: Support mouse drag selection for database items https://github.com/siyuan-note/siyuan/issues/14978

Danielc659f4399762
diff --git a/app/src/protyle/render/av/dragSelect.ts b/app/src/protyle/render/av/dragSelect.ts
new file mode 100644
index 0000000000..966ad4d4bc
--- /dev/null
+++ b/app/src/protyle/render/av/dragSelect.ts
@@ -0,0 +1,76 @@
+import {resetAVRowSelect} from "./virtualScroll";
+import {updateAVSelectionStatus} from "./row";
+import {hasClosestByClassName} from "../../util/hasClosest";
+
+const isRectIntersecting = (rect: DOMRect, selectRect: DOMRect) => {
+ return rect.height > 0 && rect.width > 0 &&
+ rect.top < selectRect.bottom && rect.bottom > selectRect.top &&
+ rect.left < selectRect.right && rect.right > selectRect.left;
+};
+
+export const isAVDragSelectSupported = (blockElement: HTMLElement) => {
+ return blockElement.classList.contains("av") &&
+ ["table", "gallery"].includes(blockElement.dataset.avType);
+};
+
+export const applyAVDragSelection = (blockElement: HTMLElement, selectRect: DOMRect) => {
+ const isTable = blockElement.dataset.avType === "table";
+ const itemSelector = isTable ? ".av__row[data-id]" :
+ ".av__gallery-item[data-id]:not([data-type=\"ghost\"])";
+ const selectedIdsByBody = new Map<HTMLElement, Set<string>>();
+ blockElement.querySelectorAll(".av__body").forEach((bodyElement: HTMLElement) => {
+ if (hasClosestByClassName(bodyElement, "av") !== blockElement) {
+ return;
+ }
+ selectedIdsByBody.set(bodyElement, new Set());
+ });
+
+ blockElement.querySelectorAll(itemSelector).forEach((item: HTMLElement) => {
+ if (hasClosestByClassName(item, "av") !== blockElement) {
+ return;
+ }
+ const bodyElement = hasClosestByClassName(item, "av__body") as HTMLElement;
+ const selected = isRectIntersecting(item.getBoundingClientRect(), selectRect);
+ if (isTable) {
+ item.classList.toggle("av__row--select", selected);
+ item.querySelector(".av__firstcol use")?.setAttribute("xlink:href", selected ? "#iconCheck" : "#iconUncheck");
+ } else {
+ item.classList.toggle("av__gallery-item--select", selected);
+ }
+ if (selected && bodyElement && item.dataset.id) {
+ selectedIdsByBody.get(bodyElement)?.add(item.dataset.id);
+ }
+ });
+
+ selectedIdsByBody.forEach((ids, bodyElement) => {
+ resetAVRowSelect(bodyElement, Array.from(ids));
+ });
+ updateAVSelectionStatus(blockElement);
+};
+
+export const clearAVDragSelection = (blockElement: HTMLElement) => {
+ const isTable = blockElement.dataset.avType === "table";
+ blockElement.querySelectorAll(".av__body").forEach((bodyElement: HTMLElement) => {
+ if (hasClosestByClassName(bodyElement, "av") !== blockElement) {
+ return;
+ }
+ resetAVRowSelect(bodyElement, []);
+ });
+ if (isTable) {
+ blockElement.querySelectorAll(".av__row--select").forEach((item: HTMLElement) => {
+ if (hasClosestByClassName(item, "av") !== blockElement) {
+ return;
+ }
+ item.classList.remove("av__row--select");
+ item.querySelector(".av__firstcol use")?.setAttribute("xlink:href", "#iconUncheck");
+ });
+ } else {
+ blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => {
+ if (hasClosestByClassName(item, "av") !== blockElement) {
+ return;
+ }
+ item.classList.remove("av__gallery-item--select");
+ });
+ }
+ updateAVSelectionStatus(blockElement);
+};
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index febe229446..46eb8525d5 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -306,36 +306,54 @@ export const updateHeader = (rowElement: HTMLElement) => {
if (!blockElement) {
return;
}
+ updateAVSelectionStatus(blockElement as HTMLElement);
+};
+
+export const updateAVSelectionStatus = (blockElement: HTMLElement) => {
const avType = blockElement.getAttribute("data-av-type") as TAVView;
- let selectCount: number;
+ let selectCount = 0;
if (avType === "table") {
- const bodyElement = rowElement.parentElement as HTMLElement;
- // 虚拟滚动下 DOM 内只有渲染窗口的行,直接计数会低估;优先用选中快照与已加载行总数。
- const stat = getAVSelectStat(bodyElement);
- selectCount = stat ? stat.selectCount : bodyElement.querySelectorAll(".av__row--select:not(.av__row--header)").length;
- const count = stat ? stat.loadedCount : bodyElement.querySelectorAll(".av__row:not(.av__row--header)").length;
- const headElement = bodyElement.firstElementChild as HTMLElement;
- const headUseElement = headElement.querySelector("use");
- if (count === selectCount && count !== 0) {
- headElement.classList.add("av__row--select");
- headUseElement.setAttribute("xlink:href", "#iconCheck");
- } else if (selectCount === 0) {
- headElement.classList.remove("av__row--select");
- headUseElement.setAttribute("xlink:href", "#iconUncheck");
- } else if (selectCount > 0) {
- headElement.classList.add("av__row--select");
- headUseElement.setAttribute("xlink:href", "#iconIndeterminateCheck");
- }
+ blockElement.querySelectorAll(".av__body").forEach((bodyElement: HTMLElement) => {
+ if (hasClosestByClassName(bodyElement, "av") !== blockElement) {
+ return;
+ }
+ // 虚拟滚动下 DOM 内只有渲染窗口的行,直接计数会低估;优先用选中快照与已加载行总数。
+ const stat = getAVSelectStat(bodyElement);
+ const bodySelectCount = stat ? stat.selectCount :
+ bodyElement.querySelectorAll(".av__row--select[data-id]").length;
+ const count = stat ? stat.loadedCount : bodyElement.querySelectorAll(".av__row[data-id]").length;
+ selectCount += bodySelectCount;
+ const headElement = bodyElement.firstElementChild as HTMLElement;
+ const headUseElement = headElement?.querySelector("use");
+ if (!headElement || !headUseElement) {
+ return;
+ }
+ if (count === bodySelectCount && count !== 0) {
+ headElement.classList.add("av__row--select");
+ headUseElement.setAttribute("xlink:href", "#iconCheck");
+ } else if (bodySelectCount === 0) {
+ headElement.classList.remove("av__row--select");
+ headUseElement.setAttribute("xlink:href", "#iconUncheck");
+ } else {
+ headElement.classList.add("av__row--select");
+ headUseElement.setAttribute("xlink:href", "#iconIndeterminateCheck");
+ }
+ });
} else {
// 卡片/看板视图按分组(.av__body)聚合选中数,看板与分组卡片视图存在多个 body。
- selectCount = 0;
blockElement.querySelectorAll(".av__body").forEach((bodyItem: HTMLElement) => {
+ if (hasClosestByClassName(bodyItem, "av") !== blockElement) {
+ return;
+ }
const stat = getAVSelectStat(bodyItem);
selectCount += stat ? stat.selectCount : bodyItem.querySelectorAll(".av__gallery-item--select").length;
});
}
- const counterElement = blockElement.querySelector(".av__counter");
+ const counterElement = blockElement.querySelector(".av__counter") as HTMLElement;
+ if (!counterElement) {
+ return;
+ }
if (selectCount === 0) {
counterElement.classList.add("fn__none");
return;
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 821b1bd0ce..58a33b32cc 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -116,6 +116,7 @@ import {enhanceRichClipboard} from "../util/richClipboard";
import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spellcheck";
import {getAVTemplateInteractiveElement, isAVTemplateLink} from "../render/av/attributeValue";
import {focusAVByArrow} from "../render/av/focus";
+import {applyAVDragSelection, clearAVDragSelection, isAVDragSelectSupported} from "../render/av/dragSelect";
export class WYSIWYG {
public lastHTMLs: { [key: string]: string } = {};
@@ -1497,7 +1498,72 @@ export class WYSIWYG {
}
const mostTop = protyleRect.top + (protyle.options.render.breadcrumb ? protyle.breadcrumb.element.parentElement.clientHeight : 0);
const selectStartScrollTop = protyle.contentElement.scrollTop;
+ let avDragSelectElement = startsFromPadding && nodeElement && !isInEmbedBlock(nodeElement) &&
+ isAVDragSelectSupported(nodeElement) ? nodeElement : undefined;
+ let avDragSelectRange: { top: number, bottom: number } | undefined;
+ if (avDragSelectElement) {
+ const rect = avDragSelectElement.getBoundingClientRect();
+ avDragSelectRange = {
+ top: rect.top + selectStartScrollTop,
+ bottom: rect.bottom + selectStartScrollTop,
+ };
+ const selectStartY = y + selectStartScrollTop;
+ if (selectStartY < avDragSelectRange.top || selectStartY > avDragSelectRange.bottom) {
+ avDragSelectElement = undefined;
+ avDragSelectRange = undefined;
+ }
+ }
let moveCellElement: HTMLElement;
+ let avDragSelectMode: "items" | "blocks" | undefined;
+ let avDragSelectFrame: number | undefined;
+ let pendingAVDragSelectRect: DOMRect | undefined;
+ let hasInitializedAVDragSelect = false;
+ const dragSelectBlockElements = new Set<Element>();
+ const initializeAVDragSelect = () => {
+ if (!avDragSelectElement || hasInitializedAVDragSelect) {
+ return;
+ }
+ protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
+ item.classList.remove("protyle-wysiwyg--select");
+ item.removeAttribute("select-start");
+ item.removeAttribute("select-end");
+ });
+ hasInitializedAVDragSelect = true;
+ };
+ const clearDragSelectBlocks = () => {
+ dragSelectBlockElements.forEach(item => item.classList.remove("protyle-wysiwyg--select"));
+ dragSelectBlockElements.clear();
+ };
+ const cancelAVDragSelect = () => {
+ if (avDragSelectFrame !== undefined) {
+ cancelAnimationFrame(avDragSelectFrame);
+ avDragSelectFrame = undefined;
+ }
+ pendingAVDragSelectRect = undefined;
+ };
+ const flushAVDragSelect = () => {
+ if (avDragSelectFrame !== undefined) {
+ cancelAnimationFrame(avDragSelectFrame);
+ avDragSelectFrame = undefined;
+ }
+ if (pendingAVDragSelectRect && avDragSelectElement && avDragSelectMode === "items") {
+ applyAVDragSelection(avDragSelectElement, pendingAVDragSelectRect);
+ }
+ pendingAVDragSelectRect = undefined;
+ };
+ const scheduleAVDragSelect = (selectRect: DOMRect) => {
+ pendingAVDragSelectRect = selectRect;
+ if (avDragSelectFrame !== undefined) {
+ return;
+ }
+ avDragSelectFrame = requestAnimationFrame(() => {
+ avDragSelectFrame = undefined;
+ if (pendingAVDragSelectRect && avDragSelectElement && avDragSelectMode === "items") {
+ applyAVDragSelection(avDragSelectElement, pendingAVDragSelectRect);
+ }
+ pendingAVDragSelectRect = undefined;
+ });
+ };
this.element.classList.add("fn__pointer-none");
// 容器类元素判断(划选时 elementFromPoint 命中它们的边缘/空白需继续探测子块)
const isContainer = (el: Element) => el.classList.contains("protyle-wysiwyg") || el.classList.contains("list") ||
@@ -1591,10 +1657,20 @@ export class WYSIWYG {
moveCellElement = undefined;
}
}
+ const scrollTop = protyle.contentElement.scrollTop;
+ const startY = y + selectStartScrollTop;
+ const moveY = Math.max(mostTop, Math.min(moveEvent.clientY, mostBottom)) + scrollTop;
+ const isAVItemMode = avDragSelectRange &&
+ Math.min(startY, moveY) >= avDragSelectRange.top &&
+ Math.max(startY, moveY) <= avDragSelectRange.bottom;
// 在包含 img, video, audio 的元素上划选后无法上下滚动 https://ld246.com/article/1681778773806
// 在包含 img, video, audio 的元素上拖拽无法划选 https://github.com/siyuan-note/siyuan/issues/11763
if (startsFromPadding) {
- dragOverScroll(moveEvent, contentRect, protyle.contentElement);
+ if (!isAVItemMode) {
+ dragOverScroll(moveEvent, contentRect, protyle.contentElement);
+ } else if (avDragSelectMode === "blocks") {
+ stopScrollAnimation();
+ }
} else if ((moveEvent.target as HTMLElement).closest("img, video, audio, .img") &&
(moveEvent.clientY < contentRect.top + Constants.SIZE_SCROLL_TB ||
moveEvent.clientY > contentRect.bottom - Constants.SIZE_SCROLL_TB)) {
@@ -1608,23 +1684,41 @@ export class WYSIWYG {
}
// 向左选择,遇到 gutter 就不会弹出 toolbar
hideElements(["gutter"], protyle);
- const scrollTop = protyle.contentElement.scrollTop;
- const startY = y + selectStartScrollTop;
- const moveY = Math.max(mostTop, Math.min(moveEvent.clientY, mostBottom)) + scrollTop;
const selectLeft = Math.max(Math.min(event.clientX, moveEvent.clientX), wysiwygRect.left);
const selectRight = Math.min(Math.max(event.clientX, moveEvent.clientX), wysiwygRect.right);
const selectTop = Math.min(startY, moveY) - scrollTop;
const selectHeight = Math.abs(moveY - startY);
if (selectHeight < 4) {
+ cancelAVDragSelect();
+ if (avDragSelectElement) {
+ clearDragSelectBlocks();
+ clearAVDragSelection(avDragSelectElement);
+ }
+ avDragSelectMode = undefined;
hideElements(["select"], protyle);
protyle.selectElement.classList.add("fn__none");
protyle.selectElement.removeAttribute("style");
return;
}
+ initializeAVDragSelect();
protyle.selectElement.classList.remove("fn__none");
protyle.selectElement.setAttribute("style", `top:${selectTop - protyleRect.top}px;height:${selectHeight}px;left:${selectLeft - protyleRect.left}px;width:${selectRight - selectLeft}px;`);
const selectRect = protyle.selectElement.getBoundingClientRect();
hideElements(["select"], protyle);
+ if (isAVItemMode && avDragSelectElement) {
+ clearDragSelectBlocks();
+ avDragSelectMode = "items";
+ scheduleAVDragSelect(selectRect);
+ return;
+ }
+ cancelAVDragSelect();
+ if (avDragSelectElement && avDragSelectMode === "items") {
+ clearAVDragSelection(avDragSelectElement);
+ }
+ avDragSelectMode = "blocks";
+ if (avDragSelectElement) {
+ clearDragSelectBlocks();
+ }
// 矩形左边缘落在 padding 内时 elementFromPoint 会命中 wysiwyg 容器,需钳制到内容区
const detectX = Math.max(mostLeft, Math.min(selectRect.left, mostRight));
let firstElement;
@@ -1737,6 +1831,9 @@ export class WYSIWYG {
}
selectElements.forEach(item => {
if (!hasClosestByClassName(item, "protyle-wysiwyg__embed")) {
+ if (avDragSelectElement && !item.classList.contains("protyle-wysiwyg--select")) {
+ dragSelectBlockElements.add(item);
+ }
item.classList.add("protyle-wysiwyg--select");
}
});
@@ -1744,6 +1841,7 @@ export class WYSIWYG {
documentSelf.onmouseup = (mouseUpEvent) => {
protyle.contentElement.removeEventListener("scroll", selectScrollEvent);
+ flushAVDragSelect();
if (startsFromPadding) {
stopScrollAnimation();
}
@@ -2099,12 +2197,17 @@ export class WYSIWYG {
}
}
- const ids: string[] = [];
const selectElement = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
- selectElement.forEach(item => {
- ids.push(item.getAttribute("data-node-id"));
- });
- countBlockWord(ids);
+ if (avDragSelectMode === "items" && avDragSelectElement) {
+ countBlockWord([]);
+ focusBlock(avDragSelectElement);
+ } else {
+ const ids: string[] = [];
+ selectElement.forEach(item => {
+ ids.push(item.getAttribute("data-node-id"));
+ });
+ countBlockWord(ids);
+ }
// 修正三击及跨块选区落在块边界时的 range
if (getSelection().rangeCount > 0) {
const range = getSelection().getRangeAt(0);