|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @module engine/utils/bindtwostepcarettoattribute |
| 8 | + */ |
| 9 | + |
| 10 | +import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard'; |
| 11 | + |
| 12 | +/** |
| 13 | + * This helper adds two-step caret movement behavior for the given attribute. |
| 14 | + * |
| 15 | + * For example, when this behavior is enabled for the `linkHref` attribute (which converts to `<a>` element in the view) |
| 16 | + * and the caret is just before an `<a>` element (at a link boundary), then pressing |
| 17 | + * the right arrow key will move caret into that `<a>`element instead of moving it after the next character: |
| 18 | + * |
| 19 | + * * With two-step caret movement: `<p>foo{}<a>bar</a>biz<p>` + <kbd>→</kbd> => `<p>foo<a>{}bar</a>biz<p>` |
| 20 | + * * Without two-step caret movement: `<p>foo{}<a>bar</a>biz<p>` + <kbd>→</kbd> => `<p>foo<a>b{}ar</a>biz<p>` |
| 21 | + * |
| 22 | + * The same behavior will be changed fo "leaving" an attribute element: |
| 23 | + * |
| 24 | + * * With two-step caret movement: `<p>foo<a>bar{}</a>biz<p>` + <kbd>→</kbd> => `<p>foo<a>bar</a>{}biz<p>` |
| 25 | + * * Without two-step caret movement: `<p>foo<a>bar{}</a>biz<p>` + <kbd>→</kbd> => `<p>foo<a>bar</a>b{}iz<p>` |
| 26 | + * |
| 27 | + * And when moving left: |
| 28 | + * |
| 29 | + * * With two-step caret movement: `<p>foo<a>bar</a>b{}iz<p>` + <kbd>←</kbd> => `<p>foo<a>bar</a>{}biz<p>` + |
| 30 | + * <kbd>←</kbd> => `<p>foo<a>bar{}</a>biz<p>` |
| 31 | + * * Without two-step caret movement: `<p>foo<a>bar</a>b{}iz<p>` + <kbd>←</kbd> => `<p>foo<a>bar{}</a>biz<p>` |
| 32 | + * |
| 33 | + * @param {module:engine/view/view~View} view View controller instance. |
| 34 | + * @param {module:engine/model/model~Model} model Data model instance. |
| 35 | + * @param {module:utils/dom/emittermixin~Emitter} emitter The emitter to which this behavior should be added |
| 36 | + * (e.g. a plugin instance). |
| 37 | + * @param {String} attribute Attribute for which this behavior will be added. |
| 38 | + */ |
| 39 | +export default function bindTwoStepCaretToAttribute( view, model, emitter, attribute ) { |
| 40 | + const modelSelection = model.document.selection; |
| 41 | + |
| 42 | + // Listen to keyboard events and handle cursor before the move. |
| 43 | + emitter.listenTo( view.document, 'keydown', ( evt, data ) => { |
| 44 | + const arrowRightPressed = data.keyCode == keyCodes.arrowright; |
| 45 | + const arrowLeftPressed = data.keyCode == keyCodes.arrowleft; |
| 46 | + |
| 47 | + // When neither left or right arrow has been pressed then do noting. |
| 48 | + if ( !arrowRightPressed && !arrowLeftPressed ) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // This implementation works only for collapsed selection. |
| 53 | + if ( !modelSelection.isCollapsed ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // When user tries to expand selection or jump over the whole word or to the beginning/end then |
| 58 | + // two-steps movement is not necessary. |
| 59 | + if ( data.shiftKey || data.altKey || data.ctrlKey ) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const position = modelSelection.getFirstPosition(); |
| 64 | + |
| 65 | + // Moving right. |
| 66 | + if ( arrowRightPressed ) { |
| 67 | + // If gravity is already overridden then do nothing. |
| 68 | + // It means that we already enter `foo<a>{}bar</a>biz` or left `foo<a>bar</a>{}biz` text with attribute |
| 69 | + // and gravity will be restored just after caret movement. |
| 70 | + if ( modelSelection.isGravityOverridden ) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // If caret sticks to the bound of Text with attribute it means that we are going to |
| 75 | + // enter `foo{}<a>bar</a>biz` or leave `foo<a>bar{}</a>biz` the text with attribute. |
| 76 | + if ( isAtAttributeBoundary( position.nodeAfter, position.nodeBefore, attribute ) ) { |
| 77 | + // So we need to prevent caret from being moved. |
| 78 | + data.preventDefault(); |
| 79 | + // And override default selection gravity. |
| 80 | + model.change( writer => writer.overrideSelectionGravity() ); |
| 81 | + } |
| 82 | + |
| 83 | + // Moving left. |
| 84 | + } else { |
| 85 | + // If caret sticks to the bound of Text with attribute and gravity is already overridden it means that |
| 86 | + // we are going to enter `foo<a>bar</a>{}biz` or leave `foo<a>{}bar</a>biz` text with attribute. |
| 87 | + if ( modelSelection.isGravityOverridden && isAtAttributeBoundary( position.nodeBefore, position.nodeAfter, attribute ) ) { |
| 88 | + // So we need to prevent cater from being moved. |
| 89 | + data.preventDefault(); |
| 90 | + // And restore the gravity. |
| 91 | + model.change( writer => writer.restoreSelectionGravity() ); |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // If we are here we need to check if caret is a one character before the text with attribute bound |
| 97 | + // `foo<a>bar</a>b{}iz` or `foo<a>b{}ar</a>biz`. |
| 98 | + const nextPosition = position.getShiftedBy( -1 ); |
| 99 | + |
| 100 | + // When position is the same it means that parent bound has been reached. |
| 101 | + if ( !nextPosition.isBefore( position ) ) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // When caret is going stick to the bound of Text with attribute after movement then we need to override |
| 106 | + // the gravity before the move. But we need to do it in a custom way otherwise `selection#change:range` |
| 107 | + // event following the overriding will restore the gravity. |
| 108 | + if ( isAtAttributeBoundary( nextPosition.nodeBefore, nextPosition.nodeAfter, attribute ) ) { |
| 109 | + model.change( writer => { |
| 110 | + let counter = 0; |
| 111 | + |
| 112 | + // So let's override the gravity. |
| 113 | + writer.overrideSelectionGravity( true ); |
| 114 | + |
| 115 | + // But skip the following `change:range` event and restore the gravity on the next one. |
| 116 | + emitter.listenTo( modelSelection, 'change:range', ( evt, data ) => { |
| 117 | + if ( counter++ && data.directChange ) { |
| 118 | + writer.restoreSelectionGravity(); |
| 119 | + evt.off(); |
| 120 | + } |
| 121 | + } ); |
| 122 | + } ); |
| 123 | + } |
| 124 | + } |
| 125 | + } ); |
| 126 | +} |
| 127 | + |
| 128 | +// @param {module:engine/model/node~Node} nextNode Node before the position. |
| 129 | +// @param {module:engine/model/node~Node} prevNode Node after the position. |
| 130 | +// @param {String} attribute Attribute name. |
| 131 | +// @returns {Boolean} `true` when position between the nodes sticks to the bound of text with given attribute. |
| 132 | +function isAtAttributeBoundary( nextNode, prevNode, attribute ) { |
| 133 | + const isAttrInNext = nextNode ? nextNode.hasAttribute( attribute ) : false; |
| 134 | + const isAttrInPrev = prevNode ? prevNode.hasAttribute( attribute ) : false; |
| 135 | + |
| 136 | + if ( isAttrInNext && isAttrInPrev && nextNode.getAttributeKeys( attribute ) !== prevNode.getAttribute( attribute ) ) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + return isAttrInNext && !isAttrInPrev || !isAttrInNext && isAttrInPrev; |
| 141 | +} |
0 commit comments