From 0d344e37ae83ab8ec887bceec08e083cb6dbe209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Thu, 13 Dec 2018 01:40:38 +0100 Subject: [PATCH] T209554 Change-Id: I816deccef00332e64bdc34917d88f19b94a8cf82 --- src/ce/ve.ce.RangeState.js | 4 ++++ src/ce/ve.ce.Surface.js | 13 +++++++++++-- src/ce/ve.ce.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/ce/ve.ce.RangeState.js b/src/ce/ve.ce.RangeState.js index 4329d7b72..0c64a256c 100644 --- a/src/ce/ve.ce.RangeState.js +++ b/src/ce/ve.ce.RangeState.js @@ -161,6 +161,10 @@ ve.ce.RangeState.prototype.saveState = function ( old, documentNode, selectionOn selection.focusOffset ); } + this.isTableAdjacent = selection.focusNode && + ve.ce.isTableAdjacent( selection.focusNode, selection.focusOffset ); + console.log(selection) + console.log(this.isTableAdjacent) // Save selection for future comparisons. (But it is not properly frozen, because the nodes // are live and mutable, and therefore the offsets may come to point to places that are diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js index a697c6579..2549b6d88 100644 --- a/src/ce/ve.ce.Surface.js +++ b/src/ce/ve.ce.Surface.js @@ -3034,7 +3034,7 @@ ve.ce.Surface.prototype.renderSelectedContentBranchNode = function () { * @param {ve.ce.RangeState} newState The changed range state */ ve.ce.Surface.prototype.handleObservedChanges = function ( oldState, newState ) { - var newSelection, transaction, removedUnicorns, offset, + var newSelection, transaction, removedUnicorns, oldBranch, offset, activeNode, coveringRange, nodeRange, containsStart, containsEnd, blockSlug, surface = this, dmDoc = this.getModel().getDocument(), @@ -3079,7 +3079,16 @@ ve.ce.Surface.prototype.handleObservedChanges = function ( oldState, newState ) oldState.veRange.equalsSelection( newState.veRange ) ) ) { if ( newState.veRange ) { - if ( newState.veRange.isCollapsed() ) { + if ( newState.veRange.isCollapsed() && newState.isTableAdjacent ) { + oldBranch = dmDoc.getBranchNodeFromOffset( oldState.veRange.from ); + } + if ( oldBranch && oldBranch.findParent( ve.dm.TableNode ) ) { + // If old selection was inside a table, and new (native) selection is adjacent to a table, + // the user probably accidentally clicked to the left/right of a table node and is about to + // change focus to the node before/after the table, which is probably not what they want. + // Restore old selection. + newSelection = this.getModel().getSelection(); + } else if ( newState.veRange.isCollapsed() ) { offset = dmDoc.getNearestCursorOffset( newState.veRange.from, 0 ); if ( offset === -1 ) { // First, if we're in a document which outright doesn't diff --git a/src/ce/ve.ce.js b/src/ce/ve.ce.js index 90468034c..2a705c58b 100644 --- a/src/ce/ve.ce.js +++ b/src/ce/ve.ce.js @@ -410,6 +410,41 @@ ve.ce.isAfterAnnotationBoundary = function ( node, offset ) { return ve.dm.modelRegistry.isAnnotation( previousNode ); }; +/** + * Test whether the DOM position is adjacent to a table node. + * + * @param {Node} node Position node + * @param {number} offset Position offset + * @return {boolean} Whether this is position is adjacent to a table + */ +ve.ce.isTableAdjacent = function ( node, offset ) { + var previousNode, nextNode; + + if ( node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 've-ce-cursorHolder' ) ) { + previousNode = node.previousSibling; + } else { + previousNode = offset > 0 && node.childNodes[ offset - 1 ]; + } + if ( previousNode && previousNode.nodeType === Node.ELEMENT_NODE ) { + if ( previousNode.nodeName.toLowerCase() === 'table' ) { + return true; + } + } + + if ( node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 've-ce-cursorHolder' ) ) { + nextNode = node.nextSibling; + } else { + nextNode = offset < node.childNodes.length && node.childNodes[ offset ]; + } + if ( nextNode && nextNode.nodeType === Node.ELEMENT_NODE ) { + if ( nextNode.nodeName.toLowerCase() === 'table' ) { + return true; + } + } + + return false; +}; + /** * Check if keyboard shortcut modifier key is pressed. * -- 2.17.1.windows.2