Permalink
Browse files

Merge pull request #1978 from avif/bugfix/#1977

Fixed #1977.
  • Loading branch information...
2 parents 42cff0d + 669f35b commit fe883c0cca3f827b7b342ff0997ee855e9ddca1c @hackerwins hackerwins committed on GitHub Aug 7, 2016
@@ -116,6 +116,35 @@ define(function () {
}).join('');
};
+ /**
+ * Returns a function, that, as long as it continues to be invoked, will not
+ * be triggered. The function will be called after it stops being called for
+ * N milliseconds. If `immediate` is passed, trigger the function on the
+ * leading edge, instead of the trailing.
+ * @param {Function} func
+ * @param {Number} wait
+ * @param {Boolean} immediate
+ * @return {Function}
+ */
+ var debounce = function (func, wait, immediate) {
+ var timeout;
+ return function () {
+ var context = this, args = arguments;
+ var later = function () {
+ timeout = null;
+ if (!immediate) {
+ func.apply(context, args);
+ }
+ };
+ var callNow = immediate && !timeout;
+ clearTimeout(timeout);
+ timeout = setTimeout(later, wait);
+ if (callNow) {
+ func.apply(context, args);
+ }
+ };
+ };
+
return {
eq: eq,
eq2: eq2,
@@ -129,7 +158,8 @@ define(function () {
uniqueId: uniqueId,
rect2bnd: rect2bnd,
invertObject: invertObject,
- namespaceToCamel: namespaceToCamel
+ namespaceToCamel: namespaceToCamel,
+ debounce: debounce
};
})();
@@ -76,9 +76,9 @@ define([
// [workaround] IE doesn't have input events for contentEditable
// - see: https://goo.gl/4bfIvA
var changeEventName = agent.isMSIE ? 'DOMCharacterDataModified DOMSubtreeModified DOMNodeInserted' : 'input';
- $editable.on(changeEventName, function () {
+ $editable.on(changeEventName, func.debounce(function () {
context.triggerEvent('change', $editable.html());
- });
+ }, 250));
$editor.on('focusin', function (event) {
context.triggerEvent('focusin', event);
@@ -96,5 +96,48 @@ define([
expect(func.namespaceToCamel('upload.image', 'summernote')).to.be.equal('summernoteUploadImage');
});
});
+
+ describe('debounce', function () {
+ it('shouldnt execute immediately', function () {
+ var hasHappened = false;
+ var fn = func.debounce(function () {
+ hasHappened = true;
+ }, 100);
+
+ expect(hasHappened).to.be.false;
+ fn();
+ expect(hasHappened).to.be.false;
+ });
+
+ it('should execute after delay', function (done) {
+ var hasHappened = false;
+ var fn = func.debounce(function () {
+ hasHappened = true;
+ }, 100);
+
+ fn();
+
+ setTimeout(function () {
+ expect(hasHappened).to.be.true;
+ done();
+ }, 101);
+ });
+
+ it('should only happen once', function (done) {
+ var count = 0;
+ var fn = func.debounce(function () {
+ count += 1;
+ }, 100);
+
+ fn();
+ fn();
+ fn();
+
+ setTimeout(function () {
+ expect(count).to.be.equal(1);
+ done();
+ }, 101);
+ });
+ });
});
});
@@ -152,6 +152,23 @@ define([
editor.pasteHTML('<span> world</span>');
expectContents(context, '<p>hello<span> world</span></p>');
});
+
+ it('should not call change change event more than once per paste event', function () {
+ var generateLargeHtml = function () {
+ var html = '<div>';
+ for (var i = 0; i < 1000; i++) {
+ html += '<p>HTML element #' + i + '</p>';
+ }
+ html += '</div>';
+ return html;
+ };
+ var $note = context.layoutInfo.note;
+ var spy = chai.spy();
+ $note.on('summernote.change', spy);
+ var html = generateLargeHtml();
+ editor.pasteHTML(html);
+ expect(spy).to.have.been.called.once;
+ });
});
describe('insertHorizontalRule', function () {

0 comments on commit fe883c0

Please sign in to comment.