2015-12-21 10:38:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const DELAY_TIME_MS = 500;
|
|
|
|
const KEY_TAB = 9;
|
|
|
|
const KEY_SHIFT = 16;
|
|
|
|
|
2015-12-21 16:35:39 +00:00
|
|
|
// word boundary -> 1 or more non-whitespace chars (group) -> end of line
|
|
|
|
const MATCH_REGEX = /\b(\S+)$/;
|
|
|
|
|
2015-12-21 10:38:37 +00:00
|
|
|
class TabComplete {
|
|
|
|
|
|
|
|
constructor(opts) {
|
|
|
|
opts.startingWordSuffix = opts.startingWordSuffix || "";
|
|
|
|
opts.wordSuffix = opts.wordSuffix || "";
|
2015-12-21 17:16:49 +00:00
|
|
|
opts.allowLooping = opts.allowLooping || false;
|
2015-12-21 17:58:36 +00:00
|
|
|
opts.autoEnterTabComplete = opts.autoEnterTabComplete || false;
|
2015-12-21 10:38:37 +00:00
|
|
|
this.opts = opts;
|
2015-12-21 14:34:25 +00:00
|
|
|
this.completing = false;
|
2015-12-21 16:35:39 +00:00
|
|
|
this.list = []; // full set of tab-completable things
|
|
|
|
this.matchedList = []; // subset of completable things to loop over
|
|
|
|
this.currentIndex = 0; // index in matchedList currently
|
|
|
|
this.originalText = null; // original input text when tab was first hit
|
|
|
|
this.textArea = opts.textArea; // DOMElement
|
|
|
|
this.isFirstWord = false; // true if you tab-complete on the first word
|
2015-12-21 17:58:36 +00:00
|
|
|
this.enterTabCompleteTimerId = null;
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-21 14:34:25 +00:00
|
|
|
* @param {TabComplete.Entry[]} completeList
|
2015-12-21 10:38:37 +00:00
|
|
|
*/
|
|
|
|
setCompletionList(completeList) {
|
|
|
|
this.list = completeList;
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:35:39 +00:00
|
|
|
/**
|
|
|
|
* @param {DOMElement}
|
|
|
|
*/
|
2015-12-21 10:38:37 +00:00
|
|
|
setTextArea(textArea) {
|
|
|
|
this.textArea = textArea;
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:35:39 +00:00
|
|
|
/**
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2015-12-21 10:59:10 +00:00
|
|
|
isTabCompleting() {
|
2015-12-21 14:34:25 +00:00
|
|
|
return this.completing;
|
2015-12-21 10:59:10 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 17:16:49 +00:00
|
|
|
stopTabCompleting() {
|
|
|
|
this.completing = false;
|
|
|
|
this.currentIndex = 0;
|
|
|
|
this._notifyStateChange();
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:58:36 +00:00
|
|
|
startTabCompleting() {
|
|
|
|
this.completing = true;
|
|
|
|
this.currentIndex = 0;
|
|
|
|
this._calculateCompletions();
|
|
|
|
}
|
|
|
|
|
2015-12-21 14:34:25 +00:00
|
|
|
/**
|
2015-12-21 16:35:39 +00:00
|
|
|
* @param {Number} numAheadToPeek Return *up to* this many elements.
|
2015-12-21 14:34:25 +00:00
|
|
|
* @return {TabComplete.Entry[]}
|
|
|
|
*/
|
|
|
|
peek(numAheadToPeek) {
|
2015-12-21 16:35:39 +00:00
|
|
|
if (this.matchedList.length === 0) {
|
|
|
|
return [];
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
2015-12-21 17:16:49 +00:00
|
|
|
var peekList = [];
|
2015-12-21 10:38:37 +00:00
|
|
|
|
2015-12-21 16:35:39 +00:00
|
|
|
// return the current match item and then one with an index higher, and
|
2015-12-21 17:16:49 +00:00
|
|
|
// so on until we've reached the requested limit. If we hit the end of
|
|
|
|
// the list of options we're done.
|
|
|
|
for (var i = 0; i < numAheadToPeek; i++) {
|
|
|
|
var nextIndex;
|
|
|
|
if (this.opts.allowLooping) {
|
|
|
|
nextIndex = (this.currentIndex + i) % this.matchedList.length;
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
2015-12-21 17:16:49 +00:00
|
|
|
else {
|
|
|
|
nextIndex = this.currentIndex + i;
|
|
|
|
if (nextIndex === this.matchedList.length) {
|
|
|
|
break;
|
|
|
|
}
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
2015-12-21 16:35:39 +00:00
|
|
|
peekList.push(this.matchedList[nextIndex]);
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
2015-12-21 17:16:49 +00:00
|
|
|
// console.log("Peek list(%s): %s", numAheadToPeek, JSON.stringify(peekList));
|
2015-12-21 16:35:39 +00:00
|
|
|
return peekList;
|
2015-12-21 14:34:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 10:59:10 +00:00
|
|
|
/**
|
|
|
|
* @param {DOMEvent} e
|
|
|
|
* @return {Boolean} True if the tab complete state changed as a result of
|
|
|
|
* this event.
|
|
|
|
*/
|
2015-12-21 10:38:37 +00:00
|
|
|
onKeyDown(ev) {
|
|
|
|
if (ev.keyCode !== KEY_TAB) {
|
2015-12-21 14:34:25 +00:00
|
|
|
if (ev.keyCode !== KEY_SHIFT && this.completing) {
|
2015-12-21 10:38:37 +00:00
|
|
|
// they're resuming typing; reset tab complete state vars.
|
2015-12-21 17:16:49 +00:00
|
|
|
this.stopTabCompleting();
|
2015-12-21 10:59:10 +00:00
|
|
|
return true;
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
2015-12-21 17:58:36 +00:00
|
|
|
|
|
|
|
if (this.opts.autoEnterTabComplete) {
|
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
- This is passive so we shouldn't clobber the partial word that
|
|
|
|
the user may have entered. This requires more logic to handle
|
|
|
|
that vs a normal TAB which does clobber.
|
|
|
|
- The first invocation of this timer will give no results because
|
|
|
|
we horribly set the enumeration onKeyDown in MessageComposer, which
|
|
|
|
was never actually hit. We should hook into RoomView's RoomState.members
|
|
|
|
event and set the list there.
|
|
|
|
|
|
|
|
|
|
|
|
clearTimeout(this.enterTabCompleteTimerId);
|
|
|
|
this.enterTabCompleteTimerId = setTimeout(() => {
|
|
|
|
if (!this.completing) {
|
|
|
|
// inject a fake tab event so we use the same code paths
|
|
|
|
this.onKeyDown({
|
|
|
|
keyCode: KEY_TAB,
|
|
|
|
preventDefault: function(){} // NOP
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, DELAY_TIME_MS); */
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:38:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-21 10:59:10 +00:00
|
|
|
|
|
|
|
if (!this.textArea) {
|
|
|
|
console.error("onKeyDown called before a <textarea> was set!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:38:37 +00:00
|
|
|
// init struct if necessary
|
2015-12-21 14:34:25 +00:00
|
|
|
if (!this.completing) {
|
2015-12-21 17:58:36 +00:00
|
|
|
this.startTabCompleting();
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.shiftKey) {
|
2015-12-21 16:35:39 +00:00
|
|
|
this.nextMatchedEntry(-1);
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-12-21 16:35:39 +00:00
|
|
|
this.nextMatchedEntry(1);
|
2015-12-21 10:38:37 +00:00
|
|
|
}
|
|
|
|
// prevent the default TAB operation (typically focus shifting)
|
|
|
|
ev.preventDefault();
|
2015-12-21 16:35:39 +00:00
|
|
|
this._notifyStateChange();
|
2015-12-21 10:38:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:35:39 +00:00
|
|
|
/**
|
|
|
|
* Set the textarea to the next value in the matched list.
|
|
|
|
* @param {Number} offset Offset to apply *before* setting the next value.
|
|
|
|
*/
|
|
|
|
nextMatchedEntry(offset) {
|
|
|
|
if (this.matchedList.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// work out the new index, wrapping if necessary.
|
|
|
|
this.currentIndex += offset;
|
|
|
|
if (this.currentIndex >= this.matchedList.length) {
|
|
|
|
this.currentIndex = 0;
|
|
|
|
}
|
|
|
|
else if (this.currentIndex < 0) {
|
|
|
|
this.currentIndex = this.matchedList.length - 1;
|
|
|
|
}
|
2015-12-21 17:16:49 +00:00
|
|
|
var looped = this.currentIndex === 0; // catch forward and backward looping
|
2015-12-21 16:35:39 +00:00
|
|
|
|
|
|
|
var suffix = "";
|
|
|
|
|
|
|
|
if (this.currentIndex !== 0) { // don't suffix the original text!
|
|
|
|
suffix = this.isFirstWord ? this.opts.startingWordSuffix : this.opts.wordSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set textarea to this new value
|
|
|
|
this.textArea.value = this._replaceWith(
|
|
|
|
this.matchedList[this.currentIndex].text + suffix
|
|
|
|
);
|
|
|
|
|
|
|
|
// visual display to the user that we looped - TODO: This should be configurable
|
|
|
|
if (looped) {
|
|
|
|
this.textArea.style["background-color"] = "#faa";
|
|
|
|
setTimeout(() => { // yay for lexical 'this'!
|
|
|
|
this.textArea.style["background-color"] = "";
|
|
|
|
}, 150);
|
2015-12-21 17:16:49 +00:00
|
|
|
|
|
|
|
if (!this.opts.allowLooping) {
|
|
|
|
this.stopTabCompleting();
|
|
|
|
}
|
2015-12-21 16:35:39 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.textArea.style["background-color"] = ""; // cancel blinks TODO: required?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_replaceWith(newVal) {
|
|
|
|
return this.originalText.replace(MATCH_REGEX, newVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
_calculateCompletions() {
|
|
|
|
this.originalText = this.textArea.value; // cache starting text
|
|
|
|
|
|
|
|
// grab the partial word from the text which we'll be tab-completing
|
|
|
|
var res = MATCH_REGEX.exec(this.originalText);
|
|
|
|
if (!res) {
|
|
|
|
this.matchedList = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var [ ,group] = res; // ES6 destructuring; ignore first element
|
|
|
|
this.isFirstWord = group.length === this.originalText.length;
|
|
|
|
|
|
|
|
this.matchedList = [
|
|
|
|
new TabComplete.Entry(group) // first entry is always the original partial
|
|
|
|
];
|
|
|
|
|
|
|
|
// find matching entries in the set of entries given to us
|
|
|
|
this.list.forEach((entry) => {
|
|
|
|
if (entry.text.toLowerCase().indexOf(group.toLowerCase()) === 0) {
|
|
|
|
this.matchedList.push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// console.log("_calculateCompletions => %s", JSON.stringify(this.matchedList));
|
|
|
|
}
|
|
|
|
|
|
|
|
_notifyStateChange() {
|
|
|
|
if (this.opts.onStateChange) {
|
|
|
|
this.opts.onStateChange(this.completing);
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 10:38:37 +00:00
|
|
|
};
|
|
|
|
|
2015-12-21 17:28:04 +00:00
|
|
|
TabComplete.Entry = function(text, imgUrl) {
|
2015-12-21 14:34:25 +00:00
|
|
|
this.text = text;
|
2015-12-21 17:28:04 +00:00
|
|
|
this.imgUrl = imgUrl;
|
2015-12-21 14:34:25 +00:00
|
|
|
};
|
|
|
|
|
2015-12-21 10:38:37 +00:00
|
|
|
|
|
|
|
module.exports = TabComplete;
|