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