hack to make it work.

update-sendkeys
John McLear 2020-12-28 12:33:44 +00:00
parent cc040cc733
commit f225574d25
1 changed files with 15 additions and 20 deletions

View File

@ -1,15 +1,11 @@
'use strict';
let bililiteRange; // create one global variable
(function(){ (function(){
const datakey = Symbol(); // use as the key to modify elements. const datakey = Symbol(); // use as the key to modify elements.
bililiteRange = function(el){ bililiteRange = function(el){
var ret; var ret;
if (el.setSelectionRange){ if (el.setSelectionRange){
// Element is an input or textarea // Element is an input or textarea
// note that some input elements do not allow selections // note that some input elements do not allow selections
try{ try{
el.selectionStart = el.selectionStart; el.selectionStart = el.selectionStart;
@ -26,16 +22,16 @@ bililiteRange = function(el){
ret._doc = el.ownerDocument; ret._doc = el.ownerDocument;
ret._win = ret._doc.defaultView; ret._win = ret._doc.defaultView;
ret._bounds = [0, ret.length]; ret._bounds = [0, ret.length];
// selection tracking. We want clicks to set the selection to the clicked location but tabbing in or element.focus() should restore // selection tracking. We want clicks to set the selection to the clicked location but tabbing in or element.focus() should restore
// the selection to what it was. // the selection to what it was.
// There's no good way to do this. I just assume that a mousedown (or a drag and drop // There's no good way to do this. I just assume that a mousedown (or a drag and drop
// into the element) within 100 ms of the focus event must have caused the focus, and // into the element) within 100 ms of the focus event must have caused the focus, and
// therefore we should not restore the selection. // therefore we should not restore the selection.
if (!(el[datakey])){ // we haven't processed this element yet if (!(el[datakey])){ // we haven't processed this element yet
const data = createDataObject (el); const data = createDataObject (el);
startupHooks.forEach ( hook => hook (el, ret, data) ); startupHooks.forEach ( hook => hook (el, ret, data) );
} }
return ret; return ret;
} }
@ -49,7 +45,7 @@ startupHooks.add (correctNewlines);
function trackSelection (element, range, data){ function trackSelection (element, range, data){
data.selection = [0,0]; data.selection = [0,0];
range.listen('focusout', evt => data.selection = range._nativeSelection() ); range.listen('focusout', evt => data.selection = range._nativeSelection() );
range.listen('mousedown', evt => data.mousetime = evt.timeStamp ); range.listen('mousedown', evt => data.mousetime = evt.timeStamp );
range.listen('drop', evt => data.mousetime = evt.timeStamp ); range.listen('drop', evt => data.mousetime = evt.timeStamp );
range.listen('focus', evt => { range.listen('focus', evt => {
@ -63,7 +59,7 @@ function fixInputEvents (element, range, data){
// have a data field with the text inserted, but thatisn't enough to fully describe the change; // have a data field with the text inserted, but thatisn't enough to fully describe the change;
// we need to know the old text (or at least its length) // we need to know the old text (or at least its length)
// and *where* the new text was inserted. // and *where* the new text was inserted.
// So we enhance input events with that information. // So we enhance input events with that information.
// the "newText" should always be the same as the 'data' field, if it is defined // the "newText" should always be the same as the 'data' field, if it is defined
data.oldText = range.all(); data.oldText = range.all();
data.liveRanges = new Set(); data.liveRanges = new Set();
@ -77,7 +73,7 @@ function fixInputEvents (element, range, data){
} }
} }
data.oldText = newText; data.oldText = newText;
// Also update live ranges on this element // Also update live ranges on this element
data.liveRanges.forEach( rng => { data.liveRanges.forEach( rng => {
const start = evt.bililiteRange.start; const start = evt.bililiteRange.start;
@ -99,7 +95,7 @@ function fixInputEvents (element, range, data){
}else{ }else{
b1 = start; b1 = start;
} }
rng.bounds([b0, b1]); rng.bounds([b0, b1]);
}) })
}); });
} }
@ -181,7 +177,7 @@ function inputEventInit(type, oldText, newText, start, inputType){
// base class // base class
function Range(){} function Range(){}
Range.prototype = { Range.prototype = {
// allow use of range[0] and range[1] for start and end of bounds // allow use of range[0] and range[1] for start and end of bounds
get 0(){ get 0(){
return this.bounds()[0]; return this.bounds()[0];
}, },
@ -457,7 +453,7 @@ W3CRange.prototype._nativeRange = function (bounds){
rng.collapse (true); rng.collapse (true);
w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el); w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el);
} }
return rng; return rng;
}; };
W3CRange.prototype._nativeSelect = function (rng){ W3CRange.prototype._nativeSelect = function (rng){
this._win.getSelection().removeAllRanges(); this._win.getSelection().removeAllRanges();
@ -556,11 +552,11 @@ var START_TO_END = 1;
var END_TO_END = 2; var END_TO_END = 2;
var END_TO_START = 3; var END_TO_START = 3;
// from the Mozilla documentation, for range.compareBoundaryPoints(how, sourceRange) // from the Mozilla documentation, for range.compareBoundaryPoints(how, sourceRange)
// -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before, equal to, or after the corresponding boundary-point of sourceRange. // -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before, equal to, or after the corresponding boundary-point of sourceRange.
// * Range.END_TO_END compares the end boundary-point of sourceRange to the end boundary-point of range. // * Range.END_TO_END compares the end boundary-point of sourceRange to the end boundary-point of range.
// * Range.END_TO_START compares the end boundary-point of sourceRange to the start boundary-point of range. // * Range.END_TO_START compares the end boundary-point of sourceRange to the start boundary-point of range.
// * Range.START_TO_END compares the start boundary-point of sourceRange to the end boundary-point of range. // * Range.START_TO_END compares the start boundary-point of sourceRange to the end boundary-point of range.
// * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range. // * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range.
function w3cstart(rng, constraint){ function w3cstart(rng, constraint){
if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning
if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length; if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length;
@ -610,7 +606,7 @@ function signalMonitor(prop, value, element){
const attr = `data-${prop}`; const attr = `data-${prop}`;
element.dispatchEvent(new CustomEvent(attr, {bubbles: true, detail: value})); element.dispatchEvent(new CustomEvent(attr, {bubbles: true, detail: value}));
try{ try{
element.setAttribute (attr, value); // illegal attribute names will throw. Ignore it element.setAttribute (attr, value); // illegal attribute names will throw. Ignore it
} finally { /* ignore */ } } finally { /* ignore */ }
} }
@ -619,7 +615,7 @@ function createDataObject (el){
set(obj, prop, value) { set(obj, prop, value) {
obj[prop] = value; obj[prop] = value;
if (monitored.has(prop)) signalMonitor(prop, value, obj.sourceElement); if (monitored.has(prop)) signalMonitor(prop, value, obj.sourceElement);
return true; // in strict mode, 'set' returns a success flag return true; // in strict mode, 'set' returns a success flag
} }
}); });
} }
@ -666,4 +662,3 @@ bililiteRange.createOption = function (name, desc = {}){
} }
})(); })();