documentselection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/documentselection
  7. */
  8. import Selection from './selection';
  9. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  10. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  11. /**
  12. * Class representing the document selection in the view.
  13. *
  14. * Its instance is available in {@link module:engine/view/document~Document#selection `Document#selection`}.
  15. *
  16. * It is similar to {@link module:engine/view/selection~Selection} but
  17. * it has a read-only API and can be modified only by the writer available in
  18. * the {@link module:engine/view/view~View#change `View#change()`} block
  19. * (so via {@link module:engine/view/downcastwriter~DowncastWriter#setSelection `DowncastWriter#setSelection()`}).
  20. */
  21. export default class DocumentSelection {
  22. /**
  23. * Creates new DocumentSelection instance.
  24. *
  25. * // Creates empty selection without ranges.
  26. * const selection = new DocumentSelection();
  27. *
  28. * // Creates selection at the given range.
  29. * const range = writer.createRange( start, end );
  30. * const selection = new DocumentSelection( range );
  31. *
  32. * // Creates selection at the given ranges
  33. * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( start2, end2 ) ];
  34. * const selection = new DocumentSelection( ranges );
  35. *
  36. * // Creates selection from the other selection.
  37. * const otherSelection = writer.createSelection();
  38. * const selection = new DocumentSelection( otherSelection );
  39. *
  40. * // Creates selection at the given position.
  41. * const position = writer.createPositionAt( root, offset );
  42. * const selection = new DocumentSelection( position );
  43. *
  44. * // Creates collapsed selection at the position of given item and offset.
  45. * const paragraph = writer.createContainerElement( 'paragraph' );
  46. * const selection = new DocumentSelection( paragraph, offset );
  47. *
  48. * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
  49. * // first child of that element and ends after the last child of that element.
  50. * const selection = new DocumentSelection( paragraph, 'in' );
  51. *
  52. * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
  53. * // just after the item.
  54. * const selection = new DocumentSelection( paragraph, 'on' );
  55. *
  56. * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
  57. *
  58. * // Creates backward selection.
  59. * const selection = new DocumentSelection( range, { backward: true } );
  60. *
  61. * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
  62. * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
  63. * represented in other way, for example by applying proper CSS class.
  64. *
  65. * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
  66. * (and be properly handled by screen readers).
  67. *
  68. * // Creates fake selection with label.
  69. * const selection = new DocumentSelection( range, { fake: true, label: 'foo' } );
  70. *
  71. * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
  72. * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|
  73. * module:engine/view/item~Item|null} [selectable=null]
  74. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
  75. * @param {Object} [options]
  76. * @param {Boolean} [options.backward] Sets this selection instance to be backward.
  77. * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
  78. * @param {String} [options.label] Label for the fake selection.
  79. */
  80. constructor( selectable = null, placeOrOffset, options ) {
  81. /**
  82. * Selection is used internally (`DocumentSelection` is a proxy to that selection).
  83. *
  84. * @private
  85. * @member {module:engine/view/selection~Selection}
  86. */
  87. this._selection = new Selection();
  88. // Delegate change event to be fired on DocumentSelection instance.
  89. this._selection.delegate( 'change' ).to( this );
  90. // Set selection data.
  91. this._selection.setTo( selectable, placeOrOffset, options );
  92. }
  93. /**
  94. * Returns true if selection instance is marked as `fake`.
  95. *
  96. * @see #_setTo
  97. * @returns {Boolean}
  98. */
  99. get isFake() {
  100. return this._selection.isFake;
  101. }
  102. /**
  103. * Returns fake selection label.
  104. *
  105. * @see #_setTo
  106. * @returns {String}
  107. */
  108. get fakeSelectionLabel() {
  109. return this._selection.fakeSelectionLabel;
  110. }
  111. /**
  112. * Selection anchor. Anchor may be described as a position where the selection starts. Together with
  113. * {@link #focus focus} they define the direction of selection, which is important
  114. * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
  115. * It may be a bit unintuitive when there are multiple ranges in selection.
  116. *
  117. * @see #focus
  118. * @type {module:engine/view/position~Position}
  119. */
  120. get anchor() {
  121. return this._selection.anchor;
  122. }
  123. /**
  124. * Selection focus. Focus is a position where the selection ends.
  125. *
  126. * @see #anchor
  127. * @type {module:engine/view/position~Position}
  128. */
  129. get focus() {
  130. return this._selection.focus;
  131. }
  132. /**
  133. * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
  134. * collapsed.
  135. *
  136. * @type {Boolean}
  137. */
  138. get isCollapsed() {
  139. return this._selection.isCollapsed;
  140. }
  141. /**
  142. * Returns number of ranges in selection.
  143. *
  144. * @type {Number}
  145. */
  146. get rangeCount() {
  147. return this._selection.rangeCount;
  148. }
  149. /**
  150. * Specifies whether the {@link #focus} precedes {@link #anchor}.
  151. *
  152. * @type {Boolean}
  153. */
  154. get isBackward() {
  155. return this._selection.isBackward;
  156. }
  157. /**
  158. * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this selection, or `null`
  159. * if the selection is not inside an editable element.
  160. *
  161. * @type {module:engine/view/editableelement~EditableElement|null}
  162. */
  163. get editableElement() {
  164. return this._selection.editableElement;
  165. }
  166. /**
  167. * Used for the compatibility with the {@link module:engine/view/selection~Selection#isEqual} method.
  168. *
  169. * @protected
  170. */
  171. get _ranges() {
  172. return this._selection._ranges;
  173. }
  174. /**
  175. * Returns an iterable that contains copies of all ranges added to the selection.
  176. *
  177. * @returns {Iterable.<module:engine/view/range~Range>}
  178. */
  179. * getRanges() {
  180. yield* this._selection.getRanges();
  181. }
  182. /**
  183. * Returns copy of the first range in the selection. First range is the one which
  184. * {@link module:engine/view/range~Range#start start} position {@link module:engine/view/position~Position#isBefore is before} start
  185. * position of all other ranges (not to confuse with the first range added to the selection).
  186. * Returns `null` if no ranges are added to selection.
  187. *
  188. * @returns {module:engine/view/range~Range|null}
  189. */
  190. getFirstRange() {
  191. return this._selection.getFirstRange();
  192. }
  193. /**
  194. * Returns copy of the last range in the selection. Last range is the one which {@link module:engine/view/range~Range#end end}
  195. * position {@link module:engine/view/position~Position#isAfter is after} end position of all other ranges (not to confuse
  196. * with the last range added to the selection). Returns `null` if no ranges are added to selection.
  197. *
  198. * @returns {module:engine/view/range~Range|null}
  199. */
  200. getLastRange() {
  201. return this._selection.getLastRange();
  202. }
  203. /**
  204. * Returns copy of the first position in the selection. First position is the position that
  205. * {@link module:engine/view/position~Position#isBefore is before} any other position in the selection ranges.
  206. * Returns `null` if no ranges are added to selection.
  207. *
  208. * @returns {module:engine/view/position~Position|null}
  209. */
  210. getFirstPosition() {
  211. return this._selection.getFirstPosition();
  212. }
  213. /**
  214. * Returns copy of the last position in the selection. Last position is the position that
  215. * {@link module:engine/view/position~Position#isAfter is after} any other position in the selection ranges.
  216. * Returns `null` if no ranges are added to selection.
  217. *
  218. * @returns {module:engine/view/position~Position|null}
  219. */
  220. getLastPosition() {
  221. return this._selection.getLastPosition();
  222. }
  223. /**
  224. * Returns the selected element. {@link module:engine/view/element~Element Element} is considered as selected if there is only
  225. * one range in the selection, and that range contains exactly one element.
  226. * Returns `null` if there is no selected element.
  227. *
  228. * @returns {module:engine/view/element~Element|null}
  229. */
  230. getSelectedElement() {
  231. return this._selection.getSelectedElement();
  232. }
  233. /**
  234. * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
  235. * same number of ranges and all ranges from one selection equal to a range from other selection.
  236. *
  237. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
  238. * Selection to compare with.
  239. * @returns {Boolean} `true` if selections are equal, `false` otherwise.
  240. */
  241. isEqual( otherSelection ) {
  242. return this._selection.isEqual( otherSelection );
  243. }
  244. /**
  245. * Checks whether this selection is similar to given selection. Selections are similar if they have same directions, same
  246. * number of ranges, and all {@link module:engine/view/range~Range#getTrimmed trimmed} ranges from one selection are
  247. * equal to any trimmed range from other selection.
  248. *
  249. * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
  250. * Selection to compare with.
  251. * @returns {Boolean} `true` if selections are similar, `false` otherwise.
  252. */
  253. isSimilar( otherSelection ) {
  254. return this._selection.isSimilar( otherSelection );
  255. }
  256. /**
  257. * Sets this selection's ranges and direction to the specified location based on the given
  258. * {@link module:engine/view/documentselection~DocumentSelection document selection},
  259. * {@link module:engine/view/selection~Selection selection}, {@link module:engine/view/position~Position position},
  260. * {@link module:engine/view/item~Item item}, {@link module:engine/view/range~Range range},
  261. * an iterable of {@link module:engine/view/range~Range ranges} or null.
  262. *
  263. * // Sets selection to the given range.
  264. * const range = writer.createRange( start, end );
  265. * documentSelection._setTo( range );
  266. *
  267. * // Sets selection to given ranges.
  268. * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( start2, end2 ) ];
  269. * documentSelection._setTo( range );
  270. *
  271. * // Sets selection to the other selection.
  272. * const otherSelection = writer.createSelection();
  273. * documentSelection._setTo( otherSelection );
  274. *
  275. * // Sets collapsed selection at the given position.
  276. * const position = writer.createPositionAt( root, offset );
  277. * documentSelection._setTo( position );
  278. *
  279. * // Sets collapsed selection at the position of given item and offset.
  280. * documentSelection._setTo( paragraph, offset );
  281. *
  282. * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
  283. * that element and ends after the last child of that element.
  284. *
  285. * documentSelection._setTo( paragraph, 'in' );
  286. *
  287. * Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends just after the item.
  288. *
  289. * documentSelection._setTo( paragraph, 'on' );
  290. *
  291. * // Clears selection. Removes all ranges.
  292. * documentSelection._setTo( null );
  293. *
  294. * `Selection#_setTo()` method allow passing additional options (`backward`, `fake` and `label`) as the last argument.
  295. *
  296. * // Sets selection as backward.
  297. * documentSelection._setTo( range, { backward: true } );
  298. *
  299. * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
  300. * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
  301. * represented in other way, for example by applying proper CSS class.
  302. *
  303. * Additionally fake's selection label can be provided. It will be used to des cribe fake selection in DOM
  304. * (and be properly handled by screen readers).
  305. *
  306. * // Creates fake selection with label.
  307. * documentSelection._setTo( range, { fake: true, label: 'foo' } );
  308. *
  309. * @protected
  310. * @fires change
  311. * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
  312. * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|module:engine/view/item~Item|null} selectable
  313. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
  314. * @param {Object} [options]
  315. * @param {Boolean} [options.backward] Sets this selection instance to be backward.
  316. * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
  317. * @param {String} [options.label] Label for the fake selection.
  318. */
  319. _setTo( selectable, placeOrOffset, options ) {
  320. this._selection.setTo( selectable, placeOrOffset, options );
  321. }
  322. /**
  323. * Moves {@link #focus} to the specified location.
  324. *
  325. * The location can be specified in the same form as {@link module:engine/view/view~View#createPositionAt view.createPositionAt()}
  326. * parameters.
  327. *
  328. * @protected
  329. * @fires change
  330. * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
  331. * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
  332. * first parameter is a {@link module:engine/view/item~Item view item}.
  333. */
  334. _setFocus( itemOrPosition, offset ) {
  335. this._selection.setFocus( itemOrPosition, offset );
  336. }
  337. /**
  338. * Fired whenever selection ranges are changed through {@link ~DocumentSelection Selection API}.
  339. *
  340. * @event change
  341. */
  342. }
  343. mix( DocumentSelection, EmitterMixin );