mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
add matchProvider and valueProvider to autocomplete options
This commit is contained in:
@ -194,6 +194,11 @@ export class AutoComplete {
|
|||||||
* @returns The option.
|
* @returns The option.
|
||||||
*/
|
*/
|
||||||
fuzzyScore(option) {
|
fuzzyScore(option) {
|
||||||
|
// might have been matched by the options matchProvider function instead
|
||||||
|
if (!this.fuzzyRegex.test(option.name)) {
|
||||||
|
option.score = new AutoCompleteFuzzyScore(Number.MAX_SAFE_INTEGER, -1);
|
||||||
|
return option;
|
||||||
|
}
|
||||||
const parts = this.fuzzyRegex.exec(option.name).slice(1, -1);
|
const parts = this.fuzzyRegex.exec(option.name).slice(1, -1);
|
||||||
let start = null;
|
let start = null;
|
||||||
let consecutive = [];
|
let consecutive = [];
|
||||||
@ -344,7 +349,7 @@ export class AutoComplete {
|
|||||||
|
|
||||||
this.result = this.effectiveParserResult.optionList
|
this.result = this.effectiveParserResult.optionList
|
||||||
// filter the list of options by the partial name according to the matching type
|
// filter the list of options by the partial name according to the matching type
|
||||||
.filter(it => this.isReplaceable || it.name == '' ? matchers[this.matchType](it.name) : it.name.toLowerCase() == this.name)
|
.filter(it => this.isReplaceable || it.name == '' ? (it.matchProvider ? it.matchProvider(this.name) : matchers[this.matchType](it.name)) : it.name.toLowerCase() == this.name)
|
||||||
// remove aliases
|
// remove aliases
|
||||||
.filter((it,idx,list) => list.findIndex(opt=>opt.value == it.value) == idx);
|
.filter((it,idx,list) => list.findIndex(opt=>opt.value == it.value) == idx);
|
||||||
|
|
||||||
@ -362,10 +367,11 @@ export class AutoComplete {
|
|||||||
// build element
|
// build element
|
||||||
option.dom = this.makeItem(option);
|
option.dom = this.makeItem(option);
|
||||||
// update replacer and add quotes if necessary
|
// update replacer and add quotes if necessary
|
||||||
|
const optionName = option.valueProvider ? option.valueProvider(this.name) : option.name;
|
||||||
if (this.effectiveParserResult.canBeQuoted) {
|
if (this.effectiveParserResult.canBeQuoted) {
|
||||||
option.replacer = option.name.includes(' ') || this.startQuote || this.endQuote ? `"${option.name}"` : `${option.name}`;
|
option.replacer = optionName.includes(' ') || this.startQuote || this.endQuote ? `"${optionName}"` : `${optionName}`;
|
||||||
} else {
|
} else {
|
||||||
option.replacer = option.name;
|
option.replacer = optionName;
|
||||||
}
|
}
|
||||||
// calculate fuzzy score if matching is fuzzy
|
// calculate fuzzy score if matching is fuzzy
|
||||||
if (this.matchType == 'fuzzy') this.fuzzyScore(option);
|
if (this.matchType == 'fuzzy') this.fuzzyScore(option);
|
||||||
|
@ -11,6 +11,8 @@ export class AutoCompleteOption {
|
|||||||
/**@type {AutoCompleteFuzzyScore}*/ score;
|
/**@type {AutoCompleteFuzzyScore}*/ score;
|
||||||
/**@type {string}*/ replacer;
|
/**@type {string}*/ replacer;
|
||||||
/**@type {HTMLElement}*/ dom;
|
/**@type {HTMLElement}*/ dom;
|
||||||
|
/**@type {(input:string)=>boolean}*/ matchProvider;
|
||||||
|
/**@type {(input:string)=>string}*/ valueProvider;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,10 +27,12 @@ export class AutoCompleteOption {
|
|||||||
/**
|
/**
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
*/
|
*/
|
||||||
constructor(name, typeIcon = ' ', type = '') {
|
constructor(name, typeIcon = ' ', type = '', matchProvider = null, valueProvider = null) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.typeIcon = typeIcon;
|
this.typeIcon = typeIcon;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
this.matchProvider = matchProvider;
|
||||||
|
this.valueProvider = valueProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ export class SlashCommandEnumAutoCompleteOption extends AutoCompleteOption {
|
|||||||
* @param {SlashCommandEnumValue} enumValue
|
* @param {SlashCommandEnumValue} enumValue
|
||||||
*/
|
*/
|
||||||
constructor(cmd, enumValue) {
|
constructor(cmd, enumValue) {
|
||||||
super(enumValue.value, enumValue.typeIcon, enumValue.type);
|
super(enumValue.value, enumValue.typeIcon, enumValue.type, enumValue.matchProvider, enumValue.valueProvider);
|
||||||
this.cmd = cmd;
|
this.cmd = cmd;
|
||||||
this.enumValue = enumValue;
|
this.enumValue = enumValue;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,21 @@
|
|||||||
|
import { SlashCommandExecutor } from './SlashCommandExecutor.js';
|
||||||
|
import { SlashCommandScope } from './SlashCommandScope.js';
|
||||||
|
|
||||||
export class SlashCommandEnumValue {
|
export class SlashCommandEnumValue {
|
||||||
/**@type {string}*/ value;
|
/**@type {string}*/ value;
|
||||||
/**@type {string}*/ description;
|
/**@type {string}*/ description;
|
||||||
/**@type {string}*/ type = 'enum';
|
/**@type {string}*/ type = 'enum';
|
||||||
/**@type {string}*/ typeIcon = '◊';
|
/**@type {string}*/ typeIcon = '◊';
|
||||||
|
/**@type {(input:string)=>boolean}*/ matchProvider;
|
||||||
|
/**@type {(input:string)=>string}*/ valueProvider;
|
||||||
|
|
||||||
constructor(value, description = null, type = 'enum', typeIcon = '◊') {
|
constructor(value, description = null, type = 'enum', typeIcon = '◊', matchProvider, valueProvider) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.typeIcon = typeIcon;
|
this.typeIcon = typeIcon;
|
||||||
|
this.matchProvider = matchProvider;
|
||||||
|
this.valueProvider = valueProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
|
Reference in New Issue
Block a user