8
0

classiceditorui.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 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 { assertBinding } 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. 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', () => {
  132. describe( '#items', () => {
  133. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  134. return VirtualClassicTestEditor
  135. .create( '', {
  136. toolbar: [ 'foo', 'bar' ]
  137. } )
  138. .then( editor => {
  139. const items = editor.ui.view.toolbar.items;
  140. expect( items.get( 0 ).name ).to.equal( 'foo' );
  141. expect( items.get( 1 ).name ).to.equal( 'bar' );
  142. return editor.destroy();
  143. } );
  144. } );
  145. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  146. return VirtualClassicTestEditor
  147. .create( '', {
  148. toolbar: {
  149. items: [ 'foo', 'bar' ],
  150. viewportTopOffset: 100
  151. }
  152. } )
  153. .then( editor => {
  154. const items = editor.ui.view.toolbar.items;
  155. expect( items.get( 0 ).name ).to.equal( 'foo' );
  156. expect( items.get( 1 ).name ).to.equal( 'bar' );
  157. return editor.destroy();
  158. } );
  159. } );
  160. } );
  161. } );
  162. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  163. return VirtualClassicTestEditor.create( '' )
  164. .then( editor => {
  165. const ui = editor.ui;
  166. const view = ui.view;
  167. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  168. ui.focusTracker.isFocused = true;
  169. ui.view.toolbar.focusTracker.isFocused = false;
  170. editor.keystrokes.press( {
  171. keyCode: keyCodes.f10,
  172. altKey: true,
  173. preventDefault: sinon.spy(),
  174. stopPropagation: sinon.spy()
  175. } );
  176. sinon.assert.calledOnce( spy );
  177. return editor.destroy();
  178. } );
  179. } );
  180. } );
  181. describe( 'destroy()', () => {
  182. it( 'detaches the DOM root then destroys the UI view', () => {
  183. return VirtualClassicTestEditor.create( '' )
  184. .then( newEditor => {
  185. const destroySpy = sinon.spy( newEditor.ui.view, 'destroy' );
  186. const detachSpy = sinon.spy( newEditor.editing.view, 'detachDomRoot' );
  187. return newEditor.destroy()
  188. .then( () => {
  189. sinon.assert.callOrder( detachSpy, destroySpy );
  190. } );
  191. } );
  192. } );
  193. it( 'restores the editor element back to its original state', () => {
  194. const domElement = document.createElement( 'div' );
  195. domElement.setAttribute( 'foo', 'bar' );
  196. domElement.setAttribute( 'data-baz', 'qux' );
  197. domElement.classList.add( 'foo-class' );
  198. return VirtualClassicTestEditor.create( domElement )
  199. .then( newEditor => {
  200. return newEditor.destroy()
  201. .then( () => {
  202. const attributes = {};
  203. for ( const attribute of Array.from( domElement.attributes ) ) {
  204. attributes[ attribute.name ] = attribute.value;
  205. }
  206. expect( attributes ).to.deep.equal( {
  207. foo: 'bar',
  208. 'data-baz': 'qux',
  209. class: 'foo-class'
  210. } );
  211. } );
  212. } );
  213. } );
  214. } );
  215. describe( 'view()', () => {
  216. it( 'returns view instance', () => {
  217. expect( ui.view ).to.equal( view );
  218. } );
  219. } );
  220. describe( 'element()', () => {
  221. it( 'returns correct element instance', () => {
  222. expect( ui.element ).to.equal( viewElement );
  223. } );
  224. } );
  225. describe( 'getEditableElement()', () => {
  226. it( 'returns editable element (default)', () => {
  227. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  228. } );
  229. it( 'returns editable element (root name passed)', () => {
  230. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  231. } );
  232. it( 'returns undefined if editable with the given name is absent', () => {
  233. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  234. } );
  235. } );
  236. } );
  237. function viewCreator( name ) {
  238. return locale => {
  239. const view = new View( locale );
  240. view.name = name;
  241. view.element = document.createElement( 'a' );
  242. return view;
  243. };
  244. }
  245. class VirtualClassicTestEditor extends VirtualTestEditor {
  246. constructor( sourceElementOrData, config ) {
  247. super( config );
  248. if ( isElement( sourceElementOrData ) ) {
  249. this.sourceElement = sourceElementOrData;
  250. }
  251. const view = new ClassicEditorUIView( this.locale, this.editing.view );
  252. this.ui = new ClassicEditorUI( this, view );
  253. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  254. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  255. }
  256. destroy() {
  257. this.ui.destroy();
  258. return super.destroy();
  259. }
  260. static create( sourceElementOrData, config ) {
  261. return new Promise( resolve => {
  262. const editor = new this( sourceElementOrData, config );
  263. resolve(
  264. editor.initPlugins()
  265. .then( () => {
  266. editor.ui.init();
  267. const initialData = isElement( sourceElementOrData ) ?
  268. sourceElementOrData.innerHTML :
  269. sourceElementOrData;
  270. editor.data.init( initialData );
  271. editor.fire( 'ready' );
  272. } )
  273. .then( () => editor )
  274. );
  275. } );
  276. }
  277. }