8
0

ballooneditor.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event, HTMLElement */
  6. import BalloonEditorUI from '../src/ballooneditorui';
  7. import BalloonEditorUIView from '../src/ballooneditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  10. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  11. import BalloonEditor from '../src/ballooneditor';
  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 BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  16. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  17. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  18. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. testUtils.createSinonSandbox();
  21. describe( 'BalloonEditor', () => {
  22. let editor, editorElement;
  23. beforeEach( () => {
  24. editorElement = document.createElement( 'div' );
  25. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  26. document.body.appendChild( editorElement );
  27. } );
  28. afterEach( () => {
  29. editorElement.remove();
  30. } );
  31. describe( 'constructor()', () => {
  32. beforeEach( () => {
  33. editor = new BalloonEditor( editorElement, {
  34. plugins: [ BalloonToolbar, Bold ],
  35. toolbar: [ 'Bold' ]
  36. } );
  37. } );
  38. it( 'pushes BalloonToolbar to the list of plugins', () => {
  39. expect( editor.config.get( 'plugins' ) ).to.include( BalloonToolbar );
  40. } );
  41. it( 'pipes config#toolbar to config#balloonToolbar', () => {
  42. expect( editor.config.get( 'balloonToolbar' ) ).to.have.members( [ 'Bold' ] );
  43. } );
  44. it( 'uses HTMLDataProcessor', () => {
  45. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  46. } );
  47. it( 'has a Data Interface', () => {
  48. testUtils.isMixed( BalloonEditor, DataApiMixin );
  49. } );
  50. it( 'has a Element Interface', () => {
  51. testUtils.isMixed( BalloonEditor, ElementApiMixin );
  52. } );
  53. it( 'creates main root element', () => {
  54. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  55. } );
  56. it( 'handles form element', () => {
  57. const form = document.createElement( 'form' );
  58. const textarea = document.createElement( 'textarea' );
  59. form.appendChild( textarea );
  60. document.body.appendChild( form );
  61. // Prevents page realods in Firefox ;|
  62. form.addEventListener( 'submit', evt => {
  63. evt.preventDefault();
  64. } );
  65. return BalloonEditor.create( textarea, {
  66. plugins: [ Paragraph ]
  67. } ).then( editor => {
  68. expect( textarea.value ).to.equal( '' );
  69. editor.setData( '<p>Foo</p>' );
  70. form.dispatchEvent( new Event( 'submit', {
  71. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  72. cancelable: true
  73. } ) );
  74. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  75. return editor.destroy().then( () => {
  76. form.remove();
  77. } );
  78. } );
  79. } );
  80. it( 'allows to pass data to the constructor', () => {
  81. return BalloonEditor.create( '<p>Hello world!</p>', {
  82. plugins: [ Paragraph ]
  83. } ).then( editor => {
  84. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  85. } );
  86. } );
  87. it( 'editor.element should contain created div element', () => {
  88. return BalloonEditor.create( '<p>Hello world!</p>', {
  89. plugins: [ Paragraph ]
  90. } ).then( editor => {
  91. expect( editor.element ).to.be.instanceOf( HTMLElement );
  92. expect( editor.element.tagName ).to.equal( 'DIV' );
  93. expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
  94. } );
  95. } );
  96. } );
  97. describe( 'create()', () => {
  98. beforeEach( function() {
  99. return BalloonEditor
  100. .create( editorElement, {
  101. plugins: [ Paragraph, Bold ]
  102. } )
  103. .then( newEditor => {
  104. editor = newEditor;
  105. } );
  106. } );
  107. afterEach( () => {
  108. return editor.destroy();
  109. } );
  110. it( 'creates an instance which inherits from the BalloonEditor', () => {
  111. expect( editor ).to.be.instanceof( BalloonEditor );
  112. } );
  113. it( 'creates element–less UI view', () => {
  114. expect( editor.ui.view.element ).to.be.null;
  115. } );
  116. it( 'attaches editable UI as view\'s DOM root', () => {
  117. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  118. } );
  119. it( 'creates the UI using BalloonEditorUI classes', () => {
  120. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  121. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  122. } );
  123. it( 'loads data from the editor element', () => {
  124. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  125. } );
  126. // ckeditor/ckeditor5-editor-classic#53
  127. it( 'creates an instance of a BalloonEditor child class', () => {
  128. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  129. // editor being initialized on one element.
  130. const editorElement = document.createElement( 'div' );
  131. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  132. document.body.appendChild( editorElement );
  133. class CustomBalloonEditor extends BalloonEditor {}
  134. return CustomBalloonEditor
  135. .create( editorElement, {
  136. plugins: [ Paragraph, Bold ]
  137. } )
  138. .then( newEditor => {
  139. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  140. expect( newEditor ).to.be.instanceof( BalloonEditor );
  141. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  142. editorElement.remove();
  143. return newEditor.destroy();
  144. } );
  145. } );
  146. } );
  147. describe( 'create - events', () => {
  148. afterEach( () => {
  149. return editor.destroy();
  150. } );
  151. it( 'fires all events in the right order', () => {
  152. const fired = [];
  153. function spy( evt ) {
  154. fired.push( evt.name );
  155. }
  156. class EventWatcher extends Plugin {
  157. init() {
  158. this.editor.on( 'pluginsReady', spy );
  159. this.editor.on( 'uiReady', spy );
  160. this.editor.on( 'dataReady', spy );
  161. this.editor.on( 'ready', spy );
  162. }
  163. }
  164. return BalloonEditor
  165. .create( editorElement, {
  166. plugins: [ EventWatcher ]
  167. } )
  168. .then( newEditor => {
  169. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  170. editor = newEditor;
  171. } );
  172. } );
  173. it( 'fires dataReady once data is loaded', () => {
  174. let data;
  175. class EventWatcher extends Plugin {
  176. init() {
  177. this.editor.on( 'dataReady', () => {
  178. data = this.editor.getData();
  179. } );
  180. }
  181. }
  182. return BalloonEditor
  183. .create( editorElement, {
  184. plugins: [ EventWatcher, Paragraph, Bold ]
  185. } )
  186. .then( newEditor => {
  187. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  188. editor = newEditor;
  189. } );
  190. } );
  191. it( 'fires uiReady once UI is ready', () => {
  192. let isRendered;
  193. class EventWatcher extends Plugin {
  194. init() {
  195. this.editor.on( 'uiReady', () => {
  196. isRendered = this.editor.ui.view.isRendered;
  197. } );
  198. }
  199. }
  200. return BalloonEditor
  201. .create( editorElement, {
  202. plugins: [ EventWatcher ]
  203. } )
  204. .then( newEditor => {
  205. expect( isRendered ).to.be.true;
  206. editor = newEditor;
  207. } );
  208. } );
  209. } );
  210. describe( 'destroy()', () => {
  211. beforeEach( function() {
  212. return BalloonEditor
  213. .create( editorElement, { plugins: [ Paragraph ] } )
  214. .then( newEditor => {
  215. editor = newEditor;
  216. const schema = editor.model.schema;
  217. schema.register( 'heading' );
  218. schema.extend( 'heading', { allowIn: '$root' } );
  219. schema.extend( '$text', { allowIn: 'heading' } );
  220. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading', view: 'heading' } ) );
  221. editor.conversion.for( 'dataDowncast' ).add( downcastElementToElement( { model: 'heading', view: 'heading' } ) );
  222. editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  223. model: 'heading',
  224. view: 'heading-editing-representation'
  225. } ) );
  226. } );
  227. } );
  228. it( 'sets the data back to the editor element', () => {
  229. editor.setData( '<p>a</p><heading>b</heading>' );
  230. return editor.destroy()
  231. .then( () => {
  232. expect( editorElement.innerHTML )
  233. .to.equal( '<p>a</p><heading>b</heading>' );
  234. } );
  235. } );
  236. } );
  237. } );