2022-06-19 00:25:21 +00:00
|
|
|
import constants from "./constants";
|
2019-12-17 22:10:50 +00:00
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
import Mousetrap from "mousetrap";
|
2024-05-02 06:21:51 +00:00
|
|
|
import {Textcomplete, StrategyProps} from "@textcomplete/core";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {TextareaEditor} from "@textcomplete/textarea";
|
2022-04-12 01:11:43 +00:00
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
import fuzzy from "fuzzy";
|
|
|
|
|
|
|
|
import emojiMap from "./helpers/simplemap.json";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {store} from "./store";
|
2024-05-02 06:21:51 +00:00
|
|
|
import {ChanType} from "../../shared/types/chan";
|
2017-08-24 14:44:40 +00:00
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
export default enableAutocomplete;
|
2017-12-05 17:44:40 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
const emojiSearchTerms = Object.keys(emojiMap);
|
2022-06-19 00:25:21 +00:00
|
|
|
const emojiStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "emoji",
|
2020-01-12 14:24:48 +00:00
|
|
|
match: /(^|\s):([-+\w:?]{2,}):?$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matches) => void) {
|
2017-08-24 14:44:40 +00:00
|
|
|
// Trim colon from the matched term,
|
|
|
|
// as we are unable to get a clean string from match regex
|
2020-01-12 14:24:48 +00:00
|
|
|
term = term.replace(/:$/, "");
|
|
|
|
callback(fuzzyGrep(term, emojiSearchTerms));
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template([string, original]: [string, string]) {
|
|
|
|
return `<span class="emoji">${String(emojiMap[original])}</span> ${string}`;
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace([, original]: [string, string]) {
|
|
|
|
return "$1" + String(emojiMap[original]);
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2020-01-12 14:24:48 +00:00
|
|
|
index: 2,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const nicksStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "nicks",
|
2020-01-12 14:24:48 +00:00
|
|
|
match: /(^|\s)(@([a-zA-Z_[\]\\^{}|`@][a-zA-Z0-9_[\]\\^{}|`-]*)?)$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matches: string[] | string[][]) => void) {
|
2017-08-24 14:44:40 +00:00
|
|
|
term = term.slice(1);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-08-24 14:44:40 +00:00
|
|
|
if (term[0] === "@") {
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO: type
|
2019-07-17 09:33:59 +00:00
|
|
|
callback(completeNicks(term.slice(1), true).map((val) => ["@" + val[0], "@" + val[1]]));
|
2017-08-24 14:44:40 +00:00
|
|
|
} else {
|
|
|
|
callback(completeNicks(term, true));
|
|
|
|
}
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template([string]: [string, string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace([, original]: [string, string]) {
|
2020-01-23 20:25:34 +00:00
|
|
|
return "$1" + replaceNick(original);
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2020-01-12 14:24:48 +00:00
|
|
|
index: 2,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const chanStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "chans",
|
2020-01-12 14:24:48 +00:00
|
|
|
match: /(^|\s)((?:#|\+|&|![A-Z0-9]{5})(?:[^\s]+)?)$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matches: string[][]) => void) {
|
2020-01-12 14:24:48 +00:00
|
|
|
callback(completeChans(term));
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template([string]: [string, string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace([, original]: [string, string]) {
|
2020-01-12 14:24:48 +00:00
|
|
|
return "$1" + original;
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2020-01-12 14:24:48 +00:00
|
|
|
index: 2,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const commandStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "commands",
|
|
|
|
match: /^\/(\w*)$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matches: string[][]) => void) {
|
2017-08-24 14:44:40 +00:00
|
|
|
callback(completeCommands("/" + term));
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template([string]: [string, string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return string;
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace([, original]: [string, string]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return original;
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const foregroundColorStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "foreground-colors",
|
|
|
|
match: /\x03(\d{0,2}|[A-Za-z ]{0,10})$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matches: string[][]) => void) {
|
2017-08-24 14:44:40 +00:00
|
|
|
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,
|
|
|
|
];
|
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 i;
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template(value: string[]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return `<span class="irc-fg${parseInt(value[0], 10)}">${value[1]}</span>`;
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace(value: string) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return "\x03" + value[0];
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 1,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const backgroundColorStrategy: StrategyProps = {
|
2017-08-24 14:44:40 +00:00
|
|
|
id: "background-colors",
|
|
|
|
match: /\x03(\d{2}),(\d{0,2}|[A-Za-z ]{0,10})$/,
|
2022-06-19 00:25:21 +00:00
|
|
|
search(term: string, callback: (matchingColorCodes: string[][]) => void, match: string[]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
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,
|
|
|
|
];
|
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 pair;
|
|
|
|
})
|
|
|
|
.map((pair) => pair.concat(match[1])); // Needed to pass fg color to `template`...
|
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
template(value: string[]) {
|
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>`;
|
2017-08-24 14:44:40 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
replace(value: string[]) {
|
2017-08-24 14:44:40 +00:00
|
|
|
return "\x03$1," + value[0];
|
|
|
|
},
|
2017-11-15 06:35:15 +00:00
|
|
|
index: 2,
|
2017-08-24 14:44:40 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function enableAutocomplete(input: HTMLTextAreaElement) {
|
2017-12-05 19:05:53 +00:00
|
|
|
let tabCount = 0;
|
2017-12-05 16:05:14 +00:00
|
|
|
let lastMatch = "";
|
2022-06-19 00:25:21 +00:00
|
|
|
let currentMatches: string[] | string[][] = [];
|
2017-12-05 19:05:53 +00:00
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
input.addEventListener("input", (e) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
if ((e as CustomEvent).detail === "autocomplete") {
|
2018-07-17 08:21:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-05 19:05:53 +00:00
|
|
|
tabCount = 0;
|
|
|
|
currentMatches = [];
|
2017-12-05 16:05:14 +00:00
|
|
|
lastMatch = "";
|
2017-08-24 14:44:40 +00:00
|
|
|
});
|
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
Mousetrap(input).bind(
|
2019-07-17 09:33:59 +00:00
|
|
|
"tab",
|
|
|
|
(e) => {
|
2019-11-02 19:40:59 +00:00
|
|
|
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) {
|
2022-06-19 00:25:21 +00:00
|
|
|
lastMatch = text.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;
|
2020-01-23 20:25:34 +00:00
|
|
|
const newMatch = replaceNick(
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO: type this properly
|
|
|
|
String(currentMatches[tabCount % currentMatches.length]),
|
2019-07-17 09:33:59 +00:00
|
|
|
position
|
|
|
|
);
|
2022-06-19 00:25:21 +00:00
|
|
|
const remainder = text.substring(input.selectionStart);
|
2017-12-05 16:05:14 +00:00
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
input.value = text.substr(0, position) + newMatch + remainder;
|
2022-06-19 00:25:21 +00:00
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
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
|
|
|
|
2022-04-12 01:11:43 +00:00
|
|
|
const strategies = [
|
2017-12-05 17:44:40 +00:00
|
|
|
emojiStrategy,
|
|
|
|
nicksStrategy,
|
|
|
|
chanStrategy,
|
|
|
|
commandStrategy,
|
|
|
|
foregroundColorStrategy,
|
|
|
|
backgroundColorStrategy,
|
2022-04-12 01:11:43 +00:00
|
|
|
];
|
2017-12-05 17:44:40 +00:00
|
|
|
|
2022-04-12 01:11:43 +00:00
|
|
|
const editor = new TextareaEditor(input);
|
|
|
|
const textcomplete = new Textcomplete(editor, strategies, {
|
|
|
|
dropdown: {
|
|
|
|
className: "textcomplete-menu",
|
|
|
|
placement: "top",
|
|
|
|
},
|
2017-12-05 17:44:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
textcomplete.on("show", () => {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("isAutoCompleting", true);
|
2017-12-05 17:44:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
textcomplete.on("hidden", () => {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("isAutoCompleting", false);
|
2017-12-05 17:44:40 +00:00
|
|
|
});
|
2018-07-31 14:39:03 +00:00
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
return {
|
|
|
|
hide() {
|
|
|
|
textcomplete.hide();
|
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
textcomplete.destroy();
|
|
|
|
store.commit("isAutoCompleting", false);
|
|
|
|
},
|
|
|
|
};
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function replaceNick(original: string, position = 1) {
|
2020-01-23 20:25:34 +00:00
|
|
|
// If no postfix specified, return autocompleted nick as-is
|
|
|
|
if (!store.state.settings.nickPostfix) {
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is whitespace in the input already, append space to nick
|
2022-06-19 00:25:21 +00:00
|
|
|
if (position > 0 && /\s/.test(store.state.activeChannel?.channel.pendingMessage || "")) {
|
2020-01-23 20:25:34 +00:00
|
|
|
return original + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nick is first in the input, append specified postfix
|
|
|
|
return original + store.state.settings.nickPostfix;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function fuzzyGrep<T>(term: string, array: Array<T>) {
|
2019-07-17 09:33:59 +00:00
|
|
|
const results = fuzzy.filter(term, array, {
|
|
|
|
pre: "<b>",
|
|
|
|
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() {
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!store.state.activeChannel) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
if (store.state.activeChannel.channel.users.length > 0) {
|
|
|
|
const users = store.state.activeChannel.channel.users.slice();
|
2017-10-29 03:03:27 +00:00
|
|
|
|
2018-07-08 14:57:02 +00:00
|
|
|
return users.sort((a, b) => b.lastMessage - a.lastMessage).map((u) => u.nick);
|
2017-10-29 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
const me = store.state.activeChannel.network.nick;
|
|
|
|
const otherUser = store.state.activeChannel.channel.name;
|
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
|
2024-05-02 06:21:51 +00:00
|
|
|
if (me !== otherUser && store.state.activeChannel.channel.type === ChanType.QUERY) {
|
2017-10-29 03:03:27 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function completeNicks(word: string, isFuzzy: boolean) {
|
2017-10-29 03:03:27 +00:00
|
|
|
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
|
|
|
|
2019-11-15 18:53:38 +00:00
|
|
|
return users.filter((w) => !w.toLowerCase().indexOf(word));
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 02:15:33 +00:00
|
|
|
function getCommands() {
|
2021-11-15 21:57:20 +00:00
|
|
|
let cmds = constants.commands.slice();
|
2021-07-04 02:15:33 +00:00
|
|
|
|
2021-11-15 21:57:20 +00:00
|
|
|
if (!store.state.settings.searchEnabled) {
|
|
|
|
cmds = cmds.filter((c) => c !== "/search");
|
2021-07-04 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:43:02 +00:00
|
|
|
return cmds;
|
2021-07-04 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function completeCommands(word: string) {
|
2021-11-15 21:57:20 +00:00
|
|
|
const commands = getCommands();
|
|
|
|
return fuzzyGrep(word, commands);
|
2017-08-24 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function completeChans(word: string) {
|
|
|
|
const words: string[] = [];
|
2017-08-24 14:44:40 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (store.state.activeChannel) {
|
|
|
|
for (const channel of store.state.activeChannel.network.channels) {
|
|
|
|
// Push all channels that start with the same CHANTYPE
|
2024-05-02 06:21:51 +00:00
|
|
|
if (channel.type === ChanType.CHANNEL && channel.name[0] === word[0]) {
|
2022-06-19 00:25:21 +00:00
|
|
|
words.push(channel.name);
|
|
|
|
}
|
2018-07-08 14:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-24 14:44:40 +00:00
|
|
|
|
|
|
|
return fuzzyGrep(word, words);
|
|
|
|
}
|