DLynch has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/381805 )
Change subject: ce.Surface: handle table-adjacent observed selection changes better ...................................................................... ce.Surface: handle table-adjacent observed selection changes better Moving to the closest cursorable location produces better results, and means clicking to the left of a table no longer vanishes the cursor. There's a remaining bug when a table is the very first node in a document, and you click to the left of it *twice*, which deselects the block slug and places the cursor in an invalid position. This needs more changes to various optimizations selection-changes try to make to avoid, however. Bug: T148679 Bug: T176936 Change-Id: I8a3dde56d52342316c00a909ac9369b2a9f6b409 --- M src/ce/ve.ce.Surface.js M src/dm/ve.dm.Document.js 2 files changed, 19 insertions(+), 6 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/05/381805/1 diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js index 63c056f..f340f80 100644 --- a/src/ce/ve.ce.Surface.js +++ b/src/ce/ve.ce.Surface.js @@ -2762,7 +2762,7 @@ * @param {ve.ce.RangeState} newState The changed range state */ ve.ce.Surface.prototype.handleObservedChanges = function ( oldState, newState ) { - var newSelection, transaction, removedUnicorns, + var newSelection, transaction, removedUnicorns, offset, activeNode, coveringRange, nodeRange, containsStart, containsEnd, surface = this, dmDoc = this.getModel().getDocument(), @@ -2812,9 +2812,16 @@ // cursorable location. Failure to do this can result in // strange behavior when inserting content immediately after // clicking on the surface. - newSelection = new ve.dm.LinearSelection( dmDoc, new ve.Range( - dmDoc.getNearestCursorOffset( newState.veRange.from, 1 ) - ) ); + offset = dmDoc.getNearestCursorOffset( newState.veRange.from, 0 ); + if ( dmDoc.data.getNearestContentOffset( offset ) === -1 ) { + // First, if we're in a document which outright doesn't + // have any cursorable locations, don't try to set one. + // These would be niche documents, since slugs normally + // catch those cases. + newSelection = new ve.dm.NullSelection( dmDoc ); + } else { + newSelection = new ve.dm.LinearSelection( dmDoc, new ve.Range( offset ) ); + } } else { newSelection = new ve.dm.LinearSelection( dmDoc, newState.veRange ); } diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js index 6087c9b..a75b1aa 100644 --- a/src/dm/ve.dm.Document.js +++ b/src/dm/ve.dm.Document.js @@ -920,11 +920,17 @@ * * @method * @param {number} offset Offset to start looking at - * @param {number} [direction=-1] Direction to look in, +1 or -1 + * @param {number} [direction=-1] Direction to look in, +1 or -1; if 0, find the closest offset * @return {number} Nearest offset a cursor can be placed at */ ve.dm.Document.prototype.getNearestCursorOffset = function ( offset, direction ) { - var contentOffset, structuralOffset; + var contentOffset, structuralOffset, left, right; + + if ( direction === 0 ) { + left = this.getNearestCursorOffset( offset, -1 ); + right = this.getNearestCursorOffset( offset, 1 ); + return offset - left < right - offset ? left : right; + } direction = direction > 0 ? 1 : -1; if ( -- To view, visit https://gerrit.wikimedia.org/r/381805 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8a3dde56d52342316c00a909ac9369b2a9f6b409 Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: DLynch <dly...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits