Esanders has uploaded a new change for review.

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

Change subject: Use surface fragments from ListAction methods
......................................................................

Use surface fragments from ListAction methods

Using surface fragments allows for greater abstraction.

Change-Id: I2f7a2575d0d6888eb60712b982afc01da4fcfc91
---
M src/dm/ve.dm.SurfaceFragment.js
M src/ui/actions/ve.ui.ListAction.js
2 files changed, 52 insertions(+), 64 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/34/298334/1

diff --git a/src/dm/ve.dm.SurfaceFragment.js b/src/dm/ve.dm.SurfaceFragment.js
index ba2a552..593d100 100644
--- a/src/dm/ve.dm.SurfaceFragment.js
+++ b/src/dm/ve.dm.SurfaceFragment.js
@@ -1120,28 +1120,41 @@
  * Example:
  *     // fragment is a selection of: <p>a</p><p>b</p>
  *     fragment.wrapAllNodes(
+ *         { type: 'list', attributes: { style: 'bullet' } },
+ *         { type: 'listItem' }
+ *     )
+ *     // fragment is now a selection of: 
<ul><li><p>a</p></li><li><p>b</p></li></ul>
+ *
+ * Example:
+ *     // fragment is a selection of: <p>a</p><p>b</p>
+ *     fragment.wrapAllNodes(
  *         [{ type: 'list', attributes: { style: 'bullet' } }, { type: 
'listItem' }]
  *     )
  *     // fragment is now a selection of: <ul><li><p>a</p><p>b</p></li></ul>
  *
  * @method
- * @param {Object|Object[]} wrapper Wrapper object, or array of wrapper 
objects (see above)
- * @param {string} wrapper.type Node type of wrapper
- * @param {Object} [wrapper.attributes] Attributes of wrapper
+ * @param {Object|Object[]} wrapOuter Opening element(s) to wrap around the 
range
+ * @param {Object|Object[]} wrapEach Opening element(s) to wrap around each 
top-level element in the range
  * @chainable
  */
-ve.dm.SurfaceFragment.prototype.wrapAllNodes = function ( wrapper ) {
+ve.dm.SurfaceFragment.prototype.wrapAllNodes = function ( wrapOuter, wrapEach 
) {
        var range = this.getSelection().getCoveringRange();
        if ( !range ) {
                return this;
        }
 
-       if ( !Array.isArray( wrapper ) ) {
-               wrapper = [ wrapper ];
+       if ( !Array.isArray( wrapOuter ) ) {
+               wrapOuter = [ wrapOuter ];
+       }
+
+       wrapEach = wrapEach || [];
+
+       if ( !Array.isArray( wrapEach ) ) {
+               wrapEach = [ wrapEach ];
        }
 
        this.change(
-               ve.dm.Transaction.newFromWrap( this.document, range, [], 
wrapper, [], [] )
+               ve.dm.Transaction.newFromWrap( this.document, range, [], 
wrapOuter, [], wrapEach )
        );
 
        return this;
diff --git a/src/ui/actions/ve.ui.ListAction.js 
b/src/ui/actions/ve.ui.ListAction.js
index 6dc94d8..2399249 100644
--- a/src/ui/actions/ve.ui.ListAction.js
+++ b/src/ui/actions/ve.ui.ListAction.js
@@ -99,8 +99,9 @@
  * @return {boolean} Action was executed
  */
 ve.ui.ListAction.prototype.wrap = function ( style, noBreakpoints ) {
-       var tx, i, previousList, groupRange, group, range,
+       var i, previousList, groupRange, group, range,
                surfaceModel = this.surface.getModel(),
+               fragment = surfaceModel.getFragment( null, true ),
                documentModel = surfaceModel.getDocument(),
                selection = surfaceModel.getSelection(),
                groups;
@@ -124,67 +125,41 @@
                documentModel.hasSlugAtOffset( range.to )
        ) {
                // Inside block level slug
-               surfaceModel.change(
-                       ve.dm.Transaction.newFromInsertion(
-                               documentModel,
-                               range.from,
-                               [
-                                       { type: 'list', attributes: { style: 
style } },
-                                       { type: 'listItem' },
-                                       { type: 'paragraph' },
-                                       { type: '/paragraph' },
-                                       { type: '/listItem' },
-                                       { type: '/list' }
+               fragment = fragment
+                       .insertContent( [
+                               { type: 'paragraph' },
+                               { type: '/paragraph' }
+                       ] )
+                       .collapseToStart()
+                       .adjustLinearSelection( 1, 1 )
+                       .select();
+               range = fragment.getSelection().getRange();
+       }
 
-                               ]
-                       ),
-                       new ve.dm.LinearSelection( documentModel, new ve.Range( 
range.to + 3 ) )
-               );
-       } else {
-               groups = documentModel.getCoveredSiblingGroups( range );
-               for ( i = 0; i < groups.length; i++ ) {
-                       group = groups[ i ];
-                       if ( group.grandparent && group.grandparent.getType() 
=== 'list' ) {
-                               if ( group.grandparent !== previousList ) {
+       groups = documentModel.getCoveredSiblingGroups( range );
+       for ( i = 0; i < groups.length; i++ ) {
+               group = groups[ i ];
+               if ( group.grandparent && group.grandparent.getType() === 
'list' ) {
+                       if ( group.grandparent !== previousList ) {
+                               surfaceModel.getLinearFragment( 
group.grandparent.getOuterRange(), true )
                                        // Change the list style
-                                       surfaceModel.change(
-                                               
ve.dm.Transaction.newFromAttributeChanges(
-                                                       documentModel, 
group.grandparent.getOffset(), { style: style }
-                                               ),
-                                               selection
-                                       );
-                                       // Skip this one next time
-                                       previousList = group.grandparent;
-                               }
-                       } else {
-                               // Get a range that covers the whole group
-                               groupRange = new ve.Range(
-                                       group.nodes[ 0 ].getOuterRange().start,
-                                       group.nodes[ group.nodes.length - 1 
].getOuterRange().end
-                               );
-                               // Convert everything to paragraphs first
-                               surfaceModel.change(
-                                       
ve.dm.Transaction.newFromContentBranchConversion(
-                                               documentModel, groupRange, 
'paragraph'
-                                       ),
-                                       selection
-                               );
-                               // Wrap everything in a list and each content 
branch in a listItem
-                               tx = ve.dm.Transaction.newFromWrap(
-                                       documentModel,
-                                       groupRange,
-                                       [],
-                                       [ { type: 'list', attributes: { style: 
style } } ],
-                                       [],
-                                       [ { type: 'listItem' } ]
-                               );
-                               surfaceModel.change(
-                                       tx,
-                                       new ve.dm.LinearSelection( 
documentModel, tx.translateRange( range ) )
-                               );
+                                       .changeAttributes( { style: style } );
+                               previousList = group.grandparent;
                        }
+               } else {
+                       // Get a range that covers the whole group
+                       groupRange = new ve.Range(
+                               group.nodes[ 0 ].getOuterRange().start,
+                               group.nodes[ group.nodes.length - 1 
].getOuterRange().end
+                       );
+                       surfaceModel.getLinearFragment( groupRange, true )
+                               // Convert everything to paragraphs first
+                               .convertNodes( 'paragraph' )
+                               // Wrap everything in a list and each content 
branch in a listItem
+                               .wrapAllNodes( { type: 'list', attributes: { 
style: style } }, { type: 'listItem' } );
                }
        }
+
        if ( !noBreakpoints ) {
                surfaceModel.breakpoint();
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2f7a2575d0d6888eb60712b982afc01da4fcfc91
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/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