Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix custom key with multiple characters #32

Closed
wants to merge 3 commits into from
Closed

Conversation

skullab
Copy link

@skullab skullab commented Nov 28, 2021

fix for issue #31

NOTE:
no distribution version created

Copy link
Owner

@furcan furcan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

  • .gitignore file changes should be discarded.
  • package-lock.json file should be removed.
  • yarn.lock file changes should be discarded.
  • I've added a suggestion for the fixes that you have committed. src/kioskboard.js

In addition,
No worries about the distribution, the contribution guideline is not ready yet. So, only the fixes are enough in this case.

Thanks,
Furkan.

.gitignore Show resolved Hide resolved
@@ -0,0 +1,7849 @@
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this file as well. We are using yarn instead of npm.

yarn.lock Outdated
@@ -2,29 +2,29 @@
# yarn lockfile v1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore this file as well.

Comment on lines 650 to 654
e.preventDefault();

// update the selectionStart
theInputSelIndex = input.selectionStart || (input.value || '').length;
// Math.min prevents out of bound, theInputSelIndex cannot be greater than the length of theInputValArray
theInputSelIndex = Math.min((input.selectionStart || (input.value || '').length),theInputValArray.length);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes should be discarded if my suggestion on the adding part is ok for you.

Comment on lines 601 to 607
theInputValArray.splice(theInputSelIndex, 0, keyValue);
/** Prevents breaking on custom keys that insert multiple characters, for example, imagine a ".com" key,
* it would be inserted into the array as a single element and could no longer be deleted because the length of
* the input would be greater than the length of the array, adding Math.min (and comparing the respective lengths)
* to the backspace key prevents this problem but would delete the entire contents of the key (all characters, for example ".com")
* Instead, concatenate the main array of the contents with a possible array of keyValue, you can delete any single character.
**/
theInputValArray = theInputValArray.concat(keyValue.split(''));
Copy link
Owner

@furcan furcan Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use and update the selection index for adding each character because of the allowRealKeyboard option.

So, my suggestion is:

  • Spliting the keyValue for the possibility of the multiple characters.
  • Creating a for loop for the new array of the keyValue.
  • add, update, and dispatch events will be moved in this loop.
keyElm.addEventListener('click', function (e) {
  e.preventDefault();

  // check input max & maxlength
  var maxLength = (input.getAttribute('maxlength') || '') * 1;
  var max = (input.getAttribute('max') || '') * 1;
  var liveValueLength = (input.value || '').length || 0;
  if (maxLength > 0 && liveValueLength >= maxLength) { return false; }
  if (max > 0 && liveValueLength >= max) { return false; }

  // input trigger focus
  input.focus();

  // key's value
  var keyValue = e.currentTarget.dataset.value || '';

  // check capslock
  if (isCapsLockActive) {
    keyValue = keyValue.toLocaleUpperCase(keyboardLanguage);
  } else {
    keyValue = keyValue.toLocaleLowerCase(keyboardLanguage);
  }

  var keyValArr = keyValue.split('');
  for (var keyValIndex = 0; keyValIndex < keyValArr.length; keyValIndex++) {
    // update the selectionStart
    theInputSelIndex = input.selectionStart || (input.value || '').length;

    // add value by index
    theInputValArray.splice(theInputSelIndex, 0, keyValArr[keyValIndex]);

    // update input value
    input.value = theInputValArray.join('');

    // set next selection index
    input.setSelectionRange(theInputSelIndex + 1, theInputSelIndex + 1);

    // input trigger change event for update the value
    input.dispatchEvent(changeEvent);
  }
}, false);

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all ok for me 👍

Copy link
Owner

@furcan furcan Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to replace the keyElm.addEventListener('click', function (e) {... code block(above) with your current one and update this MR? (with discarding other ones)

Copy link
Author

@skullab skullab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all changed as you suggested

@furcan
Copy link
Owner

furcan commented Dec 11, 2021

Hi @skullab ,
It seems you have removed/changed more code than you have to change. I am going to close this PR and will fix the issue by myself if you don't mind.
Many thanks for reporting this issue.

@furcan furcan closed this Dec 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants