Skip to content

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

:bug: Keep mobile table resize controls aligned with table edges https://github.com/siyuan-note/siyuan/issues/20635

Daniel8a12ee58ae14
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index 47db9b02ef..742ea24982 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -560,7 +560,7 @@ export class TableControl {
}
private getTouchState(): ITableTouchState | undefined {
- const cell = this.selection?.activeCell || this.caretCell || this.hoverCell;
+ const cell = [this.selection?.activeCell, this.caretCell, this.hoverCell].find(item => item?.isConnected);
const node = getTableNode(cell);
if (this.protyle.disabled || !cell?.isConnected || !node || !this.wysiwygElement.contains(cell) ||
this.protyle.element.closest(".fn__none, [inert]")) {
@@ -1489,10 +1489,12 @@ export class TableControl {
if (!state) {
return;
}
- [this.rowHandle, this.columnHandle, this.cellHandle, this.addRowButton, this.addColumnButton,
- this.addBothButton].forEach(item => {
- item.classList.add("fn__none");
- item.classList.remove("protyle-table-control__add--active");
+ const [addRowButton, addColumnButton, addBothButton] = this.touch?.renderResize() ||
+ [this.addRowButton, this.addColumnButton, this.addBothButton];
+ [this.rowHandle, this.columnHandle, this.cellHandle, addRowButton, addColumnButton,
+ addBothButton].forEach(item => {
+ item?.classList.add("fn__none");
+ item?.classList.remove("protyle-table-control__add--active");
});
this.selectionElements.forEach(item => item.classList.add("fn__none"));
const gridRect = getTableGridRect(state.table);
@@ -1506,28 +1508,28 @@ export class TableControl {
const columnControlVisible = addColumnEdge <= viewportRect.right + 1 &&
isTableResizeControlVisible(addColumnEdge, contentRect.right, TABLE_ADD_CONTROL_THICKNESS,
TABLE_ADD_CONTROL_GAP);
- if (state.mode === "row" || state.mode === "both") {
- this.addRowButton.classList.remove("fn__none");
- this.addRowButton.classList.add("protyle-table-control__add--active");
- this.addRowButton.style.width = `${Math.max(0, tableViewportRect.width)}px`;
- this.addRowButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.setPosition(this.addRowButton, tableViewportRect.left + tableViewportRect.width / 2, rowControlCenter);
- }
- if ((state.mode === "column" || state.mode === "both") && columnControlVisible) {
- this.addColumnButton.classList.remove("fn__none");
- this.addColumnButton.classList.add("protyle-table-control__add--active");
- this.addColumnButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.addColumnButton.style.height = `${Math.max(0, tableViewportRect.height)}px`;
- this.setPosition(this.addColumnButton,
+ if (addRowButton && (state.mode === "row" || state.mode === "both")) {
+ addRowButton.classList.remove("fn__none");
+ addRowButton.classList.add("protyle-table-control__add--active");
+ addRowButton.style.width = `${Math.max(0, tableViewportRect.width)}px`;
+ addRowButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
+ this.setPosition(addRowButton, tableViewportRect.left + tableViewportRect.width / 2, rowControlCenter);
+ }
+ if (addColumnButton && (state.mode === "column" || state.mode === "both") && columnControlVisible) {
+ addColumnButton.classList.remove("fn__none");
+ addColumnButton.classList.add("protyle-table-control__add--active");
+ addColumnButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
+ addColumnButton.style.height = `${Math.max(0, tableViewportRect.height)}px`;
+ this.setPosition(addColumnButton,
addColumnEdge + TABLE_ADD_CONTROL_GAP + TABLE_ADD_CONTROL_THICKNESS / 2,
tableViewportRect.top + tableViewportRect.height / 2);
}
- if (state.mode === "both" && columnControlVisible) {
- this.addBothButton.classList.remove("fn__none");
- this.addBothButton.classList.add("protyle-table-control__add--active");
- this.addBothButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.addBothButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.setPosition(this.addBothButton,
+ if (addBothButton && state.mode === "both" && columnControlVisible) {
+ addBothButton.classList.remove("fn__none");
+ addBothButton.classList.add("protyle-table-control__add--active");
+ addBothButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
+ addBothButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
+ this.setPosition(addBothButton,
addColumnEdge + TABLE_ADD_CONTROL_GAP + TABLE_ADD_CONTROL_THICKNESS / 2,
rowControlCenter);
}
diff --git a/app/src/protyle/util/tableTouch.test.ts b/app/src/protyle/util/tableTouch.test.ts
index 5f1a80a13b..08c6613810 100644
--- a/app/src/protyle/util/tableTouch.test.ts
+++ b/app/src/protyle/util/tableTouch.test.ts
@@ -537,6 +537,86 @@ const browserCases = async (sources: Record<string, string>) => {
check.equal(f.table.rows.length, 3, "shrinking preserves all nonempty rows");
f.dispose();
}
+ for (const action of ["add-row", "add-column", "add-both"]) {
+ const f = fixture();
+ f.table.style.marginBottom = "0";
+ const focus = f.cell(1, 1);
+ const range = document.createRange();
+ range.selectNodeContents(focus);
+ range.collapse(true);
+ focusByRange(range);
+ document.dispatchEvent(new Event("selectionchange"));
+ f.render();
+ let handles = ["add-row", "add-column", "add-both"].map(type => f.touch(`[data-action='${type}']`));
+ const active = f.touch(`[data-action='${action}']`);
+ const rect = active.getBoundingClientRect();
+ const x = rect.left + rect.width / 2;
+ const y = rect.top + rect.height / 2;
+ const before = f.table.innerHTML;
+ const count = transactions.length;
+ pointer(active, "pointerdown", x, y);
+ pointer(document, "pointermove", x + 60, y + 90);
+ await frames(3);
+ const checkPositions = () => {
+ const table = f.table.getBoundingClientRect();
+ const row = f.touch("[data-action='add-row']").getBoundingClientRect();
+ const column = f.touch("[data-action='add-column']").getBoundingClientRect();
+ const both = f.touch("[data-action='add-both']").getBoundingClientRect();
+ if (action !== "add-column") {
+ check.equal(row.top + row.height / 2, Math.round(table.bottom + 18), `${action}: row + follows the bottom edge during drag`);
+ check.equal(row.width, table.width);
+ } else {
+ check.equal(f.touch("[data-action='add-row']").classList.contains("fn__none"), true);
+ }
+ if (action !== "add-row") {
+ check.equal(column.left + column.width / 2, Math.round(table.right + 18), `${action}: column + follows the right edge during drag`);
+ check.equal(column.height, table.height);
+ } else {
+ check.equal(f.touch("[data-action='add-column']").classList.contains("fn__none"), true);
+ }
+ if (action === "add-both") {
+ check.equal(both.left + both.width / 2, Math.round(table.right + 18));
+ check.equal(both.top + both.height / 2, Math.round(table.bottom + 18));
+ } else {
+ check.equal(f.touch("[data-action='add-both']").classList.contains("fn__none"), true);
+ }
+ check.equal(f.touch(".protyle-table-control__touch-handle:not(.fn__none)"), null,
+ "size dragging hides row and column selection handles like desktop");
+ handles.forEach(handle => check.equal(handle.isConnected, true, "dragging retains the original input targets"));
+ };
+ checkPositions();
+ pointer(document, "pointermove", x - 30, y - 20);
+ await frames(3);
+ checkPositions();
+ pointer(document, "pointercancel", x - 30, y - 20);
+ await frames(3);
+ check.equal(f.table.innerHTML, before, "canceling a size drag restores the original table");
+ check.equal(transactions.length, count);
+ const restored = f.touch("[data-action='add-both']").getBoundingClientRect();
+ check.equal(restored.left + restored.width / 2, f.table.getBoundingClientRect().right + 18);
+ check.equal(restored.top + restored.height / 2, f.table.getBoundingClientRect().bottom + 18);
+ if (action === "add-both") {
+ handles = ["add-row", "add-column", "add-both"].map(type => f.touch(`[data-action='${type}']`));
+ const button = f.touch("[data-action='add-both']");
+ pointer(button, "pointerdown", x, y);
+ f.root.style.width = "300px";
+ pointer(document, "pointermove", x + 180, y + 90);
+ await frames(3);
+ check.equal(button.classList.contains("fn__none"), true, "an offscreen right edge does not leave controls over cells");
+ f.root.style.width = "360px";
+ pointer(document, "pointermove", x + 60, y + 90);
+ await frames(3);
+ check.equal(button.classList.contains("fn__none"), false, "controls return when the right edge fits again");
+ checkPositions();
+ pointer(document, "pointerup", x + 60, y + 90);
+ button.click();
+ await frames(3);
+ check.equal(transactions.length, count + 1, "a finished size drag creates one transaction without an extra add click");
+ check.equal(f.table.rows.length, 6);
+ check.equal(f.table.rows[0].cells.length, 4);
+ }
+ f.dispose();
+ }
{
const f = fixture();
f.root.style.height = `${window.innerHeight}px`;
@@ -713,6 +793,32 @@ const browserCases = async (sources: Record<string, string>) => {
check.equal(ran, false, "unfinished composition blocks structural menu actions");
}
mobile = false;
+ for (const mode of ["row", "column", "both"]) {
+ const f = fixture();
+ f.table.style.marginBottom = "0";
+ Reflect.set(f.core, "hoverCell", f.cell(1, 1));
+ const before = f.table.innerHTML;
+ const event = new PointerEvent("pointerdown", {pointerId: 10, button: 0, clientX: 300, clientY: 226});
+ f.invoke("startResize", event, `add-${mode}`);
+ pointer(document, "pointermove", 360, 316, 10);
+ await frames(3);
+ const row = f.touch("[data-type='add-row']");
+ const column = f.touch("[data-type='add-column']");
+ check.equal(row.classList.contains("fn__none"), mode === "column");
+ check.equal(column.classList.contains("fn__none"), mode === "row");
+ if (mode !== "column") {
+ const rect = row.getBoundingClientRect();
+ check.equal(rect.top + rect.height / 2, Math.round(f.table.getBoundingClientRect().bottom + 18));
+ }
+ if (mode !== "row") {
+ const rect = column.getBoundingClientRect();
+ check.equal(rect.left + rect.width / 2, Math.round(f.table.getBoundingClientRect().right + 18));
+ }
+ pointer(document, "pointercancel", 360, 316, 10);
+ await frames(3);
+ check.equal(f.table.innerHTML, before, "desktop size-drag cancellation preserves the table");
+ f.dispose();
+ }
{
const f = fixture();
check.equal(f.touch(".protyle-table-control__toolbar"), null, "desktop retains its existing controls");
diff --git a/app/src/protyle/util/tableTouch.ts b/app/src/protyle/util/tableTouch.ts
index f28436e190..8e5936adfd 100644
--- a/app/src/protyle/util/tableTouch.ts
+++ b/app/src/protyle/util/tableTouch.ts
@@ -253,6 +253,14 @@ export class TableTouchControl {
this.renderEndpoints(state);
}
+ public renderResize() {
+ this.controls.querySelectorAll<HTMLElement>(".protyle-table-control__handle, .protyle-table-control__width-guide")
+ .forEach(control => control.classList.add("fn__none"));
+ this.endpoints.forEach(button => button.classList.add("fn__none"));
+ return ["add-row", "add-column", "add-both"].map(type =>
+ this.controls.querySelector<HTMLButtonElement>(`[data-action="${type}"]`));
+ }
+
private renderEndpoints(state?: ITableTouchState) {
this.endpoints.forEach(button => button.classList.add("fn__none"));
const selection = state?.selection;