Skip to content

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

:art: Refine table row and column drag preview https://github.com/siyuan-note/siyuan/issues/18489

Danielf6a527cf30a9
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index d3777d54b8..2abd11f5dc 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -851,6 +851,10 @@ table + .protyle-action__table {
background-color: var(--b3-theme-primary-lightest);
border: 1px solid var(--b3-theme-primary);
pointer-events: none;
+
+ &--dragging {
+ opacity: 0;
+ }
}
&__drop {
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index ecbd262c4d..93be83f72c 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -248,6 +248,8 @@ export class TableControl {
}
public clear() {
+ this.clearDragPreview();
+ this.dragState = undefined;
this.selection = undefined;
this.selectionGrid = undefined;
this.selectedCells = [];
@@ -371,7 +373,7 @@ export class TableControl {
}
private handleTablePointerMove(event: PointerEvent, fromControl: boolean) {
- if (event.buttons !== 0 && !this.dragState) {
+ if (event.buttons !== 0) {
return;
}
const targetCell = getCell(event.target);
@@ -835,6 +837,8 @@ export class TableControl {
selectionElement.style.top = `${visibleRect.top}px`;
selectionElement.style.width = `${visibleRect.width}px`;
selectionElement.style.height = `${visibleRect.height}px`;
+ selectionElement.classList.toggle("protyle-table-control__selection--dragging",
+ !!this.dragState?.dragging);
}
private render() {
@@ -1607,6 +1611,7 @@ export class TableControl {
return;
}
this.dragState.dragging = true;
+ this.updateDragPreview(event);
const grid = this.selectionGrid || buildTableGrid(this.selection.table);
const viewportRect = this.getTableViewportRect(this.selection.table);
let cell: HTMLTableCellElement;
@@ -1643,7 +1648,13 @@ export class TableControl {
}
const after = this.dragState.mode === "row" ? event.clientY > rect.top + rect.height / 2 :
event.clientX > rect.left + rect.width / 2;
- this.dragState.target = (this.dragState.mode === "row" ? info.row : info.col) + (after ? 1 : 0);
+ const target = (this.dragState.mode === "row" ? info.row : info.col) + (after ? 1 : 0);
+ if (!this.getMoveTarget(target)) {
+ this.dragState.target = -1;
+ this.dropIndicator.classList.add("fn__none");
+ return;
+ }
+ this.dragState.target = target;
this.dropIndicator.classList.remove("fn__none");
if (this.dragState.mode === "row") {
this.dropIndicator.classList.add("protyle-table-control__drop--row");
@@ -1664,11 +1675,56 @@ export class TableControl {
}
}
+ private updateDragPreview(event: PointerEvent) {
+ const state = this.dragState;
+ if (!state) {
+ return;
+ }
+ const offsetX = state.mode === "column" ? event.clientX - state.startX : 0;
+ const offsetY = state.mode === "row" ? event.clientY - state.startY : 0;
+ const translate = `${Math.round(offsetX)}px ${Math.round(offsetY)}px`;
+ const handle = state.mode === "row" ? this.rowHandle : this.columnHandle;
+ handle.style.translate = translate;
+ this.selectionElements.slice(0, this.selectionElementIndex).forEach(item => {
+ item.classList.add("protyle-table-control__selection--dragging");
+ });
+ }
+
+ private clearDragPreview() {
+ this.rowHandle.style.removeProperty("translate");
+ this.columnHandle.style.removeProperty("translate");
+ this.selectionElements.forEach(item => {
+ item.classList.remove("protyle-table-control__selection--dragging");
+ });
+ }
+
+ private getMoveTarget(target: number) {
+ if (!this.selection || this.selection.mode === "cell") {
+ return;
+ }
+ const selected = Array.from(this.selection.indexes).sort((a, b) => a - b);
+ if (this.selection.mode === "row" && selected.length > 1 && (selected.includes(0) || target === 0)) {
+ return;
+ }
+ const adjustedTarget = target - selected.filter(index => index < target).length;
+ const itemCount = this.selection.mode === "row" ? this.selection.table.rows.length :
+ this.selection.table.rows[0]?.cells.length || 0;
+ const indexes = Array.from({length: itemCount}, (_, index) => index);
+ const moving = indexes.filter(index => selected.includes(index));
+ const remaining = indexes.filter(index => !selected.includes(index));
+ remaining.splice(Math.max(0, adjustedTarget), 0, ...moving);
+ if (remaining.every((index, position) => index === position)) {
+ return;
+ }
+ return {selected, adjustedTarget};
+ }
+
private handleDragEnd(event: PointerEvent) {
if (!this.dragState) {
return;
}
const state = this.dragState;
+ this.clearDragPreview();
this.dropIndicator.classList.add("fn__none");
if (state.dragging && state.target >= 0) {
this.clearJoinedControlTable();
@@ -1681,14 +1737,11 @@ export class TableControl {
}
private moveSelection(target: number) {
- if (!this.selection || this.selection.mode === "cell") {
+ const moveTarget = this.getMoveTarget(target);
+ if (!moveTarget || !this.selection) {
return;
}
- const selected = Array.from(this.selection.indexes).sort((a, b) => a - b);
- if (this.selection.mode === "row" && selected.length > 1 && (selected.includes(0) || target === 0)) {
- return;
- }
- const adjustedTarget = target - selected.filter(index => index < target).length;
+ const {selected, adjustedTarget} = moveTarget;
const oldHTML = this.selection.node.outerHTML;
if (this.selection.mode === "row") {
const rows = Array.from(this.selection.table.rows);