8
0

inlineeditorui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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( 'should set inline toolbar max-width to the width of the editable element', 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. }, 500 );
  111. } );
  112. } );
  113. describe( 'editable', () => {
  114. let editable;
  115. beforeEach( () => {
  116. editable = editor.editing.view.document.getRoot();
  117. } );
  118. it( 'registers view.editable#element in editor focus tracker', () => {
  119. ui.focusTracker.isFocused = false;
  120. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  121. expect( ui.focusTracker.isFocused ).to.true;
  122. } );
  123. it( 'sets view.editable#name', () => {
  124. expect( view.editable.name ).to.equal( editable.rootName );
  125. } );
  126. it( 'binds view.editable#isFocused', () => {
  127. assertBinding(
  128. view.editable,
  129. { isFocused: false },
  130. [
  131. [ ui.focusTracker, { isFocused: true } ]
  132. ],
  133. { isFocused: true }
  134. );
  135. } );
  136. } );
  137. describe( 'placeholder', () => {
  138. it( 'sets placeholder from editor.config.placeholder', () => {
  139. return VirtualInlineTestEditor
  140. .create( 'foo', {
  141. extraPlugins: [ Paragraph ],
  142. placeholder: 'placeholder-text',
  143. } )
  144. .then( newEditor => {
  145. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  146. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  147. return newEditor.destroy();
  148. } );
  149. } );
  150. it( 'sets placeholder from the "placeholder" attribute of a passed <textarea>', () => {
  151. const element = document.createElement( 'textarea' );
  152. element.setAttribute( 'placeholder', 'placeholder-text' );
  153. return VirtualInlineTestEditor
  154. .create( element, {
  155. extraPlugins: [ Paragraph ]
  156. } )
  157. .then( newEditor => {
  158. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  159. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  160. return newEditor.destroy();
  161. } );
  162. } );
  163. it( 'uses editor.config.placeholder rather than the "placeholder" attribute of a passed <textarea>', () => {
  164. const element = document.createElement( 'textarea' );
  165. element.setAttribute( 'placeholder', 'placeholder-text' );
  166. return VirtualInlineTestEditor
  167. .create( element, {
  168. placeholder: 'config takes precedence',
  169. extraPlugins: [ Paragraph ]
  170. } )
  171. .then( newEditor => {
  172. const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
  173. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
  174. return newEditor.destroy();
  175. } );
  176. } );
  177. } );
  178. describe( 'view.toolbar#items', () => {
  179. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  180. return VirtualInlineTestEditor
  181. .create( '', {
  182. toolbar: [ 'foo', 'bar' ]
  183. } )
  184. .then( editor => {
  185. const items = editor.ui.view.toolbar.items;
  186. expect( items.get( 0 ).name ).to.equal( 'foo' );
  187. expect( items.get( 1 ).name ).to.equal( 'bar' );
  188. return editor.destroy();
  189. } );
  190. } );
  191. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  192. return VirtualInlineTestEditor
  193. .create( '', {
  194. toolbar: {
  195. items: [ 'foo', 'bar' ],
  196. viewportTopOffset: 100
  197. }
  198. } )
  199. .then( editor => {
  200. const items = editor.ui.view.toolbar.items;
  201. expect( items.get( 0 ).name ).to.equal( 'foo' );
  202. expect( items.get( 1 ).name ).to.equal( 'bar' );
  203. return editor.destroy();
  204. } );
  205. } );
  206. } );
  207. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  208. return VirtualInlineTestEditor.create( '' )
  209. .then( editor => {
  210. const ui = editor.ui;
  211. const view = ui.view;
  212. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  213. ui.focusTracker.isFocused = true;
  214. ui.view.toolbar.focusTracker.isFocused = false;
  215. editor.keystrokes.press( {
  216. keyCode: keyCodes.f10,
  217. altKey: true,
  218. preventDefault: sinon.spy(),
  219. stopPropagation: sinon.spy()
  220. } );
  221. sinon.assert.calledOnce( spy );
  222. return editor.destroy();
  223. } );
  224. } );
  225. } );
  226. describe( 'destroy()', () => {
  227. it( 'detaches the DOM root then destroys the UI view', () => {
  228. return VirtualInlineTestEditor.create( '' )
  229. .then( newEditor => {
  230. const destroySpy = sinon.spy( newEditor.ui.view, 'destroy' );
  231. const detachSpy = sinon.spy( newEditor.editing.view, 'detachDomRoot' );
  232. return newEditor.destroy()
  233. .then( () => {
  234. sinon.assert.callOrder( detachSpy, destroySpy );
  235. } );
  236. } );
  237. } );
  238. it( 'restores the editor element back to its original state', () => {
  239. const domElement = document.createElement( 'div' );
  240. domElement.setAttribute( 'foo', 'bar' );
  241. domElement.setAttribute( 'data-baz', 'qux' );
  242. domElement.classList.add( 'foo-class' );
  243. return VirtualInlineTestEditor.create( domElement )
  244. .then( newEditor => {
  245. return newEditor.destroy()
  246. .then( () => {
  247. const attributes = {};
  248. for ( const attribute of Array.from( domElement.attributes ) ) {
  249. attributes[ attribute.name ] = attribute.value;
  250. }
  251. expect( attributes ).to.deep.equal( {
  252. foo: 'bar',
  253. 'data-baz': 'qux',
  254. class: 'foo-class'
  255. } );
  256. } );
  257. } );
  258. } );
  259. } );
  260. describe( 'element()', () => {
  261. it( 'returns correct element instance', () => {
  262. expect( ui.element ).to.equal( viewElement );
  263. } );
  264. } );
  265. describe( 'getEditableElement()', () => {
  266. it( 'returns editable element (default)', () => {
  267. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  268. } );
  269. it( 'returns editable element (root name passed)', () => {
  270. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  271. } );
  272. it( 'returns undefined if editable with the given name is absent', () => {
  273. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  274. } );
  275. } );
  276. } );
  277. function viewCreator( name ) {
  278. return locale => {
  279. const view = new View( locale );
  280. view.name = name;
  281. view.element = document.createElement( 'a' );
  282. return view;
  283. };
  284. }
  285. class VirtualInlineTestEditor extends VirtualTestEditor {
  286. constructor( sourceElementOrData, config ) {
  287. super( config );
  288. if ( isElement( sourceElementOrData ) ) {
  289. this.sourceElement = sourceElementOrData;
  290. }
  291. const view = new InlineEditorUIView( this.locale, this.editing.view );
  292. this.ui = new InlineEditorUI( this, view );
  293. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  294. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  295. }
  296. destroy() {
  297. this.ui.destroy();
  298. return super.destroy();
  299. }
  300. static create( sourceElementOrData, config ) {
  301. return new Promise( resolve => {
  302. const editor = new this( sourceElementOrData, config );
  303. resolve(
  304. editor.initPlugins()
  305. .then( () => {
  306. editor.ui.init();
  307. const initialData = isElement( sourceElementOrData ) ?
  308. sourceElementOrData.innerHTML :
  309. sourceElementOrData;
  310. editor.data.init( initialData );
  311. editor.fire( 'ready' );
  312. } )
  313. .then( () => editor )
  314. );
  315. } );
  316. }
  317. }