@@ -842,18 +842,20 @@ MediumEditor.extensions = {};
842842
843843 // http://stackoverflow.com/questions/6690752/insert-html-at-caret-in-a-contenteditable-div
844844 insertHTMLCommand : function ( doc , html ) {
845- var selection , range , el , fragment , node , lastNode , toReplace ;
845+ var selection , range , el , fragment , node , lastNode , toReplace ,
846+ res = false ,
847+ ecArgs = [ 'insertHTML' , false , html ] ;
846848
847849 /* Edge's implementation of insertHTML is just buggy right now:
848850 * - Doesn't allow leading white space at the beginning of an element
849851 * - Found a case when a <font size="2"> tag was inserted when calling alignCenter inside a blockquote
850852 *
851- * There are likely many other bugs, these are just the ones we found so far.
853+ * There are likely other bugs, these are just the ones we found so far.
852854 * For now, let's just use the same fallback we did for IE
853855 */
854856 if ( ! MediumEditor . util . isEdge && doc . queryCommandSupported ( 'insertHTML' ) ) {
855857 try {
856- return doc . execCommand ( 'insertHTML' , false , html ) ;
858+ return doc . execCommand . apply ( doc , ecArgs ) ;
857859 } catch ( ignore ) { }
858860 }
859861
@@ -898,7 +900,15 @@ MediumEditor.extensions = {};
898900 selection . removeAllRanges ( ) ;
899901 selection . addRange ( range ) ;
900902 }
903+ res = true ;
901904 }
905+
906+ // https://github.com/yabwe/medium-editor/issues/992
907+ // If we're monitoring calls to execCommand, notify listeners as if a real call had happened
908+ if ( doc . execCommand . callListeners ) {
909+ doc . execCommand . callListeners ( ecArgs , res ) ;
910+ }
911+ return res ;
902912 } ,
903913
904914 execFormatBlock : function ( doc , tagName ) {
@@ -2538,37 +2548,47 @@ MediumEditor.extensions = {};
25382548 return ;
25392549 }
25402550
2551+ // Helper method to call all listeners to execCommand
2552+ var callListeners = function ( args , result ) {
2553+ if ( doc . execCommand . listeners ) {
2554+ doc . execCommand . listeners . forEach ( function ( listener ) {
2555+ listener ( {
2556+ command : args [ 0 ] ,
2557+ value : args [ 2 ] ,
2558+ args : args ,
2559+ result : result
2560+ } ) ;
2561+ } ) ;
2562+ }
2563+ } ,
2564+
25412565 // Create a wrapper method for execCommand which will:
25422566 // 1) Call document.execCommand with the correct arguments
25432567 // 2) Loop through any listeners and notify them that execCommand was called
25442568 // passing extra info on the call
25452569 // 3) Return the result
2546- var wrapper = function ( aCommandName , aShowDefaultUI , aValueArgument ) {
2547- var result = doc . execCommand . orig . apply ( this , arguments ) ;
2570+ wrapper = function ( ) {
2571+ var result = doc . execCommand . orig . apply ( this , arguments ) ;
25482572
2549- if ( ! doc . execCommand . listeners ) {
2550- return result ;
2551- }
2573+ if ( ! doc . execCommand . listeners ) {
2574+ return result ;
2575+ }
25522576
2553- var args = Array . prototype . slice . call ( arguments ) ;
2554- doc . execCommand . listeners . forEach ( function ( listener ) {
2555- listener ( {
2556- command : aCommandName ,
2557- value : aValueArgument ,
2558- args : args ,
2559- result : result
2560- } ) ;
2561- } ) ;
2577+ var args = Array . prototype . slice . call ( arguments ) ;
2578+ callListeners ( args , result ) ;
25622579
2563- return result ;
2564- } ;
2580+ return result ;
2581+ } ;
25652582
25662583 // Store a reference to the original execCommand
25672584 wrapper . orig = doc . execCommand ;
25682585
25692586 // Attach an array for storing listeners
25702587 wrapper . listeners = [ ] ;
25712588
2589+ // Helper for notifying listeners
2590+ wrapper . callListeners = callListeners ;
2591+
25722592 // Overwrite execCommand
25732593 doc . execCommand = wrapper ;
25742594 } ,
@@ -4172,6 +4192,7 @@ MediumEditor.extensions = {};
41724192 documentModified = this . removeObsoleteAutoLinkSpans ( blockElements [ i ] ) || documentModified ;
41734193 documentModified = this . performLinkingWithinElement ( blockElements [ i ] ) || documentModified ;
41744194 }
4195+ this . base . events . updateInput ( contenteditable , { target : contenteditable , currentTarget : contenteditable } ) ;
41754196 return documentModified ;
41764197 } ,
41774198
@@ -4375,20 +4396,18 @@ MediumEditor.extensions = {};
43754396 } ,
43764397
43774398 insertImageFile : function ( file ) {
4399+ if ( typeof FileReader !== 'function' ) {
4400+ return ;
4401+ }
43784402 var fileReader = new FileReader ( ) ;
43794403 fileReader . readAsDataURL ( file ) ;
43804404
4381- var id = 'medium-img-' + ( + new Date ( ) ) ;
4382- MediumEditor . util . insertHTMLCommand ( this . document , '<img class="medium-editor-image-loading" id="' + id + '" />' ) ;
4383-
4384- fileReader . onload = function ( ) {
4385- var img = this . document . getElementById ( id ) ;
4386- if ( img ) {
4387- img . removeAttribute ( 'id' ) ;
4388- img . removeAttribute ( 'class' ) ;
4389- img . src = fileReader . result ;
4390- }
4391- } . bind ( this ) ;
4405+ // attach the onload event handler, makes it easier to listen in with jasmine
4406+ fileReader . addEventListener ( 'load' , function ( e ) {
4407+ var addImageElement = this . document . createElement ( 'img' ) ;
4408+ addImageElement . src = e . target . result ;
4409+ MediumEditor . util . insertHTMLCommand ( this . document , addImageElement . outerHTML ) ;
4410+ } . bind ( this ) ) ;
43924411 }
43934412 } ) ;
43944413
@@ -6108,16 +6127,19 @@ MediumEditor.extensions = {};
61086127 this . options . ownerDocument . execCommand ( 'formatBlock' , false , 'p' ) ;
61096128 }
61106129
6111- if ( MediumEditor . util . isKey ( event , MediumEditor . util . keyCode . ENTER ) && ! MediumEditor . util . isListItem ( node ) ) {
6130+ // https://github.com/yabwe/medium-editor/issues/834
6131+ // https://github.com/yabwe/medium-editor/pull/382
6132+ // Don't call format block if this is a block element (ie h1, figCaption, etc.)
6133+ if ( MediumEditor . util . isKey ( event , MediumEditor . util . keyCode . ENTER ) &&
6134+ ! MediumEditor . util . isListItem ( node ) &&
6135+ ! MediumEditor . util . isBlockContainer ( node ) ) {
6136+
61126137 tagName = node . nodeName . toLowerCase ( ) ;
61136138 // For anchor tags, unlink
61146139 if ( tagName === 'a' ) {
61156140 this . options . ownerDocument . execCommand ( 'unlink' , false , null ) ;
61166141 } else if ( ! event . shiftKey && ! event . ctrlKey ) {
6117- // only format block if this is not a header tag
6118- if ( ! / h \d / . test ( tagName ) ) {
6119- this . options . ownerDocument . execCommand ( 'formatBlock' , false , 'p' ) ;
6120- }
6142+ this . options . ownerDocument . execCommand ( 'formatBlock' , false , 'p' ) ;
61216143 }
61226144 }
61236145 }
@@ -7082,7 +7104,7 @@ MediumEditor.parseVersionString = function (release) {
70827104
70837105MediumEditor . version = MediumEditor . parseVersionString . call ( this , ( {
70847106 // grunt-bump looks for this:
7085- 'version' : '5.14.3 '
7107+ 'version' : '5.14.4 '
70867108} ) . version ) ;
70877109
70887110 return MediumEditor ;
0 commit comments