Skip to content

Commit ac1a4af

Browse files
committed
Squashed 'libs/editor/' changes from abfcce5..17887d3
17887d3 Merge pull request #354 from wordpress-mobile/issue/230-first-char-zw-space c5c0593 Added 'grave accent' keyCode to list of keyCodes ignored by paragraph handler 4156c10 Merge pull request #356 from wordpress-mobile/issue/353-title-html-entities e6344a1 Return plaintext for the title field in getHTMLForCallback, to avoid encoding HTML entities 4e38e7d Fixed hardcoded ZSSField name in handleKeyDownEvent f63c4fa Corrected a comment 6b1fb19 Merge branch 'develop' into issue/230-first-char-zw-space d066fbe On API 19, don't wrap the first line in paragraph tags - wait until first newline ecf7e40 s/wrappedDomNode()/getWrappedDomNode()/ 56eff9b Added editor-utils.js with some utility tag builder functions for ZSSEditor 9ec61ed Refactor ZSSEditor.resetSelectionOnField to accept an offset value for the range f4c135b Don't handle paragraph wrapping for non-printable keyCodes (hardware keyboards) git-subtree-dir: libs/editor git-subtree-split: 17887d367d19e3f94f6f0be18db334a38fa60899
1 parent be4aac0 commit ac1a4af

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

libs/editor-common/assets/ZSSRichTextEditor.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,14 @@ ZSSEditor.restoreRange = function(){
386386
}
387387
};
388388

