decouplededitor.js 7.5 KB

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