Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch t/ckeditor5-engine/1210
Browse files Browse the repository at this point in the history
Internal: Align code to the new view API, introduced in engine t/1210.
  • Loading branch information
Piotr Jasiun committed Feb 16, 2018
2 parents 352d056 + 6427e3b commit 34fad4d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/panel/balloon/contextualballoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default class ContextualBalloon extends Plugin {
*/
this.positionLimiter = () => {
const view = this.editor.editing.view;
const editableElement = view.selection.editableElement;
const viewDocument = view.document;
const editableElement = viewDocument.selection.editableElement;

if ( editableElement ) {
return view.domConverter.mapViewToDom( editableElement.root );
Expand Down
15 changes: 8 additions & 7 deletions src/toolbar/contextual/contextualtoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class ContextualToolbar extends Plugin {
*/
_handleSelectionChange() {
const selection = this.editor.model.document.selection;
const editingView = this.editor.editing.view;
const viewDocument = this.editor.editing.view.document;

this.listenTo( selection, 'change:range', ( evt, data ) => {
// When the selection is not changed by a collaboration and when is not collapsed.
Expand All @@ -144,7 +144,7 @@ export default class ContextualToolbar extends Plugin {
// Hide the toolbar when the selection stops changing.
this.listenTo( this, '_selectionChangeDebounced', () => {
// This implementation assumes that only non–collapsed selections gets the contextual toolbar.
if ( editingView.isFocused && !editingView.selection.isCollapsed ) {
if ( viewDocument.isFocused && !viewDocument.selection.isCollapsed ) {
this.show();
}
} );
Expand All @@ -167,7 +167,7 @@ export default class ContextualToolbar extends Plugin {
return;
}

// Update the toolbar position upon #render (e.g. external document changes)
// Update the toolbar position upon change (e.g. external document changes)
// while it's visible.
this.listenTo( this.editor.editing.view, 'render', () => {
this._balloon.updatePosition( this._getBalloonPositionData() );
Expand Down Expand Up @@ -200,19 +200,20 @@ export default class ContextualToolbar extends Plugin {
*/
_getBalloonPositionData() {
const editor = this.editor;
const editingView = editor.editing.view;
const view = editor.editing.view;
const viewDocument = view.document;

// Get direction of the selection.
const isBackward = editingView.selection.isBackward;
const isBackward = viewDocument.selection.isBackward;

return {
// Because the target for BalloonPanelView is a Rect (not DOMRange), it's geometry will stay fixed
// as the window scrolls. To let the BalloonPanelView follow such Rect, is must be continuously
// computed and hence, the target is defined as a function instead of a static value.
// https://github.com/ckeditor/ckeditor5-ui/issues/195
target: () => {
const range = editingView.selection.getFirstRange();
const rangeRects = Rect.getDomRangeRects( editingView.domConverter.viewRangeToDom( range ) );
const range = viewDocument.selection.getFirstRange();
const rangeRects = Rect.getDomRangeRects( view.domConverter.viewRangeToDom( range ) );

// Select the proper range rect depending on the direction of the selection.
if ( isBackward ) {
Expand Down
9 changes: 5 additions & 4 deletions tests/panel/balloon/contextualballoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,19 @@ describe( 'ContextualBalloon', () => {
} );

describe( 'positionLimiter', () => {
let model, viewDocument, root;
let model, view, viewDocument, root;

beforeEach( () => {
model = editor.model;
viewDocument = editor.editing.view;
view = editor.editing.view;
viewDocument = view.document;
root = viewDocument.getRoot();
} );

it( 'obtains the root of the selection', () => {
setModelData( model, '<paragraph>[]bar</paragraph>' );

expect( balloon.positionLimiter() ).to.equal( viewDocument.domConverter.mapViewToDom( root ) );
expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
} );

it( 'does not fail if selection has no #editableElement', () => {
Expand Down Expand Up @@ -114,7 +115,7 @@ describe( 'ContextualBalloon', () => {

setModelData( model, '<widget><nestedEditable>[]foo</nestedEditable></widget>' );

expect( balloon.positionLimiter() ).to.equal( viewDocument.domConverter.mapViewToDom( root ) );
expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
} );
} );

Expand Down
14 changes: 7 additions & 7 deletions tests/toolbar/contextual/contextualtoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe( 'ContextualToolbar', () => {
sandbox.stub( balloon.view, 'pin' ).returns( {} );

// Focus the engine.
editingView.isFocused = true;
editingView.document.isFocused = true;
editingView.getDomRoot().focus();

// Remove all selection ranges from DOM before testing.
Expand Down Expand Up @@ -170,7 +170,7 @@ describe( 'ContextualToolbar', () => {
] );

balloonAddSpy = sandbox.spy( balloon, 'add' );
editingView.isFocused = true;
editingView.document.isFocused = true;
} );

it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
Expand Down Expand Up @@ -242,7 +242,7 @@ describe( 'ContextualToolbar', () => {
expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
} );

it( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon', () => {
it( 'should update balloon position on view#change event while balloon is added to the #_balloon', () => {
setData( model, '<paragraph>b[a]r</paragraph>' );

const spy = sandbox.spy( balloon, 'updatePosition' );
Expand Down Expand Up @@ -296,7 +296,7 @@ describe( 'ContextualToolbar', () => {

it( 'should not be called when the editor is not focused', () => {
setData( model, '<paragraph>b[a]r</paragraph>' );
editingView.isFocused = false;
editingView.document.isFocused = false;

contextualToolbar.fire( '_selectionChangeDebounced' );
sinon.assert.notCalled( showSpy );
Expand All @@ -311,7 +311,7 @@ describe( 'ContextualToolbar', () => {

it( 'should be called when the selection is not collapsed and editor is focused', () => {
setData( model, '<paragraph>b[a]r</paragraph>' );
editingView.isFocused = true;
editingView.document.isFocused = true;

contextualToolbar.fire( '_selectionChangeDebounced' );
sinon.assert.calledOnce( showSpy );
Expand All @@ -324,7 +324,7 @@ describe( 'ContextualToolbar', () => {

beforeEach( () => {
removeBalloonSpy = sandbox.stub( balloon, 'remove' ).returns( {} );
editingView.isFocused = true;
editingView.document.isFocused = true;
} );

it( 'should remove #toolbarView from the #_balloon', () => {
Expand All @@ -336,7 +336,7 @@ describe( 'ContextualToolbar', () => {
sinon.assert.calledWithExactly( removeBalloonSpy, contextualToolbar.toolbarView );
} );

it( 'should stop update balloon position on ViewDocument#render event', () => {
it( 'should stop update balloon position on ViewDocument#change event', () => {
setData( model, '<paragraph>b[a]r</paragraph>' );

const spy = sandbox.spy( balloon, 'updatePosition' );
Expand Down

0 comments on commit 34fad4d

Please sign in to comment.