verticalnavigation.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  6. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  7. /**
  8. * @module widget/verticalnavigationhandler
  9. */
  10. /**
  11. * Returns 'keydown' handler for up/down arrow keys that modifies the caret movement if it's in a text line next to an object.
  12. *
  13. * @param {module:engine/controller/editingcontroller~EditingController} editing The editing controller.
  14. * @returns {Function}
  15. */
  16. export default function verticalNavigationHandler( editing ) {
  17. const model = editing.model;
  18. return ( evt, data ) => {
  19. const arrowUpPressed = data.keyCode == keyCodes.arrowup;
  20. const arrowDownPressed = data.keyCode == keyCodes.arrowdown;
  21. const expandSelection = data.shiftKey;
  22. const selection = model.document.selection;
  23. if ( !arrowUpPressed && !arrowDownPressed ) {
  24. return;
  25. }
  26. const isForward = arrowDownPressed;
  27. // Navigation is in the opposite direction than the selection direction so this is shrinking of the selection.
  28. // Selection for sure will not approach any object.
  29. if ( expandSelection && selectionWillShrink( selection, isForward ) ) {
  30. return;
  31. }
  32. // Find a range between selection and closest limit element.
  33. const range = findTextRangeFromSelection( editing, selection, isForward );
  34. if ( !range || range.isCollapsed ) {
  35. return;
  36. }
  37. // If the range is a single line (there is no word wrapping) then move the selection to the position closest to the limit element.
  38. //
  39. // We can't move the selection directly to the isObject element (eg. table cell) because of dual position at the end/beginning
  40. // of wrapped line (it's at the same time at the end of one line and at the start of the next line).
  41. if ( isSingleLineRange( editing, range, isForward ) ) {
  42. model.change( writer => {
  43. const newPosition = isForward ? range.end : range.start;
  44. if ( expandSelection ) {
  45. const newSelection = model.createSelection( selection.anchor );
  46. newSelection.setFocus( newPosition );
  47. writer.setSelection( newSelection );
  48. } else {
  49. writer.setSelection( newPosition );
  50. }
  51. } );
  52. evt.stop();
  53. data.preventDefault();
  54. data.stopPropagation();
  55. }
  56. };
  57. }
  58. // Finds the range between selection and closest limit element (in the direction of navigation).
  59. // The position next to limit element is adjusted to the closest allowed `$text` position.
  60. //
  61. // Returns `null` if, according to the schema, the resulting range cannot contain a `$text` element.
  62. //
  63. // @param {module:engine/controller/editingcontroller~EditingController} editing The editing controller.
  64. // @param {module:engine/model/selection~Selection} selection The current selection.
  65. // @param {Boolean} isForward The expected navigation direction.
  66. // @returns {module:engine/model/range~Range|null}
  67. //
  68. function findTextRangeFromSelection( editing, selection, isForward ) {
  69. const model = editing.model;
  70. if ( isForward ) {
  71. const startPosition = selection.isCollapsed ? selection.focus : selection.getLastPosition();
  72. const endPosition = getNearestNonInlineLimit( model, startPosition, 'forward' );
  73. // There is no limit element, browser should handle this.
  74. if ( !endPosition ) {
  75. return null;
  76. }
  77. const range = model.createRange( startPosition, endPosition );
  78. const lastRangePosition = getNearestTextPosition( model.schema, range, 'backward' );
  79. if ( lastRangePosition && startPosition.isBefore( lastRangePosition ) ) {
  80. return model.createRange( startPosition, lastRangePosition );
  81. }
  82. return null;
  83. } else {
  84. const endPosition = selection.isCollapsed ? selection.focus : selection.getFirstPosition();
  85. const startPosition = getNearestNonInlineLimit( model, endPosition, 'backward' );
  86. // There is no limit element, browser should handle this.
  87. if ( !startPosition ) {
  88. return null;
  89. }
  90. const range = model.createRange( startPosition, endPosition );
  91. const firstRangePosition = getNearestTextPosition( model.schema, range, 'forward' );
  92. if ( firstRangePosition && endPosition.isAfter( firstRangePosition ) ) {
  93. return model.createRange( firstRangePosition, endPosition );
  94. }
  95. return null;
  96. }
  97. }
  98. // Finds the limit element position that is closest to startPosition.
  99. //
  100. // @param {module:engine/model/model~Model} model
  101. // @param {<module:engine/model/position~Position>} startPosition
  102. // @param {'forward'|'backward'} direction Search direction.
  103. // @returns {<module:engine/model/position~Position>|null}
  104. //
  105. function getNearestNonInlineLimit( model, startPosition, direction ) {
  106. const schema = model.schema;
  107. const range = model.createRangeIn( startPosition.root );
  108. const walkerValueType = direction == 'forward' ? 'elementStart' : 'elementEnd';
  109. for ( const { previousPosition, item, type } of range.getWalker( { startPosition, direction } ) ) {
  110. if ( schema.isLimit( item ) && !schema.isInline( item ) ) {
  111. return previousPosition;
  112. }
  113. // Stop looking for isLimit element if the next element is a block element (it is for sure not single line).
  114. if ( type == walkerValueType && schema.isBlock( item ) ) {
  115. return null;
  116. }
  117. }
  118. return null;
  119. }
  120. // Basing on the provided range, finds the first or last (depending on `direction`) position inside the range
  121. // that can contain `$text` (according to schema).
  122. //
  123. // @param {module:engine/model/schema~Schema} schema The schema.
  124. // @param {module:engine/model/range~Range} range The range to find the position in.
  125. // @param {'forward'|'backward'} direction Search direction.
  126. // @returns {module:engine/model/position~Position} The nearest selection range.
  127. //
  128. function getNearestTextPosition( schema, range, direction ) {
  129. const position = direction == 'backward' ? range.end : range.start;
  130. if ( schema.checkChild( position, '$text' ) ) {
  131. return position;
  132. }
  133. for ( const { nextPosition } of range.getWalker( { direction } ) ) {
  134. if ( schema.checkChild( nextPosition, '$text' ) ) {
  135. return nextPosition;
  136. }
  137. }
  138. }
  139. // Checks if the DOM range corresponding to the provided model range renders as a single line by analyzing DOMRects
  140. // (verifying if they visually wrap content to the next line).
  141. //
  142. // @param {module:engine/controller/editingcontroller~EditingController} editing The editing controller.
  143. // @param {module:engine/model/range~Range} modelRange The current table cell content range.
  144. // @param {Boolean} isForward The expected navigation direction.
  145. // @returns {Boolean}
  146. //
  147. function isSingleLineRange( editing, modelRange, isForward ) {
  148. const model = editing.model;
  149. const domConverter = editing.view.domConverter;
  150. // Wrapped lines contain exactly the same position at the end of current line
  151. // and at the beginning of next line. That position's client rect is at the end
  152. // of current line. In case of caret at first position of the last line that 'dual'
  153. // position would be detected as it's not the last line.
  154. if ( isForward ) {
  155. const probe = model.createSelection( modelRange.start );
  156. model.modifySelection( probe );
  157. // If the new position is at the end of the container then we can't use this position
  158. // because it would provide incorrect result for eg caption of image and selection
  159. // just before end of it. Also in this case there is no "dual" position.
  160. if ( !probe.focus.isAtEnd && !modelRange.start.isEqual( probe.focus ) ) {
  161. modelRange = model.createRange( probe.focus, modelRange.end );
  162. }
  163. }
  164. const viewRange = editing.mapper.toViewRange( modelRange );
  165. const domRange = domConverter.viewRangeToDom( viewRange );
  166. const rects = Rect.getDomRangeRects( domRange );
  167. let boundaryVerticalPosition;
  168. for ( const rect of rects ) {
  169. if ( boundaryVerticalPosition === undefined ) {
  170. boundaryVerticalPosition = Math.round( rect.bottom );
  171. continue;
  172. }
  173. // Let's check if this rect is in new line.
  174. if ( Math.round( rect.top ) >= boundaryVerticalPosition ) {
  175. return false;
  176. }
  177. boundaryVerticalPosition = Math.max( boundaryVerticalPosition, Math.round( rect.bottom ) );
  178. }
  179. return true;
  180. }
  181. function selectionWillShrink( selection, isForward ) {
  182. return !selection.isCollapsed && selection.isBackward == isForward;
  183. }