2015-12-21 18:38:37 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-12-21 18:38:37 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-07-15 23:10:27 +08:00
|
|
|
|
|
|
|
import { Entry, MemberEntry, CommandEntry } from './TabCompleteEntries';
|
|
|
|
import SlashCommands from './SlashCommands';
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2015-12-21 18:38:37 +08:00
|
|
|
|
2015-12-23 00:49:58 +08:00
|
|
|
const DELAY_TIME_MS = 1000;
|
2015-12-21 18:38:37 +08:00
|
|
|
const KEY_TAB = 9;
|
|
|
|
const KEY_SHIFT = 16;
|
2015-12-22 23:13:11 +08:00
|
|
|
const KEY_WINDOWS = 91;
|
2015-12-21 18:38:37 +08:00
|
|
|
|
2015-12-23 01:38:24 +08:00
|
|
|
// NB: DO NOT USE \b its "words" are roman alphabet only!
|
|
|
|
//
|
|
|
|
// Capturing group containing the start
|
|
|
|
// of line or a whitespace char
|
2016-03-17 07:11:07 +08:00
|
|
|
// \_______________ __________Capturing group of 0 or more non-whitespace chars
|
2015-12-23 01:38:24 +08:00
|
|
|
// _|__ _|_ followed by the end of line
|
|
|
|
// / \/ \
|
2016-03-17 07:11:07 +08:00
|
|
|
const MATCH_REGEX = /(^|\s)(\S*)$/;
|
2015-12-22 00:35:39 +08:00
|
|
|
|
2015-12-21 18:38:37 +08:00
|
|
|
class TabComplete {
|
|
|
|
|
|
|
|
constructor(opts) {
|
2015-12-22 01:16:49 +08:00
|
|
|
opts.allowLooping = opts.allowLooping || false;
|
2015-12-22 01:58:36 +08:00
|
|
|
opts.autoEnterTabComplete = opts.autoEnterTabComplete || false;
|
2015-12-22 19:14:36 +08:00
|
|
|
opts.onClickCompletes = opts.onClickCompletes || false;
|
2015-12-21 18:38:37 +08:00
|
|
|
this.opts = opts;
|
2015-12-21 22:34:25 +08:00
|
|
|
this.completing = false;
|
2015-12-22 00:35:39 +08: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-22 01:58:36 +08:00
|
|
|
this.enterTabCompleteTimerId = null;
|
2015-12-23 00:49:58 +08:00
|
|
|
this.inPassiveMode = false;
|
2016-07-16 00:15:51 +08:00
|
|
|
|
|
|
|
// Map tracking ordering of the room members.
|
|
|
|
// userId: integer, highest comes first.
|
2016-07-15 23:10:27 +08:00
|
|
|
this.memberTabOrder = {};
|
2016-07-16 00:15:51 +08:00
|
|
|
|
|
|
|
// monotonically increasing counter used for tracking ordering of members
|
2016-07-15 23:10:27 +08:00
|
|
|
this.memberOrderSeq = 0;
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-15 23:10:27 +08:00
|
|
|
* Call this when a a UI element representing a tab complete entry has been clicked
|
|
|
|
* @param {entry} The entry that was clicked
|
2015-12-21 18:38:37 +08:00
|
|
|
*/
|
2016-07-15 23:10:27 +08:00
|
|
|
onEntryClick(entry) {
|
2015-12-22 19:14:36 +08:00
|
|
|
if (this.opts.onClickCompletes) {
|
2016-07-15 23:10:27 +08:00
|
|
|
this.completeTo(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadEntries(room) {
|
|
|
|
this._makeEntries(room);
|
|
|
|
this._initSorting(room);
|
|
|
|
this._sortEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
onMemberSpoke(member) {
|
|
|
|
if (this.memberTabOrder[member.userId] === undefined) {
|
|
|
|
this.list.push(new MemberEntry(member));
|
2015-12-22 19:14:36 +08:00
|
|
|
}
|
2016-07-15 23:10:27 +08:00
|
|
|
this.memberTabOrder[member.userId] = this.memberOrderSeq++;
|
|
|
|
this._sortEntries();
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 00:35:39 +08:00
|
|
|
/**
|
|
|
|
* @param {DOMElement}
|
|
|
|
*/
|
2015-12-21 18:38:37 +08:00
|
|
|
setTextArea(textArea) {
|
|
|
|
this.textArea = textArea;
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:35:39 +08:00
|
|
|
/**
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2015-12-21 18:59:10 +08:00
|
|
|
isTabCompleting() {
|
2015-12-23 00:49:58 +08:00
|
|
|
// actually have things to tab over
|
|
|
|
return this.completing && this.matchedList.length > 1;
|
2015-12-21 18:59:10 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 01:16:49 +08:00
|
|
|
stopTabCompleting() {
|
|
|
|
this.completing = false;
|
|
|
|
this.currentIndex = 0;
|
|
|
|
this._notifyStateChange();
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:34:40 +08:00
|
|
|
startTabCompleting(passive) {
|
2016-03-18 00:46:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// ES6 destructuring; ignore first element (the complete match)
|
|
|
|
var [ , boundaryGroup, partialGroup] = res;
|
|
|
|
|
|
|
|
if (partialGroup.length === 0 && passive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isFirstWord = partialGroup.length === this.originalText.length;
|
|
|
|
|
2015-12-22 01:58:36 +08:00
|
|
|
this.completing = true;
|
|
|
|
this.currentIndex = 0;
|
2016-03-18 00:46:54 +08:00
|
|
|
|
|
|
|
this.matchedList = [
|
|
|
|
new Entry(partialGroup) // 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(partialGroup.toLowerCase()) === 0) {
|
|
|
|
this.matchedList.push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// console.log("calculated completions => %s", JSON.stringify(this.matchedList));
|
2015-12-22 01:58:36 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 19:14:36 +08:00
|
|
|
/**
|
|
|
|
* Do an auto-complete with the given word. This terminates the tab-complete.
|
2016-01-14 01:46:36 +08:00
|
|
|
* @param {Entry} entry The tab-complete entry to complete to.
|
2015-12-22 19:14:36 +08:00
|
|
|
*/
|
2016-01-14 01:46:36 +08:00
|
|
|
completeTo(entry) {
|
2016-01-14 19:39:24 +08:00
|
|
|
this.textArea.value = this._replaceWith(
|
2016-01-14 22:39:58 +08:00
|
|
|
entry.getFillText(), true, entry.getSuffix(this.isFirstWord)
|
2016-01-14 19:39:24 +08:00
|
|
|
);
|
2015-12-22 19:14:36 +08:00
|
|
|
this.stopTabCompleting();
|
|
|
|
// keep focus on the text area
|
|
|
|
this.textArea.focus();
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:34:25 +08:00
|
|
|
/**
|
2015-12-22 00:35:39 +08:00
|
|
|
* @param {Number} numAheadToPeek Return *up to* this many elements.
|
2015-12-22 18:00:30 +08:00
|
|
|
* @return {Entry[]}
|
2015-12-21 22:34:25 +08:00
|
|
|
*/
|
|
|
|
peek(numAheadToPeek) {
|
2015-12-22 00:35:39 +08:00
|
|
|
if (this.matchedList.length === 0) {
|
|
|
|
return [];
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
2015-12-22 01:16:49 +08:00
|
|
|
var peekList = [];
|
2015-12-21 18:38:37 +08:00
|
|
|
|
2015-12-22 00:35:39 +08:00
|
|
|
// return the current match item and then one with an index higher, and
|
2015-12-22 01:16:49 +08: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 18:38:37 +08:00
|
|
|
}
|
2015-12-22 01:16:49 +08:00
|
|
|
else {
|
|
|
|
nextIndex = this.currentIndex + i;
|
|
|
|
if (nextIndex === this.matchedList.length) {
|
|
|
|
break;
|
|
|
|
}
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
2015-12-22 00:35:39 +08:00
|
|
|
peekList.push(this.matchedList[nextIndex]);
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
2015-12-22 01:16:49 +08:00
|
|
|
// console.log("Peek list(%s): %s", numAheadToPeek, JSON.stringify(peekList));
|
2015-12-22 00:35:39 +08:00
|
|
|
return peekList;
|
2015-12-21 22:34:25 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 21:48:44 +08:00
|
|
|
handleTabPress(passive, shiftKey) {
|
|
|
|
var wasInPassiveMode = this.inPassiveMode && !passive;
|
|
|
|
this.inPassiveMode = passive;
|
|
|
|
|
|
|
|
if (!this.completing) {
|
2016-03-17 23:34:40 +08:00
|
|
|
this.startTabCompleting(passive);
|
2015-12-23 21:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (shiftKey) {
|
|
|
|
this.nextMatchedEntry(-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if we were in passive mode we got out of sync by incrementing the
|
|
|
|
// index to show the peek view but not set the text area. Therefore,
|
|
|
|
// we want to set the *current* index rather than the *next* index.
|
|
|
|
this.nextMatchedEntry(wasInPassiveMode ? 0 : 1);
|
|
|
|
}
|
|
|
|
this._notifyStateChange();
|
|
|
|
}
|
|
|
|
|
2015-12-21 18:59:10 +08:00
|
|
|
/**
|
|
|
|
* @param {DOMEvent} e
|
|
|
|
*/
|
2015-12-21 18:38:37 +08:00
|
|
|
onKeyDown(ev) {
|
2015-12-23 21:48:44 +08:00
|
|
|
if (!this.textArea) {
|
|
|
|
console.error("onKeyDown called before a <textarea> was set!");
|
|
|
|
return;
|
|
|
|
}
|
2015-12-23 00:49:58 +08:00
|
|
|
|
2015-12-21 18:38:37 +08:00
|
|
|
if (ev.keyCode !== KEY_TAB) {
|
2015-12-23 21:33:44 +08:00
|
|
|
// pressing any key (except shift, windows, cmd (OSX) and ctrl/alt combinations)
|
|
|
|
// aborts the current tab completion
|
2015-12-22 23:13:11 +08:00
|
|
|
if (this.completing && ev.keyCode !== KEY_SHIFT &&
|
|
|
|
!ev.metaKey && !ev.ctrlKey && !ev.altKey && ev.keyCode !== KEY_WINDOWS) {
|
2015-12-21 18:38:37 +08:00
|
|
|
// they're resuming typing; reset tab complete state vars.
|
2015-12-22 01:16:49 +08:00
|
|
|
this.stopTabCompleting();
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
2015-12-22 01:58:36 +08:00
|
|
|
|
2015-12-24 19:56:50 +08:00
|
|
|
|
|
|
|
// explicitly pressing any key except tab removes passive mode. Tab doesn't remove
|
|
|
|
// passive mode because handleTabPress needs to know when passive mode is toggling
|
|
|
|
// off so it can resync the textarea/peek list. If tab did remove passive mode then
|
|
|
|
// handleTabPress would never be able to tell when passive mode toggled off.
|
|
|
|
this.inPassiveMode = false;
|
|
|
|
|
2015-12-23 21:33:44 +08:00
|
|
|
// pressing any key at all (except tab) restarts the automatic tab-complete timer
|
2015-12-22 01:58:36 +08:00
|
|
|
if (this.opts.autoEnterTabComplete) {
|
2016-11-30 18:49:40 +08:00
|
|
|
const cachedText = ev.target.value;
|
2015-12-22 01:58:36 +08:00
|
|
|
clearTimeout(this.enterTabCompleteTimerId);
|
|
|
|
this.enterTabCompleteTimerId = setTimeout(() => {
|
2016-11-30 18:49:40 +08:00
|
|
|
if (this.completing) {
|
|
|
|
// If you highlight text and CTRL+X it, tab-completing will not be reset.
|
|
|
|
// This check makes sure that if something like a cut operation has been
|
|
|
|
// done, that we correctly refresh the tab-complete list. Normal backspace
|
|
|
|
// operations get caught by the stopTabCompleting() section above, but
|
|
|
|
// because the CTRL key is held, this does not execute for CTRL+X.
|
|
|
|
if (cachedText !== this.textArea.value) {
|
|
|
|
this.stopTabCompleting();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:58:36 +08:00
|
|
|
if (!this.completing) {
|
2015-12-23 21:48:44 +08:00
|
|
|
this.handleTabPress(true, false);
|
2015-12-22 01:58:36 +08:00
|
|
|
}
|
2015-12-23 00:49:58 +08:00
|
|
|
}, DELAY_TIME_MS);
|
2015-12-22 01:58:36 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 18:35:54 +08:00
|
|
|
return;
|
2015-12-21 18:38:37 +08:00
|
|
|
}
|
2015-12-21 18:59:10 +08:00
|
|
|
|
2016-04-10 21:27:30 +08:00
|
|
|
// ctrl-tab/alt-tab etc shouldn't trigger a complete
|
|
|
|
if (ev.ctrlKey || ev.metaKey || ev.altKey) return;
|
|
|
|
|
2015-12-23 21:33:44 +08:00
|
|
|
// tab key has been pressed at this point
|
2015-12-23 21:48:44 +08:00
|
|
|
this.handleTabPress(false, ev.shiftKey)
|
2015-12-23 00:49:58 +08:00
|
|
|
|
2015-12-21 18:38:37 +08:00
|
|
|
// prevent the default TAB operation (typically focus shifting)
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:35:39 +08: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-23 17:34:34 +08:00
|
|
|
var isTransitioningToOriginalText = (
|
|
|
|
// impossible to transition if they've never hit tab
|
2015-12-23 00:49:58 +08:00
|
|
|
!this.inPassiveMode && this.currentIndex === 0
|
2015-12-22 00:35:39 +08:00
|
|
|
);
|
|
|
|
|
2015-12-23 00:49:58 +08:00
|
|
|
if (!this.inPassiveMode) {
|
|
|
|
// set textarea to this new value
|
|
|
|
this.textArea.value = this._replaceWith(
|
2016-01-14 22:39:58 +08:00
|
|
|
this.matchedList[this.currentIndex].getFillText(),
|
2016-01-14 01:46:36 +08:00
|
|
|
this.currentIndex !== 0, // don't suffix the original text!
|
2016-01-14 19:39:24 +08:00
|
|
|
this.matchedList[this.currentIndex].getSuffix(this.isFirstWord)
|
2015-12-23 00:49:58 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:35:39 +08:00
|
|
|
// visual display to the user that we looped - TODO: This should be configurable
|
2015-12-23 17:34:34 +08:00
|
|
|
if (isTransitioningToOriginalText) {
|
2015-12-22 00:35:39 +08:00
|
|
|
this.textArea.style["background-color"] = "#faa";
|
|
|
|
setTimeout(() => { // yay for lexical 'this'!
|
|
|
|
this.textArea.style["background-color"] = "";
|
|
|
|
}, 150);
|
2015-12-22 01:16:49 +08:00
|
|
|
|
|
|
|
if (!this.opts.allowLooping) {
|
|
|
|
this.stopTabCompleting();
|
|
|
|
}
|
2015-12-22 00:35:39 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.textArea.style["background-color"] = ""; // cancel blinks TODO: required?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:39:24 +08:00
|
|
|
_replaceWith(newVal, includeSuffix, suffix) {
|
2015-12-23 01:38:24 +08:00
|
|
|
// The regex to replace the input matches a character of whitespace AND
|
|
|
|
// the partial word. If we just use string.replace() with the regex it will
|
|
|
|
// replace the partial word AND the character of whitespace. We want to
|
|
|
|
// preserve whatever that character is (\n, \t, etc) so find out what it is now.
|
|
|
|
var boundaryChar;
|
|
|
|
var res = MATCH_REGEX.exec(this.originalText);
|
|
|
|
if (res) {
|
|
|
|
boundaryChar = res[1]; // the first captured group
|
|
|
|
}
|
|
|
|
if (boundaryChar === undefined) {
|
|
|
|
console.warn("Failed to find boundary char on text: '%s'", this.originalText);
|
|
|
|
boundaryChar = "";
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:39:24 +08:00
|
|
|
suffix = suffix || "";
|
|
|
|
if (!includeSuffix) {
|
|
|
|
suffix = "";
|
2016-01-14 01:46:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var replacementText = boundaryChar + newVal + suffix;
|
2015-12-23 17:34:34 +08:00
|
|
|
return this.originalText.replace(MATCH_REGEX, function() {
|
|
|
|
return replacementText; // function form to avoid `$` special-casing
|
|
|
|
});
|
2015-12-22 00:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_notifyStateChange() {
|
|
|
|
if (this.opts.onStateChange) {
|
|
|
|
this.opts.onStateChange(this.completing);
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 23:10:27 +08:00
|
|
|
|
|
|
|
_sortEntries() {
|
|
|
|
// largest comes first
|
|
|
|
const KIND_ORDER = {
|
|
|
|
command: 1,
|
|
|
|
member: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.list.sort((a, b) => {
|
|
|
|
const kindOrderDifference = KIND_ORDER[b.kind] - KIND_ORDER[a.kind];
|
|
|
|
if (kindOrderDifference != 0) {
|
|
|
|
return kindOrderDifference;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.kind == 'member') {
|
2016-07-21 23:25:51 +08:00
|
|
|
let orderA = this.memberTabOrder[a.member.userId];
|
|
|
|
let orderB = this.memberTabOrder[b.member.userId];
|
|
|
|
if (orderA === undefined) orderA = -1;
|
|
|
|
if (orderB === undefined) orderB = -1;
|
|
|
|
|
|
|
|
return orderB - orderA;
|
2016-07-15 23:10:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// anything else we have no ordering for
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_makeEntries(room) {
|
|
|
|
const myUserId = MatrixClientPeg.get().credentials.userId;
|
|
|
|
|
|
|
|
const members = room.getJoinedMembers().filter(function(member) {
|
|
|
|
if (member.userId !== myUserId) return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.list = MemberEntry.fromMemberList(members).concat(
|
|
|
|
CommandEntry.fromCommands(SlashCommands.getCommandList())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_initSorting(room) {
|
|
|
|
this.memberTabOrder = {};
|
|
|
|
this.memberOrderSeq = 0;
|
|
|
|
|
|
|
|
for (const ev of room.getLiveTimeline().getEvents()) {
|
|
|
|
this.memberTabOrder[ev.getSender()] = this.memberOrderSeq++;
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 18:38:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TabComplete;
|