inlineeditorui.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 utils 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.hide();
  71. editor.ui.fire( 'update' );
  72. sinon.assert.notCalled( spy );
  73. view.panel.show();
  74. editor.ui.fire( 'update' );
  75. sinon.assert.calledOnce( spy );
  76. sinon.assert.calledWithExactly( spy, {
  77. target: view.editable.element,
  78. positions: sinon.match.array
  79. } );
  80. } );
  81. } );
  82. describe( 'editable', () => {
  83. let editable;
  84. beforeEach( () => {
  85. editable = editor.editing.view.document.getRoot();
  86. } );
  87. it( 'registers view.editable#element in editor focus tracker', () => {
  88. ui.focusTracker.isFocused = false;
  89. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  90. expect( ui.focusTracker.isFocused ).to.true;
  91. } );
  92. it( 'sets view.editable#name', () => {
  93. expect( view.editable.name ).to.equal( editable.rootName );
  94. } );
  95. it( 'binds view.editable#isFocused', () => {
  96. utils.assertBinding(
  97. view.editable,
  98. { isFocused: false },
  99. [
  100. [ ui.focusTracker, { isFocused: true } ]
  101. ],
  102. { isFocused: true }
  103. );
  104. } );
  105. } );
  106. describe( 'placeholder', () => {
  107. it( 'sets placeholder from editor.config.placeholder', () => {
  108. return VirtualInlineTestEditor
  109. .create( 'foo', {
  110. extraPlugins: [ Paragraph ],
  111. placeholder: 'placeholder-text',
  112. } )
  113. .then( newEditor => {
  114. editor = newEditor;
  115. const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
  116. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  117. return editor.destroy();
  118. } );
  119. } );
  120. it( 'sets placeholder from "placeholder" attribute of a passed element', () => {
  121. const element = document.createElement( 'div' );
  122. element.setAttribute( 'placeholder', 'placeholder-text' );
  123. return VirtualInlineTestEditor
  124. .create( element, {
  125. extraPlugins: [ Paragraph ]
  126. } )
  127. .then( newEditor => {
  128. editor = newEditor;
  129. const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
  130. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
  131. return editor.destroy();
  132. } );
  133. } );
  134. it( 'uses editor.config.placeholder rather than "placeholder" attribute of a passed element', () => {
  135. const element = document.createElement( 'div' );
  136. element.setAttribute( 'placeholder', 'placeholder-text' );
  137. return VirtualInlineTestEditor
  138. .create( element, {
  139. placeholder: 'config takes precedence',
  140. extraPlugins: [ Paragraph ]
  141. } )
  142. .then( newEditor => {
  143. editor = newEditor;
  144. const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
  145. expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
  146. return editor.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( 'element()', () => {
  199. it( 'returns correct element instance', () => {
  200. expect( ui.element ).to.equal( viewElement );
  201. } );
  202. } );
  203. describe( 'getEditableElement()', () => {
  204. it( 'returns editable element (default)', () => {
  205. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  206. } );
  207. it( 'returns editable element (root name passed)', () => {
  208. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  209. } );
  210. it( 'returns undefined if editable with the given name is absent', () => {
  211. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  212. } );
  213. } );
  214. } );
  215. function viewCreator( name ) {
  216. return locale => {
  217. const view = new View( locale );
  218. view.name = name;
  219. view.element = document.createElement( 'a' );
  220. return view;
  221. };
  222. }
  223. class VirtualInlineTestEditor extends VirtualTestEditor {
  224. constructor( sourceElementOrData, config ) {
  225. super( config );
  226. if ( isElement( sourceElementOrData ) ) {
  227. this.sourceElement = sourceElementOrData;
  228. }
  229. const view = new InlineEditorUIView( this.locale, this.editing.view );
  230. this.ui = new InlineEditorUI( this, view );
  231. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  232. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  233. }
  234. destroy() {
  235. this.ui.destroy();
  236. return super.destroy();
  237. }
  238. static create( sourceElementOrData, config ) {
  239. return new Promise( resolve => {
  240. const editor = new this( sourceElementOrData, config );
  241. resolve(
  242. editor.initPlugins()
  243. .then( () => {
  244. editor.ui.init();
  245. editor.fire( 'ready' );
  246. } )
  247. .then( () => editor )
  248. );
  249. } );
  250. }
  251. }