decouplededitor.js 8.3 KB

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