Trumbowyg icon indicating copy to clipboard operation
Trumbowyg copied to clipboard

`tbwchange` acts like `input` events, not `change`

Open timint opened this issue 3 years ago • 6 comments

I discovered something unusal. The tbwchange event acts like the input event and fires upon every input event rather than change. Is that by design? Thinking it should have been named tbwinput. Change is passed on blur after there has been input.

Would it be a better solution to delegate the normal change and input events back to the input field? .trigger('input')

I was attempting to do the following when I discovered there are no events firing at all for the input field:

$(':input[name="wysiwyg"]').on('change', function(){
  ...
});

timint avatar Jan 26 '22 19:01 timint

The initial element can be a div or anything, so I can't move to just 'input' but yes, maybe its name is wrong now ^^

Alex-D avatar Nov 14 '22 15:11 Alex-D

Hello, I did a workaround:

let timeOutId;
let lastValue;
// delay change notification during user editing
var delay = 300;
var editor = $(':input[name="wysiwyg"]');

editor.on('tbwchange',function(){
 clearTimeout(timeOutId);
 timeOutId = setTimeout(() => {
  var curValue = editor.trumbowyg('html');
  // check if it was modified
  if (!lastValue || lastValue.length != curValue.length || lastValue !== curValue) {
    lastValue = curValue;
    // YOUR CODE HERE
  }
 }, delay);
});

Version without LAZY:

let lastValue;
var editor = $(':input[name="wysiwyg"]');

editor.on('tbwchange',function(){
  var curValue = editor.trumbowyg('html');
  // check if it was modified
  if (!lastValue || lastValue.length != curValue.length || lastValue !== curValue) {
    lastValue = curValue;
    // YOUR CODE HERE
  }
});

dyorgio avatar Mar 10 '23 14:03 dyorgio

What is the goal of this workaround? Is that to squash multiple tbwchange triggers in a single call?

Alex-D avatar Mar 11 '23 16:03 Alex-D

Hi @Alex-D, yes.

The first version is the real workaround, because also include delayed support (don’t call code multiple during user fast input).

Both only call code if there is a real change on contents.

dyorgio avatar Mar 11 '23 19:03 dyorgio

@Alex-D I would like to include an explanation about motivation.

When you use Frontend components that binds with backend with statefull session you probably don’t want so much calls to server.

the code above tries to reduce it.

dyorgio avatar Mar 11 '23 19:03 dyorgio

It looks like the tbwchange event is working as a click event. Every time we click on the text area, it got triggered. The tbwchange event triggers after every word we type.

Manaspaul623 avatar Aug 21 '23 09:08 Manaspaul623