Skip to content

Commit

Permalink
Merge pull request #317 from desmosinc/small-tweak-to-focus-behavior
Browse files Browse the repository at this point in the history
Small tweak to focus behavior
  • Loading branch information
eluberoff authored Dec 16, 2024
2 parents c508d3b + 70dae6a commit 13a922b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/mathquill.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ declare namespace MathQuill {
setAriaLabel(str: string): this;
blur(): this;
focus(): this;
select(): this;
}

interface EditableMathQuill extends BaseMathQuill {
select: () => EditableMathQuill;
moveToRightEnd: () => EditableMathQuill;
moveToLeftEnd: () => EditableMathQuill;
cmd: (latex: string) => EditableMathQuill;
Expand Down
8 changes: 4 additions & 4 deletions src/publicapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ function getInterface(v: number): MathQuill.v3.API | MathQuill.v1.API {
if (this.__controller.editable) this.__controller.scrollHoriz();
return this;
}
select() {
this.__controller.selectAll();
return this;
}
blur() {
this.__controller.getTextarea().blur();
return this;
Expand All @@ -376,10 +380,6 @@ function getInterface(v: number): MathQuill.v3.API | MathQuill.v1.API {
this.__controller.editablesTextareaEvents();
return this;
}
select() {
this.__controller.selectAll();
return this;
}
clearSelection() {
this.__controller.cursor.clearSelection();
return this;
Expand Down
3 changes: 2 additions & 1 deletion src/services/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class Controller_mouse extends Controller_latex {
};

if (ctrlr.blurred) {
textarea.focus();
//for static mathquills, we focus on mousemove
if (this.editable) textarea.focus();
// focus call may bubble to clients, who may then write to
// mathquill, triggering cancelSelectionOnEdit. If that happens, we
// don't want to stop the cursor blink or bind listeners,
Expand Down
34 changes: 34 additions & 0 deletions test/unit/focusBlur.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,38 @@ suite('focusBlur', function () {
mq.blur();
assertHasFocus(mq, 'math field', 'not');
});

test('static math does not focus on click', function (done) {
var mq = MQ.StaticMath(
$('<span>1234\\times 10^{23}</span>').appendTo('#mock')[0]
);

const clickEvent = new Event('mousedown', {
bubbles: true,
cancelable: true
});

mq.el().dispatchEvent(clickEvent);
setTimeout(function () {
assertHasFocus(mq, 'math field', 'not');
done();
}, 100);
});

test('editable math does focus on click', function (done) {
var mq = MQ.MathField(
$('<span>1234\\times 10^{23}</span>').appendTo('#mock')[0]
);

const clickEvent = new Event('mousedown', {
bubbles: true,
cancelable: true
});

mq.el().dispatchEvent(clickEvent);
setTimeout(function () {
assertHasFocus(mq, 'math field');
done();
}, 100);
});
});

0 comments on commit 13a922b

Please sign in to comment.