2017-08-24 14:44:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const fuzzy = require("fuzzy");
|
2017-12-05 19:05:53 +00:00
|
|
|
const Mousetrap = require("mousetrap");
|
2017-12-19 04:15:08 +00:00
|
|
|
const {Textcomplete, Textarea} = require("textcomplete");
|
2017-08-24 14:44:40 +00:00
|
|
|
const emojiMap = require("./libs/simplemap.json");
|
2017-12-05 16:05:14 +00:00
|
|
|
const options = require("./options");
|
2017-08-24 14:44:40 +00:00
|
|
|
const constants = require("./constants");
|
|
|
|
|
2017-12-05 17:44:40 +00:00
|
|
|
const input = $("#input");
|
|
|
|
let textcomplete;
|
2017-12-11 19:01:15 +00:00
|
|
|
let enabled = false;
|
2017-12-05 17:44:40 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
enable: enableAutocomplete,
|
2018-03-05 00:59:16 +00:00
|
|
|
disable() {
|
2017-12-11 19:01:15 +00:00
|
|
|
if (enabled) {
|
|
|
|
input.off("input.tabcomplete");
|
|
|
|
Mousetrap(input.get(0)).off("tab", "keydown");
|
2018-01-31 19:06:29 +00:00
|
|
|
textcomplete.destroy();
|
2017-12-11 19:01:15 +00:00
|
|
|
enabled = false;
|
2018-01-31 19:06:29 +00:00
|
|
|
}
|
2017-12-05 19:05:53 +00:00
|
|
|
},
|
2017-12-05 17:44:40 +00:00
|
|
|
};
|
|
|
|
|
2018-03-23 09:20:53 +00:00
|
|
|
$("#form").on("submit", () => {
|
|
|
|
if (enabled) {
|
|
|
|
textcomplete.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
const chat = $("#chat");
|
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
const emojiSearchTerms = Object.keys(emojiMap);
|
|
|
|
const emojiStrategy = {
|
|
|
|
id: "emoji",
|
|
|
|
match: /\B:([-+\w:?]{2,}):?$/,
|
|
|
|
search(term, callback) {
|
|
|
|
// Trim colon from the matched term,
|
|
|
|
// as we are unable to get a clean string from match regex
|
|
|
|
term = term.replace(/:$/, ""),
|
|
|
|
callback(fuzzyGrep(term, emojiSearchTerms));
|
|
|
|
},
|
|
|
|
template([string, original]) {
|
|
|
|
return `<span class="emoji">${emojiMap[original]}</span> ${string}`;
|
|
|
|
},
|
|
|
|
replace([, original]) {
|
|
|
|
return emojiMap[original];
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const nicksStrategy = {
|
|
|
|
id: "nicks",
|
|
|
|
match: /\B(@([a-zA-Z_[\]\\^{}|`@][a-zA-Z0-9_[\]\\^{}|`-]*)?)$/,
|
|
|
|
search(term, callback) {
|
|
|
|
term = term.slice(1);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
if (term[0] === "@") {
|
|
|
|
callback(completeNicks(term.slice(1), true)
|
|
|
|
.map((val) => ["@" + val[0], "@" + val[1]]));
|
|
|
|
} else {
|
|
|
|
callback(completeNicks(term, true));
|
|
|
|
}
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
template([string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
2017-12-05 16:05:14 +00:00
|
|
|
replace([, original], position = 1) {
|
|
|
|
// If no postfix specified, return autocompleted nick as-is
|
2017-12-11 19:01:15 +00:00
|
|
|
if (!options.settings.nickPostfix) {
|
2017-12-05 16:05:14 +00:00
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is whitespace in the input already, append space to nick
|
|
|
|
if (position > 0 && /\s/.test(input.val())) {
|
|
|
|
return original + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nick is first in the input, append specified postfix
|
2017-12-11 19:01:15 +00:00
|
|
|
return original + options.settings.nickPostfix;
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const chanStrategy = {
|
|
|
|
id: "chans",
|
|
|
|
match: /\B((#|\+|&|![A-Z0-9]{5})([^\x00\x0A\x0D\x20\x2C\x3A]+(:[^\x00\x0A\x0D\x20\x2C\x3A]*)?)?)$/,
|
|
|
|
search(term, callback, match) {
|
|
|
|
callback(completeChans(match[0]));
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
template([string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
|
|
|
replace([, original]) {
|
|
|
|
return original;
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const commandStrategy = {
|
|
|
|
id: "commands",
|
|
|
|
match: /^\/(\w*)$/,
|
|
|
|
search(term, callback) {
|
|
|
|
callback(completeCommands("/" + term));
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
template([string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
|
|
|
replace([, original]) {
|
|
|
|
return original;
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const foregroundColorStrategy = {
|
|
|
|
id: "foreground-colors",
|
|
|
|
match: /\x03(\d{0,2}|[A-Za-z ]{0,10})$/,
|
|
|
|
search(term, callback) {
|
|
|
|
term = term.toLowerCase();
|
|
|
|
|
|
|
|
const matchingColorCodes = constants.colorCodeMap
|
|
|
|
.filter((i) => fuzzy.test(term, i[0]) || fuzzy.test(term, i[1]))
|
|
|
|
.map((i) => {
|
|
|
|
if (fuzzy.test(term, i[1])) {
|
|
|
|
return [i[0], fuzzy.match(term, i[1], {
|
|
|
|
pre: "<b>",
|
2017-11-15 06:35:15 +00:00
|
|
|
post: "</b>",
|
2017-08-24 14:44:40 +00:00
|
|
|
}).rendered];
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
return i;
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
|
|
|
template(value) {
|
|
|
|
return `<span class="irc-fg${parseInt(value[0], 10)}">${value[1]}</span>`;
|
|
|
|
},
|
|
|
|
replace(value) {
|
|
|
|
return "\x03" + value[0];
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const backgroundColorStrategy = {
|
|
|
|
id: "background-colors",
|
|
|
|
match: /\x03(\d{2}),(\d{0,2}|[A-Za-z ]{0,10})$/,
|
|
|
|
search(term, callback, match) {
|
|
|
|
term = term.toLowerCase();
|
|
|
|
const matchingColorCodes = constants.colorCodeMap
|
|
|
|
.filter((i) => fuzzy.test(term, i[0]) || fuzzy.test(term, i[1]))
|
|
|
|
.map((pair) => {
|
|
|
|
if (fuzzy.test(term, pair[1])) {
|
|
|
|
return [pair[0], fuzzy.match(term, pair[1], {
|
|
|
|
pre: "<b>",
|
2017-11-15 06:35:15 +00:00
|
|
|
post: "</b>",
|
2017-08-24 14:44:40 +00:00
|
|
|
}).rendered];
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
return pair;
|
|
|
|
})
|
|
|
|
.map((pair) => pair.concat(match[1])); // Needed to pass fg color to `template`...
|
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
|
|
|
template(value) {
|
|
|
|
return `<span class="irc-fg${parseInt(value[2], 10)} irc-bg irc-bg${parseInt(value[0], 10)}">${value[1]}</span>`;
|
|
|
|
},
|
|
|
|
replace(value) {
|
|
|
|
return "\x03$1," + value[0];
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 2,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2017-12-05 19:05:53 +00:00
|
|
|
function enableAutocomplete() {
|
2017-12-11 19:01:15 +00:00
|
|
|
enabled = true;
|
2017-12-05 19:05:53 +00:00
|
|
|
let tabCount = 0;
|
2017-12-05 16:05:14 +00:00
|
|
|
let lastMatch = "";
|
2017-12-05 19:05:53 +00:00
|
|
|
let currentMatches = [];
|
|
|
|
|
|
|
|
input.on("input.tabcomplete", () => {
|
|
|
|
tabCount = 0;
|
|
|
|
currentMatches = [];
|
2017-12-05 16:05:14 +00:00
|
|
|
lastMatch = "";
|
2017-08-24 14:44:40 +00:00
|
|
|
});
|
|
|
|
|
2017-12-05 19:05:53 +00:00
|
|
|
Mousetrap(input.get(0)).bind("tab", (e) => {
|
|
|
|
if (input.data("autocompleting")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const text = input.val();
|
|
|
|
|
|
|
|
if (input.get(0).selectionStart !== text.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tabCount === 0) {
|
2017-12-05 16:05:14 +00:00
|
|
|
lastMatch = text.split(/\s/).pop();
|
2017-12-05 19:05:53 +00:00
|
|
|
|
2017-12-05 16:05:14 +00:00
|
|
|
if (lastMatch.length === 0) {
|
2017-12-05 19:05:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:05:14 +00:00
|
|
|
currentMatches = completeNicks(lastMatch, false);
|
2017-12-05 19:05:53 +00:00
|
|
|
|
|
|
|
if (currentMatches.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:05:14 +00:00
|
|
|
const position = input.get(0).selectionStart - lastMatch.length;
|
|
|
|
const newMatch = nicksStrategy.replace([0, currentMatches[tabCount % currentMatches.length]], position);
|
|
|
|
|
|
|
|
input.val(text.substr(0, position) + newMatch);
|
2017-12-05 19:05:53 +00:00
|
|
|
|
2017-12-05 16:05:14 +00:00
|
|
|
lastMatch = newMatch;
|
2017-12-05 19:05:53 +00:00
|
|
|
tabCount++;
|
|
|
|
}, "keydown");
|
|
|
|
|
|
|
|
const editor = new Textarea(input.get(0));
|
2017-12-05 17:44:40 +00:00
|
|
|
textcomplete = new Textcomplete(editor, {
|
|
|
|
dropdown: {
|
|
|
|
className: "textcomplete-menu",
|
|
|
|
placement: "top",
|
2017-11-15 06:35:15 +00:00
|
|
|
},
|
2017-08-24 14:44:40 +00:00
|
|
|
});
|
2017-12-05 17:44:40 +00:00
|
|
|
|
|
|
|
textcomplete.register([
|
|
|
|
emojiStrategy,
|
|
|
|
nicksStrategy,
|
|
|
|
chanStrategy,
|
|
|
|
commandStrategy,
|
|
|
|
foregroundColorStrategy,
|
|
|
|
backgroundColorStrategy,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Activate the first item by default
|
|
|
|
// https://github.com/yuku-t/textcomplete/issues/93
|
|
|
|
textcomplete.on("rendered", () => {
|
|
|
|
if (textcomplete.dropdown.items.length > 0) {
|
|
|
|
textcomplete.dropdown.items[0].activate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
textcomplete.on("show", () => {
|
|
|
|
input.data("autocompleting", true);
|
|
|
|
});
|
|
|
|
|
|
|
|
textcomplete.on("hidden", () => {
|
|
|
|
input.data("autocompleting", false);
|
|
|
|
});
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fuzzyGrep(term, array) {
|
|
|
|
const results = fuzzy.filter(
|
|
|
|
term,
|
|
|
|
array,
|
|
|
|
{
|
|
|
|
pre: "<b>",
|
2017-11-15 06:35:15 +00:00
|
|
|
post: "</b>",
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return results.map((el) => [el.string, el.original]);
|
|
|
|
}
|
|
|
|
|
2017-10-29 03:03:27 +00:00
|
|
|
function rawNicks() {
|
|
|
|
const chan = chat.find(".active");
|
2018-03-04 20:03:11 +00:00
|
|
|
const users = chan.find(".userlist");
|
2017-10-29 03:03:27 +00:00
|
|
|
|
|
|
|
// If this channel has a list of nicks, just return it
|
|
|
|
if (users.length > 0) {
|
|
|
|
return users.data("nicks");
|
|
|
|
}
|
|
|
|
|
|
|
|
const me = $("#nick-value").text();
|
|
|
|
const otherUser = chan.attr("aria-label");
|
2017-08-24 14:44:40 +00:00
|
|
|
|
2017-10-29 03:03:27 +00:00
|
|
|
// If this is a query, add their name to autocomplete
|
|
|
|
if (me !== otherUser && chan.data("type") === "query") {
|
|
|
|
return [otherUser, me];
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 03:03:27 +00:00
|
|
|
// Return our own name by default for anything that isn't a channel or query
|
|
|
|
return [me];
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeNicks(word, isFuzzy) {
|
|
|
|
const users = rawNicks();
|
|
|
|
word = word.toLowerCase();
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
if (isFuzzy) {
|
2017-10-29 03:03:27 +00:00
|
|
|
return fuzzyGrep(word, users);
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
return $.grep(
|
2017-10-29 03:03:27 +00:00
|
|
|
users,
|
2017-08-24 14:44:40 +00:00
|
|
|
(w) => !w.toLowerCase().indexOf(word)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeCommands(word) {
|
|
|
|
const words = constants.commands.slice();
|
|
|
|
|
|
|
|
return fuzzyGrep(word, words);
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeChans(word) {
|
|
|
|
const words = [];
|
|
|
|
|
2018-01-20 11:55:36 +00:00
|
|
|
sidebar.find(".chan.active")
|
|
|
|
.parent()
|
|
|
|
.find(".chan")
|
2017-08-24 14:44:40 +00:00
|
|
|
.each(function() {
|
|
|
|
const self = $(this);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
if (!self.hasClass("lobby")) {
|
2017-12-28 12:15:28 +00:00
|
|
|
words.push(self.attr("aria-label"));
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return fuzzyGrep(word, words);
|
|
|
|
}
|