8
0

editingcontroller.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/controller/editingcontroller
  7. */
  8. import RootEditableElement from '../view/rooteditableelement';
  9. import View from '../view/view';
  10. import Mapper from '../conversion/mapper';
  11. import DowncastDispatcher from '../conversion/downcastdispatcher';
  12. import {
  13. insertText,
  14. remove
  15. } from '../conversion/downcast-converters';
  16. import { convertSelectionChange } from '../conversion/upcast-selection-converters';
  17. import {
  18. convertRangeSelection,
  19. convertCollapsedSelection,
  20. clearAttributes,
  21. clearFakeSelection
  22. } from '../conversion/downcast-selection-converters';
  23. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  24. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  25. /**
  26. * Controller for the editing pipeline. The editing pipeline controls {@link ~EditingController#model model} rendering,
  27. * including selection handling. It also creates the {@link ~EditingController#view view document} which builds a
  28. * browser-independent virtualization over the DOM elements. The editing controller also attaches default converters.
  29. *
  30. * @mixes module:utils/observablemixin~ObservableMixin
  31. */
  32. export default class EditingController {
  33. /**
  34. * Creates an editing controller instance.
  35. *
  36. * @param {module:engine/model/model~Model} model Editing model.
  37. */
  38. constructor( model ) {
  39. /**
  40. * Editing model.
  41. *
  42. * @readonly
  43. * @member {module:engine/model/model~Model}
  44. */
  45. this.model = model;
  46. /**
  47. * Editing view controller.
  48. *
  49. * @readonly
  50. * @member {module:engine/view/view~View}
  51. */
  52. this.view = new View();
  53. /**
  54. * Mapper which describes the model-view binding.
  55. *
  56. * @readonly
  57. * @member {module:engine/conversion/mapper~Mapper}
  58. */
  59. this.mapper = new Mapper();
  60. /**
  61. * Downcast dispatcher that converts changes from the model to {@link #view the editing view}.
  62. *
  63. * @readonly
  64. * @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher} #downcastDispatcher
  65. */
  66. this.downcastDispatcher = new DowncastDispatcher( {
  67. mapper: this.mapper
  68. } );
  69. const doc = this.model.document;
  70. const selection = doc.selection;
  71. const markers = this.model.markers;
  72. this.listenTo( doc, 'change', () => {
  73. this.view.change( writer => {
  74. this.downcastDispatcher.convertChanges( doc.differ, writer );
  75. this.downcastDispatcher.convertSelection( selection, markers, writer );
  76. } );
  77. }, { priority: 'low' } );
  78. // Convert selection from view to model.
  79. this.listenTo( this.view.document, 'selectionChange', convertSelectionChange( this.model, this.mapper ) );
  80. // Attach default model converters.
  81. this.downcastDispatcher.on( 'insert:$text', insertText(), { priority: 'lowest' } );
  82. this.downcastDispatcher.on( 'remove', remove(), { priority: 'low' } );
  83. // Attach default model selection converters.
  84. this.downcastDispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  85. this.downcastDispatcher.on( 'selection', clearFakeSelection(), { priority: 'low' } );
  86. this.downcastDispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  87. this.downcastDispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  88. // Convert markers removal.
  89. //
  90. // Markers should be removed from the view before changes to the model are applied. This is because otherwise
  91. // it would be impossible to map some markers to the view (if, for example, the marker's boundary parent got removed).
  92. //
  93. // `removedMarkers` keeps information which markers already has been removed to prevent removing them twice.
  94. const removedMarkers = new Set();
  95. this.listenTo( model, 'applyOperation', ( evt, args ) => {
  96. // Before operation is applied...
  97. const operation = args[ 0 ];
  98. for ( const marker of model.markers ) {
  99. // Check all markers, that aren't already removed...
  100. if ( removedMarkers.has( marker.name ) ) {
  101. continue;
  102. }
  103. const markerRange = marker.getRange();
  104. if ( _operationAffectsMarker( operation, marker ) ) {
  105. // And if the operation in any way modifies the marker, remove the marker from the view.
  106. removedMarkers.add( marker.name );
  107. this.view.change( writer => {
  108. this.downcastDispatcher.convertMarkerRemove( marker.name, markerRange, writer );
  109. } );
  110. // TODO: This stinks but this is the safest place to have this code.
  111. this.model.document.differ.bufferMarkerChange( marker.name, markerRange, markerRange );
  112. }
  113. }
  114. }, { priority: 'high' } );
  115. // If an existing marker is updated through `model.Model#markers` directly (not through operation), just remove it.
  116. this.listenTo( model.markers, 'update', ( evt, marker, oldRange ) => {
  117. if ( oldRange && !removedMarkers.has( marker.name ) ) {
  118. removedMarkers.add( marker.name );
  119. this.view.change( writer => {
  120. this.downcastDispatcher.convertMarkerRemove( marker.name, oldRange, writer );
  121. } );
  122. }
  123. } );
  124. // When all changes are done, clear `removedMarkers` set.
  125. this.listenTo( model, '_change', () => {
  126. removedMarkers.clear();
  127. }, { priority: 'low' } );
  128. // Binds {@link module:engine/view/document~Document#roots view roots collection} to
  129. // {@link module:engine/model/document~Document#roots model roots collection} so creating
  130. // model root automatically creates corresponding view root.
  131. this.view.document.roots.bindTo( this.model.document.roots ).using( root => {
  132. // $graveyard is a special root that has no reflection in the view.
  133. if ( root.rootName == '$graveyard' ) {
  134. return null;
  135. }
  136. const viewRoot = new RootEditableElement( root.name );
  137. viewRoot.rootName = root.rootName;
  138. viewRoot._document = this.view.document;
  139. this.mapper.bindElements( root, viewRoot );
  140. return viewRoot;
  141. } );
  142. }
  143. /**
  144. * Removes all event listeners attached to the `EditingController`. Destroys all objects created
  145. * by `EditingController` that need to be destroyed.
  146. */
  147. destroy() {
  148. this.view.destroy();
  149. this.stopListening();
  150. }
  151. }
  152. mix( EditingController, ObservableMixin );
  153. // Helper function which checks whether given operation will affect given marker after the operation is applied.
  154. function _operationAffectsMarker( operation, marker ) {
  155. const range = marker.getRange();
  156. if ( operation.type == 'insert' || operation.type == 'rename' ) {
  157. return _positionAffectsRange( operation.position, range );
  158. } else if ( operation.type == 'move' || operation.type == 'remove' || operation.type == 'reinsert' ) {
  159. return _positionAffectsRange( operation.targetPosition, range ) || _positionAffectsRange( operation.sourcePosition, range );
  160. } else if ( operation.type == 'marker' && operation.name == marker.name ) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. // Helper function which checks whether change at given position affects given range.
  166. function _positionAffectsRange( position, range ) {
  167. return range.containsPosition( position ) || !range.start._getTransformedByInsertion( position, 1, true ).isEqual( range.start );
  168. }