Esanders has uploaded a new change for review.

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


Change subject: Pressing enter in a list with no nearby content creates a 
paragraph
......................................................................

Pressing enter in a list with no nearby content creates a paragraph

Currently we assume getNearestContentOffset will give us something
sane however it can return -1 when there is no nearby content and so
an exception is thrown. In this case we have to create an empty
paragraph to place the cursor in.

Change-Id: Ic6c19da881e47ff6be45cdaa4b71bfcc1c654796
---
M modules/ve/ce/ve.ce.Surface.js
M modules/ve/test/ce/ve.ce.Surface.test.js
2 files changed, 26 insertions(+), 5 deletions(-)


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

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 7f7a57c..4d49053 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -1658,13 +1658,20 @@
 
        // Now we can move the cursor forward
        if ( advanceCursor ) {
-               this.model.setSelection(
-                       new ve.Range( 
documentModel.data.getRelativeContentOffset( selection.from, 1 ) )
-               );
+               cursor = documentModel.data.getRelativeContentOffset( 
selection.from, 1 );
        } else {
-               this.model.setSelection(
-                       new ve.Range( 
documentModel.data.getNearestContentOffset( selection.from ) )
+               cursor = documentModel.data.getNearestContentOffset( 
selection.from );
+       }
+       if ( cursor === -1 ) {
+               // Cursor couldn't be placed in a nearby content node, so 
create an empty paragraph
+               this.model.change(
+                       ve.dm.Transaction.newFromInsertion(
+                               documentModel, selection.from, emptyParagraph
+                       )
                );
+               this.model.setSelection( new ve.Range( selection.from + 1 ) );
+       } else {
+               this.model.setSelection( new ve.Range( cursor ) );
        }
        // Reset and resume polling
        this.surfaceObserver.clear();
diff --git a/modules/ve/test/ce/ve.ce.Surface.test.js 
b/modules/ve/test/ce/ve.ce.Surface.test.js
index ed765a1..7468978 100644
--- a/modules/ve/test/ce/ve.ce.Surface.test.js
+++ b/modules/ve/test/ce/ve.ce.Surface.test.js
@@ -312,6 +312,20 @@
                                },
                                'expectedRange': new ve.Range( 1 ),
                                'msg': 'Enter in an empty list at start of 
document destroys it and moves to next paragraph'
+                       },
+                       {
+                               'html': emptyList,
+                               'range': new ve.Range( 3 ),
+                               'operations': ['enter'],
+                               'expectedData': function ( data ) {
+                                       data.splice(
+                                               0, 6,
+                                               { 'type': 'paragraph' },
+                                               { 'type': '/paragraph' }
+                                       );
+                               },
+                               'expectedRange': new ve.Range( 1 ),
+                               'msg': 'Enter in an empty list with no adjacent 
content destroys it and creates a paragraph'
                        }
                ];
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic6c19da881e47ff6be45cdaa4b71bfcc1c654796
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to