8
0

editingcontroller.js 5.5 KB

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