bindtwostepcarettoattribute.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const position = modelSelection.getFirstPosition();
  50. // Moving right.
  51. if ( arrowRightPressed ) {
  52. // If gravity is already overridden then do nothing.
  53. // It means that we already enter `foo<a>{}bar</a>biz` or left `foo<a>bar</a>{}biz` text with attribute
  54. // and gravity will be restored just after caret movement.
  55. if ( modelSelection.isGravityOverridden ) {
  56. return;
  57. }
  58. // If caret sticks to the bound of Text with attribute it means that we are going to
  59. // enter `foo{}<a>bar</a>biz` or leave `foo<a>bar{}</a>biz` the text with attribute.
  60. if ( isStickToAttribute( position.nodeAfter, position.nodeBefore, attribute ) ) {
  61. // So we need to prevent caret from being moved.
  62. data.preventDefault();
  63. // And override default selection gravity.
  64. model.change( writer => writer.overrideSelectionGravity() );
  65. }
  66. // Moving left.
  67. } else {
  68. // If caret sticks to the bound of Text with attribute and gravity is already overridden it means that
  69. // we are going to enter `foo<a>bar</a>{}biz` or leave `foo<a>{}bar</a>biz` text with attribute.
  70. if ( modelSelection.isGravityOverridden && isStickToAttribute( position.nodeBefore, position.nodeAfter, attribute ) ) {
  71. // So we need to prevent cater from being moved.
  72. data.preventDefault();
  73. // And restore the gravity.
  74. model.change( writer => writer.restoreSelectionGravity() );
  75. return;
  76. }
  77. // If we are here we need to check if caret is a one character before the text with attribute bound
  78. // `foo<a>bar</a>b{}iz` or `foo<a>b{}ar</a>biz`.
  79. const nextPosition = position.getShiftedBy( -1 );
  80. // When position is the same it means that parent bound has been reached.
  81. if ( !nextPosition.isBefore( position ) ) {
  82. return;
  83. }
  84. // When caret is going stick to the bound of Text with attribute after movement then we need to override
  85. // the gravity before the move. But we need to do it in a custom way otherwise `selection#change:range`
  86. // event following the overriding will restore the gravity.
  87. if ( isStickToAttribute( nextPosition.nodeBefore, nextPosition.nodeAfter, attribute ) ) {
  88. model.change( writer => {
  89. let counter = 0;
  90. // So let's override the gravity.
  91. writer.overrideSelectionGravity( true );
  92. // But skip the following `change:range` event and restore the gravity on the next one.
  93. emitter.listenTo( modelSelection, 'change:range', ( evt, data ) => {
  94. if ( counter++ && data.directChange ) {
  95. writer.restoreSelectionGravity();
  96. evt.off();
  97. }
  98. } );
  99. } );
  100. }
  101. }
  102. } );
  103. }
  104. // @param {module:engine/model/node~Node} nextNode Node before the position.
  105. // @param {module:engine/model/node~Node} prevNode Node after the position.
  106. // @param {String} attribute Attribute name.
  107. // @returns {Boolean} `true` when position between the nodes sticks to the bound of text with given attribute.
  108. function isStickToAttribute( nextNode, prevNode, attribute ) {
  109. const isAttrInNext = nextNode ? nextNode.hasAttribute( attribute ) : false;
  110. const isAttrInPrev = prevNode ? prevNode.hasAttribute( attribute ) : false;
  111. if ( isAttrInNext && isAttrInPrev && nextNode.getAttributeKeys( attribute ) !== prevNode.getAttribute( attribute ) ) {
  112. return true;
  113. }
  114. return isAttrInNext && !isAttrInPrev || !isAttrInNext && isAttrInPrev;
  115. }