classiceditorui.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 "placeholder" attribute of a passed element', () => {
  104. const element = document.createElement( 'div' );
  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 "placeholder" attribute of a passed element', () => {
  117. const element = document.createElement( 'div' );
  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( 'view()', () => {
  180. it( 'returns view instance', () => {
  181. expect( ui.view ).to.equal( view );
  182. } );
  183. } );
  184. describe( 'element()', () => {
  185. it( 'returns correct element instance', () => {
  186. expect( ui.element ).to.equal( viewElement );
  187. } );
  188. } );
  189. describe( 'getEditableElement()', () => {
  190. it( 'returns editable element (default)', () => {
  191. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  192. } );
  193. it( 'returns editable element (root name passed)', () => {
  194. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  195. } );
  196. it( 'returns undefined if editable with the given name is absent', () => {
  197. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  198. } );
  199. } );
  200. } );
  201. function viewCreator( name ) {
  202. return locale => {
  203. const view = new View( locale );
  204. view.name = name;
  205. view.element = document.createElement( 'a' );
  206. return view;
  207. };
  208. }
  209. class VirtualClassicTestEditor extends VirtualTestEditor {
  210. constructor( sourceElementOrData, config ) {
  211. super( config );
  212. if ( isElement( sourceElementOrData ) ) {
  213. this.sourceElement = sourceElementOrData;
  214. }
  215. const view = new ClassicEditorUIView( this.locale, this.editing.view );
  216. this.ui = new ClassicEditorUI( this, view );
  217. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  218. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  219. }
  220. destroy() {
  221. this.ui.destroy();
  222. return super.destroy();
  223. }
  224. static create( sourceElementOrData, config ) {
  225. return new Promise( resolve => {
  226. const editor = new this( sourceElementOrData, config );
  227. resolve(
  228. editor.initPlugins()
  229. .then( () => {
  230. editor.ui.init();
  231. const initialData = isElement( sourceElementOrData ) ?
  232. sourceElementOrData.innerHTML :
  233. sourceElementOrData;
  234. editor.data.init( initialData );
  235. editor.fire( 'ready' );
  236. } )
  237. .then( () => editor )
  238. );
  239. } );
  240. }
  241. }