decouplededitorui.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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, Event */
  6. import View from '@ckeditor/ckeditor5-ui/src/view';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import DecoupledEditorUI from '../src/decouplededitorui';
  9. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import { assertBinding } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. import { isElement } from 'lodash-es';
  16. describe( 'DecoupledEditorUI', () => {
  17. let editor, view, ui, viewElement;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. return VirtualDecoupledTestEditor
  21. .create( '', {
  22. toolbar: [ 'foo', 'bar' ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. ui = editor.ui;
  27. view = ui.view;
  28. viewElement = view.element;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editor.destroy();
  33. } );
  34. describe( 'constructor()', () => {
  35. it( 'extends EditorUI', () => {
  36. expect( ui ).to.instanceof( EditorUI );
  37. } );
  38. } );
  39. describe( 'init()', () => {
  40. it( 'renders the #view', () => {
  41. expect( view.isRendered ).to.be.true;
  42. } );
  43. describe( 'editable', () => {
  44. it( 'registers view.editable#element in editor focus tracker', () => {
  45. ui.focusTracker.isFocused = false;
  46. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  47. expect( ui.focusTracker.isFocused ).to.true;
  48. } );
  49. it( 'sets view.editable#name', () => {
  50. const editable = editor.editing.view.document.getRoot();
  51. expect( view.editable.name ).to.equal( editable.rootName );
  52. } );
  53. it( 'binds view.editable#isFocused', () => {
  54. assertBinding(
  55. view.editable,
  56. { isFocused: false },
  57. [
  58. [ ui.focusTracker, { isFocused: true } ]
  59. ],
  60. { isFocused: true }
  61. );
  62. } );
  63. it( 'attaches editable UI as view\'s DOM root', () => {
  64. expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
  65. } );
  66. } );
  67. describe( 'placeholder', () => {
  68. it( 'sets placeholder from editor.config.placeholder', () => {
  69. return VirtualDecoupledTestEditor
  70. .create( 'foo', {
  71. extraPlugins: [ Paragraph ],
  72. placeholder: 'placeholder-text'
  73. } )
  74. .then( newEditor => {
  75. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  76. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  77. return newEditor.destroy();
  78. } );
  79. } );
  80. it( 'sets placeholder from the "placeholder" attribute of a passed <textarea>', () => {
  81. const element = document.createElement( 'textarea' );
  82. element.setAttribute( 'placeholder', 'placeholder-text' );
  83. return VirtualDecoupledTestEditor
  84. .create( element, {
  85. extraPlugins: [ Paragraph ]
  86. } )
  87. .then( newEditor => {
  88. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  89. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  90. return newEditor.destroy();
  91. } );
  92. } );
  93. it( 'uses editor.config.placeholder rather than the "placeholder" attribute of a passed <textarea>', () => {
  94. const element = document.createElement( 'textarea' );
  95. element.setAttribute( 'placeholder', 'placeholder-text' );
  96. return VirtualDecoupledTestEditor
  97. .create( element, {
  98. placeholder: 'config takes precedence',
  99. extraPlugins: [ Paragraph ]
  100. } )
  101. .then( newEditor => {
  102. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  103. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
  104. return newEditor.destroy();
  105. } );
  106. } );
  107. } );
  108. describe( 'view.toolbar', () => {
  109. describe( '#items', () => {
  110. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  111. return VirtualDecoupledTestEditor
  112. .create( '', {
  113. toolbar: [ 'foo', 'bar' ]
  114. } )
  115. .then( editor => {
  116. const items = editor.ui.view.toolbar.items;
  117. expect( items.get( 0 ).name ).to.equal( 'foo' );
  118. expect( items.get( 1 ).name ).to.equal( 'bar' );
  119. return editor.destroy();
  120. } );
  121. } );
  122. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  123. return VirtualDecoupledTestEditor
  124. .create( '', {
  125. toolbar: {
  126. items: [ 'foo', 'bar' ],
  127. viewportTopOffset: 100
  128. }
  129. } )
  130. .then( editor => {
  131. const items = editor.ui.view.toolbar.items;
  132. expect( items.get( 0 ).name ).to.equal( 'foo' );
  133. expect( items.get( 1 ).name ).to.equal( 'bar' );
  134. return editor.destroy();
  135. } );
  136. } );
  137. } );
  138. } );
  139. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  140. return VirtualDecoupledTestEditor.create( '' )
  141. .then( editor => {
  142. const ui = editor.ui;
  143. const view = ui.view;
  144. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  145. ui.focusTracker.isFocused = true;
  146. ui.view.toolbar.focusTracker.isFocused = false;
  147. editor.keystrokes.press( {
  148. keyCode: keyCodes.f10,
  149. altKey: true,
  150. preventDefault: sinon.spy(),
  151. stopPropagation: sinon.spy()
  152. } );
  153. sinon.assert.calledOnce( spy );
  154. return editor.destroy();
  155. } );
  156. } );
  157. } );
  158. describe( 'destroy()', () => {
  159. it( 'detaches the DOM root then destroys the UI view', () => {
  160. return VirtualDecoupledTestEditor.create( '' )
  161. .then( newEditor => {
  162. const destroySpy = sinon.spy( newEditor.ui.view, 'destroy' );
  163. const detachSpy = sinon.spy( newEditor.editing.view, 'detachDomRoot' );
  164. return newEditor.destroy()
  165. .then( () => {
  166. sinon.assert.callOrder( detachSpy, destroySpy );
  167. } );
  168. } );
  169. } );
  170. it( 'restores the editor element back to its original state', () => {
  171. const domElement = document.createElement( 'div' );
  172. domElement.setAttribute( 'foo', 'bar' );
  173. domElement.setAttribute( 'data-baz', 'qux' );
  174. domElement.classList.add( 'foo-class' );
  175. return VirtualDecoupledTestEditor.create( domElement )
  176. .then( newEditor => {
  177. return newEditor.destroy()
  178. .then( () => {
  179. const attributes = {};
  180. for ( const attribute of Array.from( domElement.attributes ) ) {
  181. attributes[ attribute.name ] = attribute.value;
  182. }
  183. expect( attributes ).to.deep.equal( {
  184. foo: 'bar',
  185. 'data-baz': 'qux',
  186. class: 'foo-class'
  187. } );
  188. } );
  189. } );
  190. } );
  191. } );
  192. describe( 'element()', () => {
  193. it( 'returns correct element instance', () => {
  194. expect( ui.element ).to.equal( viewElement );
  195. } );
  196. } );
  197. describe( 'getEditableElement()', () => {
  198. it( 'returns editable element (default)', () => {
  199. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  200. } );
  201. it( 'returns editable element (root name passed)', () => {
  202. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  203. } );
  204. it( 'returns undefined if editable with the given name is absent', () => {
  205. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  206. } );
  207. } );
  208. } );
  209. function viewCreator( name ) {
  210. return locale => {
  211. const view = new View( locale );
  212. view.name = name;
  213. view.element = document.createElement( 'a' );
  214. return view;
  215. };
  216. }
  217. class VirtualDecoupledTestEditor extends VirtualTestEditor {
  218. constructor( sourceElementOrData, config ) {
  219. super( config );
  220. if ( isElement( sourceElementOrData ) ) {
  221. this.sourceElement = sourceElementOrData;
  222. }
  223. const view = new DecoupledEditorUIView( this.locale, this.editing.view );
  224. this.ui = new DecoupledEditorUI( this, view );
  225. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  226. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  227. }
  228. destroy() {
  229. this.ui.destroy();
  230. return super.destroy();
  231. }
  232. static create( sourceElementOrData, config ) {
  233. return new Promise( resolve => {
  234. const editor = new this( sourceElementOrData, config );
  235. resolve(
  236. editor.initPlugins()
  237. .then( () => {
  238. editor.ui.init();
  239. const initialData = isElement( sourceElementOrData ) ?
  240. sourceElementOrData.innerHTML :
  241. sourceElementOrData;
  242. editor.data.init( initialData );
  243. editor.fire( 'ready' );
  244. } )
  245. .then( () => editor )
  246. );
  247. } );
  248. }
  249. }