Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Update Medium Editor files
Browse files Browse the repository at this point in the history
  • Loading branch information
marjinal1st committed Feb 17, 2015
1 parent 0b99797 commit c57cd48
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions vendor/assets/javascripts/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2127,11 +2127,38 @@ function MediumEditor(elements, options) {
},

execAction: function (action, e) {
if (action.indexOf('append-') > -1) {
this.execFormatBlock(action.replace('append-', ''));
/*jslint regexp: true*/
var fullAction = /^full-(.+)$/gi,
appendAction = /^append-(.+)$/gi,
justifyAction = /^justify(left|center|right|full)$/gi,
match;
/*jslint regexp: false*/

// Actions starting with 'full-' should be applied to to the entire contents of the editable element
// (ie full-bold, full-append-pre, etc.)
match = fullAction.exec(action);
if (match) {
// Store the current selection to be restored after applying the action
this.saveSelection();
// Select all of the contents before calling the action
this.selectAllContents();
this.execAction(match[1], e);
// Restore the previous selection
this.restoreSelection();
return;
}

// Actions starting with 'append-' should attempt to format a block of text ('formatBlock') using a specific
// type of block element (ie append-blockquote, append-h1, append-pre, etc.)
match = appendAction.exec(action);
if (match) {
this.execFormatBlock(match[1]);
this.setToolbarPosition();
this.setToolbarButtonStates();
} else if (action === 'anchor') {
return;
}

if (action === 'anchor') {
if (!this.options.disableAnchorForm) {
this.triggerAnchorAction(e);
}
Expand All @@ -2140,7 +2167,8 @@ function MediumEditor(elements, options) {
} else {
this.options.ownerDocument.execCommand(action, false, null);
this.setToolbarPosition();
if (action.indexOf('justify') === 0) {
// Manually update the toolbar for text-alignment actions
if (justifyAction.test(action)) {
this.setToolbarButtonStates();
}
}
Expand Down Expand Up @@ -2276,6 +2304,23 @@ function MediumEditor(elements, options) {
});
},

selectAllContents: function () {
var range = this.options.ownerDocument.createRange(),
sel = this.options.contentWindow.getSelection(),
currNode = meSelection.getSelectionElement(this.options.contentWindow);

if (currNode) {
// Move to the lowest descendant node that still selects all of the contents
while (currNode.children.length === 1) {
currNode = currNode.children[0];
}

range.selectNodeContents(currNode);
sel.removeAllRanges();
sel.addRange(range);
}
},

// http://stackoverflow.com/questions/17678843/cant-restore-selection-after-html-modify-even-if-its-the-same-html
// Tim Down
// TODO: move to selection.js and clean up old methods there
Expand Down

0 comments on commit c57cd48

Please sign in to comment.