8
0

classiceditorui.js 9.3 KB

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