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

Fixed: onChange event doesn't work in IE11 #135

Open
wants to merge 1 commit 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
44 changes: 41 additions & 3 deletions dist/pell.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument

var defaultParagraphSeparatorString = 'defaultParagraphSeparator';
var formatBlock = 'formatBlock';

var debounce = function debounce(fn, time) {
var timeout = void 0;

return function () {
var _this = this,
_arguments = arguments;

var functionCall = function functionCall() {
return fn.apply(_this, _arguments);
};

clearTimeout(timeout);
timeout = setTimeout(functionCall, time);
};
};
var getMutationObserver = function getMutationObserver(elementSelector, callback) {
var observer = new MutationObserver(callback);
var config = {
attributes: true,
childList: true,
characterData: true,
subtree: true
};
observer.observe(elementSelector, config);

return observer;
};
var addEventListener = function addEventListener(parent, type, listener) {
return parent.addEventListener(type, listener);
};
Expand Down Expand Up @@ -170,12 +198,22 @@ var init = function init(settings) {
var content = settings.element.content = createElement('div');
content.contentEditable = true;
content.className = classes.content;
content.oninput = function (_ref) {
var firstChild = _ref.target.firstChild;

if (firstChild && firstChild.nodeType === 3) exec(formatBlock, '<' + defaultParagraphSeparator + '>');else if (content.innerHTML === '<br>') content.innerHTML = '';
var divOnInputChange = function divOnInputChange(observer) {
var firstChild = observer[0].target.firstChild;

if (firstChild && firstChild.nodeType === 3) {
exec(formatBlock, '<' + defaultParagraphSeparator + '>');
} else if (content.innerHTML === '<br>') {
content.innerHTML = '';
}
settings.onChange(content.innerHTML);
};

// Observe any changes on content block (work on IE)
// Rate-limit event handling with debounce to reduce performance impact
getMutationObserver(content, debounce(divOnInputChange, 250));

content.onkeydown = function (event) {
if (event.key === 'Tab') {
event.preventDefault();
Expand Down
2 changes: 1 addition & 1 deletion dist/pell.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 37 additions & 3 deletions src/pell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const defaultParagraphSeparatorString = 'defaultParagraphSeparator'
const formatBlock = 'formatBlock'

const debounce = (fn, time) => {
let timeout

return function() {
const functionCall = () => fn.apply(this, arguments);

clearTimeout(timeout)
timeout = setTimeout(functionCall, time)
}
}
const getMutationObserver = (elementSelector, callback) => {
const observer = new MutationObserver(callback)
const config = {
attributes: true,
childList: true,
characterData: true,
subtree: true
}
observer.observe(elementSelector, config)

return observer
}
const addEventListener = (parent, type, listener) => parent.addEventListener(type, listener)
const appendChild = (parent, child) => parent.appendChild(child)
const createElement = tag => document.createElement(tag)
Expand Down Expand Up @@ -120,11 +143,22 @@ export const init = settings => {
const content = settings.element.content = createElement('div')
content.contentEditable = true
content.className = classes.content
content.oninput = ({ target: { firstChild } }) => {
if (firstChild && firstChild.nodeType === 3) exec(formatBlock, `<${defaultParagraphSeparator}>`)
else if (content.innerHTML === '<br>') content.innerHTML = ''

const divOnInputChange = (observer) => {
const firstChild = observer[0].target.firstChild;

if (firstChild && firstChild.nodeType === 3) {
exec(formatBlock, '<' + defaultParagraphSeparator + '>')
} else if (content.innerHTML === '<br>') {
content.innerHTML = ''
}
settings.onChange(content.innerHTML)
}

// Observe any changes on content block (work on IE)
// Rate-limit event handling with debounce to reduce performance impact
getMutationObserver(content, debounce(divOnInputChange, 250))

content.onkeydown = event => {
if (event.key === 'Tab') {
event.preventDefault()
Expand Down