decouplededitor.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, setTimeout */
  6. import DecoupledEditorUI from '../src/decouplededitorui';
  7. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import DecoupledEditor from '../src/decouplededitor';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  14. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. const editorData = '<p><strong>foo</strong> bar</p>';
  18. describe( 'DecoupledEditor', () => {
  19. let editor;
  20. describe( 'constructor()', () => {
  21. beforeEach( () => {
  22. editor = new DecoupledEditor();
  23. } );
  24. it( 'uses HTMLDataProcessor', () => {
  25. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  26. } );
  27. it( 'has a Data Interface', () => {
  28. expect( testUtils.isMixed( DecoupledEditor, DataApiMixin ) ).to.be.true;
  29. } );
  30. it( 'implements the EditorWithUI interface', () => {
  31. expect( editor.element ).to.be.null;
  32. } );
  33. it( 'creates main root element', () => {
  34. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  35. } );
  36. describe( 'ui', () => {
  37. it( 'is created', () => {
  38. editor.ui.init();
  39. expect( editor.ui ).to.be.instanceof( DecoupledEditorUI );
  40. expect( editor.ui.view ).to.be.instanceof( DecoupledEditorUIView );
  41. editor.ui.destroy();
  42. } );
  43. } );
  44. } );
  45. describe( 'create()', () => {
  46. it( 'should properly handled async data initialization', done => {
  47. const spy = sinon.spy();
  48. let resolver;
  49. class AsyncDataInit extends Plugin {
  50. init() {
  51. this.editor.on( 'dataReady', () => spy( 'dataReady' ) );
  52. this.editor.data.on( 'init', evt => {
  53. evt.stop();
  54. evt.return = new Promise( resolve => {
  55. resolver = () => {
  56. spy( 'asyncInit' );
  57. resolve();
  58. };
  59. } );
  60. }, { priority: 'high' } );
  61. }
  62. }
  63. DecoupledEditor.create( '<p>foo bar</p>', {
  64. plugins: [ Paragraph, Bold, AsyncDataInit ]
  65. } ).then( editor => {
  66. sinon.assert.calledWith( spy.firstCall, 'asyncInit' );
  67. sinon.assert.calledWith( spy.secondCall, 'dataReady' );
  68. editor.destroy().then( done );
  69. } );
  70. // Resolve init promise in next cycle to hold data initialization.
  71. setTimeout( () => resolver() );
  72. } );
  73. describe( 'editor with data', () => {
  74. test( () => editorData );
  75. } );
  76. describe( 'editor with editable element', () => {
  77. let editableElement;
  78. beforeEach( () => {
  79. editableElement = document.createElement( 'div' );
  80. editableElement.innerHTML = editorData;
  81. } );
  82. test( () => editableElement );
  83. } );
  84. function test( getElementOrData ) {
  85. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  86. return DecoupledEditor
  87. .create( getElementOrData(), {
  88. plugins: [ Paragraph, Bold ]
  89. } )
  90. .then( newEditor => {
  91. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  92. return newEditor.destroy();
  93. } );
  94. } );
  95. it( 'loads the initial data', () => {
  96. return DecoupledEditor
  97. .create( getElementOrData(), {
  98. plugins: [ Paragraph, Bold ]
  99. } )
  100. .then( newEditor => {
  101. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  102. return newEditor.destroy();
  103. } );
  104. } );
  105. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  106. it( 'creates an instance of a DecoupledEditor child class', () => {
  107. class CustomDecoupledEditor extends DecoupledEditor {}
  108. return CustomDecoupledEditor
  109. .create( getElementOrData(), {
  110. plugins: [ Paragraph, Bold ]
  111. } )
  112. .then( newEditor => {
  113. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  114. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  115. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  116. return newEditor.destroy();
  117. } );
  118. } );
  119. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  120. it( 'initializes the data controller', () => {
  121. let dataInitSpy;
  122. class DataInitAssertPlugin extends Plugin {
  123. constructor( editor ) {
  124. super();
  125. this.editor = editor;
  126. }
  127. init() {
  128. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  129. }
  130. }
  131. return DecoupledEditor
  132. .create( getElementOrData(), {
  133. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  134. } )
  135. .then( newEditor => {
  136. sinon.assert.calledOnce( dataInitSpy );
  137. return newEditor.destroy();
  138. } );
  139. } );
  140. describe( 'events', () => {
  141. it( 'fires all events in the right order', () => {
  142. const fired = [];
  143. function spy( evt ) {
  144. fired.push( evt.name );
  145. }
  146. class EventWatcher extends Plugin {
  147. init() {
  148. this.editor.on( 'pluginsReady', spy );
  149. this.editor.on( 'uiReady', spy );
  150. this.editor.on( 'dataReady', spy );
  151. this.editor.on( 'ready', spy );
  152. }
  153. }
  154. return DecoupledEditor
  155. .create( getElementOrData(), {
  156. plugins: [ EventWatcher ]
  157. } )
  158. .then( newEditor => {
  159. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  160. return newEditor.destroy();
  161. } );
  162. } );
  163. it( 'fires dataReady once data is loaded', () => {
  164. let data;
  165. class EventWatcher extends Plugin {
  166. init() {
  167. this.editor.on( 'dataReady', () => {
  168. data = this.editor.getData();
  169. } );
  170. }
  171. }
  172. return DecoupledEditor
  173. .create( getElementOrData(), {
  174. plugins: [ EventWatcher, Paragraph, Bold ]
  175. } )
  176. .then( newEditor => {
  177. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  178. return newEditor.destroy();
  179. } );
  180. } );
  181. it( 'fires uiReady once UI is rendered', () => {
  182. let isReady;
  183. class EventWatcher extends Plugin {
  184. init() {
  185. this.editor.on( 'uiReady', () => {
  186. isReady = this.editor.ui.view.isRendered;
  187. } );
  188. }
  189. }
  190. return DecoupledEditor
  191. .create( getElementOrData(), {
  192. plugins: [ EventWatcher ]
  193. } )
  194. .then( newEditor => {
  195. expect( isReady ).to.be.true;
  196. return newEditor.destroy();
  197. } );
  198. } );
  199. } );
  200. }
  201. } );
  202. describe( 'destroy', () => {
  203. describe( 'editor with data', () => {
  204. beforeEach( function() {
  205. return DecoupledEditor
  206. .create( editorData, { plugins: [ Paragraph ] } )
  207. .then( newEditor => {
  208. editor = newEditor;
  209. } );
  210. } );
  211. test( () => editorData );
  212. } );
  213. describe( 'editor with editable element', () => {
  214. let editableElement;
  215. beforeEach( function() {
  216. editableElement = document.createElement( 'div' );
  217. editableElement.innerHTML = editorData;
  218. return DecoupledEditor
  219. .create( editableElement, { plugins: [ Paragraph ] } )
  220. .then( newEditor => {
  221. editor = newEditor;
  222. } );
  223. } );
  224. it( 'sets data back to the element', () => {
  225. editor.setData( '<p>foo</p>' );
  226. return editor.destroy()
  227. .then( () => {
  228. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  229. } );
  230. } );
  231. test( () => editableElement );
  232. } );
  233. function test() {
  234. it( 'destroys the UI', () => {
  235. const spy = sinon.spy( editor.ui, 'destroy' );
  236. return editor.destroy()
  237. .then( () => {
  238. sinon.assert.calledOnce( spy );
  239. } );
  240. } );
  241. }
  242. } );
  243. } );