8
0

editingcontroller.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/controller/editingcontroller
  7. */
  8. import ViewDocument from '../view/document';
  9. import Mapper from '../conversion/mapper';
  10. import ModelConversionDispatcher from '../conversion/modelconversiondispatcher';
  11. import {
  12. insertText,
  13. remove,
  14. move,
  15. rename,
  16. insertIntoMarker,
  17. moveInOutOfMarker
  18. } from '../conversion/model-to-view-converters';
  19. import { convertSelectionChange } from '../conversion/view-selection-to-model-converters';
  20. import {
  21. convertRangeSelection,
  22. convertCollapsedSelection,
  23. clearAttributes,
  24. clearFakeSelection
  25. } from '../conversion/model-selection-to-view-converters';
  26. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  27. /**
  28. * Controller for the editing pipeline. The editing pipeline controls {@link ~EditingController#model model} rendering,
  29. * including selection handling. It also creates {@link ~EditingController#view view document} which build a
  30. * browser-independent virtualization over the DOM elements. Editing controller also attach default converters.
  31. */
  32. export default class EditingController {
  33. /**
  34. * Creates editing controller instance.
  35. *
  36. * @param {module:engine/model/document~Document} model Document model.
  37. */
  38. constructor( model ) {
  39. /**
  40. * Document model.
  41. *
  42. * @readonly
  43. * @member {module:engine/model/document~Document}
  44. */
  45. this.model = model;
  46. /**
  47. * View document.
  48. *
  49. * @readonly
  50. * @member {module:engine/view/document~Document}
  51. */
  52. this.view = new ViewDocument();
  53. /**
  54. * Mapper which describes model-view binding.
  55. *
  56. * @readonly
  57. * @member {module:engine/conversion/mapper~Mapper}
  58. */
  59. this.mapper = new Mapper();
  60. /**
  61. * Model to view conversion dispatcher, which converts changes from the model to
  62. * {@link #view editing view}.
  63. *
  64. * To attach model to view converter to the editing pipeline you need to add lister to this property:
  65. *
  66. * editing.modelToView( 'insert:$element', customInsertConverter );
  67. *
  68. * Or use {@link module:engine/conversion/buildmodelconverter~ModelConverterBuilder}:
  69. *
  70. * buildModelConverter().for( editing.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
  71. *
  72. * @readonly
  73. * @member {module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher} #modelToView
  74. */
  75. this.modelToView = new ModelConversionDispatcher( {
  76. mapper: this.mapper,
  77. viewSelection: this.view.selection
  78. } );
  79. /**
  80. * Property keeping all listenters attached by controller on other objects, so it can
  81. * stop listening on {@link #destroy}.
  82. *
  83. * @private
  84. * @member {utils.EmitterMixin} #_listenter
  85. */
  86. this._listener = Object.create( EmitterMixin );
  87. // Convert changes in model to view.
  88. this._listener.listenTo( this.model, 'change', ( evt, type, changes ) => {
  89. this.modelToView.convertChange( type, changes );
  90. }, { priority: 'low' } );
  91. // Convert model selection to view.
  92. this._listener.listenTo( this.model, 'changesDone', () => {
  93. this.modelToView.convertSelection( model.selection );
  94. this.view.render();
  95. }, { priority: 'low' } );
  96. // Convert model markers changes.
  97. this._listener.listenTo( this.model.markers, 'add', ( evt, marker ) => {
  98. this.modelToView.convertMarker( 'addMarker', marker.name, marker.getRange() );
  99. } );
  100. this._listener.listenTo( this.model.markers, 'remove', ( evt, marker ) => {
  101. this.modelToView.convertMarker( 'removeMarker', marker.name, marker.getRange() );
  102. } );
  103. // Convert view selection to model.
  104. this._listener.listenTo( this.view, 'selectionChange', convertSelectionChange( model, this.mapper ) );
  105. // Attach default content converters.
  106. this.modelToView.on( 'insert:$text', insertText(), { priority: 'lowest' } );
  107. this.modelToView.on( 'remove', remove(), { priority: 'low' } );
  108. this.modelToView.on( 'move', move(), { priority: 'low' } );
  109. this.modelToView.on( 'rename', rename(), { priority: 'low' } );
  110. // Attach default markers converters.
  111. this.modelToView.on( 'insert', insertIntoMarker( this.model.markers ), { priority: 'lowest' } );
  112. this.modelToView.on( 'move', moveInOutOfMarker( this.model.markers ), { priority: 'lowest' } );
  113. // Attach default selection converters.
  114. this.modelToView.on( 'selection', clearAttributes(), { priority: 'low' } );
  115. this.modelToView.on( 'selection', clearFakeSelection(), { priority: 'low' } );
  116. this.modelToView.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  117. this.modelToView.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  118. }
  119. /**
  120. * {@link module:engine/view/document~Document#createRoot Creates} a view root
  121. * and {@link module:engine/conversion/mapper~Mapper#bindElements binds}
  122. * the model root with view root and and view root with DOM element:
  123. *
  124. * editing.createRoot( document.querySelector( div#editor ) );
  125. *
  126. * If the DOM element is not available at the time you want to create a view root, for instance it is iframe body
  127. * element, it is possible to create view element and bind the DOM element later:
  128. *
  129. * editing.createRoot( 'body' );
  130. * editing.view.attachDomRoot( iframe.contentDocument.body );
  131. *
  132. * @param {Element|String} domRoot DOM root element or the name of view root element if the DOM element will be
  133. * attached later.
  134. * @param {String} [name='main'] Root name.
  135. * @returns {module:engine/view/containerelement~ContainerElement} View root element.
  136. */
  137. createRoot( domRoot, name = 'main' ) {
  138. const viewRoot = this.view.createRoot( domRoot, name );
  139. const modelRoot = this.model.getRoot( name );
  140. this.mapper.bindElements( modelRoot, viewRoot );
  141. return viewRoot;
  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._listener.stopListening();
  150. }
  151. }