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

IME composition handling, fixes #138 #139

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
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h3>HTML output:</h3>
<script>
var editor = window.pell.init({
element: document.getElementById('pell'),
defaultParagraphSeparator: 'p',
defaultParagraphSeparator: 'div',
styleWithCSS: false,
onChange: function (html) {
document.getElementById('text-output').innerHTML = html
Expand Down
29 changes: 26 additions & 3 deletions dist/pell.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,35 @@ 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 formatIfTextNode = function formatIfTextNode(node) {
if (node && node.nodeType === 3) exec(formatBlock, '<' + defaultParagraphSeparator + '>');else if (content.innerHTML === '<br>') content.innerHTML = '';
settings.onChange(content.innerHTML);
};

/*
* Prevent formatting text before IME composition ended
* (which breaks composition of Chinese characters for example)
* https://developer.mozilla.org/en-US/docs/Mozilla/IME_handling_guide
**/
var isComposing = false;
addEventListener(content, 'compositionstart', function () {
isComposing = true;
});
addEventListener(content, 'compositionend', function (_ref) {
var firstChild = _ref.target.firstChild;

isComposing = false;
formatIfTextNode(firstChild);
});

content.oninput = function (_ref2) {
var firstChild = _ref2.target.firstChild;

if (!isComposing) {
formatIfTextNode(firstChild);
}
};
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.

23 changes: 21 additions & 2 deletions src/pell.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,30 @@ 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}>`)

const formatIfTextNode = node => {
if (node && node.nodeType === 3) exec(formatBlock, `<${defaultParagraphSeparator}>`)
else if (content.innerHTML === '<br>') content.innerHTML = ''
settings.onChange(content.innerHTML)
}

/*
* Prevent formatting text before IME composition ended
* (which breaks composition of Chinese characters for example)
* https://developer.mozilla.org/en-US/docs/Mozilla/IME_handling_guide
**/
let isComposing = false
addEventListener(content, 'compositionstart', () => { isComposing = true })
addEventListener(content, 'compositionend', ({ target: { firstChild } }) => {
isComposing = false
formatIfTextNode(firstChild)
})

content.oninput = ({ target: { firstChild } }) => {
if (!isComposing) {
formatIfTextNode(firstChild)
}
}
content.onkeydown = event => {
if (event.key === 'Tab') {
event.preventDefault()
Expand Down