saneKeyboardEvents handles cut/copy, not just paste
Requested by @mikehaverstock:
https://github.com/desmosinc/mathquill/pull/27#discussion_r65231110
It really should've in the first place, as the source-of-truth on
keyboard/textare events.
--
Previously listened for `copy` event on textarea for both editable
fields and static math, now only for static math (which doesn't use
`saneKeyboardEvents()`), for editable fields the `cut` and `copy`
handlers are just methods on the controller object.
diff --git a/src/services/saneKeyboardEvents.util.js b/src/services/saneKeyboardEvents.util.js
index cdb9686..a713586 100644
--- a/src/services/saneKeyboardEvents.util.js
+++ b/src/services/saneKeyboardEvents.util.js
@@ -229,6 +229,8 @@
keydown: onKeydown,
keypress: onKeypress,
focusout: onBlur,
+ cut: function(e) { handlers.cut(e); },
+ copy: function(e) { handlers.copy(e); },
paste: onPaste
});
diff --git a/src/services/textarea.js b/src/services/textarea.js
index 31ff8d0..eaed10c 100644
--- a/src/services/textarea.js
+++ b/src/services/textarea.js
@@ -18,7 +18,6 @@
var ctrlr = this;
ctrlr.cursor.selectionChanged = function() { ctrlr.selectionChanged(); };
- ctrlr.container.bind('copy', function() { ctrlr.setTextareaSelection(); });
};
_.selectionChanged = function() {
var ctrlr = this;
@@ -52,6 +51,7 @@
this.container.prepend('<span class="mq-selectable">$'+ctrlr.exportLatex()+'$</span>');
ctrlr.blurred = true;
textarea.bind('cut paste', false)
+ .bind('copy', function() { ctrlr.setTextareaSelection(); })
.focus(function() { ctrlr.blurred = false; }).blur(function() {
if (cursor.selection) cursor.selection.clear();
setTimeout(detach); //detaching during blur explodes in WebKit
@@ -68,22 +68,11 @@
};
Options.p.substituteKeyboardEvents = saneKeyboardEvents;
_.editablesTextareaEvents = function() {
- var ctrlr = this, root = ctrlr.root, cursor = ctrlr.cursor,
- textarea = ctrlr.textarea, textareaSpan = ctrlr.textareaSpan;
+ var ctrlr = this, textarea = ctrlr.textarea, textareaSpan = ctrlr.textareaSpan;
var keyboardEventsShim = this.options.substituteKeyboardEvents(textarea, this);
this.selectFn = function(text) { keyboardEventsShim.select(text); };
-
- this.container.prepend(textareaSpan)
- .on('cut', function(e) {
- if (cursor.selection) {
- setTimeout(function() {
- ctrlr.notify('edit'); // deletes selection if present
- cursor.parent.bubble('reflow');
- });
- }
- });
-
+ this.container.prepend(textareaSpan);
this.focusBlurEvents();
};
_.typedText = function(ch) {
@@ -92,6 +81,18 @@
cursor.parent.write(cursor, ch);
this.scrollHoriz();
};
+ _.cut = function() {
+ var ctrlr = this, cursor = ctrlr.cursor;
+ if (cursor.selection) {
+ setTimeout(function() {
+ ctrlr.notify('edit'); // deletes selection if present
+ cursor.parent.bubble('reflow');
+ });
+ }
+ };
+ _.copy = function() {
+ this.setTextareaSelection();
+ };
_.paste = function(text) {
// TODO: document `statelessClipboard` config option in README, after
// making it work like it should, that is, in both text and math mode