Esanders has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/403472 )

Change subject: WIP Create transactions for changes to tempWikitextEditor
......................................................................

WIP Create transactions for changes to tempWikitextEditor

Change-Id: Iebcfe2ed7edb7a097914a6775c43bcc96236009a
---
M modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
1 file changed, 40 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/72/403472/1

diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js 
b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
index 5109a35..dcc3733 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
@@ -21,7 +21,7 @@
        var conf, tabMessages, uri, pageExists, viewUri, veEditUri, 
veEditSourceUri, isViewPage, isEditPage,
                pageCanLoadEditor, init, targetPromise, enable, tempdisable, 
autodisable,
                tabPreference, enabledForUser, initialWikitext, oldId,
-               isLoading, tempWikitextEditor, $toolbarPlaceholder,
+               isLoading, tempWikitextEditor, $toolbarPlaceholder, 
originalContent,
                editModes = {
                        edit: 'visual'
                },
@@ -107,18 +107,8 @@
        }
 
        function setupTempWikitextEditor( data ) {
-               tempWikitextEditor = new mw.libs.ve.MWTempWikitextEditorWidget( 
{
-                       value: data.content,
-                       onChange: function () {
-                               // Write changes back to response data object,
-                               // which will be used to construct the surface.
-                               data.content = tempWikitextEditor.getValue();
-                               // TODO: Consider writing changes using a
-                               // transaction so they can be undone.
-                               // For now, just mark surface as pre-modified
-                               data.fromEditedState = true;
-                       }
-               } );
+               originalContent = data.content;
+               tempWikitextEditor = new mw.libs.ve.MWTempWikitextEditorWidget( 
{ value: data.content } );
 
                // Create an equal-height placeholder for the toolbar to avoid 
vertical jump
                // when the real toolbar is ready.
@@ -144,9 +134,44 @@
        }
 
        function teardownTempWikitextEditor() {
-               var range,
+               var range, offset, diff,
                        nativeRange = tempWikitextEditor.getRange(),
-                       surfaceModel = ve.init.target.getSurface().getModel();
+                       surfaceModel = ve.init.target.getSurface().getModel(),
+                       newContent = tempWikitextEditor.getValue();
+
+               // If the textarea was modified, apply transactions based on 
the diff.
+               /* global diff_match_patch */
+               if ( originalContent !== newContent ) {
+                       offset = 1;
+                       // eslint-disable-next-line new-cap
+                       diff = new diff_match_patch().diff_main( 
originalContent, newContent );
+                       diff.forEach( function ( change ) {
+                               var data,
+                                       lines = change[ 1 ].match( /\n/g ),
+                                       length = change[ 1 ].length + ( lines ? 
lines.length : 0 );
+                               switch ( change[ 0 ] ) {
+                                       case 
ve.DiffMatchPatch.static.DIFF_EQUAL:
+                                               offset += length;
+                                               break;
+                                       case 
ve.DiffMatchPatch.static.DIFF_INSERT:
+                                               data = change[ 1 ].split( '' 
).reduce( function ( res, char ) {
+                                                       if ( char === '\n' ) {
+                                                               res.push( { 
type: '/paragraph' }, { type: 'paragraph' } );
+                                                       } else {
+                                                               res.push( char 
);
+                                                       }
+                                                       return res;
+                                               }, [] );
+                                               surfaceModel.getLinearFragment( 
new ve.Range( offset ) ).insertContent( data );
+                                               offset += length;
+                                               break;
+                                       case 
ve.DiffMatchPatch.static.DIFF_DELETE:
+                                               lines = change[ 1 ].match( 
/\n/g );
+                                               surfaceModel.getLinearFragment( 
new ve.Range( offset, offset + length ) ).removeContent();
+                                               break;
+                               }
+                       } );
+               }
 
                // Transfer the last-seen selection to the VE surface
                range = surfaceModel.getRangeFromSourceOffsets( 
nativeRange.from, nativeRange.to );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iebcfe2ed7edb7a097914a6775c43bcc96236009a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

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

Reply via email to