mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-02-07 23:03:09 +08:00
2.6.0 cleanup
This commit is contained in:
parent
48c4e362b3
commit
3c5b94abf8
@ -37,8 +37,10 @@ class SimpleKeyboard {
|
|||||||
*/
|
*/
|
||||||
if(this.keyboardDOM)
|
if(this.keyboardDOM)
|
||||||
this.render();
|
this.render();
|
||||||
else
|
else {
|
||||||
console.error(`"${keyboardDOMQuery}" was not found in the DOM.`);
|
console.warn(`"${keyboardDOMQuery}" was not found in the DOM.`);
|
||||||
|
throw new Error("KEYBOARD_DOM_ERROR");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saving instance
|
* Saving instance
|
||||||
@ -165,8 +167,8 @@ class SimpleKeyboard {
|
|||||||
|
|
||||||
dispatch = (callback) => {
|
dispatch = (callback) => {
|
||||||
if(!window['SimpleKeyboardInstances']){
|
if(!window['SimpleKeyboardInstances']){
|
||||||
console.error("SimpleKeyboardInstances is not defined. Dispatch cannot be called.")
|
console.warn(`SimpleKeyboardInstances is not defined. Dispatch cannot be called.`);
|
||||||
return false;
|
throw new Error("INSTANCES_VAR_ERROR");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.keys(window['SimpleKeyboardInstances']).forEach((key) => {
|
return Object.keys(window['SimpleKeyboardInstances']).forEach((key) => {
|
||||||
@ -245,11 +247,7 @@ class SimpleKeyboard {
|
|||||||
(className && className.includes(buttonTheme.class)) ||
|
(className && className.includes(buttonTheme.class)) ||
|
||||||
!className
|
!className
|
||||||
){
|
){
|
||||||
let filteredButtonArray;
|
let filteredButtonArray = buttonTheme.buttons.split(" ").filter(item => item !== button);
|
||||||
|
|
||||||
if(buttonArray.includes(button)){
|
|
||||||
filteredButtonArray = buttonTheme.buttons.split(" ").filter(item => item !== button);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If buttons left, return them, otherwise, remove button Theme
|
* If buttons left, return them, otherwise, remove button Theme
|
||||||
@ -291,24 +289,24 @@ class SimpleKeyboard {
|
|||||||
console.log("Caret handling started");
|
console.log("Caret handling started");
|
||||||
}
|
}
|
||||||
|
|
||||||
let handler = (event) => {
|
document.addEventListener("keyup", this.caretEventHandler);
|
||||||
let targetTagName = event.target.tagName.toLowerCase();
|
document.addEventListener("mouseup", this.caretEventHandler);
|
||||||
|
document.addEventListener("touchend", this.caretEventHandler);
|
||||||
|
}
|
||||||
|
|
||||||
if(
|
caretEventHandler = (event) => {
|
||||||
targetTagName === "textarea" ||
|
let targetTagName = event.target.tagName.toLowerCase();
|
||||||
targetTagName === "input"
|
|
||||||
){
|
|
||||||
this.caretPosition = event.target.selectionStart;
|
|
||||||
|
|
||||||
if(this.options.debug){
|
if(
|
||||||
console.log('Caret at: ', event.target.selectionStart, event.target.tagName.toLowerCase());
|
targetTagName === "textarea" ||
|
||||||
}
|
targetTagName === "input"
|
||||||
}
|
){
|
||||||
};
|
this.caretPosition = event.target.selectionStart;
|
||||||
|
|
||||||
document.addEventListener("keyup", handler);
|
if(this.options.debug){
|
||||||
document.addEventListener("mouseup", handler);
|
console.log('Caret at: ', event.target.selectionStart, event.target.tagName.toLowerCase());
|
||||||
document.addEventListener("touchend", handler);
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onInit = () => {
|
onInit = () => {
|
||||||
@ -337,7 +335,7 @@ class SimpleKeyboard {
|
|||||||
this.clear();
|
this.clear();
|
||||||
|
|
||||||
let layoutClass = this.options.layout ? "hg-layout-custom" : `hg-layout-${this.options.layoutName}`;
|
let layoutClass = this.options.layout ? "hg-layout-custom" : `hg-layout-${this.options.layoutName}`;
|
||||||
let layout = this.options.layout || KeyboardLayout.getLayout(this.options.layoutName);
|
let layout = this.options.layout || KeyboardLayout.getDefaultLayout();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Account for buttonTheme, if set
|
* Account for buttonTheme, if set
|
||||||
@ -346,17 +344,25 @@ class SimpleKeyboard {
|
|||||||
if(Array.isArray(this.options.buttonTheme)){
|
if(Array.isArray(this.options.buttonTheme)){
|
||||||
this.options.buttonTheme.forEach(themeObj => {
|
this.options.buttonTheme.forEach(themeObj => {
|
||||||
if(themeObj.buttons && themeObj.class){
|
if(themeObj.buttons && themeObj.class){
|
||||||
let themeButtons = themeObj.buttons.split(' ');
|
let themeButtons;
|
||||||
|
|
||||||
if(Array.isArray(themeButtons)){
|
if(typeof themeObj.buttons === "string"){
|
||||||
|
themeButtons = themeObj.buttons.split(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
if(themeButtons){
|
||||||
themeButtons.forEach(themeButton => {
|
themeButtons.forEach(themeButton => {
|
||||||
let themeParsed = buttonThemesParsed[themeButton];
|
let themeParsed = buttonThemesParsed[themeButton];
|
||||||
|
|
||||||
// If the button has already been added
|
// If the button has already been added
|
||||||
if(themeParsed)
|
if(themeParsed){
|
||||||
buttonThemesParsed[themeButton] = `${themeParsed} ${themeObj.class}`;
|
// Making sure we don't add duplicate classes, even when buttonTheme has duplicates
|
||||||
else
|
if(!this.utilities.countInArray(themeParsed.split(" "), themeObj.class)){
|
||||||
|
buttonThemesParsed[themeButton] = `${themeParsed} ${themeObj.class}`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
buttonThemesParsed[themeButton] = themeObj.class;
|
buttonThemesParsed[themeButton] = themeObj.class;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -409,6 +415,11 @@ class SimpleKeyboard {
|
|||||||
let buttonUID = `${this.options.layoutName}-r${rIndex}b${bIndex}`;
|
let buttonUID = `${this.options.layoutName}-r${rIndex}b${bIndex}`;
|
||||||
buttonDOM.setAttribute("data-skBtnUID", buttonUID);
|
buttonDOM.setAttribute("data-skBtnUID", buttonUID);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adding display label
|
||||||
|
*/
|
||||||
|
buttonDOM.setAttribute("data-displayLabel", buttonDisplayName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adding button label to button
|
* Adding button label to button
|
||||||
*/
|
*/
|
||||||
|
@ -1,37 +1,22 @@
|
|||||||
class KeyboardLayout {
|
class KeyboardLayout {
|
||||||
|
static getDefaultLayout = () => {
|
||||||
static getLayout = layout => {
|
return {
|
||||||
if(layout === "qwerty"){
|
'default': [
|
||||||
return {
|
'` 1 2 3 4 5 6 7 8 9 0 - = {bksp}',
|
||||||
'default': [
|
'{tab} q w e r t y u i o p [ ] \\',
|
||||||
'` 1 2 3 4 5 6 7 8 9 0 - = {bksp}',
|
'{lock} a s d f g h j k l ; \' {enter}',
|
||||||
'{tab} q w e r t y u i o p [ ] \\',
|
'{shift} z x c v b n m , . / {shift}',
|
||||||
'{lock} a s d f g h j k l ; \' {enter}',
|
'.com @ {space}'
|
||||||
'{shift} z x c v b n m , . / {shift}',
|
],
|
||||||
'.com @ {space}'
|
'shift': [
|
||||||
],
|
'~ ! @ # $ % ^ & * ( ) _ + {bksp}',
|
||||||
'shift': [
|
'{tab} Q W E R T Y U I O P { } |',
|
||||||
'~ ! @ # $ % ^ & * ( ) _ + {bksp}',
|
'{lock} A S D F G H J K L : " {enter}',
|
||||||
'{tab} Q W E R T Y U I O P { } |',
|
'{shift} Z X C V B N M < > ? {shift}',
|
||||||
'{lock} A S D F G H J K L : " {enter}',
|
'.com @ {space}'
|
||||||
'{shift} Z X C V B N M < > ? {shift}',
|
]
|
||||||
'.com @ {space}'
|
}
|
||||||
]
|
|
||||||
};
|
|
||||||
} else if(layout === "numeric"){
|
|
||||||
return {
|
|
||||||
'default': [
|
|
||||||
'1 2 3',
|
|
||||||
'4 5 6',
|
|
||||||
'7 8 9',
|
|
||||||
'{//} 0 {bksp}'
|
|
||||||
]
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return KeyboardLayout.getLayout("qwerty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default KeyboardLayout;
|
export default KeyboardLayout;
|
@ -2,11 +2,6 @@ class PhysicalKeyboard {
|
|||||||
constructor(simpleKeyboardInstance){
|
constructor(simpleKeyboardInstance){
|
||||||
this.simpleKeyboardInstance = simpleKeyboardInstance;
|
this.simpleKeyboardInstance = simpleKeyboardInstance;
|
||||||
|
|
||||||
if(!window['SimpleKeyboardPhysicalKeyboardInit'])
|
|
||||||
window['SimpleKeyboardPhysicalKeyboardInit'] = true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
|
|
||||||
this.initKeyboardListener();
|
this.initKeyboardListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,56 +3,13 @@ class Utilities {
|
|||||||
this.simpleKeyboardInstance = simpleKeyboardInstance;
|
this.simpleKeyboardInstance = simpleKeyboardInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
normalizeString(string){
|
|
||||||
let output;
|
|
||||||
|
|
||||||
if(string === "@")
|
|
||||||
output = 'at';
|
|
||||||
else if(string === ",")
|
|
||||||
output = 'comma';
|
|
||||||
else if(string === ".")
|
|
||||||
output = 'dot';
|
|
||||||
else if(string === "\\")
|
|
||||||
output = 'backslash';
|
|
||||||
else if(string === "/")
|
|
||||||
output = 'fordardslash';
|
|
||||||
else if(string === "*")
|
|
||||||
output = 'asterisk';
|
|
||||||
else if(string === "&")
|
|
||||||
output = 'ampersand';
|
|
||||||
else if(string === "$")
|
|
||||||
output = 'dollarsign';
|
|
||||||
else if(string === "=")
|
|
||||||
output = 'equals';
|
|
||||||
else if(string === "+")
|
|
||||||
output = 'plus';
|
|
||||||
else if(string === "-")
|
|
||||||
output = 'minus';
|
|
||||||
else if(string === "'")
|
|
||||||
output = 'apostrophe';
|
|
||||||
else if(string === ";")
|
|
||||||
output = 'colon';
|
|
||||||
else if(string === "[")
|
|
||||||
output = 'openbracket';
|
|
||||||
else if(string === "]")
|
|
||||||
output = 'closebracket';
|
|
||||||
else if(string === "//")
|
|
||||||
output = 'emptybutton';
|
|
||||||
else if(string === ".com")
|
|
||||||
output = 'com';
|
|
||||||
else
|
|
||||||
output = '';
|
|
||||||
|
|
||||||
return output ? ` hg-button-${output}` : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
getButtonClass = button => {
|
getButtonClass = button => {
|
||||||
let buttonTypeClass = (button.includes("{") && button.includes("}") && button !== '{//}') ? "functionBtn" : "standardBtn";
|
let buttonTypeClass = (button.includes("{") && button.includes("}") && button !== '{//}') ? "functionBtn" : "standardBtn";
|
||||||
let buttonWithoutBraces = button.replace("{", "").replace("}", "");
|
let buttonWithoutBraces = button.replace("{", "").replace("}", "");
|
||||||
|
let buttonNormalized = '';
|
||||||
|
|
||||||
let buttonNormalized =
|
if(buttonTypeClass !== "standardBtn")
|
||||||
buttonTypeClass === "standardBtn" ?
|
buttonNormalized = ` hg-button-${buttonWithoutBraces}`;
|
||||||
this.normalizeString(buttonWithoutBraces) : ` hg-button-${buttonWithoutBraces}`;
|
|
||||||
|
|
||||||
return `hg-${buttonTypeClass}${buttonNormalized}`;
|
return `hg-${buttonTypeClass}${buttonNormalized}`;
|
||||||
}
|
}
|
||||||
@ -161,9 +118,6 @@ class Utilities {
|
|||||||
else if(button === "{numpadadd}")
|
else if(button === "{numpadadd}")
|
||||||
output = this.addStringAt(output, '+', caretPos);
|
output = this.addStringAt(output, '+', caretPos);
|
||||||
|
|
||||||
else if(button === "{numpadadd}")
|
|
||||||
output = this.addStringAt(output, '+', caretPos);
|
|
||||||
|
|
||||||
else if(button === "{numpaddecimal}")
|
else if(button === "{numpaddecimal}")
|
||||||
output = this.addStringAt(output, '.', caretPos);
|
output = this.addStringAt(output, '.', caretPos);
|
||||||
|
|
||||||
@ -305,6 +259,10 @@ class Utilities {
|
|||||||
return string.toLowerCase().trim().split(/[.\-_\s]/g).reduce((string, word) => string + word[0].toUpperCase() + word.slice(1));
|
return string.toLowerCase().trim().split(/[.\-_\s]/g).reduce((string, word) => string + word[0].toUpperCase() + word.slice(1));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
countInArray = (array, value) => {
|
||||||
|
return array.reduce((n, x) => n + (x === value), 0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Utilities;
|
export default Utilities;
|
Loading…
Reference in New Issue
Block a user