389-
ZSSEditor.resetSelectionOnField = function(fieldId) {
389+
ZSSEditor.resetSelectionOnField = function(fieldId, offset) {
390+
offset = typeof offset !== 'undefined' ? offset : 0;
391+
390392
var query = "div#" + fieldId;
391393
var field = document.querySelector(query);
392394
var range = document.createRange();
393-
range.setStart(field, 0);
394-
range.setEnd(field, 0);
395+
range.setStart(field, offset);
396+
range.setEnd(field, offset);
395397

396398
var selection = document.getSelection();
397399
selection.removeAllRanges();
@@ -554,12 +556,6 @@ ZSSEditor.getYCaretInfo = function() {
554556
return this.caretInfo;
555557
};
556558

557-
// MARK: - Default paragraph separator
558-
559-
ZSSEditor.defaultParagraphSeparatorTag = function() {
560-
return '<' + this.defaultParagraphSeparator + '>';
561-
};
562-
563559
// MARK: - Styles
564560

565561
ZSSEditor.setBold = function() {
@@ -638,7 +634,7 @@ ZSSEditor.setUnderline = function() {
638634
/**
639635
* @brief Turns blockquote ON or OFF for the current selection.
640636
* @details This method makes sure that the contents of the blockquotes are surrounded by the
641-
* defaultParagraphSeparatorTag (by default '<p>'). This ensures parity with the web
637+
* defaultParagraphSeparator tag (by default '<p>'). This ensures parity with the web
642638
* editor.
643639
*/
644640
ZSSEditor.setBlockquote = function() {
@@ -682,9 +678,9 @@ ZSSEditor.setHeading = function(heading) {
682678
var formatBlock = document.queryCommandValue('formatBlock');
683679

684680
if (formatBlock.length > 0 && formatBlock.toLowerCase() == formatTag) {
685-
document.execCommand('formatBlock', false, this.defaultParagraphSeparatorTag());
681+
document.execCommand('formatBlock', false, Util.buildOpeningTag(this.defaultParagraphSeparator));
686682
} else {
687-
document.execCommand('formatBlock', false, '<' + formatTag + '>');
683+
document.execCommand('formatBlock', false, Util.buildOpeningTag(formatTag));
688684
}
689685

690686
ZSSEditor.sendEnabledStyles();
@@ -695,9 +691,9 @@ ZSSEditor.setParagraph = function() {
695691
var formatBlock = document.queryCommandValue('formatBlock');
696692

697693
if (formatBlock.length > 0 && formatBlock.toLowerCase() == formatTag) {
698-
document.execCommand('formatBlock', false, this.defaultParagraphSeparatorTag());
694+
document.execCommand('formatBlock', false, Util.buildOpeningTag(this.defaultParagraphSeparator));
699695
} else {
700-
document.execCommand('formatBlock', false, '<' + formatTag + '>');
696+
document.execCommand('formatBlock', false, Util.buildOpeningTag(formatTag));
701697
}
702698

703699
ZSSEditor.sendEnabledStyles();
@@ -790,8 +786,8 @@ ZSSEditor.setBackgroundColor = function(color) {
790786
*/
791787
ZSSEditor.insertHTMLWrappedInParagraphTags = function(html) {
792788
var space = '<br>';
793-
var paragraphOpenTag = '<' + this.defaultParagraphSeparator + '>';
794-
var paragraphCloseTag = '</' + this.defaultParagraphSeparator + '>';
789+
var paragraphOpenTag = Util.buildOpeningTag(this.defaultParagraphSeparator);
790+
var paragraphCloseTag = Util.buildClosingTag(this.defaultParagraphSeparator);
795791

796792
if (this.getFocusedField().getHTML().length == 0) {
797793
html = paragraphOpenTag + html;
@@ -816,7 +812,7 @@ ZSSEditor.insertLink = function(url, title) {
816812
var html = '<a href="' + url + '">' + title + "</a>";
817813

818814
if (this.getFocusedField().getHTML().length == 0) {
819-
html = '<' + this.defaultParagraphSeparator + '>' + html;
815+
html = Util.buildOpeningTag(this.defaultParagraphSeparator) + html;
820816
}
821817

822818
this.insertHTML(html);
@@ -2785,7 +2781,7 @@ ZSSEditor.joinAdjacentSiblingsBlockquotes = function(node) {
27852781
ZSSEditor.joinAdjacentSiblingsOrAncestorBlockquotes = function(node) {
27862782

27872783
var currentNode = node;
2788-
var rootNode = this.getFocusedField().wrappedDomNode();
2784+
var rootNode = this.getFocusedField().getWrappedDomNode();
27892785
var joined = false;
27902786

27912787
while (currentNode
@@ -3008,7 +3004,7 @@ function ZSSField(wrappedObject) {
30083004
this.multiline = false;
30093005
this.wrappedObject = wrappedObject;
30103006

3011-
if (this.wrappedDomNode().hasAttribute('nostyle')) {
3007+
if (this.getWrappedDomNode().hasAttribute('nostyle')) {
30123008
this.hasNoStyle = true;
30133009
}
30143010

@@ -3080,9 +3076,35 @@ ZSSField.prototype.handleKeyDownEvent = function(e) {
30803076
} else if (wasEnterPressed && !this.isMultiline()) {
30813077
e.preventDefault();
30823078
} else if (this.isMultiline()) {
3083-
this.wrapCaretInParagraphIfNecessary();
3079+
// For hardware keyboards, don't do any paragraph handling for non-printable keyCodes
3080+
// https://css-tricks.com/snippets/javascript/javascript-keycodes/
3081+
// This avoids the filler zero-width space character from being inserted and displayed in the content field
3082+
// when special keys are pressed in new posts
3083+
var wasTabPressed = (e.keyCode == '9');
3084+
var intKeyCode = parseInt(e.keyCode, 10);
3085+
if (wasTabPressed || (intKeyCode > 13 && intKeyCode < 46) || intKeyCode == 192) {
3086+
return;
3087+
}
3088+
3089+
// This is intended to work around an API19-only bug where paragraph wrapping the first character in a post
3090+
// will display a zero-width space character (from ZSSField.wrapCaretInParagraphIfNecessary)
3091+
// We can drop the if statement wrapping wrapCaretInParagraphIfNecessary() if we find a way to stop using
3092+
// zero-width space characters (e.g., autocorrect issues are fixed and we switch back to p tags)
3093+
var containsParagraphSeparators = this.getWrappedDomNode().innerHTML.search(
3094+
'<' + ZSSEditor.defaultParagraphSeparator) > -1;
3095+
if (nativeState.androidApiLevel != 19 || containsParagraphSeparators) {
3096+
this.wrapCaretInParagraphIfNecessary();
3097+
}
30843098

30853099
if (wasEnterPressed) {
3100+
// Wrap the existing text in paragraph tags if necessary (this should only be needed if
3101+
// wrapCaretInParagraphIfNecessary() was skipped earlier (API19))
3102+
var currentHtml = this.getWrappedDomNode().innerHTML;
3103+
if (currentHtml.search('<' + ZSSEditor.defaultParagraphSeparator) == -1) {
3104+
ZSSEditor.focusedField.setHTML(Util.wrapHTMLInTag(currentHtml, ZSSEditor.defaultParagraphSeparator));
3105+
ZSSEditor.resetSelectionOnField(this.getWrappedDomNode().id, 1);
3106+
}
3107+
30863108
var sel = window.getSelection();
30873109
if (sel.rangeCount < 1) {
30883110
return null;
@@ -3358,7 +3380,7 @@ ZSSField.prototype.disableEditing = function () {
33583380
ZSSField.prototype.wrapCaretInParagraphIfNecessary = function()
33593381
{
33603382
var closerParentNode = ZSSEditor.closerParentNode();
3361-
var parentNodeShouldBeParagraph = (closerParentNode == this.wrappedDomNode()
3383+
var parentNodeShouldBeParagraph = (closerParentNode == this.getWrappedDomNode()
33623384
|| closerParentNode.nodeName == NodeName.BLOCKQUOTE);
33633385

33643386
if (parentNodeShouldBeParagraph) {
@@ -3419,7 +3441,14 @@ ZSSField.prototype.getHTML = function() {
34193441
ZSSField.prototype.getHTMLForCallback = function() {
34203442
var functionArgument = "function=getHTMLForCallback";
34213443
var idArgument = "id=" + this.getNodeId();
3422-
var contentsArgument = "contents=" + this.getHTML();
3444+
var contentsArgument;
3445+
3446+
if (this.hasNoStyle) {
3447+
contentsArgument = "contents=" + this.strippedHTML();
3448+
} else {
3449+
contentsArgument = "contents=" + this.getHTML();
3450+
}
3451+
34233452
var joinedArguments = functionArgument + defaultCallbackSeparator + idArgument + defaultCallbackSeparator +
34243453
contentsArgument;
34253454
ZSSEditor.callback('callback-response-string', joinedArguments);
@@ -3459,6 +3488,6 @@ ZSSField.prototype.setPlaceholderText = function(placeholder) {
34593488

34603489
// MARK: - Wrapped Object
34613490

3462-
ZSSField.prototype.wrappedDomNode = function() {
3491+
ZSSField.prototype.getWrappedDomNode = function() {
34633492
return this.wrappedObject[0];
34643493
};

libs/editor-common/assets/android-editor.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="underscore-min.js"></script>
1414
<script src="shortcode.js"></script>
1515
<script src="jquery.mobile-events.min.js"></script>
16+
<script src="editor-utils.js"></script>
1617
<script src="ZSSRichTextEditor.js"></script>
1718
<script src="wpload.js"></script>
1819
<script src="wpsave.js"></script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function Util () {}
2+
3+
Util.buildOpeningTag = function(tagName) {
4+
return '<' + tagName + '>';
5+
};
6+
7+
Util.buildClosingTag = function(tagName) {
8+
return '</' + tagName + '>';
9+
};
10+
11+
Util.wrapHTMLInTag = function(html, tagName) {
12+
return Util.buildOpeningTag(tagName) + html + Util.buildClosingTag(tagName);
13+
};

0 commit comments

Comments
 (0)