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

'keyup' method not being called correctly with wysihtml. #74

Closed
jengel3 opened this issue Aug 19, 2014 · 4 comments
Closed

'keyup' method not being called correctly with wysihtml. #74

jengel3 opened this issue Aug 19, 2014 · 4 comments

Comments

@jengel3
Copy link

jengel3 commented Aug 19, 2014

I have code such as:

    $(text).keyup(function() {
      console.log("called");
      updateCountdown(text, countdown, chars);
    });

which I use to count characters in a text box. However, once adding the 'wysihtml5' class to the textbox, it is no longer called, even though it worked perfectly fine without it. Is it possible that wysi is intercepting this regular method, and not dispatching a new one?

@jengel3 jengel3 closed this as completed Aug 19, 2014
@kasoban
Copy link

kasoban commented Aug 19, 2014

I guess you figured this out yourself since you closed the ticket, but let's explain this for anyone stumbling upon this through a search:

The event handlers on the textbox will not get called due to wysihtml creating a contenteditable iframe copied from the textbox attributes and hiding the actual textbox, so there won't be actual user input to the textbox.

@jengel3
Copy link
Author

jengel3 commented Aug 19, 2014

I actually ended up submitting this to the wrong version of wysi, but they are fairly similar, and I'd be willing to use any version that I can get to work properly. How would I go about tracking user input instead of the way I'm doing it now?

@kasoban
Copy link

kasoban commented Aug 20, 2014

Well, you submitted it to Waxolunist/bootstrap3-wysihtml5-bower which is effectively based on this fork.
Anyway, to access the editor you can do the following:

After you initialized the editor on an element by calling element.wysihtml5(), the initialized editor is stored as a data field for that element. So you can get a hold of it through element.data('wysihtml5').editor for example, if you have jQuery available. From there on you could apply a listener like element.data('wysihtml5').editor.on('change', function() {doSomething;}) or the like.

Here is a list of events exposed by the editor:
https://github.com/xing/wysihtml5/wiki/Events
At the very bottom there is an example for a keydown listener.

@priithaamer
Copy link

The event system in wysihtml5x needs an overhaul as the change event does not work properly. Until then we're using a combination of change event listeners and mutation observers to get proper events about changes in editable element. Please note that the following works when using contentEditable version of wysihtml. I have not tested it with iframes:

var el = document.getElementById('editor'),
  editor = new wysihtml5.Editor(el, {});

var handleContentMutation = function(mutations) {
  var mutation = _.some(mutations, function(mutation) {
    return !(mutation.type == 'attributes' && mutation.target == el);
  });

  if (mutation) {
    // It is advised to defer the invocation of this method here, i.e. by
    // using _.defer() from Underscore
    handleContentChange();
  }
}

var handleContentChange = function() {
  // Well, content has been changed now...
};

el.addEventListener('input', handleContentChange);

// Listen for change events on editor in case MutationObserver is not present.
editor.addEventListener('change', handleContentChange);

if (window.MutationObserver) {
  var observer = new MutationObserver(handleContentMutation);
  observer.observe(el, {attributes: true, childList: true, characterData: true});
}

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

No branches or pull requests

3 participants