balloontoolbareditor.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import BalloonToolbarEditorUI from '../src/balloontoolbareditorui';
  7. import BalloonToolbarEditorUIView from '../src/balloontoolbareditoruiview';
  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 BalloonToolbarEditor from '../src/balloontoolbareditor';
  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 ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. import count from '@ckeditor/ckeditor5-utils/src/count';
  18. testUtils.createSinonSandbox();
  19. describe( 'BalloonToolbarEditor', () => {
  20. let editor, editorElement;
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  24. document.body.appendChild( editorElement );
  25. } );
  26. afterEach( () => {
  27. editorElement.remove();
  28. } );
  29. describe( 'constructor()', () => {
  30. beforeEach( () => {
  31. editor = new BalloonToolbarEditor( editorElement, {
  32. plugins: [ Bold ],
  33. toolbar: [ 'Bold' ]
  34. } );
  35. } );
  36. it( 'pushes ContextualToolbar to the list of plugins', () => {
  37. expect( editor.config.get( 'plugins' ) ).to.include( ContextualToolbar );
  38. } );
  39. it( 'pipes config#toolbar to config#contextualToolbar', () => {
  40. expect( editor.config.get( 'contextualToolbar' ) ).to.have.members( [ 'Bold' ] );
  41. } );
  42. it( 'creates a single div editable root in the view', () => {
  43. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  44. } );
  45. it( 'creates a single document root', () => {
  46. expect( count( editor.document.getRootNames() ) ).to.equal( 1 );
  47. expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
  48. } );
  49. it( 'uses HTMLDataProcessor', () => {
  50. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  51. } );
  52. it( 'creates the UI using BalloonToolbarEditorUI classes', () => {
  53. expect( editor.ui ).to.be.instanceof( BalloonToolbarEditorUI );
  54. expect( editor.ui.view ).to.be.instanceof( BalloonToolbarEditorUIView );
  55. } );
  56. } );
  57. describe( 'create()', () => {
  58. beforeEach( function() {
  59. return BalloonToolbarEditor.create( editorElement, {
  60. plugins: [ Paragraph, Bold ]
  61. } )
  62. .then( newEditor => {
  63. editor = newEditor;
  64. } );
  65. } );
  66. afterEach( () => {
  67. return editor.destroy();
  68. } );
  69. it( 'creates an instance which inherits from the BalloonToolbarEditor', () => {
  70. expect( editor ).to.be.instanceof( BalloonToolbarEditor );
  71. } );
  72. it( 'creates element–less UI view', () => {
  73. expect( editor.ui.view.element ).to.be.null;
  74. } );
  75. it( 'attaches editable UI as view\'s DOM root', () => {
  76. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  77. } );
  78. it( 'loads data from the editor element', () => {
  79. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  80. } );
  81. } );
  82. describe( 'create - events', () => {
  83. afterEach( () => {
  84. return editor.destroy();
  85. } );
  86. it( 'fires all events in the right order', () => {
  87. const fired = [];
  88. function spy( evt ) {
  89. fired.push( evt.name );
  90. }
  91. class EventWatcher extends Plugin {
  92. init() {
  93. this.editor.on( 'pluginsReady', spy );
  94. this.editor.on( 'uiReady', spy );
  95. this.editor.on( 'dataReady', spy );
  96. this.editor.on( 'ready', spy );
  97. }
  98. }
  99. return BalloonToolbarEditor.create( editorElement, {
  100. plugins: [ EventWatcher ]
  101. } )
  102. .then( newEditor => {
  103. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  104. editor = newEditor;
  105. } );
  106. } );
  107. it( 'fires dataReady once data is loaded', () => {
  108. let data;
  109. class EventWatcher extends Plugin {
  110. init() {
  111. this.editor.on( 'dataReady', () => {
  112. data = this.editor.getData();
  113. } );
  114. }
  115. }
  116. return BalloonToolbarEditor.create( editorElement, {
  117. plugins: [ EventWatcher, Paragraph, Bold ]
  118. } )
  119. .then( newEditor => {
  120. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  121. editor = newEditor;
  122. } );
  123. } );
  124. it( 'fires uiReady once UI is ready', () => {
  125. let isReady;
  126. class EventWatcher extends Plugin {
  127. init() {
  128. this.editor.on( 'uiReady', () => {
  129. isReady = this.editor.ui.view.ready;
  130. } );
  131. }
  132. }
  133. return BalloonToolbarEditor.create( editorElement, {
  134. plugins: [ EventWatcher ]
  135. } )
  136. .then( newEditor => {
  137. expect( isReady ).to.be.true;
  138. editor = newEditor;
  139. } );
  140. } );
  141. } );
  142. describe( 'destroy()', () => {
  143. beforeEach( function() {
  144. return BalloonToolbarEditor.create( editorElement, { plugins: [ Paragraph ] } )
  145. .then( newEditor => {
  146. editor = newEditor;
  147. const schema = editor.document.schema;
  148. schema.registerItem( 'heading' );
  149. schema.allow( { name: 'heading', inside: '$root' } );
  150. schema.allow( { name: '$text', inside: 'heading' } );
  151. buildModelConverter().for( editor.data.modelToView )
  152. .fromElement( 'heading' )
  153. .toElement( 'heading' );
  154. buildViewConverter().for( editor.data.viewToModel )
  155. .fromElement( 'heading' )
  156. .toElement( 'heading' );
  157. buildModelConverter().for( editor.editing.modelToView )
  158. .fromElement( 'heading' )
  159. .toElement( 'heading-editing-representation' );
  160. } );
  161. } );
  162. it( 'sets the data back to the editor element', () => {
  163. editor.setData( '<p>a</p><heading>b</heading>' );
  164. return editor.destroy()
  165. .then( () => {
  166. expect( editorElement.innerHTML )
  167. .to.equal( '<p>a</p><heading>b</heading>' );
  168. } );
  169. } );
  170. } );
  171. } );