decouplededitor.js 8.3 KB

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