Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/mathquill.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ declare namespace MathQuill {
disableAutoSubstitutionInSubscripts?: boolean | { except: string };
interpretTildeAsSim?: boolean;
handlers?: HandlerOptions<BaseMathQuill<$>>;
askIfShouldIgnoreMousemove?: (
evt: MouseEvent,
rootElt: HTMLElement
) => boolean;
}

interface Handler<MQClass> {
Expand Down
5 changes: 5 additions & 0 deletions src/publicapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class Options {
constructor(public version: 1 | 2 | 3) {}

ignoreNextMousedown: (_el: MouseEvent) => boolean;
askIfShouldIgnoreMousemove: (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: should this just be ignoreMousemove? That would be consistent with the existing ignoreNextMousedown

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. That one is weird becuase we dont use the callback. We use the method that you can call. So the callback version doesnt really need to exist. And this definitely isn't something you should call to ignore a mousedown. It really is only there to answer the question when mathquill needs it answered

evt: MouseEvent,
rootDOM: HTMLElement
) => boolean;

substituteTextarea: () => HTMLElement;
/** Only used in interface versions 1 and 2. */
substituteKeyboardEvents: SubstituteKeyboardEvents;
Expand Down
25 changes: 23 additions & 2 deletions src/services/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const ignoreNextMouseDownNoop = (_el: MouseEvent) => {
};
Options.prototype.ignoreNextMousedown = ignoreNextMouseDownNoop;

const askIfShouldIgnoreMousemoveNoop = (_evt: MouseEvent, _el: HTMLElement) => {
return false;
};
Options.prototype.askIfShouldIgnoreMousemove = askIfShouldIgnoreMousemoveNoop;

// Whenever edits to the tree occur, in-progress selection events
// must be invalidated and selection changes must not be applied to
// the edited tree. cancelSelectionOnEdit takes care of this.
Expand Down Expand Up @@ -62,11 +67,25 @@ class Controller_mouse extends Controller_latex {
}

var lastMousemoveTarget: HTMLElement | null = null;
function mousemove(e: Event) {
function mousemove(e: MouseEvent) {
if (
rootElement &&
cursor.options.askIfShouldIgnoreMousemove(e, rootElement)
)
return;
lastMousemoveTarget = e.target as HTMLElement | null;
}
function onDocumentMouseMove(e: MouseEvent) {
if (!cursor.anticursor) cursor.startSelection();
if (
rootElement &&
cursor.options.askIfShouldIgnoreMousemove(e, rootElement)
)
return;

if (!cursor.anticursor) {
ctrlr.restoreLatexSelection(originalSelection);
cursor.startSelection();
}
ctrlr.seek(lastMousemoveTarget, e.clientX, e.clientY).cursor.select();
if (cursor.selection)
cursor.controller.aria
Expand Down Expand Up @@ -131,6 +150,8 @@ class Controller_mouse extends Controller_latex {
.seek(e.target as HTMLElement | null, e.clientX, e.clientY)
.cursor.startSelection();

const originalSelection = ctrlr.exportLatexSelection().selection;

rootElement?.addEventListener('mousemove', mousemove);
ownerDocument?.addEventListener('mousemove', onDocumentMouseMove);
ownerDocument?.addEventListener('mouseup', onDocumentMouseUp);
Expand Down
54 changes: 54 additions & 0 deletions test/unit/mouse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,60 @@ suite('mouse', function () {
endIndex: 12
});
});

test('askIfShouldIgnoreMousemove prevents drag selection', function () {
const mq = MQ.MathField($('<span></span>').appendTo('#mock')[0]);

let shouldIgnore = false;
mq.__controller.cursor.options.askIfShouldIgnoreMousemove = () => {
return shouldIgnore;
};

mq.latex('1+3+5+7+1');
const rect = mq.el().getBoundingClientRect();
const y = rect.top + 10;

const three = $('#mock .mq-digit:contains(3)');
const five = $('#mock .mq-digit:contains(5)');
const beforeOne = rect.left + 1;
const afterThree = three.get(0).getBoundingClientRect().right;
const afterFive = five.get(0).getBoundingClientRect().right;

// Baseline: normal drag selects
dispatchMouseEventAtPoint('mousedown', beforeOne, y);
dispatchMouseEventAtPoint('mousemove', afterThree, y);
assertDeepEqual(mq.selection(), {
latex: '1+3+5+7+1',
startIndex: 0,
endIndex: 3
});

// With ignore=true: drag should not extend selection
shouldIgnore = true;
dispatchMouseEventAtPoint('mousemove', afterFive, y);
assertDeepEqual(mq.selection(), {
latex: '1+3+5+7+1',
startIndex: 0,
endIndex: 3
});

// With ignore=false again: drag works
shouldIgnore = false;
dispatchMouseEventAtPoint('mousemove', afterFive + 1, y);
assertDeepEqual(mq.selection(), {
latex: '1+3+5+7+1',
startIndex: 0,
endIndex: 5
});

// can go back to 3 with ignore=false
dispatchMouseEventAtPoint('mousemove', afterThree, y);
assertDeepEqual(mq.selection(), {
latex: '1+3+5+7+1',
startIndex: 0,
endIndex: 3
});
});
});

function assertDeepEqual(a, b) {
Expand Down