Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: fix unwanted selection while scrolling sideways with the middle mouse button

Adorian Doranf597c634f160
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
index 0996114d80..f12c6bac5b 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
@@ -431,6 +431,37 @@ describe("TableContextMenu", () => {
expect(paragraph && mouseDown(paragraph, 2).defaultPrevented).toBe(false);
});
});
+
+ describe("mouse cell selection", () => {
+ it("selects cells only for gestures started with the primary button", () => {
+ const tableSelection = editor.plugins.get(TableSelection);
+ const placeCaret = () =>
+ setModelData(editor.model, modelTable([["1[]1", "12"], ["21", "22"]]));
+ const hasCellSelection = () => tableSelection.getSelectedTableCells() !== null;
+
+ // Middle and right drags.
+ for (const [button, buttons] of [[1, 4], [2, 2]]) {
+ placeCaret();
+ mouseDown(domCell(editor, 0, 0), button, { buttons });
+ mouseMove(domCell(editor, 1, 1), buttons);
+ expect(hasCellSelection(), `button ${button}`).toBe(false);
+ mouseUp(domCell(editor, 1, 1), button);
+ }
+
+ // Shift with the middle button.
+ placeCaret();
+ mouseDown(domCell(editor, 1, 1), 1, { buttons: 4, shiftKey: true });
+ expect(hasCellSelection(), "shift").toBe(false);
+ mouseUp(domCell(editor, 1, 1), 1);
+
+ // Primary drag.
+ placeCaret();
+ mouseDown(domCell(editor, 0, 0), 0, { buttons: 1 });
+ mouseMove(domCell(editor, 1, 1), 1);
+ expect(selectionCells(editor)).toHaveLength(4);
+ mouseUp(domCell(editor, 1, 1), 0);
+ });
+ });
});
function modelTable(rows: string[][], attributes = ""): string {
@@ -494,8 +525,16 @@ function RawTableWidget(editor: Editor) {
});
}
-function mouseDown(target: HTMLElement, button: number): MouseEvent {
- const event = new MouseEvent("mousedown", { button, bubbles: true, cancelable: true });
+function mouseDown(target: HTMLElement, button: number, init: MouseEventInit = {}): MouseEvent {
+ const event = new MouseEvent("mousedown", { ...init, button, bubbles: true, cancelable: true });
target.dispatchEvent(event);
return event;
}
+
+function mouseMove(target: HTMLElement, buttons: number) {
+ target.dispatchEvent(new MouseEvent("mousemove", { buttons, bubbles: true }));
+}
+
+function mouseUp(target: HTMLElement, button: number) {
+ target.dispatchEvent(new MouseEvent("mouseup", { button, bubbles: true }));
+}
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.ts b/packages/ckeditor5/src/plugins/table_context_menu.ts
index b4576eb912..0f1427ca3f 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.ts
@@ -4,6 +4,7 @@ import {
Plugin,
Table,
TableColumnResize,
+ TableMouse,
TableSelection,
TableUtils,
TableWalker
@@ -17,10 +18,11 @@ 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, 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.
+ * `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
+ * `mergeTableCells`, `splitTableCell*` and `removeTableRow` / `removeTableColumn` commands already
+ * act on the whole selection.
*/
export default class TableContextMenu extends Plugin {
@@ -54,12 +56,22 @@ export default class TableContextMenu extends Plugin {
new TableHeaderRowsCommand(editor, "laterRow"));
const view = editor.editing.view;
+ const tableMouse = editor.plugins.get(TableMouse);
view.addObserver(MouseObserver);
this.listenTo<ViewDocumentMouseDownEvent>(view.document, "mousedown", (evt, data) => {
+ const { button } = data.domEvent;
+
+ // Limits `TableMouse` drag and Shift+click cell selection to the primary button.
+ // `TableMouse` checks `isEnabled` only on mousedown.
+ if (button === PRIMARY_MOUSE_BUTTON) {
+ tableMouse.clearForceDisabled(TableContextMenu.pluginName);
+ } else {
+ tableMouse.forceDisabled(TableContextMenu.pluginName);
+ }
+
// Stops the caret move that would collapse a multi-cell selection before the
// `contextmenu` event fires.
- const isRightButton = data.domEvent.button === RIGHT_MOUSE_BUTTON;
- if (isRightButton && this.isInsideCellSelection(data.domTarget)) {
+ if (button === RIGHT_MOUSE_BUTTON && this.isInsideCellSelection(data.domTarget)) {
data.preventDefault();
}
}, { priority: "high" });
@@ -102,6 +114,7 @@ export default class TableContextMenu extends Plugin {
}
+const PRIMARY_MOUSE_BUTTON = 0;
const RIGHT_MOUSE_BUTTON = 2;
/** Where {@link TableMultiInsertCommand} inserts, relative to the rows or columns it spans. */