8
0

selection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import CKEditorError from '../../utils/ckeditorerror.js';
  6. import Range from './range.js';
  7. import Position from './position.js';
  8. import mix from '../../utils/mix.js';
  9. import EmitterMixin from '../../utils/emittermixin.js';
  10. /**
  11. * Class representing selection in tree view.
  12. *
  13. * Selection can consist of {@link engine.view.Range ranges} that can be added using
  14. * {@link engine.view.Selection#addRange addRange} and {@link engine.view.Selection#setRanges setRanges} methods.
  15. * Both methods create copies of provided ranges and store those copies internally. Further modifications to passed
  16. * ranges will not change selection's state.
  17. * Selection's ranges can be obtained via {@link engine.view.Selection#getRanges getRanges},
  18. * {@link engine.view.Selection#getFirstRange getFirstRange} and {@link engine.view.Selection#getLastRange getLastRange}
  19. * methods, which return copies of ranges stored inside selection. Modifications made on these copies will not change
  20. * selection's state. Similar situation occurs when getting {@link engine.view.Selection#anchor anchor},
  21. * {@link engine.view.Selection#focus focus}, {@link engine.view.Selection#getFirstPosition first} and
  22. * {@link engine.view.Selection#getLastPosition last} positions - all will return copies of requested positions.
  23. *
  24. * @memberOf engine.view
  25. */
  26. export default class Selection {
  27. /**
  28. * Creates new selection instance.
  29. */
  30. constructor() {
  31. /**
  32. * Stores all ranges that are selected.
  33. *
  34. * @protected
  35. * @member {Array.<engine.view.Range>} engine.view.Selection#_ranges
  36. */
  37. this._ranges = [];
  38. /**
  39. * Specifies whether the last added range was added as a backward or forward range.
  40. *
  41. * @protected
  42. * @member {Boolean} engine.view.Selection#_lastRangeBackward
  43. */
  44. this._lastRangeBackward = false;
  45. }
  46. /**
  47. * Selection anchor. Anchor may be described as a position where the selection starts. Together with
  48. * {@link engine.view.Selection#focus focus} they define the direction of selection, which is important
  49. * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
  50. * It may be a bit unintuitive when there are multiple ranges in selection.
  51. *
  52. * @see engine.view.Selection#focus
  53. * @type {engine.view.Position}
  54. */
  55. get anchor() {
  56. if ( !this._ranges.length ) {
  57. return null;
  58. }
  59. const range = this._ranges[ this._ranges.length - 1 ];
  60. const anchor = this._lastRangeBackward ? range.end : range.start;
  61. return Position.createFromPosition( anchor );
  62. }
  63. /**
  64. * Selection focus. Focus is a position where the selection ends.
  65. *
  66. * @see engine.view.Selection#anchor
  67. * @type {engine.view.Position}
  68. */
  69. get focus() {
  70. if ( !this._ranges.length ) {
  71. return null;
  72. }
  73. const range = this._ranges[ this._ranges.length - 1 ];
  74. const focus = this._lastRangeBackward ? range.start : range.end;
  75. return Position.createFromPosition( focus );
  76. }
  77. /**
  78. * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
  79. * collapsed.
  80. *
  81. * @type {Boolean}
  82. */
  83. get isCollapsed() {
  84. return this.rangeCount === 1 && this._ranges[ 0 ].isCollapsed;
  85. }
  86. /**
  87. * Returns number of ranges in selection.
  88. *
  89. * @type {Number}
  90. */
  91. get rangeCount() {
  92. return this._ranges.length;
  93. }
  94. /**
  95. * Specifies whether the {@link engine.view.Selection#focus} precedes {@link engine.view.Selection#anchor}.
  96. *
  97. * @type {Boolean}
  98. */
  99. get isBackward() {
  100. return !this.isCollapsed && this._lastRangeBackward;
  101. }
  102. /**
  103. * Returns {@link engine.view.EditableElement EditableElement} instance that contains this selection.
  104. *
  105. * @returns {engine.view.EditableElement|null} Returns closest EditableElement or null if none is found.
  106. */
  107. get editableElement() {
  108. if ( this.rangeCount ) {
  109. return this.getFirstPosition().editableElement;
  110. }
  111. return null;
  112. }
  113. /**
  114. * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
  115. * selection instance and you can safely operate on it.
  116. *
  117. * Accepts a flag describing in which way the selection is made - passed range might be selected from
  118. * {@link engine.view.Range#start start} to {@link engine.view.Range#end end}
  119. * or from {@link engine.view.Range#end end} to {@link engine.view.Range#start start}.
  120. * The flag is used to set {@link engine.view.Selection#anchor anchor} and
  121. * {@link engine.view.Selection#focus focus} properties.
  122. *
  123. * Throws {@link utils.CKEditorError CKEditorError} `selection-range-intersects` if added range intersects
  124. * with ranges already stored in Selection instance.
  125. *
  126. * @fires engine.view.Selection#change
  127. * @param {engine.view.Range} range
  128. */
  129. addRange( range, isBackward ) {
  130. if ( !( range instanceof Range ) ) {
  131. throw new CKEditorError( 'selection-invalid-range: Invalid Range.' );
  132. }
  133. this._pushRange( range );
  134. this._lastRangeBackward = !!isBackward;
  135. this.fire( 'change' );
  136. }
  137. /**
  138. * Returns an iterator that contains copies of all ranges added to the selection.
  139. *
  140. * @returns {Iterator.<engine.view.Range>}
  141. */
  142. *getRanges() {
  143. for ( let range of this._ranges ) {
  144. yield Range.createFromRange( range );
  145. }
  146. }
  147. /**
  148. * Returns copy of the first range in the selection. First range is the one which
  149. * {@link engine.view.Range#start start} position {@link engine.view.Position#isBefore is before} start
  150. * position of all other ranges (not to confuse with the first range added to the selection).
  151. * Returns `null` if no ranges are added to selection.
  152. *
  153. * @returns {engine.view.Range|null}
  154. */
  155. getFirstRange() {
  156. let first = null;
  157. for ( let range of this._ranges ) {
  158. if ( !first || range.start.isBefore( first.start ) ) {
  159. first = range;
  160. }
  161. }
  162. return first ? Range.createFromRange( first ) : null;
  163. }
  164. /**
  165. * Returns copy of the last range in the selection. Last range is the one which {@link engine.view.Range#end end}
  166. * position {@link engine.view.Position#isAfter is after} end position of all other ranges (not to confuse
  167. * with the last range added to the selection). Returns `null` if no ranges are added to selection.
  168. *
  169. * @returns {engine.view.Range|null}
  170. */
  171. getLastRange() {
  172. let last = null;
  173. for ( let range of this._ranges ) {
  174. if ( !last || range.end.isAfter( last.end ) ) {
  175. last = range;
  176. }
  177. }
  178. return last ? Range.createFromRange( last ) : null;
  179. }
  180. /**
  181. * Returns copy of the first position in the selection. First position is the position that
  182. * {@link engine.view.Position#isBefore is before} any other position in the selection ranges.
  183. * Returns `null` if no ranges are added to selection.
  184. *
  185. * @returns {engine.view.Position|null}
  186. */
  187. getFirstPosition() {
  188. const firstRange = this.getFirstRange();
  189. return firstRange ? Position.createFromPosition( firstRange.start ) : null;
  190. }
  191. /**
  192. * Returns copy of the last position in the selection. Last position is the position that
  193. * {@link engine.view.Position#isAfter is after} any other position in the selection ranges.
  194. * Returns `null` if no ranges are added to selection.
  195. *
  196. * @returns {engine.view.Position|null}
  197. */
  198. getLastPosition() {
  199. const lastRange = this.getLastRange();
  200. return lastRange ? Position.createFromPosition( lastRange.end ) : null;
  201. }
  202. /**
  203. * Checks whether, this selection is equal to given selection. Selections equal if they have the same ranges and directions.
  204. *
  205. * @param {engine.view.Selection} otherSelection Selection to compare with.
  206. * @returns {Boolean} `true` if selections are equal, `false` otherwise.
  207. */
  208. isEqual( otherSelection ) {
  209. const rangeCount = this.rangeCount;
  210. if ( rangeCount != otherSelection.rangeCount ) {
  211. return false;
  212. }
  213. for ( let i = 0; i < this.rangeCount; i++ ) {
  214. if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) {
  215. return false;
  216. }
  217. }
  218. return this._lastRangeBackward === otherSelection._lastRangeBackward;
  219. }
  220. /**
  221. * Removes all ranges that were added to the selection.
  222. *
  223. * @fires engine.view.Selection#change
  224. */
  225. removeAllRanges() {
  226. if ( this._ranges.length ) {
  227. this._ranges = [];
  228. this.fire( 'change' );
  229. }
  230. }
  231. /**
  232. * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
  233. * is treated like the last added range and is used to set {@link engine.view.Selection#anchor anchor} and
  234. * {@link engine.view.Selection#focus focus}. Accepts a flag describing in which way the selection is made
  235. * (see {@link engine.view.Selection#addRange addRange}).
  236. *
  237. * @fires engine.view.Selection#change
  238. * @param {Array.<engine.view.Range>} newRanges Array of ranges to set.
  239. * @param {Boolean} [isLastBackward] Flag describing if last added range was selected forward - from start to end
  240. * (`false`) or backward - from end to start (`true`). Defaults to `false`.
  241. */
  242. setRanges( newRanges, isLastBackward ) {
  243. this._ranges = [];
  244. for ( let range of newRanges ) {
  245. if ( !( range instanceof Range ) ) {
  246. throw new CKEditorError( 'selection-invalid-range: Invalid Range.' );
  247. }
  248. this._pushRange( range );
  249. }
  250. this._lastRangeBackward = !!isLastBackward;
  251. this.fire( 'change' );
  252. }
  253. /**
  254. * Sets this selection's ranges and direction to the ranges and direction of the given selection.
  255. *
  256. * @param {engine.view.Selection} otherSelection
  257. */
  258. setTo( otherSelection ) {
  259. this.setRanges( otherSelection.getRanges(), otherSelection.isBackward );
  260. }
  261. /**
  262. * Sets collapsed selection in the specified location.
  263. *
  264. * The location can be specified in the same form as {@link engine.view.Position.createAt} parameters.
  265. *
  266. * @fires engine.view.Selection#change
  267. * @param {engine.view.Item|engine.view.Position} itemOrPosition
  268. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  269. * first parameter is a {@link engine.view.Item view item}.
  270. */
  271. collapse( itemOrPosition, offset ) {
  272. const pos = Position.createAt( itemOrPosition, offset );
  273. const range = new Range( pos, pos );
  274. this.setRanges( [ range ] );
  275. }
  276. /**
  277. * Collapses selection to the selection's {@link engine.view.Selection#getFirstPosition first position}.
  278. * All ranges, besides the collapsed one, will be removed. Nothing will change if there are no ranges stored
  279. * inside selection.
  280. *
  281. * @fires engine.view.Selection#change
  282. */
  283. collapseToStart() {
  284. const startPosition = this.getFirstPosition();
  285. if ( startPosition !== null ) {
  286. this.setRanges( [ new Range( startPosition, startPosition ) ] );
  287. }
  288. }
  289. /**
  290. * Collapses selection to the selection's {@link engine.view.Selection#getLastPosition last position}.
  291. * All ranges, besides the collapsed one, will be removed. Nothing will change if there are no ranges stored
  292. * inside selection.
  293. *
  294. * @fires engine.view.Selection#change
  295. */
  296. collapseToEnd() {
  297. const endPosition = this.getLastPosition();
  298. if ( endPosition !== null ) {
  299. this.setRanges( [ new Range( endPosition, endPosition ) ] );
  300. }
  301. }
  302. /**
  303. * Creates and returns an instance of `Selection` that is a clone of given selection, meaning that it has same
  304. * ranges and same direction as this selection.
  305. *
  306. * @params {engine.view.Selection} otherSelection Selection to be cloned.
  307. * @returns {engine.view.Selection} `Selection` instance that is a clone of given selection.
  308. */
  309. static createFromSelection( otherSelection ) {
  310. const selection = new Selection();
  311. selection.setTo( otherSelection );
  312. return selection;
  313. }
  314. /**
  315. * Adds range to selection - creates copy of given range so it can be safely used and modified.
  316. *
  317. * Throws {@link utils.CKEditorError CKEditorError} `selection-range-intersects` if added range intersects
  318. * with ranges already stored in selection instance.
  319. *
  320. * @private
  321. * @param {engine.view.Range} range
  322. */
  323. _pushRange( range ) {
  324. for ( let storedRange of this._ranges ) {
  325. if ( range.isIntersecting( storedRange ) ) {
  326. /**
  327. * Trying to add a range that intersects with another range from selection.
  328. *
  329. * @error selection-range-intersects
  330. * @param {engine.view.Range} addedRange Range that was added to the selection.
  331. * @param {engine.view.Range} intersectingRange Range from selection that intersects with `addedRange`.
  332. */
  333. throw new CKEditorError(
  334. 'selection-range-intersects: Trying to add a range that intersects with another range from selection.',
  335. { addedRange: range, intersectingRange: storedRange }
  336. );
  337. }
  338. }
  339. this._ranges.push( Range.createFromRange( range ) );
  340. }
  341. }
  342. mix( Selection, EmitterMixin );
  343. /**
  344. * Fired whenever selection ranges are changed through {@link engine.view.Selection Selection API}.
  345. *
  346. * @event engine.view.Selection#change
  347. */