sendkeys: Merge in latest upstream commit that mostly works

5c034c108c
seems to be the newest upstream commit that still works. After that,
`{selectall}` doesn't seem to work. Starting with v3.0, sendkeys sends
input events instead of keypress events, which `ace2_inner.js` doesn't
listen for.
This commit is contained in:
Richard Hansen 2021-08-21 00:19:55 -04:00 committed by SamTV12345
parent 0900354980
commit ed991419b8
2 changed files with 215 additions and 57 deletions

View file

@ -1,7 +1,7 @@
// insert characters in a textarea or text input field
// special characters are enclosed in {}; use {{} for the { character itself
// documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/
// Version: 2.1
// Version: 2.2
// Copyright (c) 2013 Daniel Wachsstock
// MIT license:
// Permission is hereby granted, free of charge, to any person
@ -31,51 +31,33 @@ $.fn.sendkeys = function (x, opts){
return this.each( function(){
var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions
// most elements to not keep track of their selection when they lose focus, so we have to do it for them
var rng = $.data (this, 'sendkeys.selection');
if (!rng){
rng = bililiteRange(this).bounds('selection');
$.data(this, 'sendkeys.selection', rng);
$(this).bind('mouseup.sendkeys', function(){
// we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not
$.data(this, 'sendkeys.selection').bounds('selection');
}).bind('keyup.sendkeys', function(evt){
// restore the selection if we got here with a tab (a click should select what was clicked on)
if (evt.which == 9){
// there's a flash of selection when we restore the focus, but I don't know how to avoid that.
$.data(this, 'sendkeys.selection').select();
}else{
$.data(this, 'sendkeys.selection').bounds('selection');
}
});
}
var rng = $(this).selectionTracker();
$(this).trigger({type: 'beforesendkeys', which: x});
this.focus();
if (typeof x === 'undefined') return; // no string, so we just set up the event handlers
$.data(this, 'sendkeys.originalText', rng.text());
x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions
x.replace(/([^{])\n/g, '$1{enter}'). // turn line feeds into explicit break insertions, but not if escaped
replace(/{[^}]*}|[^{]+/g, function(s){
(localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s);
rng.select();
});
$(this).trigger({type: 'sendkeys', which: x});
});
}; // sendkeys
// add the functions publicly so they can be overridden
$.fn.sendkeys.defaults = {
simplechar: function (rng, s){
// deal with unknown {key}s
if (/^{.*}$/.test(s)) s = s.slice(1,-1);
rng.text(s, 'end');
if (/^{[^}]*}$/.test(s)) s = s.slice(1,-1);
for (var i =0; i < s.length; ++i){
var x = s.charCodeAt(i);
// a bit of cheating: rng._el is the element associated with rng.
$(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
$(rng.element()).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
}
rng.text(s, 'end');
},
'{enter}': function (rng){
rng.insertEOL();
rng.select();
$(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'});
rng.insertEOL();
},
'{backspace}': function (rng){
var b = rng.bounds();
@ -90,26 +72,92 @@ $.fn.sendkeys.defaults = {
'{rightarrow}': function (rng){
var b = rng.bounds();
if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right
rng.bounds([b[1], b[1]]).select();
rng.bounds([b[1], b[1]]);
},
'{leftarrow}': function (rng){
var b = rng.bounds();
if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left
rng.bounds([b[0], b[0]]).select();
rng.bounds([b[0], b[0]]);
},
'{selectall}' : function (rng){
rng.bounds('all').select();
rng.bounds('all');
},
'{selection}': function (rng){
$.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText'));
// insert the characters without the sendkeys processing
var s = $.data(rng.element(), 'sendkeys.originalText');
for (var i =0; i < s.length; ++i){
var x = s.charCodeAt(i);
$(rng.element()).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
}
rng.selection(s);
},
'{mark}' : function (rng){
var bounds = rng.bounds();
$(rng._el).one('sendkeys', function(){
$(rng.element()).one('sendkeys', function(){
// set up the event listener to change the selection after the sendkeys is done
rng.bounds(bounds).select();
});
}
};
// Most ranges do not keep track of what was selected when they lose focus.
// We have to do that for them
$.fn.selectionTracker = function(bounds){
var rng = this.data('selectionTracker');
if (!rng){
rng = bililiteRange(this[0]).bounds('selection');
this.data('selectionTracker', rng);
$(this).on('mouseup.selectionTracker', function(evt){
// we have to update the saved range.
rng.bounds('selection');
}).on('keyup.selectionTracker', function(evt){
// restore the selection if we got here with a tab (a click should select what was clicked on)
if (evt.which == 9){
// there's a flash of selection when we restore the focus, but I don't know how to avoid that.
rng.select();
}else{
rng.bounds('selection');
}
});
}
if (arguments.length > 0) rng.bounds(bounds); // change the saved selection without actually selecting
if (document.activeElement == this[0]) rng.select(); // explicitly select it if already active
return rng;
}
// monkey patch bililiteRange to reflect the saved range
var oldselect = bililiteRange.fn.select;
bililiteRange.fn.select = function(){
var $el = $(this.element());
if (
$el.data('selectionTracker') &&
document.activeElement != $el[0]
){
$el.selectionTracker(this.bounds());
}
return oldselect.apply(this, arguments);
};
var oldbounds = bililiteRange.fn.bounds;
bililiteRange.fn.bounds = function(bounds){
var $el = $(this.element());
if (
$el.data('selectionTracker') && // if we are tracking the selection
document.activeElement != $el[0] && // and the real selection isn't here
bounds == 'selection' // and we want the selection anyway
){
bounds = $el.selectionTracker().bounds(); // use the saved selection
}
return oldbounds.call(this, bounds);
}
// monkey patch focus to actually focus the element, on the saved range
var focus = $.fn.focus;
$.fn.focus = function(){
if (this.length > 0){
this[0].focus();
this.selectionTracker();
}
focus.apply(this, arguments);
}
})(jQuery)