mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-02-22 01:29:39 +08:00
Add disableCandidateNormalization option. Fixes #1719
This commit is contained in:
parent
18384c4517
commit
8282ccee5f
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
4
build/interfaces.d.ts
vendored
4
build/interfaces.d.ts
vendored
@ -200,6 +200,10 @@ export interface KeyboardOptions {
|
||||
* Determines whether layout candidate match should be case sensitive.
|
||||
*/
|
||||
layoutCandidatesCaseSensitiveMatch?: boolean;
|
||||
/**
|
||||
* Disables the automatic normalization for selected layout candidates
|
||||
*/
|
||||
disableCandidateNormalization?: boolean;
|
||||
/**
|
||||
* Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
|
||||
*/
|
||||
|
@ -133,6 +133,7 @@ class SimpleKeyboard {
|
||||
* @property {object} excludeFromLayout Buttons to exclude from layout
|
||||
* @property {number} layoutCandidatesPageSize Determines size of layout candidate list
|
||||
* @property {boolean} layoutCandidatesCaseSensitiveMatch Determines whether layout candidate match should be case sensitive.
|
||||
* @property {boolean} disableCandidateNormalization Disables the automatic normalization for selected layout candidates
|
||||
*/
|
||||
this.options = {
|
||||
layoutName: "default",
|
||||
@ -374,12 +375,17 @@ class SimpleKeyboard {
|
||||
candidateValue,
|
||||
targetElement,
|
||||
onSelect: (selectedCandidate: string, e: MouseEvent) => {
|
||||
const { layoutCandidatesCaseSensitiveMatch } = this.options;
|
||||
const { layoutCandidatesCaseSensitiveMatch, disableCandidateNormalization } = this.options;
|
||||
|
||||
let candidateStr = selectedCandidate;
|
||||
|
||||
if(!disableCandidateNormalization) {
|
||||
/**
|
||||
* Making sure that our suggestions are not composed characters
|
||||
*/
|
||||
candidateStr = selectedCandidate.normalize("NFD");
|
||||
}
|
||||
|
||||
/**
|
||||
* Making sure that our suggestions are not composed characters
|
||||
*/
|
||||
const normalizedCandidate = selectedCandidate.normalize("NFD");
|
||||
const currentInput = this.getInput(this.options.inputName, true);
|
||||
const initialCaretPosition = this.getCaretPositionEnd() || 0;
|
||||
const inputSubstr =
|
||||
@ -392,7 +398,7 @@ class SimpleKeyboard {
|
||||
);
|
||||
const newInputSubstr = inputSubstr.replace(
|
||||
regexp,
|
||||
normalizedCandidate
|
||||
candidateStr
|
||||
);
|
||||
const newInput = currentInput.replace(inputSubstr, newInputSubstr);
|
||||
|
||||
|
@ -370,3 +370,60 @@ it('CandidateBox selection should trigger onChange', () => {
|
||||
expect(keyboard.options.onChangeAll.mock.calls[1][0]).toMatchObject({"default": "1"});
|
||||
keyboard.destroy();
|
||||
});
|
||||
|
||||
it('CandidateBox normalization will work', () => {
|
||||
const keyboard = new Keyboard({
|
||||
layout: {
|
||||
default: [
|
||||
"a b {bksp}"
|
||||
]
|
||||
},
|
||||
layoutCandidates: {
|
||||
a: "신"
|
||||
},
|
||||
onChange: jest.fn(),
|
||||
onChangeAll: jest.fn()
|
||||
});
|
||||
|
||||
let candidateBoxOnItemSelected;
|
||||
|
||||
const onSelect = jest.fn().mockImplementation((selectedCandidate) => {
|
||||
candidateBoxOnItemSelected(selectedCandidate);
|
||||
keyboard.candidateBox.destroy();
|
||||
});
|
||||
|
||||
const candidateBoxRenderFn = keyboard.candidateBox.renderPage;
|
||||
|
||||
jest.spyOn(keyboard.candidateBox, "renderPage").mockImplementation((params) => {
|
||||
candidateBoxOnItemSelected = params.onItemSelected;
|
||||
params.onItemSelected = onSelect;
|
||||
candidateBoxRenderFn(params);
|
||||
});
|
||||
|
||||
keyboard.getButtonElement("a").click();
|
||||
keyboard.candidateBox.candidateBoxElement.querySelector("li").click();
|
||||
|
||||
expect(keyboard.options.onChange.mock.calls[0][0]).toBe("a");
|
||||
expect(keyboard.options.onChangeAll.mock.calls[0][0]).toMatchObject({"default": "a"});
|
||||
|
||||
// Selected candidate will be normalized
|
||||
expect(keyboard.options.onChange.mock.calls[1][0]).toBe("신");
|
||||
expect(keyboard.options.onChange.mock.calls[1][0].length).toBe(3);
|
||||
expect(keyboard.options.onChangeAll.mock.calls[1][0]).toMatchObject({"default": "신"});
|
||||
|
||||
// Selected candidate will not be normalized
|
||||
keyboard.clearInput();
|
||||
keyboard.setOptions({ disableCandidateNormalization: true });
|
||||
|
||||
keyboard.getButtonElement("a").click();
|
||||
keyboard.candidateBox.candidateBoxElement.querySelector("li").click();
|
||||
|
||||
expect(keyboard.options.onChange.mock.calls[2][0]).toBe("a");
|
||||
expect(keyboard.options.onChangeAll.mock.calls[2][0]).toMatchObject({"default": "a"});
|
||||
|
||||
expect(keyboard.options.onChange.mock.calls[3][0]).toBe("신");
|
||||
expect(keyboard.options.onChange.mock.calls[3][0].length).toBe(1);
|
||||
expect(keyboard.options.onChangeAll.mock.calls[3][0]).toMatchObject({"default": "신"});
|
||||
|
||||
keyboard.destroy();
|
||||
});
|
@ -245,6 +245,11 @@ export interface KeyboardOptions {
|
||||
*/
|
||||
layoutCandidatesCaseSensitiveMatch?: boolean;
|
||||
|
||||
/**
|
||||
* Disables the automatic normalization for selected layout candidates
|
||||
*/
|
||||
disableCandidateNormalization?: boolean;
|
||||
|
||||
/**
|
||||
* Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user