Skip to content
This repository was archived by the owner on Dec 13, 2017. It is now read-only.

Commit 5214d42

Browse files
Merge pull request #841 from wordpress-mobile/issue/839-simplifies-paragraph-wrapping
Simplifies paragraph wrapping
2 parents 61db829 + 85012d0 commit 5214d42

1 file changed

Lines changed: 57 additions & 48 deletions

File tree

Assets/ZSSRichTextEditor.js

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,12 @@ ZSSEditor.setBlockquote = function() {
457457
var sendStyles = false;
458458

459459
var ancestorElement = this.getAncestorElementForSettingBlockquote(range);
460-
460+
var focusedField = this.getFocusedField();
461+
462+
if (ancestorElement == focusedField.wrappedDomNode()) {
463+
focusedField.wrapCaretInParagraphIfNecessary()
464+
}
465+
461466
if (ancestorElement) {
462467
sendStyles = true;
463468

@@ -801,16 +806,35 @@ ZSSEditor.insertImage = function(url, alt) {
801806
* does not check for that. It would be a mistake.
802807
*/
803808
ZSSEditor.insertLocalImage = function(imageNodeIdentifier, localImageUrl) {
804-
var progressIdentifier = this.getImageProgressIdentifier(imageNodeIdentifier);
805-
var imageContainerIdentifier = this.getImageContainerIdentifier(imageNodeIdentifier);
806-
var imgContainerStart = '<span id="' + imageContainerIdentifier+'" class="img_container">';
807-
var imgContainerEnd = '</span>';
808-
var progress = '<progress id="' + progressIdentifier+'" value=0 class="wp_media_indicator"></progress>';
809-
var image = '<img data-wpid="' + imageNodeIdentifier + '" src="' + localImageUrl + '" alt="" />';
810-
var html = imgContainerStart + progress+image + imgContainerEnd;
811-
812-
this.insertHTML(html);
813-
this.sendEnabledStyles();
809+
810+
if (window.getSelection) {
811+
sel = window.getSelection();
812+
if (sel.getRangeAt && sel.rangeCount) {
813+
var progressIdentifier = this.getImageProgressIdentifier(imageNodeIdentifier);
814+
815+
var span = document.createElement("span");
816+
span.id = imageNodeIdentifier;
817+
span.className = "img_container";
818+
819+
var progress = document.createElement("progress");
820+
progress.id = progressIdentifier;
821+
progress.value = 0;
822+
progress.className = "wp_media_indicator";
823+
824+
var image = document.createElement("img");
825+
image.setAttribute("data-wpid", imageNodeIdentifier);
826+
image.src = localImageUrl;
827+
image.alt = "";
828+
829+
span.appendChild(progress);
830+
span.appendChild(image);
831+
832+
range = sel.getRangeAt(0);
833+
range.insertNode(span);
834+
835+
this.sendEnabledStyles();
836+
}
837+
}
814838
};
815839

816840
ZSSEditor.getImageNodeWithIdentifier = function(imageNodeIdentifier) {
@@ -927,36 +951,35 @@ ZSSEditor.setProgressOnImage = function(imageNodeIdentifier, progress) {
927951
* @brief Notifies that the image upload as finished
928952
*
929953
* @param imageNodeIdentifier The unique image ID for the uploaded image
930-
* @param image The image to be replaces
954+
* @param image The image to be replaced.
931955
* @param mediaID An integer that is the mediaID of the image on the server
932956
*/
933957
ZSSEditor.markImageUploadDone = function(imageNodeIdentifier, image, mediaID) {
958+
959+
var progressId = this.getImageProgressIdentifier(imageNodeIdentifier);
960+
var container = this.getImageContainerNodeWithIdentifier(imageNodeIdentifier)
961+
962+
this.log("Completing image: " + imageNodeIdentifier);
963+
this.log("Removing progress: " + progressId);
964+
965+
// Remove the progress bar from the DOM.
966+
$("#" + progressId).remove();
967+
968+
// Unwrap the image from the image container, leaving it wrapped inside a link
934969
var imageNode = this.getImageNodeWithIdentifier(imageNodeIdentifier);
935-
if (imageNode.length > 0){
936-
// remove identifier attributed from image
937-
imageNode.removeAttr('data-wpid');
938-
939-
// remove uploading style
970+
971+
if (imageNode.length > 0) {
972+
imageNode.unwrap();
973+
imageNode.wrap("<a href='" + image.src + "'></a>");
940974
imageNode.removeClass("uploading");
975+
imageNode.removeAttr('data-wpid');
941976
imageNode.removeAttr("class");
942-
943-
// set remote source
944977
imageNode.attr('src', image.src);
945-
// set attributes
946978
imageNode.attr({'width':image.width, 'height':image.height, 'class':'alignnone size-full'});
979+
947980
if (mediaID >= 0) {
948981
imageNode.addClass('wp-image-' + mediaID);
949982
}
950-
951-
// Remove all extra formatting nodes for progress
952-
if (imageNode.parent().attr("id") == this.getImageContainerIdentifier(imageNodeIdentifier)) {
953-
// remove id from container to avoid to report a user removal
954-
imageNode.parent().attr("id", "");
955-
imageNode.parent().replaceWith(imageNode);
956-
}
957-
// Wrap link around image
958-
var linkTag = '<a href="' + imageNode.attr("src") + '"></a>';
959-
imageNode.wrap(linkTag);
960983
}
961984

962985
var joinedArguments = ZSSEditor.getJoinedFocusedFieldIdAndCaretArguments();
@@ -2735,24 +2758,10 @@ ZSSField.prototype.wrapCaretInParagraphIfNecessary = function()
27352758
|| closerParentNode.nodeName == NodeName.BLOCKQUOTE);
27362759

27372760
if (parentNodeShouldBeParagraph) {
2738-
var selection = window.getSelection();
2739-
2740-
if (selection) {
2741-
var range = selection.getRangeAt(0);
2742-
2743-
if (range.startContainer == range.endContainer) {
2744-
var paragraph = document.createElement("p");
2745-
var textNode = document.createTextNode("&#x200b;");
2746-
2747-
paragraph.appendChild(textNode);
2748-
2749-
range.insertNode(paragraph);
2750-
range.selectNode(textNode);
2751-
2752-
selection.removeAllRanges();
2753-
selection.addRange(range);
2754-
}
2755-
}
2761+
2762+
var savedSelection = rangy.saveSelection();
2763+
$(closerParentNode).wrapInner("<p>");
2764+
rangy.restoreSelection(savedSelection);
27562765
}
27572766
};
27582767

0 commit comments

Comments
 (0)