From 134b62eb093f4b65662d335abedb9e201b68102e Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 25 Aug 2016 11:06:40 -0700 Subject: [PATCH] Preserve data when switching from NWE to VE Bug: T143917 Change-Id: I910e731914bd71fdb131dae79d233d4306facc5d --- ...ve.init.mw.DesktopWikitextArticleTarget.js | 31 +++++++++++++++++-- .../init/ve.init.mw.ArticleTargetLoader.js | 6 ++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js b/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js index 97290dffc8..1eb08399b2 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js +++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js @@ -75,11 +75,28 @@ ve.init.mw.DesktopWikitextArticleTarget.prototype.switchToWikitextEditor = funct this.reloadSurface( dataPromise ); }; +/** + * Switch to the visual editor. + */ ve.init.mw.DesktopWikitextArticleTarget.prototype.switchToVisualEditor = function () { + var dataPromise; + this.setMode( 'visual' ); - this.reloadSurface(); + + dataPromise = mw.libs.ve.targetLoader.requestParsoidData( + this.pageName, + this.requestedRevId, + this.constructor.name, + this.edited, + this.getWikitextFromDocument( this.getSurface().getDom() ) + ); + + this.reloadSurface( dataPromise ); }; +/** + * Reload the target surface in the new editor mode + */ ve.init.mw.DesktopWikitextArticleTarget.prototype.reloadSurface = function ( dataPromise ) { var target = this; // Create progress - will be discarded when surface is destroyed. @@ -228,7 +245,7 @@ ve.init.mw.DesktopWikitextArticleTarget.prototype.tryWithPreparedCacheKey = func if ( this.mode === 'source' ) { data = ve.extendObject( {}, options, { format: 'json' } ); - data.wikitext = Array.prototype.map.call( doc.body.children, function ( p ) { return p.innerText; } ).join( '\n' ); + data.wikitext = this.getWikitextFromDocument( doc ); return new mw.Api().post( data, { contentType: 'multipart/form-data' } ); } else { @@ -237,6 +254,16 @@ ve.init.mw.DesktopWikitextArticleTarget.prototype.tryWithPreparedCacheKey = func } }; +/** + * Get wikitext for the whole document + * + * @param {ve.dm.Document} doc Document + * @return {string} Wikitext + */ +ve.init.mw.DesktopWikitextArticleTarget.prototype.getWikitextFromDocument = function ( doc ) { + return Array.prototype.map.call( doc.body.children, function ( p ) { return p.innerText; } ).join( '\n' ); +}; + /* Registration */ ve.init.mw.targetFactory.register( ve.init.mw.DesktopWikitextArticleTarget ); diff --git a/modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js b/modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js index c1ded971e2..4640340bfc 100644 --- a/modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js +++ b/modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js @@ -113,7 +113,7 @@ } }, - requestParsoidData: function ( pageName, oldid, targetName, modified ) { + requestParsoidData: function ( pageName, oldid, targetName, modified, wikitext ) { var start, apiXhr, restbaseXhr, apiPromise, restbasePromise, dataPromise, pageHtmlUrl, switched = false, fromEditedState = false, @@ -151,7 +151,7 @@ ve.track( 'trace.restbaseLoad.enter' ); if ( conf.fullRestbaseUrl && - $( '#wpTextbox1' ).textSelection( 'getContents' ) && + ( wikitext || ( wikitext = $( '#wpTextbox1' ).textSelection( 'getContents' ) ) ) && !$( '[name=wpSection]' ).val() ) { switched = true; @@ -166,7 +166,7 @@ data: { title: pageName, oldid: oldid, - wikitext: $( '#wpTextbox1' ).textSelection( 'getContents' ), + wikitext: wikitext, stash: 'true' }, // Should be synchronised with ApiVisualEditor.php