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

[FEAT] - KeysEnter can produce a line break ? #96

Open
gdesor opened this issue Sep 30, 2024 · 0 comments
Open

[FEAT] - KeysEnter can produce a line break ? #96

gdesor opened this issue Sep 30, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@gdesor
Copy link

gdesor commented Sep 30, 2024

Why can't the enter key just give a line break? (for textarea)

I worked around the issue with the enter key callback function, but I think it would be nice if this functionality could exist by default.

              // The callback function of the Enter key. This function will be called when the enter key has been clicked.
              keysEnterCallback: function(input) {
                                     if (input.tagName.toLowerCase() === "textarea") {
            
                                     
                                       // event for input element trigger change: begin
                                       var changeEvent = new Event('change', {
                                         'bubbles': true,
                                         'cancelable': true,
                                       });
                                       // event for input element trigger change: end
                                       
                                       
                                       
                                       // input trigger focus
                                       input.focus();
                     
                                       // key's value
                                       var keyValue = '\n';
                     
                     
                                       var keyValArr = keyValue.split('');
            
                                       theInputValArray = input.value.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
                                          if (input.type !== 'number') {
                                            input.setSelectionRange(theInputSelIndex + 1, theInputSelIndex + 1);
                                          }
                      
                                          // input trigger change event for update the value
                                          input.dispatchEvent(changeEvent);                          
                                        }
                                        
                                     }
                                    
                                    },

So I had to modify the source code a little bit to :

  • avoid position errors at the cursor level.
  • get the input in my callback function
                  // check capslock
                  if (isCapsLockActive) {
                    keyValue = keyValue.toLocaleUpperCase(keyboardLanguage);
                  } else {
                    keyValue = keyValue.toLocaleLowerCase(keyboardLanguage);
                  }

                  var keyValArr = keyValue.split('');
//LINE ADDED
                  theInputValArray = input.value.split('');
//END
                  for (var keyValIndex = 0; keyValIndex < keyValArr.length; keyValIndex++) {
                if (typeof opt.keysEnterCallback === 'function') {
//input PARAM ADDED
                  opt.keysEnterCallback(input);
//END

Thank you very much for this lovely piece of work. It takes a big thorn out of my side.

@gdesor gdesor added the enhancement New feature or request label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants