8
0

decouplededitor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, setTimeout, console */
  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 { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  17. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  18. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  19. import { removeEditorBodyOrphans } from '@ckeditor/ckeditor5-core/tests/_utils/cleanup';
  20. const editorData = '<p><strong>foo</strong> bar</p>';
  21. describe( 'DecoupledEditor', () => {
  22. let editor;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. testUtils.sinon.stub( console, 'warn' ).callsFake( () => {} );
  26. } );
  27. describe( 'constructor()', () => {
  28. beforeEach( () => {
  29. editor = new DecoupledEditor();
  30. } );
  31. it( 'uses HTMLDataProcessor', () => {
  32. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  33. } );
  34. it( 'has a Data Interface', () => {
  35. expect( testUtils.isMixed( DecoupledEditor, DataApiMixin ) ).to.be.true;
  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. describe( 'automatic toolbar items groupping', () => {
  48. it( 'should be on by default', () => {
  49. const editorElement = document.createElement( 'div' );
  50. const editor = new DecoupledEditor( editorElement );
  51. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.true;
  52. editorElement.remove();
  53. } );
  54. it( 'can be disabled using config.toolbar.shouldNotGroupWhenFull', () => {
  55. const editorElement = document.createElement( 'div' );
  56. const editor = new DecoupledEditor( editorElement, {
  57. toolbar: {
  58. shouldNotGroupWhenFull: true
  59. }
  60. } );
  61. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.false;
  62. editorElement.remove();
  63. } );
  64. } );
  65. } );
  66. } );
  67. describe( 'create()', () => {
  68. it( 'should properly handled async data initialization', done => {
  69. const spy = sinon.spy();
  70. let resolver;
  71. class AsyncDataInit extends Plugin {
  72. init() {
  73. this.editor.data.on( 'ready', () => spy( 'ready' ) );
  74. this.editor.data.on( 'init', evt => {
  75. evt.stop();
  76. evt.return = new Promise( resolve => {
  77. resolver = () => {
  78. spy( 'asyncInit' );
  79. // Since we stop `init` event, `data#ready` needs to be fired manually.
  80. this.editor.data.fire( 'ready' );
  81. resolve();
  82. };
  83. } );
  84. }, { priority: 'high' } );
  85. }
  86. }
  87. DecoupledEditor.create( '<p>foo bar</p>', {
  88. plugins: [ Paragraph, Bold, AsyncDataInit ]
  89. } ).then( editor => {
  90. sinon.assert.calledWith( spy.firstCall, 'asyncInit' );
  91. sinon.assert.calledWith( spy.secondCall, 'ready' );
  92. editor.destroy().then( done );
  93. } );
  94. // Resolve init promise in next cycle to hold data initialization.
  95. setTimeout( () => resolver() );
  96. } );
  97. describe( 'editor with data', () => {
  98. test( () => editorData );
  99. } );
  100. describe( 'editor with editable element', () => {
  101. let editableElement;
  102. beforeEach( () => {
  103. editableElement = document.createElement( 'div' );
  104. editableElement.innerHTML = editorData;
  105. } );
  106. test( () => editableElement );
  107. } );
  108. it( 'initializes with config.initialData', () => {
  109. return DecoupledEditor.create( document.createElement( 'div' ), {
  110. initialData: '<p>Hello world!</p>',
  111. plugins: [ Paragraph ]
  112. } ).then( editor => {
  113. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  114. editor.destroy();
  115. } );
  116. } );
  117. // See: https://github.com/ckeditor/ckeditor5/issues/746
  118. it( 'should throw when trying to create the editor using the same source element more than once', done => {
  119. const sourceElement = document.createElement( 'div' );
  120. // eslint-disable-next-line no-new
  121. new DecoupledEditor( sourceElement );
  122. DecoupledEditor.create( sourceElement )
  123. .then(
  124. () => {
  125. expect.fail( 'Decoupled editor should not initialize on an element already used by other instance.' );
  126. },
  127. err => {
  128. assertCKEditorError( err, 'editor-source-element-already-used' );
  129. }
  130. )
  131. .then( done )
  132. .catch( done );
  133. } );
  134. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  135. DecoupledEditor.create( '<p>Hello world!</p>', {
  136. initialData: '<p>I am evil!</p>',
  137. plugins: [ Paragraph ]
  138. } )
  139. .then(
  140. () => {
  141. expect.fail( 'Decoupled editor should throw an error when both initial data are passed' );
  142. },
  143. err => {
  144. assertCKEditorError( err, 'editor-create-initial-data' );
  145. }
  146. )
  147. .then( () => {
  148. removeEditorBodyOrphans();
  149. } )
  150. .then( done )
  151. .catch( done );
  152. } );
  153. it( 'throws error if it is initialized in textarea', done => {
  154. DecoupledEditor.create( document.createElement( 'textarea' ) )
  155. .then(
  156. () => {
  157. expect.fail( 'Decoupled editor should throw an error when is initialized in textarea.' );
  158. },
  159. err => {
  160. assertCKEditorError( err, 'editor-wrong-element', null );
  161. }
  162. )
  163. .then( done )
  164. .catch( done );
  165. } );
  166. function test( getElementOrData ) {
  167. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  168. return DecoupledEditor
  169. .create( getElementOrData(), {
  170. plugins: [ Paragraph, Bold ]
  171. } )
  172. .then( newEditor => {
  173. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  174. return newEditor.destroy();
  175. } );
  176. } );
  177. it( 'loads the initial data', () => {
  178. return DecoupledEditor
  179. .create( getElementOrData(), {
  180. plugins: [ Paragraph, Bold ]
  181. } )
  182. .then( newEditor => {
  183. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  184. return newEditor.destroy();
  185. } );
  186. } );
  187. it( 'should not require config object', () => {
  188. // Just being safe with `builtinPlugins` static property.
  189. class CustomDecoupledEditor extends DecoupledEditor {}
  190. CustomDecoupledEditor.builtinPlugins = [ Paragraph, Bold ];
  191. return CustomDecoupledEditor.create( getElementOrData() )
  192. .then( newEditor => {
  193. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  194. return newEditor.destroy();
  195. } );
  196. } );
  197. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  198. it( 'creates an instance of a DecoupledEditor child class', () => {
  199. class CustomDecoupledEditor extends DecoupledEditor {}
  200. return CustomDecoupledEditor
  201. .create( getElementOrData(), {
  202. plugins: [ Paragraph, Bold ]
  203. } )
  204. .then( newEditor => {
  205. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  206. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  207. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  208. return newEditor.destroy();
  209. } );
  210. } );
  211. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  212. it( 'initializes the data controller', () => {
  213. let dataInitSpy;
  214. class DataInitAssertPlugin extends Plugin {
  215. constructor( editor ) {
  216. super();
  217. this.editor = editor;
  218. }
  219. init() {
  220. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  221. }
  222. }
  223. return DecoupledEditor
  224. .create( getElementOrData(), {
  225. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  226. } )
  227. .then( newEditor => {
  228. sinon.assert.calledOnce( dataInitSpy );
  229. return newEditor.destroy();
  230. } );
  231. } );
  232. describe( 'events', () => {
  233. it( 'fires all events in the right order', () => {
  234. const fired = [];
  235. function spy( evt ) {
  236. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  237. }
  238. class EventWatcher extends Plugin {
  239. init() {
  240. this.editor.ui.on( 'ready', spy );
  241. this.editor.data.on( 'ready', spy );
  242. this.editor.on( 'ready', spy );
  243. }
  244. }
  245. return DecoupledEditor
  246. .create( getElementOrData(), {
  247. plugins: [ EventWatcher ]
  248. } )
  249. .then( newEditor => {
  250. expect( fired ).to.deep.equal( [
  251. 'ready-decouplededitorui',
  252. 'ready-datacontroller',
  253. 'ready-decouplededitor'
  254. ] );
  255. return newEditor.destroy();
  256. } );
  257. } );
  258. it( 'fires ready once UI is rendered', () => {
  259. let isReady;
  260. class EventWatcher extends Plugin {
  261. init() {
  262. this.editor.ui.on( 'ready', () => {
  263. isReady = this.editor.ui.view.isRendered;
  264. } );
  265. }
  266. }
  267. return DecoupledEditor
  268. .create( getElementOrData(), {
  269. plugins: [ EventWatcher ]
  270. } )
  271. .then( newEditor => {
  272. expect( isReady ).to.be.true;
  273. return newEditor.destroy();
  274. } );
  275. } );
  276. } );
  277. }
  278. } );
  279. describe( 'destroy', () => {
  280. describe( 'editor with data', () => {
  281. beforeEach( function() {
  282. return DecoupledEditor
  283. .create( editorData, { plugins: [ Paragraph ] } )
  284. .then( newEditor => {
  285. editor = newEditor;
  286. } );
  287. } );
  288. test( () => editorData );
  289. } );
  290. describe( 'editor with editable element', () => {
  291. let editableElement;
  292. beforeEach( function() {
  293. editableElement = document.createElement( 'div' );
  294. editableElement.innerHTML = editorData;
  295. return DecoupledEditor
  296. .create( editableElement, { plugins: [ Paragraph ] } )
  297. .then( newEditor => {
  298. editor = newEditor;
  299. } );
  300. } );
  301. it( 'sets data back to the element', () => {
  302. editor.setData( '<p>foo</p>' );
  303. return editor.destroy()
  304. .then( () => {
  305. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  306. } );
  307. } );
  308. test( () => editableElement );
  309. } );
  310. function test() {
  311. it( 'destroys the UI', () => {
  312. const spy = sinon.spy( editor.ui, 'destroy' );
  313. return editor.destroy()
  314. .then( () => {
  315. sinon.assert.calledOnce( spy );
  316. } );
  317. } );
  318. }
  319. } );
  320. describeMemoryUsage( () => {
  321. testMemoryUsage(
  322. 'should not grow on multiple create/destroy',
  323. () => DecoupledEditor
  324. .create( document.querySelector( '#mem-editor' ), {
  325. plugins: [ ArticlePluginSet ],
  326. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  327. image: {
  328. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  329. }
  330. } ) );
  331. } );
  332. } );