markercollection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/markercollection
  7. */
  8. import LiveRange from './liverange';
  9. import Position from './position';
  10. import Range from './range';
  11. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  14. /**
  15. * Creates, stores and manages {@link ~Marker markers}.
  16. *
  17. * Markers are created by {@link ~MarkerCollection#set setting} a name for a {@link module:engine/model/liverange~LiveRange live range}
  18. * in `MarkerCollection`. Name is used to group and identify markers. Names have to be unique, but markers can be grouped by
  19. * using common prefixes, separated with `:`, for example: `user:john` or `search:3`.
  20. *
  21. * Since markers are based on {@link module:engine/model/liverange~LiveRange live ranges}, for efficiency reasons, it's
  22. * best to create and keep at least markers as possible.
  23. */
  24. export default class MarkerCollection {
  25. /**
  26. * Creates a markers collection.
  27. */
  28. constructor() {
  29. /**
  30. * Stores {@link ~Marker markers} added to the collection.
  31. *
  32. * @private
  33. * @member {Map} #_markers
  34. */
  35. this._markers = new Map();
  36. }
  37. /**
  38. * Returns an iterator that iterates over all {@link ~Marker markers} added to the collection.
  39. *
  40. * @returns {Iterator}
  41. */
  42. [ Symbol.iterator ]() {
  43. return this._markers.values();
  44. }
  45. /**
  46. * Checks if marker with given `markerName` is in the collection.
  47. *
  48. * @param {String} markerName Marker name.
  49. * @returns {Boolean} `true` if marker with given `markerName` is in the collection, `false` otherwise.
  50. */
  51. has( markerName ) {
  52. return this._markers.has( markerName );
  53. }
  54. /**
  55. * Returns {@link ~Marker marker} with given `markerName`.
  56. *
  57. * @param {String} markerName Name of marker to get.
  58. * @returns {module:engine/model/markercollection~Marker|null} Marker with given name or `null` if such marker was
  59. * not added to the collection.
  60. */
  61. get( markerName ) {
  62. return this._markers.get( markerName ) || null;
  63. }
  64. /**
  65. * Creates and adds a {@link ~Marker marker} to the `MarkerCollection` with given name on given
  66. * {@link module:engine/model/range~Range range}.
  67. *
  68. * If `MarkerCollection` already had a marker with given name (or {@link ~Marker marker} was passed) and the range to
  69. * set is different, the marker in collection is removed and then new marker is added. If the range was same, nothing
  70. * happens and `false` is returned.
  71. *
  72. * @fires module:engine/model/markercollection~MarkerCollection#event:add
  73. * @fires module:engine/model/markercollection~MarkerCollection#event:remove
  74. * @param {String|module:engine/model/markercollection~Marker} markerOrName Name of marker to add or Marker instance to update.
  75. * @param {module:engine/model/range~Range} range Marker range.
  76. * @returns {module:engine/model/markercollection~Marker} `Marker` instance added to the collection.
  77. */
  78. set( markerOrName, range ) {
  79. const markerName = markerOrName instanceof Marker ? markerOrName.name : markerOrName;
  80. const oldMarker = this._markers.get( markerName );
  81. if ( oldMarker ) {
  82. const oldRange = oldMarker.getRange();
  83. if ( oldRange.isEqual( range ) ) {
  84. return oldMarker;
  85. }
  86. this.remove( markerName );
  87. }
  88. const liveRange = LiveRange.createFromRange( range );
  89. const marker = new Marker( markerName, liveRange );
  90. this._markers.set( markerName, marker );
  91. this.fire( 'add:' + markerName, marker );
  92. return marker;
  93. }
  94. /**
  95. * Removes given {@link ~Marker marker} or a marker with given name from the `MarkerCollection`.
  96. *
  97. * @param {String} markerOrName Marker or name of a marker to remove.
  98. * @returns {Boolean} `true` if marker was found and removed, `false` otherwise.
  99. */
  100. remove( markerOrName ) {
  101. const markerName = markerOrName instanceof Marker ? markerOrName.name : markerOrName;
  102. const oldMarker = this._markers.get( markerName );
  103. if ( oldMarker ) {
  104. this._markers.delete( markerName );
  105. this.fire( 'remove:' + markerName, oldMarker );
  106. this._destroyMarker( oldMarker );
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Returns iterator that iterates over all markers, which ranges contain given {@link module:engine/model/position~Position position}.
  113. *
  114. * @param {module:engine/model/position~Position} position
  115. * @returns {Iterator.<module:engine/model/markercollection~Marker>}
  116. */
  117. * getMarkersAtPosition( position ) {
  118. for ( const marker of this ) {
  119. if ( marker.getRange().containsPosition( position ) ) {
  120. yield marker;
  121. }
  122. }
  123. }
  124. /**
  125. * Destroys markers collection.
  126. */
  127. destroy() {
  128. for ( const marker of this._markers.values() ) {
  129. this._destroyMarker( marker );
  130. }
  131. this._markers = null;
  132. this.stopListening();
  133. }
  134. /**
  135. * Iterates over all markers that starts with given `prefix`.
  136. *
  137. * const markerFooA = markersCollection.set( 'foo:a', rangeFooA );
  138. * const markerFooB = markersCollection.set( 'foo:b', rangeFooB );
  139. * const markerBarA = markersCollection.set( 'bar:a', rangeBarA );
  140. * const markerFooBarA = markersCollection.set( 'foobar:a', rangeFooBarA );
  141. * Array.from( markersCollection.getMarkersGroup( 'foo' ) ); // [ markerFooA, markerFooB ]
  142. * Array.from( markersCollection.getMarkersGroup( 'a' ) ); // []
  143. *
  144. * @param prefix
  145. * @returns {Iterator.<module:engine/model/markercollection~Marker>}
  146. */
  147. * getMarkersGroup( prefix ) {
  148. for ( const marker of this._markers.values() ) {
  149. if ( marker.name.startsWith( prefix + ':' ) ) {
  150. yield marker;
  151. }
  152. }
  153. }
  154. /**
  155. * Destroys marker.
  156. *
  157. * @private
  158. * @param {module:engine/model/markercollection~Marker} marker Marker to destroy.
  159. */
  160. _destroyMarker( marker ) {
  161. marker.stopListening();
  162. marker._liveRange.detach();
  163. marker._liveRange = null;
  164. }
  165. /**
  166. * Fired whenever marker is added to `MarkerCollection`.
  167. *
  168. * @event add
  169. * @param {module:engine/model/markercollection~Marker} The added marker.
  170. */
  171. /**
  172. * Fired whenever marker is removed from `MarkerCollection`.
  173. *
  174. * @event remove
  175. * @param {module:engine/model/markercollection~Marker} marker The removed marker.
  176. */
  177. }
  178. mix( MarkerCollection, EmitterMixin );
  179. /**
  180. * `Marker` is a continuous parts of model (like a range), is named and represent some kind of information about marked
  181. * part of model document. In contrary to {@link module:engine/model/node~Node nodes}, which are building blocks of
  182. * model document tree, markers are not stored directly in document tree. Still, they are document data, by giving
  183. * additional meaning to the part of a model document between marker start and marker end.
  184. *
  185. * In this sense, markers are similar to adding and converting attributes on nodes. The difference is that attribute is
  186. * connected with a given node (e.g. a character is bold no matter if it gets moved or content around it changes).
  187. * Markers on the other hand are continuous ranges and are characterised by their start and end position. This means that
  188. * any character in the marker is marked by the marker. For example, if a character is moved outside of marker it stops being
  189. * "special" and the marker is shrunk. Similarly, when a character is moved into the marker from other place in document
  190. * model, it starts being "special" and the marker is enlarged.
  191. *
  192. * Since markers are based on {@link module:engine/model/liverange~LiveRange live ranges}, for efficiency reasons, it's
  193. * best to create and keep at least markers as possible.
  194. *
  195. * Markers can be converted to view by adding appropriate converters for
  196. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:addMarker} and
  197. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:removeMarker}
  198. * events, or by building converters for {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}
  199. * using {@link module:engine/conversion/buildmodelconverter~buildModelConverter model converter builder}.
  200. *
  201. * Another upside of markers is that finding marked part of document is fast and easy. Using attributes to mark some nodes
  202. * and then trying to find that part of document would require traversing whole document tree. Marker gives instant access
  203. * to the range which it is marking at the moment.
  204. *
  205. * `Marker` instances are created and destroyed only by {@link ~MarkerCollection MarkerCollection}.
  206. */
  207. class Marker {
  208. /**
  209. * Creates a marker instance.
  210. *
  211. * @param {String} name Marker name.
  212. * @param {module:engine/model/liverange~LiveRange} liveRange Range marked by the marker.
  213. */
  214. constructor( name, liveRange ) {
  215. /**
  216. * Marker name.
  217. *
  218. * @readonly
  219. * @member {String} #name
  220. */
  221. this.name = name;
  222. /**
  223. * Range marked by the marker.
  224. *
  225. * @protected
  226. * @member {module:engine/model/liverange~LiveRange} #_liveRange
  227. */
  228. this._liveRange = liveRange;
  229. this._liveRange.delegate( 'change' ).to( this );
  230. }
  231. /**
  232. * Returns current marker start position.
  233. *
  234. * @returns {module:engine/model/position~Position}
  235. */
  236. getStart() {
  237. if ( !this._liveRange ) {
  238. /**
  239. * Operating on destroyed marker instance.
  240. *
  241. * @error marker-destroyed
  242. */
  243. throw new CKEditorError( 'marker-destroyed: Operating on destroyed marker instance.' );
  244. }
  245. return Position.createFromPosition( this._liveRange.start );
  246. }
  247. /**
  248. * Returns current marker end position.
  249. *
  250. * @returns {module:engine/model/position~Position}
  251. */
  252. getEnd() {
  253. if ( !this._liveRange ) {
  254. /**
  255. * Operating on destroyed marker instance.
  256. *
  257. * @error marker-destroyed
  258. */
  259. throw new CKEditorError( 'marker-destroyed: Operating on destroyed marker instance.' );
  260. }
  261. return Position.createFromPosition( this._liveRange.end );
  262. }
  263. /**
  264. * Returns a range that represents current state of marker.
  265. *
  266. * Keep in mind that returned value is a {@link module:engine/model/range~Range Range}, not a
  267. * {@link module:engine/model/liverange~LiveRange LiveRange}. This means that it is up-to-date and relevant only
  268. * until next model document change. Do not store values returned by this method. Instead, store {@link ~Marker#name}
  269. * and get `Marker` instance from {@link module:engine/model/markercollection~MarkerCollection MarkerCollection} every
  270. * time there is a need to read marker properties. This will guarantee that the marker has not been removed and
  271. * that it's data is up-to-date.
  272. *
  273. * @returns {module:engine/model/range~Range}
  274. */
  275. getRange() {
  276. if ( !this._liveRange ) {
  277. /**
  278. * Operating on destroyed marker instance.
  279. *
  280. * @error marker-destroyed
  281. */
  282. throw new CKEditorError( 'marker-destroyed: Operating on destroyed marker instance.' );
  283. }
  284. return Range.createFromRange( this._liveRange );
  285. }
  286. /**
  287. * Fired whenever {@link ~Marker#_liveRange marker range} is changed due to changes on {@link module:engine/model/document~Document}.
  288. * This is actually a delegated {@link module:engine/model/liverange~LiveRange#event:change LiveRange change event}.
  289. *
  290. * When marker is removed from {@link module:engine/model/markercollection~MarkerCollection MarkerCollection},
  291. * all event listeners listening to it should be removed. It is best to do it on
  292. * {@link module:engine/model/markercollection~MarkerCollection#event:remove MarkerCollection remove event}.
  293. *
  294. * @see module:engine/model/liverange~LiveRange#event:change
  295. * @event change
  296. * @param {module:engine/model/range~Range} oldRange Range with start and end position equal to start and end position of
  297. * this marker range before it got changed.
  298. */
  299. }
  300. mix( Marker, EmitterMixin );