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

Add fix for Chinese and Japanese characters #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 36 additions & 3 deletions views/wysiwyg_editor_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ SC.WYSIWYGEditorView = SC.View.extend({
*/
isEnabled: YES,

/**
The composition of a passage of text is in progress

This is true when compositionstart has been fired but compositionend
has not yet been fired.

@type Boolean
@default NO
*/
compositionInProgress: NO,

/**
A padding, in pixels, that is added to the editor element.

Expand Down Expand Up @@ -236,6 +247,27 @@ SC.WYSIWYGEditorView = SC.View.extend({
this.notifyDomValueChange();
SC.RunLoop.end();
});

SC.Event.add(this.$inner, 'compositionstart', this, function() {
this.set('compositionInProgress', YES);
});

SC.Event.add(this.$inner, 'compositionend', this, function() {
this.set('compositionInProgress', NO);
var inner = this.$inner,
contents = inner.contents(),
first = contents[0],
last = contents[contents.length - 1],
forceLineBreaks = this.get('forceLineBreaks');
// This is usually called in keyUp but its possible that compositionEnd is called without keyUp.
// If the first content is a text node, or a line break (and we're not forcing <br> mode), let's wrap it up.
if (!forceLineBreaks && first && (first.nodeType === 3 /* TEXT NODE */ || first.nodeName === "BR")) {
document.execCommand('formatBlock', false, 'p');
}
else if (!forceLineBreaks && last && (last.nodeType === 3 /* TEXT NODE */ || last.nodeName === "BR")) {
document.execCommand('formatBlock', false, 'p');
}
});
},

/** @private */
Expand Down Expand Up @@ -879,12 +911,13 @@ SC.WYSIWYGEditorView = SC.View.extend({
contents = inner.contents(),
first = contents[0],
last = contents[contents.length - 1],
forceLineBreaks = this.get('forceLineBreaks');
forceLineBreaks = this.get('forceLineBreaks'),
compositionInProgress = this.get('compositionInProgress');
// If the first content is a text node, or a line break (and we're not forcing <br> mode), let's wrap it up.
if (!forceLineBreaks && first && (first.nodeType === 3 /* TEXT NODE */ || first.nodeName === "BR")) {
if (!compositionInProgress && !forceLineBreaks && first && (first.nodeType === 3 /* TEXT NODE */ || first.nodeName === "BR")) {
document.execCommand('formatBlock', false, 'p');
}
else if (!forceLineBreaks && last && (last.nodeType === 3 /* TEXT NODE */ || last.nodeName === "BR")) {
else if (!compositionInProgress && !forceLineBreaks && last && (last.nodeType === 3 /* TEXT NODE */ || last.nodeName === "BR")) {
document.execCommand('formatBlock', false, 'p');
}
// In Webkit, we need to make sure that the very last <br> element, which is removed when we type above it,
Expand Down