cache autocomplete elements

This commit is contained in:
LenAnderson
2024-04-25 18:02:06 -04:00
parent 12a2f54095
commit e531da615e

View File

@ -35,21 +35,12 @@ export class SlashCommand {
/**@type {SlashCommandNamedArgument[]}*/ namedArgumentList = []; /**@type {SlashCommandNamedArgument[]}*/ namedArgumentList = [];
/**@type {SlashCommandArgument[]}*/ unnamedArgumentList = []; /**@type {SlashCommandArgument[]}*/ unnamedArgumentList = [];
get helpStringFormatted() { /**@type {Object.<string, HTMLElement>}*/ helpCache = {};
let aliases = ''; /**@type {Object.<string, DocumentFragment>}*/ helpDetailsCache = {};
if (this.aliases?.length > 0) {
aliases = ' (alias: ';
aliases += this.aliases
.map(it=>`<span class="monospace">/${it}</span>`)
.join(', ')
;
aliases += ')';
}
return `<span class="monospace">/${this.name}</span> ${this.helpString}${aliases}`;
}
renderHelpItem(key = null) { renderHelpItem(key = null) {
key = key ?? this.name; key = key ?? this.name;
if (!this.helpCache[key]) {
const typeIcon = '/'; const typeIcon = '/';
const li = document.createElement('li'); { const li = document.createElement('li'); {
li.classList.add('item'); li.classList.add('item');
@ -189,12 +180,15 @@ export class SlashCommand {
} }
} }
} }
return li; this.helpCache[key] = li;
}
return this.helpCache[key];
} }
renderHelpDetails(key = null) { renderHelpDetails(key = null) {
const frag = document.createDocumentFragment();
key = key ?? this.name; key = key ?? this.name;
if (!this.helpDetailsCache[key]) {
const frag = document.createDocumentFragment();
const cmd = this; const cmd = this;
const namedArguments = cmd.namedArgumentList ?? []; const namedArguments = cmd.namedArgumentList ?? [];
const unnamedArguments = cmd.unnamedArgumentList ?? []; const unnamedArguments = cmd.unnamedArgumentList ?? [];
@ -358,6 +352,8 @@ export class SlashCommand {
frag.append(aliases); frag.append(aliases);
} }
} }
return frag; this.helpDetailsCache[key] = frag;
}
return this.helpDetailsCache[key].cloneNode(true);
} }
} }