Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/194871

Change subject: Get rid of synchronous ajax calls
......................................................................

Get rid of synchronous ajax calls

Well, I originally wanted to do API calls & immediately
fetch the content in the editor's format (wikitext for
none, html for VE)
However, we already have the content in the textareas
(for non-JS users) and our current main editor is the one
with wikitext - seems absurd to add another API call if
we already have all we need, so I'll just stick to this.

Conversion thingy nog returns a promise, that resolves
with the converted content. 1 of the callers has been
converted to use the promise; other caller was removed
because it's no longer used anyway.

I've also removed an "error handling" line that was
outdated anyway. $.flow() no longer exists, for one...

Bug: T73474
Change-Id: I16511faa7c991dbc948292870e2f1c4e34c98b46
---
M modules/editor/ext.flow.editor.js
M modules/editor/ext.flow.parsoid.js
2 files changed, 37 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/71/194871/1

diff --git a/modules/editor/ext.flow.editor.js 
b/modules/editor/ext.flow.editor.js
index 4ee2c92..0a4f5f1 100644
--- a/modules/editor/ext.flow.editor.js
+++ b/modules/editor/ext.flow.editor.js
@@ -58,7 +58,7 @@
                 * @param {jQuery} $node
                 * @param {string} [content] Existing content to load, in any 
format
                 * @param {string} [contentFormat] The format that content is 
in, or null (defaults to wikitext)
-                * @return {jQuery.Deferred} Will resolve once editor instance 
is loaded
+                * @return {jQuery.Promise} Will resolve once editor instance 
is loaded
                 */
                load: function ( $node, content, contentFormat ) {
                        /**
@@ -83,19 +83,14 @@
                                        return;
                                }
 
-                               if ( content ) {
-                                       try {
-                                               content = 
mw.flow.parsoid.convert( contentFormat, mw.flow.editor.getFormat(), content );
-                                       } catch ( e ) {
-                                               $( '<div>' ).flow( 'showError', 
e.getErrorInfo() ).insertAfter( $node );
-                                               return;
-                                       }
-                               } else {
-                                       content = '';
-                               }
-
-                               mw.flow.editor.create( $node, content );
-                               deferred.resolve();
+                               mw.flow.parsoid.convert( contentFormat, 
mw.flow.editor.getFormat(), content )
+                                       .done( function( content ) {
+                                               mw.flow.editor.create( $node, 
content );
+                                               deferred.resolve();
+                                       })
+                                       .fail( function() {
+                                               deferred.reject();
+                                       });
                        },
                        deferred = $.Deferred(),
                        interval = setInterval( $.proxy( load, this, $node, 
content, contentFormat ), 10 );
@@ -139,25 +134,6 @@
                getRawContent: function ( $node ) {
                        var editor = mw.flow.editor.getEditor( $node );
                        return editor.getRawContent() || '';
-               },
-
-               /**
-                * Get the content, in wikitext format.
-                *
-                * @deprecated API now also accepts html content, as long as 
the format
-                * parameter is set. It's encouraged to use getRawContent + 
getFormat.
-                *
-                * @param {jQuery} $node
-                * @return {string}
-                */
-               getContent: function ( $node ) {
-                       var content = mw.flow.editor.getRawContent( $node ), 
convertedContent = '';
-                       try {
-                               convertedContent = mw.flow.parsoid.convert( 
mw.flow.editor.getFormat(), 'wikitext', content );
-                       } catch ( e ) {
-                               $( '<div>' ).flow( 'showError', 
e.getErrorInfo() ).insertAfter( $node );
-                       }
-                       return convertedContent;
                },
 
                /**
diff --git a/modules/editor/ext.flow.parsoid.js 
b/modules/editor/ext.flow.parsoid.js
index da51a24..837c442 100644
--- a/modules/editor/ext.flow.parsoid.js
+++ b/modules/editor/ext.flow.parsoid.js
@@ -8,32 +8,39 @@
                 * @param {string} to Desired output format: html|wikitext
                 * @param {string} content Content to convert
                 * @param {string} [title] Page title
-                * @return {string}
+                * @return {jQuery.Promise} Will resolve with converted content 
as data
                 */
                convert: function ( from, to, content, title ) {
-                       if ( from !== to ) {
-                               var api = new mw.Api( { ajax: { async: false } 
} );
+                       var deferred = $.Deferred(),
+                               api = new mw.Api();
 
-                               if ( !title ) {
-                                       title = mw.config.get( 'wgPageName' );
-                               }
-
-                               api.post( {
-                                       action: 'flow-parsoid-utils',
-                                       from: from,
-                                       to: to,
-                                       content: content,
-                                       title: title
-                               } )
-                               .done( function ( data ) {
-                                       content = 
data['flow-parsoid-utils'].content;
-                               } )
-                               .fail( function ( code, data ) {
-                                       throw new mw.flow.exception( code, data 
);
-                               } );
+                       if ( content === '' ) {
+                               return deferred.resolve( content );
                        }
 
-                       return content;
+                       if ( from === to ) {
+                               return deferred.resolve( content );
+                       }
+
+                       if ( !title ) {
+                               title = mw.config.get( 'wgPageName' );
+                       }
+
+                       api.post( {
+                               action: 'flow-parsoid-utils',
+                               from: from,
+                               to: to,
+                               content: content,
+                               title: title
+                       } )
+                       .done( function ( data ) {
+                               deferred.resolve( 
data['flow-parsoid-utils'].content );
+                       } )
+                       .fail( function () {
+                               deferred.reject();
+                       } );
+
+                       return deferred.promise();
                }
        };
 } ( jQuery, mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/194871
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I16511faa7c991dbc948292870e2f1c4e34c98b46
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to