8
0

inlineeditorui.js 9.7 KB

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