bindtwostepcarettoattribute.js 5.5 KB

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