Add layoutCandidatesCaseSensitiveMatch

This commit is contained in:
Francisco Hodge 2022-04-05 21:25:44 -07:00
parent f603838ff0
commit 95fe298c78
2 changed files with 13 additions and 5 deletions

View File

@ -129,7 +129,8 @@ class SimpleKeyboard {
* @property {array} modules Module classes to be loaded by simple-keyboard. * @property {array} modules Module classes to be loaded by simple-keyboard.
* @property {boolean} enableLayoutCandidates Enable input method editor candidate list support. * @property {boolean} enableLayoutCandidates Enable input method editor candidate list support.
* @property {object} excludeFromLayout Buttons to exclude from layout * @property {object} excludeFromLayout Buttons to exclude from layout
* @property {number} layoutCandidatesPageSize Determine size of layout candidate list * @property {number} layoutCandidatesPageSize Determines size of layout candidate list
* @property {boolean} layoutCandidatesCaseSensitiveMatch Determines whether layout candidate match should be case sensitive.
*/ */
this.options = { this.options = {
layoutName: "default", layoutName: "default",
@ -315,7 +316,7 @@ class SimpleKeyboard {
getInputCandidates( getInputCandidates(
input: string input: string
): { candidateKey: string; candidateValue: string } | Record<string, never> { ): { candidateKey: string; candidateValue: string } | Record<string, never> {
const { layoutCandidates: layoutCandidatesObj } = this.options; const { layoutCandidates: layoutCandidatesObj, layoutCandidatesCaseSensitiveMatch } = this.options;
if (!layoutCandidatesObj || typeof layoutCandidatesObj !== "object") { if (!layoutCandidatesObj || typeof layoutCandidatesObj !== "object") {
return {}; return {};
@ -325,7 +326,7 @@ class SimpleKeyboard {
(layoutCandidate: string) => { (layoutCandidate: string) => {
const inputSubstr = const inputSubstr =
input.substring(0, this.getCaretPositionEnd() || 0) || input; input.substring(0, this.getCaretPositionEnd() || 0) || input;
const regexp = new RegExp(`${layoutCandidate}$`, "gi"); const regexp = new RegExp(`${layoutCandidate}$`, layoutCandidatesCaseSensitiveMatch ? "g" : "gi");
const matches = [...inputSubstr.matchAll(regexp)]; const matches = [...inputSubstr.matchAll(regexp)];
return !!matches.length; return !!matches.length;
} }
@ -365,6 +366,8 @@ class SimpleKeyboard {
candidateValue, candidateValue,
targetElement, targetElement,
onSelect: (selectedCandidate: string, e: MouseEvent) => { onSelect: (selectedCandidate: string, e: MouseEvent) => {
const { layoutCandidatesCaseSensitiveMatch } = this.options;
/** /**
* Making sure that our suggestions are not composed characters * Making sure that our suggestions are not composed characters
*/ */
@ -375,7 +378,7 @@ class SimpleKeyboard {
currentInput.substring(0, initialCaretPosition || 0) || currentInput.substring(0, initialCaretPosition || 0) ||
currentInput; currentInput;
const regexp = new RegExp(`${candidateKey}$`, "gi"); const regexp = new RegExp(`${candidateKey}$`, layoutCandidatesCaseSensitiveMatch ? "g" : "gi");
const newInputSubstr = inputSubstr.replace( const newInputSubstr = inputSubstr.replace(
regexp, regexp,
normalizedCandidate normalizedCandidate

View File

@ -231,10 +231,15 @@ export interface KeyboardOptions {
excludeFromLayout?: { [key: string]: string[] }; excludeFromLayout?: { [key: string]: string[] };
/** /**
* Determine size of layout candidate list * Determines size of layout candidate list
*/ */
layoutCandidatesPageSize?: number; layoutCandidatesPageSize?: number;
/**
* Determines whether layout candidate match should be case sensitive.
*/
layoutCandidatesCaseSensitiveMatch?: boolean;
/** /**
* Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts). * Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
*/ */