Skip to content

Commit

Permalink
5.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
nmielnik committed Aug 3, 2015
1 parent 32865a6 commit 24fc2ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
5.5.3 / 2015-08-03
==================
* Fix issue with replacing a pre-existing link
* Fix issue with selection after creating a link after empty paragraphs


5.5.2 / 2015-08-02
==================
* Fix issue where block elements where cleaned up incorrectly when pasting
Expand Down
66 changes: 42 additions & 24 deletions dist/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ var Selection;
}
}

if (selectionState.emptyBlocksIndex && selectionState.end === nextCharIndex) {
if (selectionState.emptyBlocksIndex) {
var targetNode = Util.getTopBlockContainer(range.startContainer),
index = 0;
// Skip over empty blocks until we hit the block we want the selection to be in
Expand Down Expand Up @@ -1960,6 +1960,7 @@ var Selection;
}
}
range.setStart(currentNode.parentNode, currentNodeIndex + 1);
range.collapse(true);
}
}
return range;
Expand Down Expand Up @@ -6128,8 +6129,7 @@ function MediumEditor(elements, options) {
},

createLink: function (opts) {
var customEvent,
i;
var customEvent, i;

if (opts.url && opts.url.trim().length > 0) {
var currentSelection = this.options.contentWindow.getSelection();
Expand All @@ -6139,17 +6139,22 @@ function MediumEditor(elements, options) {
endContainerParentElement,
textNodes;

startContainerParentElement = Util.getClosestBlockContainer(
currentSelection.getRangeAt(0).startContainer);
endContainerParentElement = Util.getClosestBlockContainer(
currentSelection.getRangeAt(0).endContainer);
startContainerParentElement = Util.getClosestBlockContainer(currentSelection.getRangeAt(0).startContainer);
endContainerParentElement = Util.getClosestBlockContainer(currentSelection.getRangeAt(0).endContainer);

if (startContainerParentElement === endContainerParentElement) {
var currentEditor = Selection.getSelectionElement(this.options.contentWindow),
parentElement = (startContainerParentElement || currentEditor),
fragment = this.options.ownerDocument.createDocumentFragment();

// since we are going to create a link from an extracted text,
// be sure that if we are updating a link, we won't let an empty link behind (see #754)
// (Workaroung for Chrome)
this.execAction('unlink');

exportedSelection = this.exportSelection();
fragment.appendChild(parentElement.cloneNode(true));

if (currentEditor === parentElement) {
// We have to avoid the editor itself being wiped out when it's the only block element,
// as our reference inside this.elements gets detached from the page when insertHTML runs.
Expand All @@ -6162,37 +6167,50 @@ function MediumEditor(elements, options) {
// In WebKit:
// an invented <br /> tag at the end in the same situation

Selection.select(this.options.ownerDocument,
parentElement.firstChild, 0,
parentElement.lastChild, parentElement.lastChild.nodeType === 3 ?
parentElement.lastChild.nodeValue.length : parentElement.lastChild.childNodes.length);
Selection.select(
this.options.ownerDocument,
parentElement.firstChild,
0,
parentElement.lastChild,
parentElement.lastChild.nodeType === 3 ?
parentElement.lastChild.nodeValue.length : parentElement.lastChild.childNodes.length
);
} else {
Selection.select(this.options.ownerDocument,
parentElement, 0,
parentElement, parentElement.childNodes.length);
Selection.select(
this.options.ownerDocument,
parentElement,
0,
parentElement,
parentElement.childNodes.length
);
}

var modifiedExportedSelection = this.exportSelection();

textNodes = Util.findOrCreateMatchingTextNodes(this.options.ownerDocument,
fragment,
{
start: exportedSelection.start - modifiedExportedSelection.start,
end: exportedSelection.end - modifiedExportedSelection.start,
editableElementIndex: exportedSelection.editableElementIndex
});
textNodes = Util.findOrCreateMatchingTextNodes(
this.options.ownerDocument,
fragment,
{
start: exportedSelection.start - modifiedExportedSelection.start,
end: exportedSelection.end - modifiedExportedSelection.start,
editableElementIndex: exportedSelection.editableElementIndex
}
);

// Creates the link in the document fragment
Util.createLink(this.options.ownerDocument, textNodes, opts.url.trim());
// Chrome trims the leading whitespaces when inserting HTML, which messes up restoring the selection.
var leadingWhitespacesCount = (fragment.firstChild.innerHTML.match(/^\s+/) || [''])[0].length;
// Now move the created link back into the original document in a way to preserve undo/redo history
Util.insertHTMLCommand(this.options.ownerDocument,
fragment.firstChild.innerHTML.replace(/^\s+/, ''));
Util.insertHTMLCommand(this.options.ownerDocument, fragment.firstChild.innerHTML.replace(/^\s+/, ''));
exportedSelection.start -= leadingWhitespacesCount;
exportedSelection.end -= leadingWhitespacesCount;

this.importSelection(exportedSelection);
} else {
this.options.ownerDocument.execCommand('createLink', false, opts.url);
}

if (this.options.targetBlank || opts.target === '_blank') {
Util.setTargetBlank(Selection.getSelectionStart(this.options.ownerDocument), opts.url);
}
Expand Down Expand Up @@ -6249,7 +6267,7 @@ MediumEditor.parseVersionString = function (release) {

MediumEditor.version = MediumEditor.parseVersionString.call(this, ({
// grunt-bump looks for this:
'version': '5.5.2'
'version': '5.5.3'
}).version);

return MediumEditor;
Expand Down
4 changes: 2 additions & 2 deletions dist/js/medium-editor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medium-editor",
"version": "5.5.2",
"version": "5.5.3",
"author": "Davi Ferreira <hi@daviferreira.com>",
"contributors": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/js/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ MediumEditor.parseVersionString = function (release) {

MediumEditor.version = MediumEditor.parseVersionString.call(this, ({
// grunt-bump looks for this:
'version': '5.5.2'
'version': '5.5.3'
}).version);

0 comments on commit 24fc2ec

Please sign in to comment.