inlineeditorui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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, setTimeout */
  6. import View from '@ckeditor/ckeditor5-ui/src/view';
  7. import InlineEditorUI from '../src/inlineeditorui';
  8. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  9. import InlineEditorUIView from '../src/inlineeditoruiview';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  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( 'InlineEditorUI', () => {
  17. let editor, view, ui, viewElement;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. return VirtualInlineTestEditor
  21. .create( 'foo', {
  22. toolbar: [ 'foo', 'bar' ],
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. ui = editor.ui;
  27. view = ui.view;
  28. viewElement = view.editable.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( 'panel', () => {
  44. afterEach( () => {
  45. if ( document.body.contains( viewElement ) ) {
  46. document.body.removeChild( viewElement );
  47. }
  48. } );
  49. it( 'binds view.panel#isVisible to editor.ui#focusTracker', () => {
  50. ui.focusTracker.isFocused = false;
  51. expect( view.panel.isVisible ).to.be.false;
  52. ui.focusTracker.isFocused = true;
  53. expect( view.panel.isVisible ).to.be.true;
  54. } );
  55. it( 'doesn\'t set the view#viewportTopOffset, if not specified in the config', () => {
  56. expect( view.viewportTopOffset ).to.equal( 0 );
  57. } );
  58. it( 'sets view#viewportTopOffset, if specified', () => {
  59. return VirtualInlineTestEditor
  60. .create( 'foo', {
  61. toolbar: {
  62. viewportTopOffset: 100
  63. }
  64. } )
  65. .then( editor => {
  66. const ui = editor.ui;
  67. const view = ui.view;
  68. expect( view.viewportTopOffset ).to.equal( 100 );
  69. return editor.destroy();
  70. } );
  71. } );
  72. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  73. it( 'pin() is called on editor.ui#update when editable element is in the DOM', () => {
  74. const spy = sinon.stub( view.panel, 'pin' );
  75. document.body.appendChild( viewElement );
  76. view.panel.show();
  77. expect( document.body.contains( viewElement ) ).to.be.true;
  78. editor.ui.fire( 'update' );
  79. sinon.assert.calledOnce( spy );
  80. sinon.assert.calledWithExactly( spy, {
  81. target: view.editable.element,
  82. positions: sinon.match.array
  83. } );
  84. viewElement.remove();
  85. } );
  86. it( 'pin() is not called on editor.ui#update when panel is hidden', () => {
  87. const spy = sinon.stub( view.panel, 'pin' );
  88. view.panel.hide();
  89. editor.ui.fire( 'update' );
  90. sinon.assert.notCalled( spy );
  91. } );
  92. it( 'pin() is not called on editor.ui#update when panel is visible but editable element is not in the DOM', () => {
  93. const spy = sinon.stub( view.panel, 'pin' );
  94. view.panel.show();
  95. expect( document.body.contains( viewElement ) ).to.be.false;
  96. editor.ui.fire( 'update' );
  97. sinon.assert.notCalled( spy );
  98. } );
  99. it( 'toolbar max-width is set to the value of width of the editable element, otherwise it can be wider then editor', done => {
  100. document.body.appendChild( viewElement );
  101. expect( document.body.contains( viewElement ) ).to.be.true;
  102. viewElement.style.width = '400px';
  103. // Unfortunately we have to wait for async ResizeObserver execution.
  104. // ResizeObserver which has been called after changing width of viewElement,
  105. // needs 2x requestAnimationFrame or timeout to update a layout.
  106. // See more: https://twitter.com/paul_irish/status/912693347315150849/photo/1
  107. setTimeout( () => {
  108. expect( view.toolbar.maxWidth ).to.be.equal( '400px' );
  109. done();
  110. }, 100 );
  111. } );
  112. it( 'toolbar should group items by default', () => {
  113. expect( view.toolbar.options.shouldGroupWhenFull ).to.be.true;
  114. } );
  115. } );
  116. describe( 'editable', () => {
  117. let editable;
  118. beforeEach( () => {
  119. editable = editor.editing.view.document.getRoot();
  120. } );
  121. it( 'registers view.editable#element in editor focus tracker', () => {
  122. ui.focusTracker.isFocused = false;
  123. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  124. expect( ui.focusTracker.isFocused ).to.true;
  125. } );
  126. it( 'sets view.editable#name', () => {
  127. expect( view.editable.name ).to.equal( editable.rootName );
  128. } );
  129. it( 'binds view.editable#isFocused', () => {
  130. assertBinding(
  131. view.editable,
  132. { isFocused: false },
  133. [
  134. [ ui.focusTracker, { isFocused: true } ]
  135. ],
  136. { isFocused: true }
  137. );
  138. } );
  139. } );
  140. describe( 'placeholder', () => {
  141. it( 'sets placeholder from editor.config.placeholder', () => {
  142. return VirtualInlineTestEditor
  143. .create( 'foo', {
  144. extraPlugins: [ Paragraph ],
  145. placeholder: 'placeholder-text',
  146. } )
  147. .then( newEditor => {
  148. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  149. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  150. return newEditor.destroy();
  151. } );
  152. } );
  153. it( 'sets placeholder from the "placeholder" attribute of a passed <textarea>', () => {
  154. const element = document.createElement( 'textarea' );
  155. element.setAttribute( 'placeholder', 'placeholder-text' );
  156. return VirtualInlineTestEditor
  157. .create( element, {
  158. extraPlugins: [ Paragraph ]
  159. } )
  160. .then( newEditor => {
  161. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  162. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  163. return newEditor.destroy();
  164. } );
  165. } );
  166. it( 'uses editor.config.placeholder rather than the "placeholder" attribute of a passed <textarea>', () => {
  167. const element = document.createElement( 'textarea' );
  168. element.setAttribute( 'placeholder', 'placeholder-text' );
  169. return VirtualInlineTestEditor
  170. .create( element, {
  171. placeholder: 'config takes precedence',
  172. extraPlugins: [ Paragraph ]
  173. } )
  174. .then( newEditor => {
  175. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  176. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
  177. return newEditor.destroy();
  178. } );
  179. } );
  180. } );
  181. describe( 'view.toolbar#items', () => {
  182. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  183. return VirtualInlineTestEditor
  184. .create( '', {
  185. toolbar: [ 'foo', 'bar' ]
  186. } )
  187. .then( editor => {
  188. const items = editor.ui.view.toolbar.items;
  189. expect( items.get( 0 ).name ).to.equal( 'foo' );
  190. expect( items.get( 1 ).name ).to.equal( 'bar' );
  191. return editor.destroy();
  192. } );
  193. } );
  194. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  195. return VirtualInlineTestEditor
  196. .create( '', {
  197. toolbar: {
  198. items: [ 'foo', 'bar' ],
  199. viewportTopOffset: 100
  200. }
  201. } )
  202. .then( editor => {
  203. const items = editor.ui.view.toolbar.items;
  204. expect( items.get( 0 ).name ).to.equal( 'foo' );
  205. expect( items.get( 1 ).name ).to.equal( 'bar' );
  206. return editor.destroy();
  207. } );
  208. } );
  209. } );
  210. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  211. return VirtualInlineTestEditor.create( '' )
  212. .then( editor => {
  213. const ui = editor.ui;
  214. const view = ui.view;
  215. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  216. ui.focusTracker.isFocused = true;
  217. ui.view.toolbar.focusTracker.isFocused = false;
  218. editor.keystrokes.press( {
  219. keyCode: keyCodes.f10,
  220. altKey: true,
  221. preventDefault: sinon.spy(),
  222. stopPropagation: sinon.spy()
  223. } );
  224. sinon.assert.calledOnce( spy );
  225. return editor.destroy();
  226. } );
  227. } );
  228. } );
  229. describe( 'destroy()', () => {
  230. it( 'detaches the DOM root then destroys the UI view', () => {
  231. return VirtualInlineTestEditor.create( '' )
  232. .then( newEditor => {
  233. const destroySpy = sinon.spy( newEditor.ui.view, 'destroy' );
  234. const detachSpy = sinon.spy( newEditor.editing.view, 'detachDomRoot' );
  235. return newEditor.destroy()
  236. .then( () => {
  237. sinon.assert.callOrder( detachSpy, destroySpy );
  238. } );
  239. } );
  240. } );
  241. it( 'restores the editor element back to its original state', () => {
  242. const domElement = document.createElement( 'div' );
  243. domElement.setAttribute( 'foo', 'bar' );
  244. domElement.setAttribute( 'data-baz', 'qux' );
  245. domElement.classList.add( 'foo-class' );
  246. return VirtualInlineTestEditor.create( domElement )
  247. .then( newEditor => {
  248. return newEditor.destroy()
  249. .then( () => {
  250. const attributes = {};
  251. for ( const attribute of Array.from( domElement.attributes ) ) {
  252. attributes[ attribute.name ] = attribute.value;
  253. }
  254. expect( attributes ).to.deep.equal( {
  255. foo: 'bar',
  256. 'data-baz': 'qux',
  257. class: 'foo-class'
  258. } );
  259. } );
  260. } );
  261. } );
  262. } );
  263. describe( 'element()', () => {
  264. it( 'returns correct element instance', () => {
  265. expect( ui.element ).to.equal( viewElement );
  266. } );
  267. } );
  268. describe( 'getEditableElement()', () => {
  269. it( 'returns editable element (default)', () => {
  270. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  271. } );
  272. it( 'returns editable element (root name passed)', () => {
  273. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  274. } );
  275. it( 'returns undefined if editable with the given name is absent', () => {
  276. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  277. } );
  278. } );
  279. } );
  280. function viewCreator( name ) {
  281. return locale => {
  282. const view = new View( locale );
  283. view.name = name;
  284. view.element = document.createElement( 'a' );
  285. return view;
  286. };
  287. }
  288. class VirtualInlineTestEditor extends VirtualTestEditor {
  289. constructor( sourceElementOrData, config ) {
  290. super( config );
  291. if ( isElement( sourceElementOrData ) ) {
  292. this.sourceElement = sourceElementOrData;
  293. }
  294. const view = new InlineEditorUIView( this.locale, this.editing.view );
  295. this.ui = new InlineEditorUI( this, view );
  296. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  297. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  298. }
  299. destroy() {
  300. this.ui.destroy();
  301. return super.destroy();
  302. }
  303. static create( sourceElementOrData, config ) {
  304. return new Promise( resolve => {
  305. const editor = new this( sourceElementOrData, config );
  306. resolve(
  307. editor.initPlugins()
  308. .then( () => {
  309. editor.ui.init();
  310. const initialData = isElement( sourceElementOrData ) ?
  311. sourceElementOrData.innerHTML :
  312. sourceElementOrData;
  313. editor.data.init( initialData );
  314. editor.fire( 'ready' );
  315. } )
  316. .then( () => editor )
  317. );
  318. } );
  319. }
  320. }