bindtwostepcarettoattribute.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/utils/bindtwostepcarettoattribute
  7. */
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import first from '@ckeditor/ckeditor5-utils/src/first';
  11. /**
  12. * This helper adds two-steps caret movement behaviour for given attribute.
  13. *
  14. * When caret is moving by arrow keys and reach bound of text with attribute for which behaviour is enabled
  15. * then typing does not expand this attribute. Additional arrow key press is needed to "enter" to the text
  16. * and start typing with this attribute. The same is is for leaving this text.
  17. *
  18. * When behaviour is enabled for `linkHref` attribute and caret is just before the attribute element then pressing
  19. * right arrow will move caret inside the attribute element instead of moving after next character:
  20. *
  21. * <p>foo{}<a>bar</a>biz<p> `->` <p>foo<a>{}foo</a>barr<p>
  22. *
  23. * The same is for "leaving" attribute element:
  24. *
  25. * <p>foo<a>bar{}</a>biz<p> `->` <p>foo<a>bar</a>{}biz<p>
  26. *
  27. * And when moving left:
  28. *
  29. * <p>foo<a>bar</a>{}biz<p> `<-` <p>foo<a>bar{}</a>biz<p>
  30. * <p>foo<a>{}bar</a>biz<p> `<-` <p>foo{}<a>bar</a>biz<p>
  31. *
  32. * @param {module:core/editor/editor~Editor} editor The Editor instance.
  33. * @param {module:utils/dom/emittermixin~Emitter} emitter The emitter to which this behavior should be added.
  34. * @param {String} attribute Attribute for which behaviour will be added.
  35. */
  36. export default function bindTwoStepCaretToAttribute( editor, emitter, attribute ) {
  37. const model = editor.model;
  38. const editingView = editor.editing.view;
  39. const modelSelection = model.document.selection;
  40. // Listen to keyboard events and handle cursor before the move.
  41. emitter.listenTo( editingView, 'keydown', ( evt, data ) => {
  42. const arrowRightPressed = data.keyCode == keyCodes.arrowright;
  43. const arrowLeftPressed = data.keyCode == keyCodes.arrowleft;
  44. // When neither left or right arrow has been pressed then do noting.
  45. if ( !arrowRightPressed && !arrowLeftPressed ) {
  46. return;
  47. }
  48. // This implementation works only for collapsed selection.
  49. if ( !modelSelection.isCollapsed ) {
  50. return;
  51. }
  52. const position = modelSelection.getFirstPosition();
  53. // Moving right.
  54. if ( arrowRightPressed ) {
  55. // If gravity is already overridden then do nothing.
  56. // It means that we already enter `foo<a>{}bar</a>biz` or left `foo<a>bar</a>{}biz` text with attribute
  57. // and gravity will be restored just after caret movement.
  58. if ( modelSelection.isGravityOverridden ) {
  59. return;
  60. }
  61. // If caret sticks to the bound of Text with attribute it means that we are going to
  62. // enter `foo{}<a>bar</a>biz` or leave `foo<a>bar{}</a>biz` the text with attribute.
  63. if ( isStickToAttribute( position.nodeAfter, position.nodeBefore, attribute ) ) {
  64. // So we need to prevent caret from being moved.
  65. data.preventDefault();
  66. // And override default selection gravity.
  67. model.change( writer => writer.overrideSelectionGravity() );
  68. }
  69. // Moving left.
  70. } else {
  71. // If caret sticks to the bound of Text with attribute and gravity is already overridden it means that
  72. // we are going to enter `foo<a>bar</a>{}biz` or leave `foo<a>{}bar</a>biz` text with attribute.
  73. if ( modelSelection.isGravityOverridden && isStickToAttribute( position.nodeBefore, position.nodeAfter, attribute ) ) {
  74. // So we need to prevent cater from being moved.
  75. data.preventDefault();
  76. // And restore the gravity.
  77. model.change( writer => writer.restoreSelectionGravity() );
  78. return;
  79. }
  80. // If we are here we need to check if caret is a one character before the text with attribute bound
  81. // `foo<a>bar</a>b{}iz` or `foo<a>b{}ar</a>biz`.
  82. const nextPosition = getPreviousPosition( position );
  83. // When there is no position it means that parent bound has been reached.
  84. if ( !nextPosition ) {
  85. return;
  86. }
  87. // When caret is going stick to the bound of Text with attribute after movement then we need to override
  88. // the gravity before the move. But we need to do it in a custom way otherwise `selection#change:range`
  89. // event following the overriding will restore the gravity.
  90. if ( isStickToAttribute( nextPosition.nodeBefore, nextPosition.nodeAfter, attribute ) ) {
  91. model.change( writer => {
  92. let counter = 0;
  93. // So let's override the gravity.
  94. writer.overrideSelectionGravity( true );
  95. // But skip the following `change:range` event and restore the gravity on the next one.
  96. emitter.listenTo( modelSelection, 'change:range', ( evt, data ) => {
  97. if ( counter++ && data.directChange ) {
  98. writer.restoreSelectionGravity();
  99. evt.off();
  100. }
  101. } );
  102. } );
  103. }
  104. }
  105. } );
  106. }
  107. // @param {module:engine/model/node~Node} nextNode Node before the position.
  108. // @param {module:engine/model/node~Node} prevNode Node after the position.
  109. // @param {String} attribute Attribute name.
  110. // @returns {Boolean} `true` when position between the nodes sticks to the bound of text with given attribute.
  111. function isStickToAttribute( nextNode, prevNode, attribute ) {
  112. const isAttrInNext = nextNode ? nextNode.hasAttribute( attribute ) : false;
  113. const isAttrInPrev = prevNode ? prevNode.hasAttribute( attribute ) : false;
  114. return isAttrInNext && !isAttrInPrev || !isAttrInNext && isAttrInPrev;
  115. }
  116. // @param {module:engine/model/position~Position} position Initial position.
  117. // @returns {module:engine/model/position~Position|undefined} Previous position according to initial position in range.
  118. function getPreviousPosition( position ) {
  119. const iterator = Range.createIn( position.parent ).getPositions( {
  120. direction: 'backward',
  121. singleCharacters: true,
  122. startPosition: position
  123. } );
  124. // First position is the same as initial so we need to skip it.
  125. first( iterator );
  126. // Get position before the previous node of initial position.
  127. return first( iterator );
  128. }