8
0

decouplededitor.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.data.on( 'ready', () => spy( 'ready' ) );
  55. this.editor.data.on( 'init', evt => {
  56. evt.stop();
  57. evt.return = new Promise( resolve => {
  58. resolver = () => {
  59. spy( 'asyncInit' );
  60. // Since we stop `init` event, `data#ready` needs to be fired manually.
  61. this.editor.data.fire( 'ready' );
  62. resolve();
  63. };
  64. } );
  65. }, { priority: 'high' } );
  66. }
  67. }
  68. DecoupledEditor.create( '<p>foo bar</p>', {
  69. plugins: [ Paragraph, Bold, AsyncDataInit ]
  70. } ).then( editor => {
  71. sinon.assert.calledWith( spy.firstCall, 'asyncInit' );
  72. sinon.assert.calledWith( spy.secondCall, 'ready' );
  73. editor.destroy().then( done );
  74. } );
  75. // Resolve init promise in next cycle to hold data initialization.
  76. setTimeout( () => resolver() );
  77. } );
  78. describe( 'editor with data', () => {
  79. test( () => editorData );
  80. } );
  81. describe( 'editor with editable element', () => {
  82. let editableElement;
  83. beforeEach( () => {
  84. editableElement = document.createElement( 'div' );
  85. editableElement.innerHTML = editorData;
  86. } );
  87. test( () => editableElement );
  88. } );
  89. function test( getElementOrData ) {
  90. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  91. return DecoupledEditor
  92. .create( getElementOrData(), {
  93. plugins: [ Paragraph, Bold ]
  94. } )
  95. .then( newEditor => {
  96. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  97. return newEditor.destroy();
  98. } );
  99. } );
  100. it( 'loads the initial data', () => {
  101. return DecoupledEditor
  102. .create( getElementOrData(), {
  103. plugins: [ Paragraph, Bold ]
  104. } )
  105. .then( newEditor => {
  106. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  107. return newEditor.destroy();
  108. } );
  109. } );
  110. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  111. it( 'creates an instance of a DecoupledEditor child class', () => {
  112. class CustomDecoupledEditor extends DecoupledEditor {}
  113. return CustomDecoupledEditor
  114. .create( getElementOrData(), {
  115. plugins: [ Paragraph, Bold ]
  116. } )
  117. .then( newEditor => {
  118. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  119. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  120. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  121. return newEditor.destroy();
  122. } );
  123. } );
  124. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  125. it( 'initializes the data controller', () => {
  126. let dataInitSpy;
  127. class DataInitAssertPlugin extends Plugin {
  128. constructor( editor ) {
  129. super();
  130. this.editor = editor;
  131. }
  132. init() {
  133. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  134. }
  135. }
  136. return DecoupledEditor
  137. .create( getElementOrData(), {
  138. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  139. } )
  140. .then( newEditor => {
  141. sinon.assert.calledOnce( dataInitSpy );
  142. return newEditor.destroy();
  143. } );
  144. } );
  145. describe( 'events', () => {
  146. it( 'fires all events in the right order', () => {
  147. const fired = [];
  148. function spy( evt ) {
  149. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  150. }
  151. class EventWatcher extends Plugin {
  152. init() {
  153. this.editor.ui.on( 'ready', spy );
  154. this.editor.data.on( 'ready', spy );
  155. this.editor.on( 'ready', spy );
  156. }
  157. }
  158. return DecoupledEditor
  159. .create( getElementOrData(), {
  160. plugins: [ EventWatcher ]
  161. } )
  162. .then( newEditor => {
  163. expect( fired ).to.deep.equal( [
  164. 'ready-decouplededitorui',
  165. 'ready-datacontroller',
  166. 'ready-decouplededitor'
  167. ] );
  168. return newEditor.destroy();
  169. } );
  170. } );
  171. it( 'fires ready once UI is rendered', () => {
  172. let isReady;
  173. class EventWatcher extends Plugin {
  174. init() {
  175. this.editor.ui.on( 'ready', () => {
  176. isReady = this.editor.ui.view.isRendered;
  177. } );
  178. }
  179. }
  180. return DecoupledEditor
  181. .create( getElementOrData(), {
  182. plugins: [ EventWatcher ]
  183. } )
  184. .then( newEditor => {
  185. expect( isReady ).to.be.true;
  186. return newEditor.destroy();
  187. } );
  188. } );
  189. } );
  190. }
  191. } );
  192. describe( 'destroy', () => {
  193. describe( 'editor with data', () => {
  194. beforeEach( function() {
  195. return DecoupledEditor
  196. .create( editorData, { plugins: [ Paragraph ] } )
  197. .then( newEditor => {
  198. editor = newEditor;
  199. } );
  200. } );
  201. test( () => editorData );
  202. } );
  203. describe( 'editor with editable element', () => {
  204. let editableElement;
  205. beforeEach( function() {
  206. editableElement = document.createElement( 'div' );
  207. editableElement.innerHTML = editorData;
  208. return DecoupledEditor
  209. .create( editableElement, { plugins: [ Paragraph ] } )
  210. .then( newEditor => {
  211. editor = newEditor;
  212. } );
  213. } );
  214. it( 'sets data back to the element', () => {
  215. editor.setData( '<p>foo</p>' );
  216. return editor.destroy()
  217. .then( () => {
  218. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  219. } );
  220. } );
  221. test( () => editableElement );
  222. } );
  223. function test() {
  224. it( 'destroys the UI', () => {
  225. const spy = sinon.spy( editor.ui, 'destroy' );
  226. return editor.destroy()
  227. .then( () => {
  228. sinon.assert.calledOnce( spy );
  229. } );
  230. } );
  231. }
  232. } );
  233. describeMemoryUsage( () => {
  234. testMemoryUsage(
  235. 'should not grow on multiple create/destroy',
  236. () => DecoupledEditor
  237. .create( document.querySelector( '#mem-editor' ), {
  238. plugins: [ ArticlePluginSet ],
  239. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  240. image: {
  241. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  242. }
  243. } ) );
  244. } );
  245. } );