Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Debounce input event of editor container #519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @infusionsoft/pi
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ AMD syntax example as well as a CommonJS (browserify) example.
<dd>Defines which of Scribe's built-in plugins should be active</dd>
<dt><pre>defaultFormatters</pre></dt>
<dd>Defines which of Scribe's default formatters should be active</dd>
<dt><pre>inputDelay</pre></dt>
<dd>Defines the debounce delay for input events on editor container (default is 0)</dd>
</dl>

For detailed documentation see the [wiki page on options](https://github.com/guardian/scribe/wiki/Scribe-configuration-options).
Expand Down
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ define(['immutable'], function (immutable) {
defaultFormatters: [
'escapeHtmlCharactersFormatter',
'replaceNbspCharsFormatter'
]
],

inputDelay: 0
};


Expand Down
26 changes: 26 additions & 0 deletions src/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define([], function () {
return 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);
}
};
};
});
10 changes: 6 additions & 4 deletions src/scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ define([
'./node',
'immutable',
'./config',
'./events'
'./events',
'./debounce'
], function (
plugins,
commands,
Expand All @@ -25,7 +26,8 @@ define([
nodeHelpers,
Immutable,
config,
eventNames
eventNames,
debounce
) {

'use strict';
Expand Down Expand Up @@ -68,15 +70,15 @@ define([

this.el.setAttribute('contenteditable', true);

this.el.addEventListener('input', function () {
this.el.addEventListener('input', debounce(function () {
/**
* This event triggers when either the user types something or a native
* command is executed which causes the content to change (i.e.
* `document.execCommand('bold')`). We can't wrap a transaction around
* these actions, so instead we run the transaction in this event.
*/
this.transactionManager.run();
}.bind(this), false);
}.bind(this), this.options.inputDelay).bind(this), false);

/**
* Core Plugins
Expand Down