Skip to content

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

:art: Improve database range selection https://github.com/siyuan-note/siyuan/issues/11975

Daniel89996293158a
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 7e4a9f26b5..9541ab2666 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -65,7 +65,9 @@ import {hideTooltip} from "../../dialog/tooltip";
import {appearanceMenu} from "../toolbar/Font";
import {setPosition} from "../../util/setPosition";
import {emitOpenMenu} from "../../plugin/EventBus";
-import {insertAttrViewBlockAnimation, updateHeader} from "../render/av/row";
+import {insertAttrViewBlockAnimation, selectRow, updateHeader} from "../render/av/row";
+import {getAVSelectedItemPoints} from "../render/av/virtualScroll";
+import {setAVItemAnchor} from "../render/av/rangeSelect";
import {getAVFilteredTipContext, getAVViewID} from "../render/av/filteredTip";
import {avContextmenu, duplicateCompletely} from "../render/av/action";
import {getPlainText} from "../util/paste";
@@ -138,9 +140,9 @@ export class Gutter {
}
let selectIds: string[] = [];
let selectElements: Element[] = [];
- let avElement: Element;
+ let avElement: HTMLElement;
if (buttonElement.dataset.rowId) {
- avElement = Array.from(protyle.wysiwyg.element.querySelectorAll(`.av[data-node-id="${buttonElement.dataset.nodeId}"]`)).find((item: HTMLElement) => {
+ avElement = Array.from(protyle.wysiwyg.element.querySelectorAll<HTMLElement>(`.av[data-node-id="${buttonElement.dataset.nodeId}"]`)).find((item: HTMLElement) => {
if (!isInEmbedBlock(item) && !isInAVBlock(item)) {
return true;
}
@@ -159,18 +161,15 @@ export class Gutter {
}
const rowElement = avElement.querySelector(`.av__body${buttonElement.dataset.groupId ? `[data-group-id="${buttonElement.dataset.groupId}"]` : ""} .av__row[data-id="${buttonElement.dataset.rowId}"]`);
if (!rowElement.classList.contains("av__row--select")) {
- avElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(item => {
- item.classList.remove("av__row--select");
- item.querySelector("use").setAttribute("xlink:href", "#iconUncheck");
- });
+ clearSelect(["row"], avElement);
+ selectRow(rowElement.querySelector(".av__firstcol"), "select");
+ setAVItemAnchor(avElement, rowElement as HTMLElement);
}
- rowElement.classList.add("av__row--select");
- rowElement.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconCheck");
updateHeader(rowElement as HTMLElement);
+ getAVSelectedItemPoints(avElement).forEach(item => {
+ selectIds.push(item.itemID + (item.groupID ? "@" + item.groupID : ""));
+ });
avElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(item => {
- const avBodyElement = hasClosestByClassName(item, "av__body") as HTMLElement;
- const groupId = (avBodyElement ? avBodyElement.dataset.groupId : "") || "";
- selectIds.push(item.getAttribute("data-id") + (groupId ? "@" + groupId : ""));
selectElements.push(item);
});
} else {
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index e20110e0d6..8eaac89449 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -59,6 +59,7 @@ import {
} from "./coverPosition";
import {getEditableAVFields, openAVFieldEditor} from "./batchEdit";
import {getAVTemplateInteractiveElement, isAVTemplateLink} from "./attributeValue";
+import {isMobile} from "../../../util/functions";
const isDetachedDatabaseCell = (cellElement: HTMLElement) => {
return cellElement.dataset.detached === "true" || !cellElement.querySelector(".av__celltext--ref");
@@ -367,7 +368,11 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle
return;
}
if (cellType === "updated" || cellType === "created" || cellType === "lineNumber") {
- clearSelect(["row"], blockElement);
+ if (isMobile()) {
+ selectRow(rowElement.querySelector(".av__firstcol"), "toggle");
+ } else {
+ clearSelect(["row"], blockElement);
+ }
} else {
scrollElement.querySelectorAll(".av__row--select").forEach(item => {
item.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconUncheck");
@@ -503,7 +508,7 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle
} else if (target.classList.contains("av__firstcol")) {
window.siyuan.menus.menu.remove();
const rowElement = hasClosestByClassName(target, "av__row") as HTMLElement;
- if (event.shiftKey) {
+ if (!isMobile() && event.shiftKey) {
selectAVItemRange(blockElement, rowElement);
} else {
selectRow(target, "toggle");
@@ -551,7 +556,11 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle
event.stopPropagation();
return true;
} else if (target.classList.contains("av__row") && event.shiftKey && !target.classList.contains("av__row--header")) {
- selectAVItemRange(blockElement, target);
+ if (isMobile()) {
+ selectRow(target.querySelector(".av__firstcol"), "toggle");
+ } else {
+ selectAVItemRange(blockElement, target);
+ }
event.preventDefault();
event.stopPropagation();
return true;
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index 3b413ffac2..23423a98d9 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -742,7 +742,8 @@ const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: H
export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLElement, value?: any,
cElements?: HTMLElement[], columns?: IAVColumn[], html?: string, getOperations = false,
- forceOperation = false, replaceSelectValues = false) => {
+ forceOperation = false, replaceSelectValues = false,
+ stableCells?: IAVSelectedCell[]) => {
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
@@ -755,7 +756,7 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLEleme
if (cElements?.length > 0) {
cellElements = cElements;
} else {
- selectedCells = getAVSelectedCells(nodeElement);
+ selectedCells = stableCells || getAVSelectedCells(nodeElement);
if (selectedCells.length === 0) {
cellElements = Array.from(nodeElement.querySelectorAll(".av__cell--active, .av__cell--select"));
}
@@ -1184,8 +1185,7 @@ const getCellValueText = (value: IAVCellValue, column?: IAVColumn, rowIndex = 0)
return getCellText(cellElement);
};
-export const getAVSelectedCellData = (blockElement: HTMLElement) => {
- const selectedCells = getAVSelectedCells(blockElement);
+export const getAVCellData = (selectedCells: IAVSelectedCell[]) => {
const json: IAVCellValue[][] = [];
let text = "";
selectedCells.forEach((item, index) => {
@@ -1202,6 +1202,10 @@ export const getAVSelectedCellData = (blockElement: HTMLElement) => {
};
};
+export const getAVSelectedCellData = (blockElement: HTMLElement) => {
+ return getAVCellData(getAVSelectedCells(blockElement));
+};
+
const renderRollup = (cellValue: IAVCellValue, showIcon: boolean) => {
let text = "";
if (["text"].includes(cellValue.type)) {
@@ -1318,7 +1322,7 @@ export const dragFillCellsValue = (protyle: IProtyle, nodeElement: HTMLElement,
const showIcon = activeElement.querySelector(".b3-menu__avemoji") ? true : false;
Object.keys(newData).forEach((rowID, index) => {
newData[rowID].forEach((item, cellIndex) => {
- if (["rollup", "template", "created", "updated"].includes(item.type) ||
+ if (["rollup", "template", "created", "updated", "lineNumber"].includes(item.type) ||
(item.type === "block" && item.element.getAttribute("data-detached") !== "true")) {
return;
}
@@ -1341,6 +1345,7 @@ export const dragFillCellsValue = (protyle: IProtyle, nodeElement: HTMLElement,
item.element.innerHTML = renderCell(data, 0, showIcon, "table", undefined,
item.element.dataset.dateFormat as TAVDateFormat);
renderCellAttr(item.element, data);
+ updateAVSelectedCellValue(nodeElement, rowID, keyID, data);
delete item.colId;
delete item.element;
undoOperations.push({
diff --git a/app/src/protyle/render/av/dragSelect.ts b/app/src/protyle/render/av/dragSelect.ts
index a6aa77ccef..0f8f1cc791 100644
--- a/app/src/protyle/render/av/dragSelect.ts
+++ b/app/src/protyle/render/av/dragSelect.ts
@@ -1,6 +1,7 @@
import {resetAVRowSelect} from "./virtualScroll";
import {updateAVSelectionStatus} from "./row";
import {hasClosestByClassName} from "../../util/hasClosest";
+import {clearAVItemSelectionState} from "./selectionState";
const isRectIntersecting = (rect: DOMRect, selectRect: DOMRect, clipRect?: DOMRect) => {
const top = Math.max(rect.top, clipRect?.top ?? rect.top);
@@ -55,6 +56,7 @@ export const applyAVDragSelection = (blockElement: HTMLElement, selectRect: DOMR
};
export const clearAVDragSelection = (blockElement: HTMLElement) => {
+ clearAVItemSelectionState(blockElement);
const isTable = blockElement.dataset.avType === "table";
blockElement.querySelectorAll(".av__body").forEach((bodyElement: HTMLElement) => {
if (hasClosestByClassName(bodyElement, "av") !== blockElement) {
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index f61e48c25f..ed892c5d3a 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -7,7 +7,7 @@ import {bindAvSearch} from "../search";
import {processRender} from "../../../util/processCode";
import {getPageSize} from "../groups";
import {renderKanban} from "../kanban/render";
-import {getBodyVirtualData, initVirtualScroll, setAVData} from "../virtualScroll";
+import {getAVSelectedItemPoints, getBodyVirtualData, initVirtualScroll, setAVData} from "../virtualScroll";
import {getRowHTML, stickyRow, updateHeader} from "../row";
import {beginAVRender, finishAVLocate, getAVLocateParams, isCurrentAVRender, prepareAVLocate} from "../locate";
import {getCardStyle} from "./style";
@@ -165,6 +165,13 @@ export const afterRenderGallery = (options: ITableOptions) => {
if (options.cb) {
options.cb(options.data);
}
+ initVirtualScroll({
+ ...options,
+ selectedItemPoints: options.resetData.selectItemIds.map(item => ({
+ groupID: item.groupId,
+ itemID: item.fieldId,
+ })),
+ });
if (!options.renderAll) {
finishAVLocate(options.blockElement, options.protyle, options.data);
return;
@@ -178,7 +185,6 @@ export const afterRenderGallery = (options: ITableOptions) => {
isSearching: options.resetData.isSearching,
onChange: () => updateSearch(options.blockElement, options.protyle),
});
- initVirtualScroll(options);
finishAVLocate(options.blockElement, options.protyle, options.data);
};
@@ -198,16 +204,10 @@ export const renderGallery = async (options: {
fieldId: item.parentElement.getAttribute("data-id"),
});
});
- const selectItemIds: IIds[] = [];
- options.blockElement.querySelectorAll(".av__gallery-item--select").forEach(galleryItem => {
- const fieldId = galleryItem.getAttribute("data-id");
- if (fieldId) {
- selectItemIds.push({
- groupId: (hasClosestByClassName(galleryItem, "av__body") as HTMLElement).dataset.groupId || "",
- fieldId
- });
- }
- });
+ const selectItemIds: IIds[] = getAVSelectedItemPoints(options.blockElement).map(item => ({
+ groupId: item.groupID,
+ fieldId: item.itemID,
+ }));
const pageSizes: { [key: string]: string } = {};
const virtualData: { [key: string]: IAVVirtualData } = {};
options.blockElement.querySelectorAll(".av__body").forEach((item: HTMLElement) => {
diff --git a/app/src/protyle/render/av/kanban/render.ts b/app/src/protyle/render/av/kanban/render.ts
index 3432fadc9e..3455598c49 100644
--- a/app/src/protyle/render/av/kanban/render.ts
+++ b/app/src/protyle/render/av/kanban/render.ts
@@ -6,7 +6,7 @@ import {avRender, genTabHeaderHTML} from "../render";
import {afterRenderGallery, renderGallery} from "../gallery/render";
import {escapeAttr, escapeHtml} from "../../../../util/escape";
import {getRowHTML} from "../row";
-import {getBodyVirtualData} from "../virtualScroll";
+import {getAVSelectedItemPoints, getBodyVirtualData} from "../virtualScroll";
import {beginAVRender, getAVLocateParams, isCurrentAVRender, prepareAVLocate} from "../locate";
import {getCardStyle} from "../gallery/style";
@@ -91,16 +91,10 @@ export const renderKanban = async (options: {
fieldId: item.parentElement.getAttribute("data-id"),
});
});
- const selectItemIds: IIds[] = [];
- options.blockElement.querySelectorAll(".av__gallery-item--select").forEach(galleryItem => {
- const fieldId = galleryItem.getAttribute("data-id");
- if (fieldId) {
- selectItemIds.push({
- groupId: (hasClosestByClassName(galleryItem, "av__body") as HTMLElement).dataset.groupId || "",
- fieldId
- });
- }
- });
+ const selectItemIds: IIds[] = getAVSelectedItemPoints(options.blockElement).map(item => ({
+ groupId: item.groupID,
+ fieldId: item.itemID,
+ }));
const pageSizes: { [key: string]: string } = {};
const virtualData: { [key: string]: IAVVirtualData } = {};
options.blockElement.querySelectorAll(".av__body").forEach((item: HTMLElement) => {
diff --git a/app/src/protyle/render/av/keydown.ts b/app/src/protyle/render/av/keydown.ts
index 9bab687419..60590214c6 100644
--- a/app/src/protyle/render/av/keydown.ts
+++ b/app/src/protyle/render/av/keydown.ts
@@ -16,6 +16,7 @@ import {
setAVItemAnchor,
} from "./rangeSelect";
import {getAVCellSelection, getAVItemSelection} from "./selectionState";
+import {isMobile} from "../../../util/functions";
export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyle: IProtyle) => {
if (!nodeElement.classList.contains("av") || !window.siyuan.menus.menu.element.classList.contains("fn__none")) {
@@ -75,7 +76,7 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl
event.preventDefault();
return true;
}
- if (!selectCellElement && cellSelection && event.shiftKey &&
+ if (!isMobile() && !selectCellElement && cellSelection && event.shiftKey &&
["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) {
const previousFocus = `${cellSelection.focus.rowID}:${cellSelection.focus.colID}`;
const targetElement = moveAVCellRange(nodeElement,
@@ -121,7 +122,8 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl
event.preventDefault();
return true;
}
- if (event.shiftKey && ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) {
+ if (!isMobile() && event.shiftKey &&
+ ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)) {
if (!getAVCellSelection(nodeElement)) {
setAVCellAnchor(nodeElement, selectCellElement);
}
@@ -236,7 +238,7 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl
const selectRowElements = nodeElement.querySelectorAll(
".av__row--select:not(.av__row--header), .av__gallery-item--select");
const itemSelection = getAVItemSelection(nodeElement);
- if (selectRowElements.length === 0 && itemSelection && event.shiftKey &&
+ if (!isMobile() && selectRowElements.length === 0 && itemSelection && event.shiftKey &&
(event.key === "ArrowUp" || event.key === "ArrowDown")) {
const previousFocusID = itemSelection.focusID;
const targetElement = moveAVItemRange(nodeElement, event.key === "ArrowUp" ? "up" : "down");
@@ -287,7 +289,7 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl
event.preventDefault();
return true;
}
- if (event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
+ if (!isMobile() && event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
if (!getAVItemSelection(nodeElement)) {
setAVItemAnchor(nodeElement, selectRowElements[event.key === "ArrowUp" ? 0 :
selectRowElements.length - 1] as HTMLElement);
diff --git a/app/src/protyle/render/av/locate.ts b/app/src/protyle/render/av/locate.ts
index 74753e0a46..f741a1e16f 100644
--- a/app/src/protyle/render/av/locate.ts
+++ b/app/src/protyle/render/av/locate.ts
@@ -3,8 +3,9 @@ import {getCardWidth} from "./gallery/style";
import {showMessage} from "../../../dialog/message";
import {transaction} from "../../wysiwyg/transaction";
import {clearSelect} from "../../util/clear";
-import {addDragFill} from "./cell";
import {scrollCenter} from "../../../util/highlightById";
+import {setAVCellAnchor, setAVItemAnchor} from "./rangeSelect";
+import {updateAVRowSelect} from "./virtualScroll";
export interface IAVLocateRequest {
itemID: string;
@@ -343,14 +344,15 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, dat
targetElement = rowElement?.querySelector(".av__cell[data-dtype=\"block\"]") as HTMLElement;
if (targetElement && request.select !== false) {
clearSelect(["cell"], blockElement);
- targetElement.classList.add("av__cell--select");
- addDragFill(targetElement);
+ setAVCellAnchor(blockElement, targetElement);
}
} else {
targetElement = bodyElement?.querySelector(`.av__gallery-item[data-id="${request.itemID}"]`) as HTMLElement;
if (targetElement && request.select !== false && !request.highlight) {
- blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => item.classList.remove("av__gallery-item--select"));
+ clearSelect(["galleryItem"], blockElement);
targetElement.classList.add("av__gallery-item--select");
+ updateAVRowSelect(bodyElement, request.itemID, true);
+ setAVItemAnchor(blockElement, targetElement);
}
}
if (!targetElement) {
diff --git a/app/src/protyle/render/av/rangeSelect.ts b/app/src/protyle/render/av/rangeSelect.ts
index d9355593a0..49611a1c01 100644
--- a/app/src/protyle/render/av/rangeSelect.ts
+++ b/app/src/protyle/render/av/rangeSelect.ts
@@ -2,7 +2,7 @@ import {hasClosestByClassName} from "../../util/hasClosest";
import {
getAVData,
getAVLoadedItemInfos,
- getAVSelectedItemIDs,
+ getAVSelectedItemPoints,
IAVItemInfo,
resetAVRowSelect,
} from "./virtualScroll";
@@ -73,12 +73,16 @@ const buildCellSelection = (blockElement: HTMLElement, anchor: IAVCellPoint,
const rowEnd = Math.max(anchorRowIndex, focusRowIndex);
const colStart = Math.min(anchorColIndex, focusColIndex);
const colEnd = Math.max(anchorColIndex, focusColIndex);
+ const sourceColIndexes = new Map(view.columns.map((column, index) => [column.id, index]));
const selectedCells: IAVSelectedCell[] = [];
for (let rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
const row = view.rows[rowIndex];
for (let colIndex = colStart; colIndex <= colEnd; colIndex++) {
const column = visibleColumns[colIndex];
- const sourceColIndex = view.columns.findIndex(item => item.id === column.id);
+ const sourceColIndex = sourceColIndexes.get(column.id);
+ if (typeof sourceColIndex !== "number") {
+ continue;
+ }
const cell = row.cells[sourceColIndex];
if (cell) {
selectedCells.push({
@@ -191,13 +195,16 @@ const getItemElement = (blockElement: HTMLElement, item: Pick<IAVItemInfo, "item
`.av__body${groupSelector} .av__gallery-item[data-id="${item.itemID}"]`);
};
-const syncItemSelectionDOM = (blockElement: HTMLElement, selectedIDs: Set<string>) => {
+const getItemKey = (groupID: string, itemID: string) => `${groupID}:${itemID}`;
+
+const syncItemSelectionDOM = (blockElement: HTMLElement, selectedKeys: Set<string>) => {
const isTable = blockElement.dataset.avType === "table";
blockElement.querySelectorAll<HTMLElement>(".av__row[data-id], .av__gallery-item[data-id]").forEach(item => {
if (item.closest(".av") !== blockElement) {
return;
}
- const selected = selectedIDs.has(item.dataset.id);
+ const groupID = item.closest<HTMLElement>(".av__body")?.dataset.groupId || "";
+ const selected = selectedKeys.has(getItemKey(groupID, item.dataset.id));
item.classList.toggle(isTable ? "av__row--select" : "av__gallery-item--select", selected);
if (isTable) {
item.querySelector(".av__firstcol use")?.setAttribute("xlink:href",
@@ -229,7 +236,6 @@ export const setAVItemAnchor = (blockElement: HTMLElement, itemElement: HTMLElem
anchorGroupID: groupID,
focusID: itemElement.dataset.id,
focusGroupID: groupID,
- selectedIDs: getAVSelectedItemIDs(blockElement),
});
};
@@ -253,7 +259,7 @@ const applyItemRange = (blockElement: HTMLElement, target: IAVItemInfo) => {
anchorIndex = targetIndex;
}
const selectedItems = scopeItems.slice(Math.min(anchorIndex, targetIndex), Math.max(anchorIndex, targetIndex) + 1);
- const selectedIDs = new Set(selectedItems.map(item => item.itemID));
+ const selectedKeys = new Set(selectedItems.map(item => getItemKey(item.groupID, item.itemID)));
blockElement.querySelectorAll<HTMLElement>(".av__body").forEach(bodyElement => {
if (bodyElement.closest(".av") !== blockElement) {
return;
@@ -262,14 +268,13 @@ const applyItemRange = (blockElement: HTMLElement, target: IAVItemInfo) => {
resetAVRowSelect(bodyElement, selectedItems.filter(item => item.groupID === groupID).map(item => item.itemID));
});
clearAVCellRange(blockElement);
- syncItemSelectionDOM(blockElement, selectedIDs);
+ syncItemSelectionDOM(blockElement, selectedKeys);
updateAVSelectionStatus(blockElement);
setAVItemSelectionState(blockElement, {
anchorID: itemSelection.anchorID,
anchorGroupID: itemSelection.anchorGroupID,
focusID: target.itemID,
focusGroupID: target.groupID,
- selectedIDs: Array.from(selectedIDs),
});
return true;
};
@@ -304,8 +309,10 @@ export const moveAVItemRange = (blockElement: HTMLElement, direction: "up" | "do
};
export const setAVDragItemAnchor = (blockElement: HTMLElement) => {
- const selectedIDs = new Set(getAVSelectedItemIDs(blockElement));
- const selectedItems = getAVLoadedItemInfos(blockElement, true).filter(item => selectedIDs.has(item.itemID));
+ const selectedKeys = new Set(getAVSelectedItemPoints(blockElement).map(item =>
+ getItemKey(item.groupID, item.itemID)));
+ const selectedItems = getAVLoadedItemInfos(blockElement, true).filter(item =>
+ selectedKeys.has(getItemKey(item.groupID, item.itemID)));
if (selectedItems.length > 0) {
const firstSelected = selectedItems[0];
const lastSelected = selectedItems[selectedItems.length - 1];
@@ -314,7 +321,8 @@ export const setAVDragItemAnchor = (blockElement: HTMLElement) => {
anchorGroupID: firstSelected.groupID,
focusID: lastSelected.itemID,
focusGroupID: lastSelected.groupID,
- selectedIDs: selectedItems.map(item => item.itemID),
});
+ } else {
+ clearAVItemSelectionState(blockElement);
}
};
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index abe55f3fc6..797135f17b 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -19,7 +19,7 @@ import {clearSelect} from "../../util/clear";
import {showMessage} from "../../../dialog/message";
import {renderKanban} from "./kanban/render";
import {bindAvSearch} from "./search";
-import {getBodyVirtualData, initVirtualScroll, setAVData} from "./virtualScroll";
+import {getAVSelectedItemPoints, getBodyVirtualData, initVirtualScroll, setAVData} from "./virtualScroll";
import {beginAVRender, finishAVLocate, getAVLocateParams, isCurrentAVRender, prepareAVLocate, setAVLocateRequest} from "./locate";
import {setGroupFoldedStates, updateGroupFoldedStates} from "./groupFold";
import {updateHotkeyTip} from "../../util/compatibility";
@@ -301,7 +301,11 @@ const renderGroupTable = (options: ITableOptions) => {
const afterRenderTable = (options: ITableOptions) => {
setAVData(options.blockElement, options.data);
- refreshAVCellSelection(options.blockElement, options.data);
+ if (!refreshAVCellSelection(options.blockElement, options.data)) {
+ options.resetData.selectCellId = undefined;
+ options.resetData.dragFillId = undefined;
+ options.resetData.activeIds = [];
+ }
if (options.blockElement.getAttribute("data-need-focus") === "true") {
focusBlock(options.blockElement);
options.blockElement.removeAttribute("data-need-focus");
@@ -404,6 +408,13 @@ const afterRenderTable = (options: ITableOptions) => {
if (options.cb) {
options.cb(options.data);
}
+ initVirtualScroll({
+ ...options,
+ selectedItemPoints: options.resetData.selectRowIds.map(item => ({
+ groupID: item.groupId,
+ itemID: item.rowId,
+ })),
+ });
if (!options.renderAll) {
finishAVLocate(options.blockElement, options.protyle, options.data);
return;
@@ -414,7 +425,6 @@ const afterRenderTable = (options: ITableOptions) => {
isSearching: options.resetData.isSearching,
onChange: () => updateSearch(options.blockElement, options.protyle),
});
- initVirtualScroll(options);
finishAVLocate(options.blockElement, options.protyle, options.data);
};
@@ -457,16 +467,10 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:
colId: selectCellElement.getAttribute("data-col-id"),
};
}
- const selectRowIds: IIds[] = [];
- e.querySelectorAll(".av__row--select").forEach(rowItem => {
- const rowId = rowItem.getAttribute("data-id");
- if (rowId) {
- selectRowIds.push({
- groupId: (hasClosestByClassName(rowItem, "av__body") as HTMLElement).dataset.groupId || "",
- rowId
- });
- }
- });
+ const selectRowIds: IIds[] = getAVSelectedItemPoints(e).map(item => ({
+ groupId: item.groupID,
+ rowId: item.itemID,
+ }));
let dragFillId;
const dragFillElement = e.querySelector(".av__drag-fill") as HTMLElement;
if (dragFillElement) {
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index 09e1ea2f41..3e32536ce2 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -29,6 +29,7 @@ import {
import {getCardCoverHTML, getCardCoverSource} from "./cover";
import {getCardFieldsClass} from "./gallery/cardLayout";
import {getAVFilteredTipContext} from "./filteredTip";
+import {clearAVItemSelectionState} from "./selectionState";
const getGalleryActionsHTML = (data: IAVGallery | IAVKanban, row: IAVGalleryItem, primaryHidden = false) => {
const canPosition = Boolean(row.coverURL && !row.coverURL.startsWith("background") && getCardCoverSource(data));
@@ -246,6 +247,10 @@ export const selectRow = (checkElement: Element, type: "toggle" | "select" | "un
const useElement = checkElement.querySelector("use");
const bodyElement = hasClosestByClassName(rowElement, "av__body") as HTMLElement;
if (rowElement.classList.contains("av__row--header") || type === "unselectAll") {
+ const blockElement = hasClosestByClassName(rowElement, "av") as HTMLElement;
+ if (blockElement) {
+ clearAVItemSelectionState(blockElement);
+ }
if ("#iconCheck" === useElement.getAttribute("xlink:href") || type === "unselectAll") {
rowElement.parentElement.querySelectorAll(".av__firstcol").forEach(item => {
item.querySelector("use").setAttribute("xlink:href", "#iconUncheck");
diff --git a/app/src/protyle/render/av/selectionState.ts b/app/src/protyle/render/av/selectionState.ts
index 3c78c60931..decec66b37 100644
--- a/app/src/protyle/render/av/selectionState.ts
+++ b/app/src/protyle/render/av/selectionState.ts
@@ -25,7 +25,6 @@ interface IAVItemSelection {
anchorGroupID: string;
focusID: string;
focusGroupID: string;
- selectedIDs: string[];
}
interface IAVSelectionState {
@@ -85,7 +84,6 @@ export const setAVItemAnchorState = (blockElement: HTMLElement, itemID: string,
anchorGroupID: groupID,
focusID: itemID,
focusGroupID: groupID,
- selectedIDs: state.item?.selectedIDs || [],
};
};
@@ -103,7 +101,7 @@ export const clearAVItemSelectionState = (blockElement: HTMLElement) => {
export const refreshAVCellSelection = (blockElement: HTMLElement, data: IAV) => {
const selection = getAVCellSelection(blockElement);
if (!selection) {
- return;
+ return true;
}
const findView = (view: IAVView): IAVView | undefined => {
if ((!selection.anchor.groupID && !view.groups?.length) || view.id === selection.anchor.groupID) {
@@ -119,16 +117,20 @@ export const refreshAVCellSelection = (blockElement: HTMLElement, data: IAV) =>
const view = findView(data.view) as IAVTable;
if (!view?.rows || !view.columns) {
clearAVCellSelectionState(blockElement);
- return;
+ return false;
}
const columns = view.columns.filter(column => !column.hidden);
+ const rowIndexes = new Map(view.rows.map((row, index) => [row.id, index]));
+ const colIndexes = new Map(columns.map((column, index) => [column.id, index]));
+ const sourceColIndexes = new Map(view.columns.map((column, index) => [column.id, index]));
const cells: IAVSelectedCell[] = [];
selection.cells.forEach(selectedCell => {
- const rowIndex = view.rows.findIndex(row => row.id === selectedCell.rowID);
- const colIndex = columns.findIndex(column => column.id === selectedCell.colID);
- const sourceColIndex = view.columns.findIndex(column => column.id === selectedCell.colID);
- const cell = view.rows[rowIndex]?.cells[sourceColIndex];
- if (cell && colIndex >= 0) {
+ const rowIndex = rowIndexes.get(selectedCell.rowID);
+ const colIndex = colIndexes.get(selectedCell.colID);
+ const sourceColIndex = sourceColIndexes.get(selectedCell.colID);
+ const cell = typeof rowIndex === "number" && typeof sourceColIndex === "number" ?
+ view.rows[rowIndex]?.cells[sourceColIndex] : undefined;
+ if (cell && typeof colIndex === "number" && typeof rowIndex === "number") {
cells.push({
groupID: selection.anchor.groupID,
rowID: selectedCell.rowID,
@@ -142,9 +144,10 @@ export const refreshAVCellSelection = (blockElement: HTMLElement, data: IAV) =>
});
if (cells.length !== selection.cells.length) {
clearAVCellSelectionState(blockElement);
- return;
+ return false;
}
selection.cells = cells;
+ return true;
};
export const restoreAVCellSelection = (blockElement: HTMLElement) => {
@@ -152,22 +155,23 @@ export const restoreAVCellSelection = (blockElement: HTMLElement) => {
if (!selection) {
return;
}
- const selectedKeys = new Set(selection.cells.map(item => `${item.rowID}:${item.colID}`));
+ const selectedKeys = new Set(selection.cells.map(item => `${item.groupID}:${item.rowID}:${item.colID}`));
blockElement.querySelectorAll<HTMLElement>(".av__cell--active, .av__cell--select").forEach(item => {
item.classList.remove("av__cell--active", "av__cell--select");
item.querySelector(".av__drag-fill")?.remove();
});
blockElement.querySelectorAll<HTMLElement>(".av__row[data-id] .av__cell[data-col-id]").forEach(cellElement => {
const rowElement = cellElement.closest<HTMLElement>(".av__row[data-id]");
- if (!rowElement || !selectedKeys.has(`${rowElement.dataset.id}:${cellElement.dataset.colId}`)) {
+ const groupID = cellElement.closest<HTMLElement>(".av__body")?.dataset.groupId || "";
+ if (!rowElement || !selectedKeys.has(`${groupID}:${rowElement.dataset.id}:${cellElement.dataset.colId}`)) {
return;
}
cellElement.classList.add("av__cell--active");
- if (rowElement.dataset.id === selection.anchor.rowID &&
+ if (groupID === selection.anchor.groupID && rowElement.dataset.id === selection.anchor.rowID &&
cellElement.dataset.colId === selection.anchor.colID) {
cellElement.classList.add("av__cell--select");
}
- if (rowElement.dataset.id === selection.focus.rowID &&
+ if (groupID === selection.focus.groupID && rowElement.dataset.id === selection.focus.rowID &&
cellElement.dataset.colId === selection.focus.colID &&
!["template", "rollup", "lineNumber", "created", "updated"].includes(cellElement.dataset.dtype)) {
cellElement.insertAdjacentHTML("beforeend",
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
index 82d11ed3f3..b3c80fca31 100644
--- a/app/src/protyle/render/av/virtualScroll.ts
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -1,6 +1,6 @@
import {Constants} from "../../../constants";
import {getRowHTML} from "./row";
-import {getAVItemSelection, restoreAVCellSelection} from "./selectionState";
+import {IAVSelectedCell, restoreAVCellSelection} from "./selectionState";
const BUFFER_RATIO = 1;
@@ -509,6 +509,35 @@ export interface IAVItemInfo {
primaryCell?: IAVCell;
}
+export interface IAVItemPoint {
+ itemID: string;
+ groupID: string;
+}
+
+const getAVItemPointKey = (item: IAVItemPoint) => `${item.groupID}:${item.itemID}`;
+
+export const getAVSelectedItemPoints = (blockElement: HTMLElement): IAVItemPoint[] => {
+ const points: IAVItemPoint[] = [];
+ blockElement.querySelectorAll<HTMLElement>(".av__body").forEach(bodyElement => {
+ if (bodyElement.closest(".av") !== blockElement) {
+ return;
+ }
+ const groupID = bodyElement.dataset.groupId || "";
+ const state = bodyStates.get(bodyElement);
+ if (state?.selectedRowIds) {
+ state.selectedRowIds.forEach(itemID => points.push({itemID, groupID}));
+ return;
+ }
+ bodyElement.querySelectorAll<HTMLElement>(
+ ".av__row--select:not(.av__row--header), .av__gallery-item--select").forEach(item => {
+ if (item.dataset.id) {
+ points.push({itemID: item.dataset.id, groupID});
+ }
+ });
+ });
+ return points;
+};
+
export const getAVLoadedItemInfos = (blockElement: HTMLElement, visibleOnly = false): IAVItemInfo[] => {
const data = blockDataStore.get(blockElement);
if (!data) {
@@ -550,8 +579,15 @@ export const getAVLoadedItemInfos = (blockElement: HTMLElement, visibleOnly = fa
};
export const getAVSelectedItemInfos = (blockElement: HTMLElement) => {
- const selectedIDs = new Set(getAVSelectedItemIDs(blockElement));
- return getAVLoadedItemInfos(blockElement).filter(item => selectedIDs.has(item.itemID));
+ const selectedKeys = new Set(getAVSelectedItemPoints(blockElement).map(getAVItemPointKey));
+ const selectedIDs = new Set<string>();
+ return getAVLoadedItemInfos(blockElement).filter(item => {
+ if (!selectedKeys.has(getAVItemPointKey(item)) || selectedIDs.has(item.itemID)) {
+ return false;
+ }
+ selectedIDs.add(item.itemID);
+ return true;
+ });
};
export const getAVPrimaryCell = (blockElement: HTMLElement, itemID: string) => {
@@ -563,19 +599,7 @@ export const setAVData = (blockElement: HTMLElement, data: IAV) => {
};
export const getAVSelectedItemIDs = (blockElement: HTMLElement) => {
- const selectedIDs = new Set<string>();
- blockElement.querySelectorAll(".av__body").forEach((bodyElement: HTMLElement) => {
- const state = bodyStates.get(bodyElement);
- if (state?.selectedRowIds) {
- state.selectedRowIds.forEach((itemID) => selectedIDs.add(itemID));
- } else {
- bodyElement.querySelectorAll(".av__row--select:not(.av__row--header), .av__gallery-item--select").forEach((item: HTMLElement) => {
- if (item.dataset.id) {
- selectedIDs.add(item.dataset.id);
- }
- });
- }
- });
+ const selectedIDs = new Set(getAVSelectedItemPoints(blockElement).map(item => item.itemID));
const data = blockDataStore.get(blockElement);
if (!data) {
return Array.from(selectedIDs);
@@ -598,6 +622,61 @@ export const getAVSelectedItemIDs = (blockElement: HTMLElement) => {
return orderedIDs;
};
+export const getAVSelectedTableCells = (blockElement: HTMLElement): IAVSelectedCell[] => {
+ if (blockElement.dataset.avType !== "table") {
+ return [];
+ }
+ const selectedKeys = new Set(getAVSelectedItemPoints(blockElement).map(getAVItemPointKey));
+ const selectedIDs = new Set<string>();
+ const cells: IAVSelectedCell[] = [];
+ const data = blockDataStore.get(blockElement);
+ const findView = (view: IAVView, groupID: string): IAVView | undefined => {
+ if ((!groupID && !view.groups?.length) || view.id === groupID) {
+ return view;
+ }
+ for (const group of view.groups || []) {
+ const result = findView(group, groupID);
+ if (result) {
+ return result;
+ }
+ }
+ };
+ getAVLoadedItemInfos(blockElement).forEach(info => {
+ if (!selectedKeys.has(getAVItemPointKey(info)) || selectedIDs.has(info.itemID) ||
+ !("cells" in info.item)) {
+ return;
+ }
+ selectedIDs.add(info.itemID);
+ const table = data ? findView(data.view, info.groupID) as IAVTable : undefined;
+ const rowIndex = table?.rows?.findIndex(row => row.id === info.itemID);
+ if (typeof rowIndex !== "number" || rowIndex < 0) {
+ return;
+ }
+ const visibleColumns = table?.columns?.filter(column => !column.hidden) || [];
+ const sourceColIndexes = new Map(table.columns.map((column, index) => [column.id, index]));
+ const row = info.item as IAVRow;
+ visibleColumns.forEach((column, colIndex) => {
+ const sourceColIndex = sourceColIndexes.get(column.id);
+ if (typeof sourceColIndex !== "number") {
+ return;
+ }
+ const cell = row.cells[sourceColIndex];
+ if (cell) {
+ cells.push({
+ groupID: info.groupID,
+ rowID: info.itemID,
+ colID: column.id,
+ rowIndex,
+ colIndex,
+ cell,
+ column,
+ });
+ }
+ });
+ });
+ return cells;
+};
+
export const trimAVRows = (blockElement: HTMLElement, elementRect: DOMRect): void => {
if (blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true" || trimPending.has(blockElement)) {
return;
@@ -622,9 +701,9 @@ export const initVirtualScroll = (options: {
protyle: IProtyle,
blockElement: HTMLElement,
data: IAV,
+ selectedItemPoints?: IAVItemPoint[],
}): void => {
setAVData(options.blockElement, options.data);
- const itemSelection = getAVItemSelection(options.blockElement);
if (options.blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
return;
}
@@ -648,10 +727,9 @@ export const initVirtualScroll = (options: {
selectedRowIds.add(id);
}
});
- const viewItems = (view as IAVTable).rows || (view as IAVGallery).cards || [];
- itemSelection?.selectedIDs.forEach(itemID => {
- if (viewItems.some(item => item.id === itemID)) {
- selectedRowIds.add(itemID);
+ options.selectedItemPoints?.forEach(point => {
+ if (point.groupID === (item.dataset.groupId || "")) {
+ selectedRowIds.add(point.itemID);
}
});
item.querySelectorAll<HTMLElement>(".av__row[data-id], .av__gallery-item[data-id]").forEach(row => {
diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts
index 1a71f7c7cc..09d196522d 100644
--- a/app/src/protyle/upload/index.ts
+++ b/app/src/protyle/upload/index.ts
@@ -14,6 +14,8 @@ import {confirmDialog} from "../../dialog/confirmDialog";
import {filesize} from "filesize";
import {transaction} from "../wysiwyg/transaction";
import * as dayjs from "dayjs";
+import {getAVSelectedCells} from "../render/av/selectionState";
+import {getAVSelectedTableCells} from "../render/av/virtualScroll";
interface FileWithPath extends File {
path: string;
@@ -187,6 +189,9 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle) => {
return;
}
} else if (nodeElement && nodeElement.classList.contains("av")) {
+ const selectedCells = getAVSelectedCells(nodeElement);
+ const stableCellCandidates = (selectedCells.length > 0 ? selectedCells :
+ getAVSelectedTableCells(nodeElement)).filter(item => item.column.type === "mAsset");
const cellElements: HTMLElement[] = [];
nodeElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(item => {
item.querySelectorAll(".av__cell").forEach((cellItem: HTMLElement) => {
@@ -196,22 +201,28 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle) => {
});
});
if (cellElements.length === 0) {
- protyle.wysiwyg.element.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
+ nodeElement.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
if (getTypeByCellElement(item) === "mAsset") {
cellElements.push(item);
}
});
}
- if (cellElements.length === 1) {
- updateCellsValue(protyle, nodeElement, avAssets, cellElements);
- } else if (cellElements.length > 1) {
+ const stableCells = stableCellCandidates.length > cellElements.length ? stableCellCandidates : [];
+ if (stableCells.length === 1 || cellElements.length === 1) {
+ updateCellsValue(protyle, nodeElement, avAssets, cellElements, undefined, undefined,
+ false, false, false, stableCells);
+ } else if (stableCells.length > 1 || cellElements.length > 1) {
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
let currentRowElement;
- const colId = cellElements[0].getAttribute("data-col-id");
+ const colId = cellElements[0]?.getAttribute("data-col-id");
for (let i = 0; i < avAssets.length; i++) {
+ const selectedCell = stableCells[i];
+ if (stableCells.length > 0 && !selectedCell) {
+ break;
+ }
let cellElement = cellElements[i];
- if (!cellElement) {
+ if (!cellElement && stableCells.length === 0) {
if (!currentRowElement) {
currentRowElement = hasClosestByClassName(cellElements[i - 1], "av__row") as HTMLElement;
}
@@ -222,11 +233,12 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle) => {
}
}
}
- if (!cellElement) {
+ if (!cellElement && !selectedCell) {
break;
}
const operations = await updateCellsValue(protyle, nodeElement,
- [avAssets[i]], [cellElement], null, null, true);
+ [avAssets[i]], cellElement ? [cellElement] : undefined, undefined, undefined,
+ true, false, false, selectedCell ? [selectedCell] : undefined);
doOperations.push(...operations.doOperations);
undoOperations.push(...operations.undoOperations);
}
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 257ba7e5b0..e577b2dcbc 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -55,6 +55,8 @@ import {setFold} from "./blockFold";
import {isEncryptedBox} from "../../util/pathName";
import {isSameDragEditor, uniqueDragIds} from "./dragDocument";
import {getAVFilteredTipContext, getAVViewID} from "../render/av/filteredTip";
+import {getAVSelectedItemPoints, updateAVRowSelect} from "../render/av/virtualScroll";
+import {setAVItemAnchor} from "../render/av/rangeSelect";
const KANBAN_GROUP_DRAG_TYPE = `${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}Group${Constants.ZWSP}`;
@@ -955,10 +957,11 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
}
if (!target.classList.contains("av__gallery-item--select")) {
- blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => {
- item.classList.remove("av__gallery-item--select");
- });
+ clearSelect(["galleryItem"], blockElement);
target.classList.add("av__gallery-item--select");
+ const bodyElement = hasClosestByClassName(target, "av__body") as HTMLElement;
+ updateAVRowSelect(bodyElement, target.dataset.id, true);
+ setAVItemAnchor(blockElement, target);
}
const ghostElement = document.createElement("div");
ghostElement.className = "protyle-wysiwyg protyle-wysiwyg--attr";
@@ -1004,12 +1007,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
});
}
window.siyuan.dragElement = target;
- const selectIds: string[] = [];
- blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => {
- const bodyElement = hasClosestByClassName(item, "av__body") as HTMLElement;
- const groupId = bodyElement.getAttribute("data-group-id");
- selectIds.push(item.getAttribute("data-id") + (groupId ? `@${groupId}` : ""));
- });
+ const selectIds = getAVSelectedItemPoints(blockElement).map(item =>
+ item.itemID + (item.groupID ? `@${item.groupID}` : ""));
event.dataTransfer.setData(`${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}GalleryItem${Constants.ZWSP}${selectIds}`,
ghostElement.outerHTML);
}
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 8749a72f9b..9371a0e903 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -83,13 +83,14 @@ import {activeBlur} from "../../mobile/util/keyboardToolbar";
import {commonClick} from "./commonClick";
import {avClick, avContextmenu, updateAVName} from "../render/av/action";
import {selectRow, stickyRow, updateHeader} from "../render/av/row";
-import {updateAVRowSelect} from "../render/av/virtualScroll";
+import {getAVSelectedItemIDs, getAVSelectedTableCells, updateAVRowSelect} from "../render/av/virtualScroll";
import {setFreezeColumn, showColMenu} from "../render/av/col";
import {openViewMenu} from "../render/av/view";
import {checkFold} from "../../util/noRelyPCFunction";
import {
addDragFill,
dragFillCellsValue,
+ getAVCellData,
getAVSelectedCellData,
genCellValueByElement,
getCellText,
@@ -433,7 +434,9 @@ export class WYSIWYG {
}
const selectImgElement = nodeElement.querySelector(".img--select");
const selectAVElement = nodeElement.querySelector(".av__row--select, .av__cell--select") ||
- (getAVSelectedCells(nodeElement).length > 0 ? nodeElement : null);
+ (getAVSelectedCells(nodeElement).length > 0 ||
+ (nodeElement.dataset.avType === "table" && getAVSelectedItemIDs(nodeElement).length > 0) ?
+ nodeElement : null);
const selectTableElement = nodeElement.querySelector(".table__select")?.clientWidth > 0;
// 表格内跨多单元格的文本选区:range.cloneContents() 会产出残缺的 td/tr 片段,需要重建合法 table
let selectTableRange = false;
@@ -525,7 +528,9 @@ export class WYSIWYG {
selectElements[0].removeAttribute("data-reftext");
}
} else if (selectAVElement) {
- const selectedCellData = getAVSelectedCellData(nodeElement);
+ const selectedCells = getAVSelectedCells(nodeElement);
+ const selectedCellData = selectedCells.length > 0 ?
+ getAVSelectedCellData(nodeElement) : getAVCellData(getAVSelectedTableCells(nodeElement));
const cellElements: Element[] = selectedCellData.json.length > 0 ? [] :
Array.from(nodeElement.querySelectorAll(".av__cell--active, .av__cell--select"));
if (selectedCellData.json.length > 0) {
@@ -769,7 +774,8 @@ export class WYSIWYG {
const baseDragSelectElements = new Set(isToggleBlockDrag ?
Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select")) : []);
if (event.shiftKey) {
- if (!protyle.disabled && nodeElement?.dataset.avType === "table" && avCellElement?.dataset.id &&
+ if (!isMobile() && !protyle.disabled && nodeElement?.dataset.avType === "table" &&
+ avCellElement?.dataset.id &&
selectAVCellRange(nodeElement, avCellElement)) {
focusBlock(nodeElement);
this.preventClick = true;
@@ -1264,17 +1270,30 @@ export class WYSIWYG {
const originData: { [key: string]: IAVCellValue[] } = {};
let lastOriginCellElement: HTMLElement;
const originCellIds: string[] = [];
- bodyElement.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
- const rowElement = hasClosestByClassName(item, "av__row");
- if (rowElement) {
- if (!originData[rowElement.dataset.id]) {
- originData[rowElement.dataset.id] = [];
+ const stableOriginCells = getAVSelectedCells(nodeElement);
+ if (stableOriginCells.length > 0) {
+ stableOriginCells.forEach(item => {
+ if (!originData[item.rowID]) {
+ originData[item.rowID] = [];
}
- originData[rowElement.dataset.id].push(genCellValueByElement(getTypeByCellElement(item), item));
- lastOriginCellElement = item;
- originCellIds.push(item.dataset.id);
- }
- });
+ originData[item.rowID].push(item.cell.value);
+ originCellIds.push(item.cell.id);
+ });
+ lastOriginCellElement = avDragFillElement.parentElement;
+ } else {
+ bodyElement.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
+ const rowElement = hasClosestByClassName(item, "av__row");
+ if (rowElement) {
+ if (!originData[rowElement.dataset.id]) {
+ originData[rowElement.dataset.id] = [];
+ }
+ originData[rowElement.dataset.id].push(
+ genCellValueByElement(getTypeByCellElement(item), item));
+ lastOriginCellElement = item;
+ originCellIds.push(item.dataset.id);
+ }
+ });
+ }
const dragFillCellIndex = getPositionByCellElement(lastOriginCellElement);
const firstCellIndex = getPositionByCellElement(bodyElement.querySelector(".av__cell--active"));
let moveAVCellElement: HTMLElement;
@@ -1302,11 +1321,11 @@ export class WYSIWYG {
rowElement.querySelectorAll(".av__cell").forEach((cellElement: HTMLElement, cellIndex: number) => {
if (cellIndex >= firstCellIndex.celIndex && cellIndex <= newIndex.celIndex) {
cellElement.classList.add("av__cell--active");
- lastCellElement = cellElement;
}
});
}
});
+ lastCellElement = moveAVCellElement;
}
};
@@ -1317,9 +1336,9 @@ export class WYSIWYG {
documentSelf.onselectstart = null;
documentSelf.onselect = null;
if (lastCellElement) {
+ selectAVCellRange(nodeElement, lastCellElement);
dragFillCellsValue(protyle, nodeElement, originData, originCellIds, lastOriginCellElement);
- const allActiveCellsElement = bodyElement.querySelectorAll(".av__cell--active");
- addDragFill(allActiveCellsElement[allActiveCellsElement.length - 1]);
+ addDragFill(lastCellElement);
}
return false;
};
@@ -2453,7 +2472,9 @@ export class WYSIWYG {
event.preventDefault();
const selectImgElement = nodeElement.querySelector(".img--select");
const selectAVElement = nodeElement.querySelector(".av__row--select, .av__cell--select") ||
- (getAVSelectedCells(nodeElement).length > 0 ? nodeElement : null);
+ (getAVSelectedCells(nodeElement).length > 0 ||
+ (nodeElement.dataset.avType === "table" && getAVSelectedItemIDs(nodeElement).length > 0) ?
+ nodeElement : null);
const selectTableElement = nodeElement.querySelector(".table__select")?.clientWidth > 0;
// 表格内跨多单元格的文本选区:range.cloneContents() 会产出残缺的 td/tr 片段,需要重建合法 table
let selectTableRange = false;
@@ -2542,7 +2563,10 @@ export class WYSIWYG {
}
} else if (selectAVElement) {
needClipboardWrite = true;
- const cellsValue = await updateCellsValue(protyle, nodeElement);
+ const selectedCells = getAVSelectedCells(nodeElement);
+ const itemCells = selectedCells.length === 0 ? getAVSelectedTableCells(nodeElement) : undefined;
+ const cellsValue = await updateCellsValue(protyle, nodeElement, undefined, undefined, undefined,
+ undefined, false, false, false, itemCells);
html = JSON.stringify(cellsValue.json);
textPlain = cellsValue.text;
} else if (selectTableElement) {