8
0

decouplededitor.js 7.6 KB

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