hardlounge/client/js/autocompletion.js

331 lines
7.2 KiB
JavaScript
Raw Normal View History

"use strict";
2019-12-17 22:10:50 +00:00
const constants = require("./constants");
2019-11-16 17:24:03 +00:00
import Mousetrap from "mousetrap";
import {Textcomplete, Textarea} from "textcomplete";
import fuzzy from "fuzzy";
import emojiMap from "./helpers/simplemap.json";
import store from "./store";
2019-11-15 18:53:38 +00:00
export default enableAutocomplete;
2017-12-05 17:44:40 +00:00
const emojiSearchTerms = Object.keys(emojiMap);
const emojiStrategy = {
id: "emoji",
match: /(^|\s):([-+\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 "$1" + emojiMap[original];
},
index: 2,
};
const nicksStrategy = {
id: "nicks",
match: /(^|\s)(@([a-zA-Z_[\]\\^{}|`@][a-zA-Z0-9_[\]\\^{}|`-]*)?)$/,
search(term, callback) {
term = term.slice(1);
if (term[0] === "@") {
2019-07-17 09:33:59 +00:00
callback(completeNicks(term.slice(1), true).map((val) => ["@" + val[0], "@" + val[1]]));
} else {
callback(completeNicks(term, true));
}
},
template([string]) {
return string;
},
replace([, original], position = 1) {
// If no postfix specified, return autocompleted nick as-is
if (!store.state.settings.nickPostfix) {
return "$1" + original;
}
// If there is whitespace in the input already, append space to nick
2019-11-15 18:53:38 +00:00
if (position > 0 && /\s/.test(store.state.activeChannel.channel.pendingMessage)) {
return "$1" + original + " ";
}
// If nick is first in the input, append specified postfix
return "$1" + original + store.state.settings.nickPostfix;
},
index: 2,
};
const chanStrategy = {
id: "chans",
match: /(^|\s)((?:#|\+|&|![A-Z0-9]{5})(?:[^\s]+)?)$/,
search(term, callback) {
callback(completeChans(term));
},
template([string]) {
return string;
},
replace([, original]) {
return "$1" + original;
},
index: 2,
};
const commandStrategy = {
id: "commands",
match: /^\/(\w*)$/,
search(term, callback) {
callback(completeCommands("/" + term));
},
template([string]) {
return string;
},
replace([, original]) {
return original;
},
index: 1,
};
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])) {
2019-07-17 09:33:59 +00:00
return [
i[0],
fuzzy.match(term, i[1], {
pre: "<b>",
post: "</b>",
}).rendered,
];
}
return i;
});
callback(matchingColorCodes);
},
template(value) {
return `<span class="irc-fg${parseInt(value[0], 10)}">${value[1]}</span>`;
},
replace(value) {
return "\x03" + value[0];
},
index: 1,
};
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])) {
2019-07-17 09:33:59 +00:00
return [
pair[0],
fuzzy.match(term, pair[1], {
pre: "<b>",
post: "</b>",
}).rendered,
];
}
return pair;
})
.map((pair) => pair.concat(match[1])); // Needed to pass fg color to `template`...
callback(matchingColorCodes);
},
template(value) {
2019-07-17 09:33:59 +00:00
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];
},
index: 2,
};
2019-11-15 18:53:38 +00:00
function enableAutocomplete(input) {
2017-12-05 19:05:53 +00:00
let tabCount = 0;
let lastMatch = "";
2017-12-05 19:05:53 +00:00
let currentMatches = [];
2019-11-15 18:53:38 +00:00
input.addEventListener("input", (e) => {
if (e.detail === "autocomplete") {
return;
}
2017-12-05 19:05:53 +00:00
tabCount = 0;
currentMatches = [];
lastMatch = "";
});
2019-11-15 18:53:38 +00:00
Mousetrap(input).bind(
2019-07-17 09:33:59 +00:00
"tab",
(e) => {
if (store.state.isAutoCompleting) {
2019-07-17 09:33:59 +00:00
return;
}
2017-12-05 19:05:53 +00:00
2019-07-17 09:33:59 +00:00
e.preventDefault();
2017-12-05 19:05:53 +00:00
2019-11-15 18:53:38 +00:00
const text = input.value;
2017-12-05 19:05:53 +00:00
2019-07-17 09:33:59 +00:00
if (tabCount === 0) {
lastMatch = text
2019-11-15 18:53:38 +00:00
.substring(0, input.selectionStart)
.split(/\s/)
.pop();
2017-12-05 19:05:53 +00:00
2019-07-17 09:33:59 +00:00
if (lastMatch.length === 0) {
return;
}
currentMatches = completeNicks(lastMatch, false);
if (currentMatches.length === 0) {
return;
}
2017-12-05 19:05:53 +00:00
}
2019-11-15 18:53:38 +00:00
const position = input.selectionStart - lastMatch.length;
2019-07-17 09:33:59 +00:00
const newMatch = nicksStrategy.replace(
[0, currentMatches[tabCount % currentMatches.length]],
position
);
2019-11-15 18:53:38 +00:00
const remainder = text.substr(input.selectionStart);
2019-11-15 18:53:38 +00:00
input.value = text.substr(0, position) + newMatch + remainder;
input.selectionStart -= remainder.length;
input.selectionEnd = input.selectionStart;
2017-12-05 19:05:53 +00:00
2019-07-17 09:33:59 +00:00
// Propagate change to Vue model
2019-11-15 18:53:38 +00:00
input.dispatchEvent(
2019-07-17 09:33:59 +00:00
new CustomEvent("input", {
detail: "autocomplete",
})
);
2018-07-11 09:29:49 +00:00
2019-07-17 09:33:59 +00:00
lastMatch = newMatch;
tabCount++;
},
"keydown"
);
2017-12-05 19:05:53 +00:00
2019-11-15 18:53:38 +00:00
const editor = new Textarea(input);
const textcomplete = new Textcomplete(editor, {
2017-12-05 17:44:40 +00:00
dropdown: {
className: "textcomplete-menu",
placement: "top",
},
});
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", () => {
store.commit("isAutoCompleting", true);
2017-12-05 17:44:40 +00:00
});
textcomplete.on("hidden", () => {
store.commit("isAutoCompleting", false);
2017-12-05 17:44:40 +00:00
});
2019-11-15 18:53:38 +00:00
return {
hide() {
textcomplete.hide();
},
destroy() {
textcomplete.destroy();
store.commit("isAutoCompleting", false);
},
};
}
function fuzzyGrep(term, array) {
2019-07-17 09:33:59 +00:00
const results = fuzzy.filter(term, array, {
pre: "<b>",
post: "</b>",
});
return results.map((el) => [el.string, el.original]);
}
function rawNicks() {
if (store.state.activeChannel.channel.users.length > 0) {
const users = store.state.activeChannel.channel.users.slice();
2018-07-08 14:57:02 +00:00
return users.sort((a, b) => b.lastMessage - a.lastMessage).map((u) => u.nick);
}
const me = store.state.activeChannel.network.nick;
const otherUser = store.state.activeChannel.channel.name;
// If this is a query, add their name to autocomplete
if (me !== otherUser && store.state.activeChannel.channel.type === "query") {
return [otherUser, me];
}
// 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();
if (isFuzzy) {
return fuzzyGrep(word, users);
}
2019-11-15 18:53:38 +00:00
return users.filter((w) => !w.toLowerCase().indexOf(word));
}
function completeCommands(word) {
const words = constants.commands.slice();
return fuzzyGrep(word, words);
}
function completeChans(word) {
const words = [];
for (const channel of store.state.activeChannel.network.channels) {
// Push all channels that start with the same CHANTYPE
if (channel.type === "channel" && channel.name[0] === word[0]) {
2018-07-08 14:57:02 +00:00
words.push(channel.name);
}
}
return fuzzyGrep(word, words);
}