Skip to content

Commit

Permalink
add substituteKeyboardEvents option
Browse files Browse the repository at this point in the history
Allows us to spy on the sane keyboard events to decide if we want mathquill to process them or not.
  • Loading branch information
mikehaverstock committed Jun 2, 2016
1 parent 27473d5 commit 8dc9709
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ keyboards just don't work in Desmos on iOS, the tradeoffs are up to you.
[stucox]: http://www.stucox.com/blog/you-cant-detect-a-touchscreen/
[Modernizr]: https://github.com/Modernizr/Modernizr/issues/548


`substituteKeyboardEvents`, a function that wraps the normal saneKeyboardEvents util
so that normalized events can be spied on. If you specify new 'cut', 'paste', 'copy',
'typedText', or 'keystroke' handlers then it's your responsibility to make sure the
original handler gets called.

Supported handlers:
- `moveOutOf`, `deleteOutOf`, and `selectOutOf` are called with `dir` and the
math field API object as arguments
Expand Down
1 change: 1 addition & 0 deletions src/publicapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function getInterface(v) {

MQ.L = L;
MQ.R = R;
MQ.saneKeyboardEvents = saneKeyboardEvents;

function config(currentOptions, newOptions) {
if (newOptions && newOptions.handlers) {
Expand Down
3 changes: 2 additions & 1 deletion src/services/textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ Controller.open(function(_) {
if (text) textarea.select();
};
};
Options.p.substituteKeyboardEvents = saneKeyboardEvents;
_.editablesTextareaEvents = function() {
var ctrlr = this, textarea = ctrlr.textarea, textareaSpan = ctrlr.textareaSpan;

var keyboardEventsShim = saneKeyboardEvents(textarea, this);
var keyboardEventsShim = this.options.substituteKeyboardEvents(textarea, this);
this.selectFn = function(text) { keyboardEventsShim.select(text); };

this.container.prepend(textareaSpan)
Expand Down
21 changes: 21 additions & 0 deletions test/unit/publicapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,27 @@ suite('Public API', function() {
});
});

suite('substituteKeyboardEvents', function() {
test('can intercept key events', function() {
var mq = MQ.MathField($('<span>').appendTo('#mock')[0], {
substituteKeyboardEvents: function(textarea, handlers) {
return MQ.saneKeyboardEvents(textarea, jQuery.extend({}, handlers, {
keystroke: function(_key, evt) {
key = _key;
return handlers.keystroke.apply(handlers, arguments);
}
}));
}
});
var key;

$(mq.el()).find('textarea').trigger({ type: 'keydown', which: '37' });
assert.equal(key, 'Left');

$(mq.el()).remove();
});
});

suite('clickAt', function() {
test('inserts at coordinates', function() {
// Insert filler so that the page is taller than the window so this test is deterministic
Expand Down
29 changes: 29 additions & 0 deletions test/visual.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ <h3>substituteTextarea</h3>

<p>In Safari on iOS, this should be focusable but not bring up the on-screen keyboard; to test, try focusing anything else and confirm this blurs: <span id="no-kbd-math"></span> (confirmed working on iOS 6.1.3)</p>

<h3>substituteKeyboardEvents</h3>

<p>Should be able to prevent cut, typing, and pasting in this field. Should get a log of the event instead: <span id="disable-typing">1+2+3</span></p>

<p>Should wrap anything you type in '<>' <span id="wrap-typing">1+2+3</span></p>

</div>
<script type="text/javascript">
window.onerror = function() {
Expand Down Expand Up @@ -446,6 +452,29 @@ <h3>substituteTextarea</h3>
return $('<span tabindex=0 style="display:inline-block;width:1px;height:1px" />')[0];
}
});

MQ.MathField($('#disable-typing')[0], {
substituteKeyboardEvents: function(textarea, handlers) {
return MQ.saneKeyboardEvents(textarea, $.extend({}, handlers, {
cut: $.noop,
paste: $.noop,
keystroke: $.noop,
typedText: $.noop
}));
}
});

MQ.MathField($('#wrap-typing')[0], {
substituteKeyboardEvents: function(textarea, handlers) {
return MQ.saneKeyboardEvents(textarea, $.extend({}, handlers, {
typedText: function (text) {
handlers.typedText('<');
handlers.typedText(text);
handlers.typedText('>');
}
}));
}
});
</script>
</body>
</html>

0 comments on commit 8dc9709

Please sign in to comment.