Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: prevent columns being resized via the middle mouse button

Adorian Doran8fb29b9b719e
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
index f12c6bac5b..5407d7502b 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
@@ -462,6 +462,24 @@ describe("TableContextMenu", () => {
mouseUp(domCell(editor, 1, 1), 0);
});
});
+
+ describe("column resizing", () => {
+ it("starts only for the primary button", async () => {
+ setModelData(editor.model, modelTable([ [ "11", "12" ], [ "21", "22" ] ]));
+ const unresized = tableData(editor);
+
+ // Middle and right drags.
+ for (const [ button, buttons ] of [ [ 1, 4 ], [ 2, 2 ] ]) {
+ const mousedown = dragColumnResizer(editor, button, buttons);
+ expect(mousedown.defaultPrevented).toBe(false);
+ expect(tableData(editor)).toBe(unresized);
+ await waitForResizeThrottle();
+ }
+
+ dragColumnResizer(editor, 0, 1);
+ expect(tableData(editor)).not.toBe(unresized);
+ });
+ });
});
function modelTable(rows: string[][], attributes = ""): string {
@@ -531,6 +549,30 @@ function mouseDown(target: HTMLElement, button: number, init: MouseEventInit = {
return event;
}
+/**
+ * Drags the column resizer of the first cell 60px to the right with `button` and returns the
+ * `mousedown` event.
+ */
+function dragColumnResizer(editor: ClassicEditor, button: number, buttons: number) {
+ const resizer = domCell(editor, 0, 0).querySelector<HTMLElement>(".ck-table-column-resizer");
+ if (!resizer) {
+ throw new Error("The cell has no column resizer.");
+ }
+
+ const { left, top } = resizer.getBoundingClientRect();
+ const init = { button, buttons, clientY: top, bubbles: true, cancelable: true };
+ const mousedown = new MouseEvent("mousedown", { ...init, clientX: left });
+ resizer.dispatchEvent(mousedown);
+ resizer.dispatchEvent(new MouseEvent("mousemove", { ...init, clientX: left + 60 }));
+ resizer.dispatchEvent(new MouseEvent("mouseup", { ...init, clientX: left + 60 }));
+ return mousedown;
+}
+
+/** Waits out the 50ms throttle of the `mousemove` handler of `TableColumnResizeEditing`. */
+function waitForResizeThrottle() {
+ return new Promise((resolve) => setTimeout(resolve, 60));
+}
+
function mouseMove(target: HTMLElement, buttons: number) {
target.dispatchEvent(new MouseEvent("mousemove", { buttons, bubbles: true }));
}
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.ts b/packages/ckeditor5/src/plugins/table_context_menu.ts
index 0f1427ca3f..58aaee1bc3 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.ts
@@ -19,8 +19,8 @@ import { DistributeTableColumnsCommand } from "./table_distribute_columns.js";
* Registers insert commands that honor the size of the current selection (the upstream
* `insertTableRow*` / `insertTableColumn*` commands always insert one row or column) and
* `triliumDistributeTableColumns`, keeps a multi-cell selection alive under a right-click, limits
- * mouse cell selection to the primary button, and moves the selection to the cell a context menu
- * is opened on. Merge, split and delete need no counterparts here: the upstream
+ * mouse cell selection and column resizing to the primary button, and moves the selection to the
+ * cell a context menu is opened on. Merge, split and delete need no counterparts here: the upstream
* `mergeTableCells`, `splitTableCell*` and `removeTableRow` / `removeTableColumn` commands already
* act on the whole selection.
*/
@@ -74,7 +74,13 @@ export default class TableContextMenu extends Plugin {
if (button === RIGHT_MOUSE_BUTTON && this.isInsideCellSelection(data.domTarget)) {
data.preventDefault();
}
- }, { priority: "high" });
+
+ // Keeps `TableColumnResizeEditing` from starting a resize. The default action, such as
+ // the autoscroll of the middle button, still runs.
+ if (button !== PRIMARY_MOUSE_BUTTON && data.target.hasClass(COLUMN_RESIZER_CLASS)) {
+ evt.stop();
+ }
+ }, { priority: "highest" });
}
/**
@@ -116,6 +122,7 @@ export default class TableContextMenu extends Plugin {
const PRIMARY_MOUSE_BUTTON = 0;
const RIGHT_MOUSE_BUTTON = 2;
+const COLUMN_RESIZER_CLASS = "ck-table-column-resizer";
/** Where {@link TableMultiInsertCommand} inserts, relative to the rows or columns it spans. */
export type TableMultiInsertDirection = "above" | "below" | "left" | "right";