Esanders has uploaded a new change for review.

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


Change subject: Delete empty nodes instead of merging into them
......................................................................

Delete empty nodes instead of merging into them

When you press delete inside an empty node (e.g. Heading) that
node should be removed, instead of the paragraph beneath it being
merged into and effectively converted. If the heading is non-empty
then merging is still the correct behaviour.

Bug: 50254
Change-Id: If9cee79feb4b4ee9d7c367e392b00fee5e8c0669
---
M modules/ve/ce/ve.ce.Surface.js
1 file changed, 33 insertions(+), 19 deletions(-)


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

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 5eadc52..d1b5a12 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -1485,7 +1485,7 @@
  */
 ve.ce.Surface.prototype.handleDelete = function ( e, backspace ) {
        var rangeToRemove = this.model.getSelection(),
-               tx, endNode, endNodeData, nodeToDelete;
+               tx, startNode, endNode, endNodeData, nodeToDelete;
 
        if ( rangeToRemove.isCollapsed() ) {
                // In case when the range is collapsed use the same logic that 
is used for cursor left and
@@ -1508,33 +1508,47 @@
                // If after processing removal transaction range is not 
collapsed it means that not
                // everything got merged nicely (at this moment transaction 
processor is capable of merging
                // nodes of the same type and at the same depth level only), so 
we process with another
-               // merging that takes remaing data from "endNode" and inserts 
it at the end of "startNode",
-               // "endNode" or recrusivly its parent (if have only one child) 
gets removed.
+               // merging that takes remaing data from endNode and inserts it 
at the end of startNode,
+               // endNode or recrusivly its parent (if have only one child) 
gets removed.
+               //
+               // If startNode has no content then we just delete that node 
instead of merging.
+               // This prevents content being inserted into empty structure 
which, e.g. and empty heading
+               // will be deleted, rather than "converting" the paragraph 
beneath to a heading.
+
                endNode = this.documentView.getNodeFromOffset( 
rangeToRemove.end, false );
 
-               // If "endNode" is within our rangeToRemove, then we shouldn't 
delete it
+               // If endNode is within our rangeToRemove, then we shouldn't 
delete it
                if ( endNode.getModel().getRange().start >= rangeToRemove.end ) 
{
-                       endNodeData = this.documentView.model.getData( 
endNode.getModel().getRange() );
-                       nodeToDelete = endNode;
-                       nodeToDelete.traverseUpstream( function ( node ) {
-                               var parent = node.getParent();
-                               if ( parent.children.length === 1 ) {
-                                       nodeToDelete = parent;
-                                       return true;
-                               } else {
-                                       return false;
-                               }
-                       } );
-                       this.model.change(
-                               [
+                       startNode = this.documentView.getNodeFromOffset( 
rangeToRemove.start, false );
+                       if ( startNode.getModel().getRange().isCollapsed() ) {
+                               // Remove startNode
+                               this.model.change( [
+                                       ve.dm.Transaction.newFromRemoval(
+                                               this.documentView.model, 
startNode.getModel().getOuterRange()
+                                       )
+                               ] );
+                       } else {
+                               endNodeData = this.documentView.model.getData( 
endNode.getModel().getRange() );
+                               nodeToDelete = endNode;
+                               nodeToDelete.traverseUpstream( function ( node 
) {
+                                       var parent = node.getParent();
+                                       if ( parent.children.length === 1 ) {
+                                               nodeToDelete = parent;
+                                               return true;
+                                       } else {
+                                               return false;
+                                       }
+                               } );
+                               // Move contents of endNode into startNode, and 
delete nodeToDelete
+                               this.model.change( [
                                        ve.dm.Transaction.newFromRemoval(
                                                this.documentView.model, 
nodeToDelete.getModel().getOuterRange()
                                        ),
                                        ve.dm.Transaction.newFromInsertion(
                                                this.documentView.model, 
rangeToRemove.start, endNodeData
                                        )
-                               ]
-                       );
+                               ] );
+                       }
                }
        }
        this.model.change( null, new ve.Range( rangeToRemove.start ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If9cee79feb4b4ee9d7c367e392b00fee5e8c0669
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