inlineeditor.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import InlineEditorUI from '../src/inlineeditorui';
  7. import InlineEditorUIView from '../src/inlineeditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import InlineEditor from '../src/inlineeditor';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. describe( 'InlineEditor', () => {
  18. let editor, editorElement;
  19. beforeEach( () => {
  20. editorElement = document.createElement( 'div' );
  21. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  22. document.body.appendChild( editorElement );
  23. } );
  24. afterEach( () => {
  25. editorElement.remove();
  26. } );
  27. describe( 'constructor()', () => {
  28. beforeEach( () => {
  29. editor = new InlineEditor( editorElement );
  30. } );
  31. it( 'creates the UI using BoxedEditorUI classes', () => {
  32. expect( editor.ui ).to.be.instanceof( InlineEditorUI );
  33. expect( editor.ui.view ).to.be.instanceof( InlineEditorUIView );
  34. } );
  35. it( 'uses HTMLDataProcessor', () => {
  36. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  37. } );
  38. } );
  39. describe( 'create()', () => {
  40. beforeEach( function() {
  41. return InlineEditor
  42. .create( editorElement, {
  43. plugins: [ Paragraph, Bold ]
  44. } )
  45. .then( newEditor => {
  46. editor = newEditor;
  47. } );
  48. } );
  49. afterEach( () => {
  50. return editor.destroy();
  51. } );
  52. it( 'creates an instance which inherits from the InlineEditor', () => {
  53. expect( editor ).to.be.instanceof( InlineEditor );
  54. } );
  55. it( 'creates element–less UI view', () => {
  56. expect( editor.ui.view.element ).to.be.null;
  57. } );
  58. it( 'attaches editable UI as view\'s DOM root', () => {
  59. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  60. } );
  61. it( 'loads data from the editor element', () => {
  62. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  63. } );
  64. // #25
  65. it( 'creates an instance of a InlineEditor child class', () => {
  66. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  67. // editor being initialized on one element.
  68. const editorElement = document.createElement( 'div' );
  69. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  70. document.body.appendChild( editorElement );
  71. class CustomInlineEditor extends InlineEditor {}
  72. return CustomInlineEditor
  73. .create( editorElement, {
  74. plugins: [ Paragraph, Bold ]
  75. } )
  76. .then( newEditor => {
  77. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  78. expect( newEditor ).to.be.instanceof( InlineEditor );
  79. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  80. editorElement.remove();
  81. return newEditor.destroy();
  82. } );
  83. } );
  84. } );
  85. describe( 'create - events', () => {
  86. afterEach( () => {
  87. return editor.destroy();
  88. } );
  89. it( 'fires all events in the right order', () => {
  90. const fired = [];
  91. function spy( evt ) {
  92. fired.push( evt.name );
  93. }
  94. class EventWatcher extends Plugin {
  95. init() {
  96. this.editor.on( 'pluginsReady', spy );
  97. this.editor.on( 'uiReady', spy );
  98. this.editor.on( 'dataReady', spy );
  99. this.editor.on( 'ready', spy );
  100. }
  101. }
  102. return InlineEditor
  103. .create( editorElement, {
  104. plugins: [ EventWatcher ]
  105. } )
  106. .then( newEditor => {
  107. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  108. editor = newEditor;
  109. } );
  110. } );
  111. it( 'fires dataReady once data is loaded', () => {
  112. let data;
  113. class EventWatcher extends Plugin {
  114. init() {
  115. this.editor.on( 'dataReady', () => {
  116. data = this.editor.getData();
  117. } );
  118. }
  119. }
  120. return InlineEditor
  121. .create( editorElement, {
  122. plugins: [ EventWatcher, Paragraph, Bold ]
  123. } )
  124. .then( newEditor => {
  125. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  126. editor = newEditor;
  127. } );
  128. } );
  129. it( 'fires uiReady once UI is ready', () => {
  130. let isReady;
  131. class EventWatcher extends Plugin {
  132. init() {
  133. this.editor.on( 'uiReady', () => {
  134. isReady = this.editor.ui.view.isRendered;
  135. } );
  136. }
  137. }
  138. return InlineEditor
  139. .create( editorElement, {
  140. plugins: [ EventWatcher ]
  141. } )
  142. .then( newEditor => {
  143. expect( isReady ).to.be.true;
  144. editor = newEditor;
  145. } );
  146. } );
  147. } );
  148. describe( 'destroy', () => {
  149. beforeEach( function() {
  150. return InlineEditor
  151. .create( editorElement, { plugins: [ Paragraph ] } )
  152. .then( newEditor => {
  153. editor = newEditor;
  154. const schema = editor.model.schema;
  155. schema.register( 'heading' );
  156. schema.extend( 'heading', { allowIn: '$root' } );
  157. schema.extend( '$text', { allowIn: 'heading' } );
  158. buildModelConverter().for( editor.data.modelToView )
  159. .fromElement( 'heading' )
  160. .toElement( 'heading' );
  161. buildViewConverter().for( editor.data.viewToModel )
  162. .fromElement( 'heading' )
  163. .toElement( 'heading' );
  164. buildModelConverter().for( editor.editing.modelToView )
  165. .fromElement( 'heading' )
  166. .toElement( 'heading-editing-representation' );
  167. } );
  168. } );
  169. it( 'sets the data back to the editor element', () => {
  170. editor.setData( '<p>a</p><heading>b</heading>' );
  171. return editor.destroy()
  172. .then( () => {
  173. expect( editorElement.innerHTML )
  174. .to.equal( '<p>a</p><heading>b</heading>' );
  175. } );
  176. } );
  177. } );
  178. } );