8
0

getlasttextline.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module typing/utils/getlasttextline
  7. */
  8. /**
  9. * Returns the last text line from the given range.
  10. *
  11. * "The last text line" is understood as text (from one or more text nodes) which is limited either by a parent block
  12. * or by inline elements (e.g. `<softBreak>`).
  13. *
  14. * @protected
  15. * @param {module:engine/model/range~Range} range
  16. * @param {module:engine/model/model~Model} model
  17. * @returns {module:typing/utils/getlasttextline~LastTextLineData}
  18. */
  19. export default function getLastTextLine( range, model ) {
  20. let start = range.start;
  21. const text = Array.from( range.getItems() ).reduce( ( rangeText, node ) => {
  22. // Trim text to a last occurrence of an inline element and update range start.
  23. if ( !( node.is( 'text' ) || node.is( 'textProxy' ) ) ) {
  24. start = model.createPositionAfter( node );
  25. return '';
  26. }
  27. return rangeText + node.data;
  28. }, '' );
  29. return { text, range: model.createRange( start, range.end ) };
  30. }
  31. /**
  32. * The value returned by {@link module:typing/utils/getlasttextline~getLastTextLine}.
  33. *
  34. * @typedef {Object} module:typing/utils/getlasttextline~LastTextLineData
  35. *
  36. * @property {String} text The text from the text nodes in the last text line.
  37. * @property {module:engine/model/range~Range} The range set on the text nodes in the last text line.
  38. */