editingcontroller.js 5.7 KB

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