| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- /**
- * Copyright (c) 2016, CKSource - Frederico Knabben. All rights reserved.
- */
- import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
- import ContextualToolbar from '../../../src/toolbar/contextual/contextualtoolbar';
- import ContextualBalloon from '../../../src/panel/balloon/contextualballoon';
- import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
- import ToolbarView from '../../../src/toolbar/toolbarview';
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
- import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model.js';
- /* global document, setTimeout */
- describe( 'ContextualToolbar', () => {
- let sandbox, editor, contextualToolbar, balloon, editorElement;
- beforeEach( () => {
- sandbox = sinon.sandbox.create();
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
- return ClassicTestEditor.create( editorElement, {
- plugins: [ Paragraph, Bold, Italic, ContextualToolbar ],
- contextualToolbar: [ 'bold', 'italic' ]
- } )
- .then( newEditor => {
- newEditor.editing.view.attachDomRoot( editorElement );
- editor = newEditor;
- contextualToolbar = editor.plugins.get( ContextualToolbar );
- balloon = editor.plugins.get( ContextualBalloon );
- // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
- sandbox.stub( balloon.view, 'attachTo', () => {} );
- sandbox.stub( balloon.view, 'pin', () => {} );
- // Focus the engine.
- editor.editing.view.isFocused = true;
- return contextualToolbar.toolbarView.init();
- } );
- } );
- afterEach( () => {
- sandbox.restore();
- editorElement.remove();
- return editor.destroy();
- } );
- it( 'should create a plugin instance', () => {
- expect( contextualToolbar ).to.instanceOf( Plugin );
- expect( contextualToolbar ).to.instanceOf( ContextualToolbar );
- expect( contextualToolbar.toolbarView ).to.instanceof( ToolbarView );
- expect( contextualToolbar.toolbarView.element.classList.contains( 'ck-editor-toolbar' ) ).to.be.true;
- expect( contextualToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
- } );
- it( 'should load ContextualBalloon', () => {
- expect( balloon ).to.instanceof( ContextualBalloon );
- } );
- it( 'should create components from config', () => {
- expect( contextualToolbar.toolbarView.items ).to.length( 2 );
- } );
- it( 'should fire internal `_selectionChangeDebounced` event 200 ms after last selection change', done => {
- // This test uses setTimeout to test lodash#debounce because sinon fake timers
- // doesn't work with lodash. Lodash keeps time related stuff in a closure
- // and sinon is not able to override it.
- const spy = sandbox.spy();
- setData( editor.document, '<paragraph>[bar]</paragraph>' );
- contextualToolbar.on( '_selectionChangeDebounced', spy );
- editor.document.selection.fire( 'change:range', {} );
- // Not yet.
- sinon.assert.notCalled( spy );
- // Lets wait 100 ms.
- setTimeout( () => {
- // Still not yet.
- sinon.assert.notCalled( spy );
- // Fire event one more time.
- editor.document.selection.fire( 'change:range', {} );
- // Another 100 ms waiting.
- setTimeout( () => {
- // Still not yet.
- sinon.assert.notCalled( spy );
- // Another 101 ms waiting.
- setTimeout( () => {
- // And here it is.
- sinon.assert.calledOnce( spy );
- done();
- }, 100 );
- }, 101 );
- }, 100 );
- } );
- describe( 'pluginName', () => {
- it( 'should return plugin by its name', () => {
- expect( editor.plugins.get( 'ui/contextualtoolbar' ) ).to.equal( contextualToolbar );
- } );
- } );
- describe( '_showPanel()', () => {
- let balloonAddSpy, forwardSelectionRect, backwardSelectionRect;
- beforeEach( () => {
- forwardSelectionRect = {
- top: 100,
- height: 10,
- bottom: 110,
- left: 200,
- width: 50,
- right: 250
- };
- backwardSelectionRect = {
- top: 100,
- height: 10,
- bottom: 110,
- left: 200,
- width: 50,
- right: 250
- };
- stubSelectionRect( forwardSelectionRect, backwardSelectionRect );
- balloonAddSpy = sandbox.spy( balloon, 'add' );
- editor.editing.view.isFocused = true;
- } );
- it( 'should return a promise', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- const returned = contextualToolbar._showPanel();
- expect( returned ).to.instanceof( Promise );
- return returned;
- } );
- it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- const defaultPositions = BalloonPanelView.defaultPositions;
- return contextualToolbar._showPanel().then( () => {
- sinon.assert.calledWithExactly( balloonAddSpy, {
- view: contextualToolbar.toolbarView,
- balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
- position: {
- target: sinon.match( value => value() == backwardSelectionRect ),
- limiter: editor.ui.view.editable.element,
- positions: [
- defaultPositions.southEastArrowNorth,
- defaultPositions.southEastArrowNorthEast,
- defaultPositions.southEastArrowNorthWest,
- defaultPositions.northEastArrowSouth,
- defaultPositions.northEastArrowSouthEast,
- defaultPositions.northEastArrowSouthWest
- ]
- }
- } );
- } );
- } );
- it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
- const defaultPositions = BalloonPanelView.defaultPositions;
- return contextualToolbar._showPanel()
- .then( () => {
- sinon.assert.calledWithExactly( balloonAddSpy, {
- view: contextualToolbar.toolbarView,
- balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
- position: {
- target: sinon.match( value => value() == forwardSelectionRect ),
- limiter: editor.ui.view.editable.element,
- positions: [
- defaultPositions.northWestArrowSouth,
- defaultPositions.northWestArrowSouthWest,
- defaultPositions.northWestArrowSouthEast,
- defaultPositions.southWestArrowNorth,
- defaultPositions.southWestArrowNorthWest,
- defaultPositions.southWestArrowNorthEast
- ]
- }
- } );
- } );
- } );
- it( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- const spy = sandbox.spy( balloon, 'updatePosition' );
- editor.editing.view.fire( 'render' );
- return contextualToolbar._showPanel()
- .then( () => {
- sinon.assert.notCalled( spy );
- editor.editing.view.fire( 'render' );
- sinon.assert.calledOnce( spy );
- } );
- } );
- it( 'should not add #toolbarView to the #_balloon more than once', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- return contextualToolbar._showPanel()
- .then( () => contextualToolbar._showPanel() )
- .then( () => {
- sinon.assert.calledOnce( balloonAddSpy );
- } );
- } );
- it( 'should not add #toolbarView to the #_balloon when editor is not focused', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- editor.editing.view.isFocused = false;
- return contextualToolbar._showPanel()
- .then( () => {
- sinon.assert.notCalled( balloonAddSpy );
- } );
- } );
- it( 'should not add #toolbarView to the #_balloon when selection is collapsed', () => {
- setData( editor.document, '<paragraph>b[]ar</paragraph>' );
- return contextualToolbar._showPanel()
- .then( () => {
- sinon.assert.notCalled( balloonAddSpy );
- } );
- } );
- } );
- describe( '_hidePanel()', () => {
- let removeBalloonSpy;
- beforeEach( () => {
- removeBalloonSpy = sandbox.stub( balloon, 'remove', () => {} );
- editor.editing.view.isFocused = true;
- } );
- it( 'should remove #toolbarView from the #_balloon', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- return contextualToolbar._showPanel()
- .then( () => {
- contextualToolbar._hidePanel();
- sinon.assert.calledWithExactly( removeBalloonSpy, contextualToolbar.toolbarView );
- } );
- } );
- it( 'should stop update balloon position on ViewDocument#render event', () => {
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- const spy = sandbox.spy( balloon, 'updatePosition' );
- return contextualToolbar._showPanel()
- .then( () => {
- contextualToolbar._hidePanel();
- editor.editing.view.fire( 'render' );
- sinon.assert.notCalled( spy );
- } );
- } );
- it( 'should not remove #ttolbarView when is not added to the #_balloon', () => {
- contextualToolbar._hidePanel();
- sinon.assert.notCalled( removeBalloonSpy );
- } );
- } );
- describe( 'destroy()', () => {
- it( 'can be called multiple times', () => {
- expect( () => {
- contextualToolbar.destroy();
- contextualToolbar.destroy();
- } ).to.not.throw();
- } );
- it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
- const spy = sandbox.spy();
- contextualToolbar.on( '_selectionChangeDebounced', spy );
- editor.document.selection.fire( 'change:range', { directChange: true } );
- contextualToolbar.destroy();
- setTimeout( () => {
- sinon.assert.notCalled( spy );
- done();
- }, 200 );
- } );
- } );
- describe( 'showing and hiding', () => {
- let showPanelSpy, hidePanelSpy;
- beforeEach( () => {
- setData( editor.document, '<paragraph>[bar]</paragraph>' );
- // Methods are stubbed because return internal promise which can't be returned in test.
- showPanelSpy = sandbox.stub( contextualToolbar, '_showPanel', () => {} );
- hidePanelSpy = sandbox.stub( contextualToolbar, '_hidePanel', () => {} );
- } );
- it( 'should open when selection stops changing', () => {
- sinon.assert.notCalled( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- contextualToolbar.fire( '_selectionChangeDebounced' );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- } );
- it( 'should close when selection starts changing by a directChange', () => {
- contextualToolbar.fire( '_selectionChangeDebounced' );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- editor.document.selection.fire( 'change:range', { directChange: true } );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.calledOnce( hidePanelSpy );
- } );
- it( 'should not close when selection starts changing by not a directChange', () => {
- contextualToolbar.fire( '_selectionChangeDebounced' );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- editor.document.selection.fire( 'change:range', { directChange: false } );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- } );
- it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
- contextualToolbar.fire( '_selectionChangeDebounced' );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- // Collapse range silently (without firing `change:range` { directChange: true } event).
- const range = editor.document.selection._ranges[ 0 ];
- range.end = range.start;
- editor.document.selection.fire( 'change:range', { directChange: false } );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.calledOnce( hidePanelSpy );
- } );
- it( 'should hide if the editor loses focus', () => {
- editor.ui.focusTracker.isFocused = true;
- contextualToolbar.fire( '_selectionChangeDebounced' );
- // Stubbing getters doesn't wor for sandbox.
- const stub = sinon.stub( balloon, 'visibleView', { get: () => contextualToolbar.toolbarView } );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- editor.ui.focusTracker.isFocused = false;
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.calledOnce( hidePanelSpy );
- stub.restore();
- } );
- it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
- editor.ui.focusTracker.isFocused = true;
- contextualToolbar.fire( '_selectionChangeDebounced' );
- // Stubbing getters doesn't wor for sandbox.
- const stub = sinon.stub( balloon, 'visibleView', { get: () => null } );
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- editor.ui.focusTracker.isFocused = false;
- sinon.assert.calledOnce( showPanelSpy );
- sinon.assert.notCalled( hidePanelSpy );
- stub.restore();
- } );
- } );
- describe( 'beforeShow event', () => {
- it( 'should fire `beforeShow` event just before panel shows', () => {
- const spy = sinon.spy();
- contextualToolbar.on( 'beforeShow', spy );
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- const promise = contextualToolbar._showPanel();
- sinon.assert.calledOnce( spy );
- return promise;
- } );
- it( 'should not show the panel when `beforeShow` event is stopped', () => {
- const balloonAddSpy = sandbox.spy( balloon, 'add' );
- setData( editor.document, '<paragraph>b[a]r</paragraph>' );
- contextualToolbar.on( 'beforeShow', ( evt, stop ) => {
- stop();
- } );
- return contextualToolbar._showPanel().then( () => {
- sinon.assert.notCalled( balloonAddSpy );
- } );
- } );
- } );
- function stubSelectionRect( forwardSelectionRect, backwardSelectionRect ) {
- const editingView = editor.editing.view;
- const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
- // Mock selection rect.
- sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
- const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
- sandbox.stub( domRange, 'getClientRects', () => {
- return {
- length: 2,
- item( id ) {
- return id === 0 ? forwardSelectionRect : backwardSelectionRect;
- }
- };
- } );
- return domRange;
- } );
- }
- } );
|