| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import InlineEditorUIView from '../src/inlineeditoruiview';
- import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
- import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
- import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
- import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
- import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
- import Locale from '@ckeditor/ckeditor5-utils/src/locale';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- describe( 'InlineEditorUIView', () => {
- let locale, view, editingView, editingViewRoot;
- testUtils.createSinonSandbox();
- beforeEach( () => {
- locale = new Locale( 'en' );
- setUpEditingView();
- view = new InlineEditorUIView( locale, editingView );
- view.editable.name = editingViewRoot.rootName;
- } );
- describe( 'constructor()', () => {
- describe( '#toolbar', () => {
- it( 'is created', () => {
- expect( view.toolbar ).to.be.instanceof( ToolbarView );
- } );
- it( 'is given a locale object', () => {
- expect( view.toolbar.locale ).to.equal( locale );
- } );
- it( 'is not rendered', () => {
- expect( view.toolbar.isRendered ).to.be.false;
- } );
- } );
- describe( '#panel', () => {
- it( 'is created', () => {
- expect( view.panel ).to.be.instanceof( BalloonPanelView );
- } );
- it( 'is given a locale object', () => {
- expect( view.panel.locale ).to.equal( locale );
- } );
- it( 'gets view.panel#withArrow set', () => {
- expect( view.panel.withArrow ).to.be.false;
- } );
- it( 'is not rendered', () => {
- expect( view.panel.isRendered ).to.be.false;
- } );
- } );
- describe( '#editable', () => {
- it( 'is created', () => {
- expect( view.editable ).to.be.instanceof( InlineEditableUIView );
- } );
- it( 'is given a locale object', () => {
- expect( view.editable.locale ).to.equal( locale );
- } );
- it( 'is not rendered', () => {
- expect( view.editable.isRendered ).to.be.false;
- } );
- } );
- } );
- describe( 'render()', () => {
- beforeEach( () => {
- view.render();
- } );
- afterEach( () => {
- view.destroy();
- } );
- describe( '#toolbar', () => {
- it( 'is given the right CSS classes', () => {
- expect( view.toolbar.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
- } );
- it( 'sets the default value of the #viewportTopOffset attribute', () => {
- expect( view.viewportTopOffset ).to.equal( 0 );
- } );
- } );
- describe( '#panel', () => {
- it( 'is given the right CSS class', () => {
- expect( view.panel.element.classList.contains( 'ck-toolbar-container' ) ).to.be.true;
- } );
- it( 'is put into the #body collection', () => {
- expect( view.body.get( 0 ) ).to.equal( view.panel );
- } );
- } );
- describe( '#editable', () => {
- it( 'is registered as a child', () => {
- const spy = sinon.spy( view.editable, 'destroy' );
- view.destroy();
- sinon.assert.calledOnce( spy );
- } );
- } );
- } );
- describe( 'init()', () => {
- it( 'appends #toolbar to panel#content', () => {
- locale = new Locale( 'en' );
- setUpEditingView();
- const view = new InlineEditorUIView( locale, editingView );
- view.editable.name = editingViewRoot.rootName;
- expect( view.panel.content ).to.have.length( 0 );
- view.render();
- expect( view.panel.content.get( 0 ) ).to.equal( view.toolbar );
- view.destroy();
- } );
- } );
- describe( 'panelPositions', () => {
- it( 'returns the positions in the right order', () => {
- const positions = view.panelPositions;
- const editableRect = {
- top: 100,
- bottom: 200,
- left: 100,
- right: 100,
- width: 100,
- height: 100
- };
- const panelRect = {
- width: 50,
- height: 50
- };
- expect( positions ).to.have.length( 2 );
- expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
- expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
- } );
- describe( 'west', () => {
- testTopPositions( 0, 100 );
- } );
- describe( 'east', () => {
- testTopPositions( 1, 150 );
- } );
- function testTopPositions( positionIndex, expectedLeft ) {
- it( 'positions the panel above editable when there\'s enough space', () => {
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 101, // !
- bottom: 200,
- left: 100,
- right: 100,
- width: 100,
- height: 100
- };
- const panelRect = {
- width: 50,
- height: 100 // !
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 1 );
- expect( left ).to.equal( expectedLeft );
- } );
- it( 'positions the panel over the editable when there\'s not enough space above (1)', () => {
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 100, // !
- bottom: 300,
- left: 100,
- right: 100,
- width: 100,
- height: 200
- };
- const panelRect = {
- width: 50,
- height: 100 // !
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 0 );
- expect( left ).to.equal( expectedLeft );
- } );
- it( 'positions the panel over the editable when there\'s not enough space above (2)', () => {
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 99, // !
- bottom: 399,
- left: 100,
- right: 100,
- width: 100,
- height: 200
- };
- const panelRect = {
- width: 50,
- height: 100 // !
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 0 );
- expect( left ).to.equal( expectedLeft );
- } );
- it( 'positions the panel over the editable when there\'s not enough space above (3)', () => {
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 51, // !
- bottom: 399,
- left: 100,
- right: 100,
- width: 100,
- height: 200
- };
- const panelRect = {
- width: 50,
- height: 100 // !
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 0 );
- expect( left ).to.equal( expectedLeft );
- } );
- it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 50,
- bottom: 150, // !
- left: 100,
- right: 100,
- width: 100,
- height: 100
- };
- const panelRect = {
- width: 50,
- height: 100 // !
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 150 );
- expect( left ).to.equal( expectedLeft );
- } );
- describe( 'view#viewportTopOffset', () => {
- it( 'sticks the panel to the offset when there\'s not enough space above', () => {
- view.viewportTopOffset = 50;
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 0, // !
- bottom: 200,
- left: 100,
- right: 100,
- width: 100,
- height: 200
- };
- const panelRect = {
- width: 50,
- height: 50
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 50 );
- expect( left ).to.equal( expectedLeft );
- } );
- it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
- view.viewportTopOffset = 50;
- const position = view.panelPositions[ positionIndex ];
- const editableRect = {
- top: 100,
- bottom: 150,
- left: 100,
- right: 100,
- width: 100,
- height: 50
- };
- const panelRect = {
- width: 50,
- height: 80
- };
- const { top, left } = position( editableRect, panelRect );
- expect( top ).to.equal( 150 );
- expect( left ).to.equal( expectedLeft );
- } );
- } );
- }
- } );
- function setUpEditingView() {
- editingView = new EditingView();
- editingViewRoot = new ViewRootEditableElement( 'div' );
- editingViewRoot._document = editingView.document;
- editingView.document.roots.add( editingViewRoot );
- }
- } );
|