[go: nahoru, domu]

Skip to content

Commit

Permalink
Squashed 'libs/editor/' changes from abfcce5..17887d3
Browse files Browse the repository at this point in the history
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
  • Loading branch information
maxme committed Apr 21, 2016
1 parent be4aac0 commit ac1a4af
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
75 changes: 52 additions & 23 deletions libs/editor-common/assets/ZSSRichTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,14 @@ ZSSEditor.restoreRange = function(){
}
};

ZSSEditor.resetSelectionOnField = function(fieldId) {
ZSSEditor.resetSelectionOnField = function(fieldId, offset) {
offset = typeof offset !== 'undefined' ? offset : 0;

var query = "div#" + fieldId;
var field = document.querySelector(query);
var range = document.createRange();
range.setStart(field, 0);
range.setEnd(field, 0);
range.setStart(field, offset);
range.setEnd(field, offset);

var selection = document.getSelection();
selection.removeAllRanges();
Expand Down Expand Up @@ -554,12 +556,6 @@ ZSSEditor.getYCaretInfo = function() {
return this.caretInfo;
};

// MARK: - Default paragraph separator

ZSSEditor.defaultParagraphSeparatorTag = function() {
return '<' + this.defaultParagraphSeparator + '>';
};

// MARK: - Styles

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

if (formatBlock.length > 0 && formatBlock.toLowerCase() == formatTag) {
document.execCommand('formatBlock', false, this.defaultParagraphSeparatorTag());
document.execCommand('formatBlock', false, Util.buildOpeningTag(this.defaultParagraphSeparator));
} else {
document.execCommand('formatBlock', false, '<' + formatTag + '>');
document.execCommand('formatBlock', false, Util.buildOpeningTag(formatTag));
}

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

if (formatBlock.length > 0 && formatBlock.toLowerCase() == formatTag) {
document.execCommand('formatBlock', false, this.defaultParagraphSeparatorTag());
document.execCommand('formatBlock', false, Util.buildOpeningTag(this.defaultParagraphSeparator));
} else {
document.execCommand('formatBlock', false, '<' + formatTag + '>');
document.execCommand('formatBlock', false, Util.buildOpeningTag(formatTag));
}

ZSSEditor.sendEnabledStyles();
Expand Down Expand Up @@ -790,8 +786,8 @@ ZSSEditor.setBackgroundColor = function(color) {
*/
ZSSEditor.insertHTMLWrappedInParagraphTags = function(html) {
var space = '<br>';
var paragraphOpenTag = '<' + this.defaultParagraphSeparator + '>';
var paragraphCloseTag = '</' + this.defaultParagraphSeparator + '>';
var paragraphOpenTag = Util.buildOpeningTag(this.defaultParagraphSeparator);
var paragraphCloseTag = Util.buildClosingTag(this.defaultParagraphSeparator);

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

if (this.getFocusedField().getHTML().length == 0) {
html = '<' + this.defaultParagraphSeparator + '>' + html;
html = Util.buildOpeningTag(this.defaultParagraphSeparator) + html;
}

this.insertHTML(html);
Expand Down Expand Up @@ -2785,7 +2781,7 @@ ZSSEditor.joinAdjacentSiblingsBlockquotes = function(node) {
ZSSEditor.joinAdjacentSiblingsOrAncestorBlockquotes = function(node) {

var currentNode = node;
var rootNode = this.getFocusedField().wrappedDomNode();
var rootNode = this.getFocusedField().getWrappedDomNode();
var joined = false;

while (currentNode
Expand Down Expand Up @@ -3008,7 +3004,7 @@ function ZSSField(wrappedObject) {
this.multiline = false;
this.wrappedObject = wrappedObject;

if (this.wrappedDomNode().hasAttribute('nostyle')) {
if (this.getWrappedDomNode().hasAttribute('nostyle')) {
this.hasNoStyle = true;
}

Expand Down Expand Up @@ -3080,9 +3076,35 @@ ZSSField.prototype.handleKeyDownEvent = function(e) {
} else if (wasEnterPressed && !this.isMultiline()) {
e.preventDefault();
} else if (this.isMultiline()) {
this.wrapCaretInParagraphIfNecessary();
// For hardware keyboards, don't do any paragraph handling for non-printable keyCodes
// https://css-tricks.com/snippets/javascript/javascript-keycodes/
// This avoids the filler zero-width space character from being inserted and displayed in the content field
// when special keys are pressed in new posts
var wasTabPressed = (e.keyCode == '9');
var intKeyCode = parseInt(e.keyCode, 10);
if (wasTabPressed || (intKeyCode > 13 && intKeyCode < 46) || intKeyCode == 192) {
return;
}

// This is intended to work around an API19-only bug where paragraph wrapping the first character in a post
// will display a zero-width space character (from ZSSField.wrapCaretInParagraphIfNecessary)
// We can drop the if statement wrapping wrapCaretInParagraphIfNecessary() if we find a way to stop using
// zero-width space characters (e.g., autocorrect issues are fixed and we switch back to p tags)
var containsParagraphSeparators = this.getWrappedDomNode().innerHTML.search(
'<' + ZSSEditor.defaultParagraphSeparator) > -1;
if (nativeState.androidApiLevel != 19 || containsParagraphSeparators) {
this.wrapCaretInParagraphIfNecessary();
}

if (wasEnterPressed) {
// Wrap the existing text in paragraph tags if necessary (this should only be needed if
// wrapCaretInParagraphIfNecessary() was skipped earlier (API19))
var currentHtml = this.getWrappedDomNode().innerHTML;
if (currentHtml.search('<' + ZSSEditor.defaultParagraphSeparator) == -1) {
ZSSEditor.focusedField.setHTML(Util.wrapHTMLInTag(currentHtml, ZSSEditor.defaultParagraphSeparator));
ZSSEditor.resetSelectionOnField(this.getWrappedDomNode().id, 1);
}

var sel = window.getSelection();
if (sel.rangeCount < 1) {
return null;
Expand Down Expand Up @@ -3358,7 +3380,7 @@ ZSSField.prototype.disableEditing = function () {
ZSSField.prototype.wrapCaretInParagraphIfNecessary = function()
{
var closerParentNode = ZSSEditor.closerParentNode();
var parentNodeShouldBeParagraph = (closerParentNode == this.wrappedDomNode()
var parentNodeShouldBeParagraph = (closerParentNode == this.getWrappedDomNode()
|| closerParentNode.nodeName == NodeName.BLOCKQUOTE);

if (parentNodeShouldBeParagraph) {
Expand Down Expand Up @@ -3419,7 +3441,14 @@ ZSSField.prototype.getHTML = function() {
ZSSField.prototype.getHTMLForCallback = function() {
var functionArgument = "function=getHTMLForCallback";
var idArgument = "id=" + this.getNodeId();
var contentsArgument = "contents=" + this.getHTML();
var contentsArgument;

if (this.hasNoStyle) {
contentsArgument = "contents=" + this.strippedHTML();
} else {
contentsArgument = "contents=" + this.getHTML();
}

var joinedArguments = functionArgument + defaultCallbackSeparator + idArgument + defaultCallbackSeparator +
contentsArgument;
ZSSEditor.callback('callback-response-string', joinedArguments);
Expand Down Expand Up @@ -3459,6 +3488,6 @@ ZSSField.prototype.setPlaceholderText = function(placeholder) {

// MARK: - Wrapped Object

ZSSField.prototype.wrappedDomNode = function() {
ZSSField.prototype.getWrappedDomNode = function() {
return this.wrappedObject[0];
};
1 change: 1 addition & 0 deletions libs/editor-common/assets/android-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<script src="underscore-min.js"></script>
<script src="shortcode.js"></script>
<script src="jquery.mobile-events.min.js"></script>
<script src="editor-utils.js"></script>
<script src="ZSSRichTextEditor.js"></script>
<script src="wpload.js"></script>
<script src="wpsave.js"></script>
Expand Down
13 changes: 13 additions & 0 deletions libs/editor-common/assets/editor-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function Util () {}

Util.buildOpeningTag = function(tagName) {
return '<' + tagName + '>';
};

Util.buildClosingTag = function(tagName) {
return '</' + tagName + '>';
};

Util.wrapHTMLInTag = function(html, tagName) {
return Util.buildOpeningTag(tagName) + html + Util.buildClosingTag(tagName);
};

0 comments on commit ac1a4af

Please sign in to comment.