Parcourir la source

Introduced BlockToolbar plugin.

Oskar Wróbel il y a 7 ans
Parent
commit
e13356541e

+ 32 - 0
packages/ckeditor5-ui/src/toolbar/block/blocktoolbar.css

@@ -0,0 +1,32 @@
+/*
+ * What you're currently looking at is the source code of a legally protected, proprietary software.
+ * Letters is licensed under a commercial license and protected by copyright law. Where not otherwise indicated,
+ * all Letters content is authored by CKSource engineers and consists of CKSource-owned intellectual property.
+ *
+ * Copyright (c) 2003-2018, CKSource Frederico Knabben. All rights reserved.
+ */
+
+@import '../ui/styles/helpers/_shadow.css';
+
+:root {
+	--ltrs-color-blocktoolbar-icon: var(--ltrs-color-white);
+}
+
+.ck.ck-button.ltrs-toolbar-block-button {
+	position: absolute;
+	border: 0;
+	font-size: var(--ltrs-font-medium);
+	background-color: var(--ltrs-color-gray-blue-light);
+	color: var(--ltrs-color-blocktoolbar-icon);
+	z-index: var(--ltrs-zindex-air);
+	transition: 200ms background-color ease-in-out;
+
+	& .ck-icon {
+		font-size: var(--ltrs-font-size-normal);
+	}
+
+	&.ck-on {
+		background-color: var(--ltrs-color-gray-blue-hover);
+		@mixin ltrs__shadow--inset;
+	}
+}

+ 326 - 0
packages/ckeditor5-ui/src/toolbar/block/blocktoolbar.js

@@ -0,0 +1,326 @@
+/**
+ * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+/* global window */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+
+import BlockButtonView from './view/blockbuttonview';
+import BalloonPanelView from '../../panel/balloon/balloonpanelview';
+import ToolbarView from '../toolbarview';
+
+import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
+import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
+import clickOutsideHandler from '../../bindings/clickoutsidehandler';
+
+import { getOptimalPosition } from '@ckeditor/ckeditor5-utils/src/dom/position';
+import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
+
+import iconPilcrow from '../../../theme/icons/pilcrow.svg';
+
+/**
+ * The block toolbar plugin.
+ *
+ * @extends module:core/plugin~Plugin
+ */
+export default class BlockToolbar extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'BlockToolbar';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+
+		editor.editing.view.addObserver( ClickObserver );
+
+		/**
+		 * Toolbar view.
+		 *
+		 * @type {ToolbarView}
+		 */
+		this.toolbarView = new ToolbarView( editor.locale );
+
+		/**
+		 * Panel view.
+		 *
+		 * @type {BalloonPanelView}
+		 */
+		this.panelView = this._createPanelView();
+
+		/**
+		 * Button view.
+		 *
+		 * @type {ButtonView}
+		 */
+		this.buttonView = this._createButtonView();
+
+		/**
+		 * List of block element names that allow do display toolbar next to it.
+		 * This list will be updated by #afterInit method.
+		 *
+		 * @type {Array<String>}
+		 */
+		this._allowedElements = [];
+
+		// Close #panelView on click out of the plugin UI.
+		clickOutsideHandler( {
+			emitter: this.panelView,
+			contextElements: [ this.panelView.element, this.buttonView.element ],
+			activator: () => this.panelView.isVisible,
+			callback: () => this._hidePanel()
+		} );
+
+		// Hide plugin UI when editor switch to read-only.
+		this.listenTo( editor, 'change:isReadOnly', ( evt, name, isReadOnly ) => {
+			if ( isReadOnly ) {
+				this._disable();
+			} else {
+				this._enable();
+			}
+		} );
+
+		// Enable as default.
+		this._enable();
+	}
+
+	/**
+	 * Creates toolbar components based on given configuration.
+	 * This needs to be done when all plugins are ready.
+	 *
+	 * @inheritDoc
+	 */
+	afterInit() {
+		const factory = this.editor.ui.componentFactory;
+		const config = this.editor.config.get( 'blockToolbar' );
+
+		this.toolbarView.fillFromConfig( config, factory );
+
+		// Hide panel before executing each button in the panel.
+		for ( const item of this.toolbarView.items ) {
+			item.on( 'execute', () => this._hidePanel( true ), { priority: 'high' } );
+		}
+
+		this._allowedElements = this._getAllowedElements();
+	}
+
+	/**
+	 * Creates panel view.
+	 *
+	 * @private
+	 * @returns {BalloonPanelView}
+	 */
+	_createPanelView() {
+		const editor = this.editor;
+		const panelView = new BalloonPanelView( editor.locale );
+
+		panelView.content.add( this.toolbarView );
+		editor.ui.view.body.add( panelView );
+		editor.ui.focusTracker.add( panelView.element );
+
+		// Close #panelView on `Esc` press.
+		this.toolbarView.keystrokes.set( 'Esc', ( evt, cancel ) => {
+			this._hidePanel( true );
+			cancel();
+		} );
+
+		return panelView;
+	}
+
+	/**
+	 * Creates button view.
+	 *
+	 * @private
+	 * @returns {BlockButtonView}
+	 */
+	_createButtonView() {
+		const editor = this.editor;
+		const buttonView = new BlockButtonView( editor.locale );
+
+		buttonView.label = editor.t( 'Edit block' );
+		buttonView.icon = iconPilcrow;
+		buttonView.withText = false;
+
+		// Bind panelView to buttonView.
+		buttonView.bind( 'isOn' ).to( this.panelView, 'isVisible' );
+		buttonView.bind( 'tooltip' ).to( this.panelView, 'isVisible', isVisible => !isVisible );
+
+		// Toggle panelView on buttonView#execute.
+		this.listenTo( buttonView, 'execute', () => {
+			if ( !this.panelView.isVisible ) {
+				this._showPanel();
+			} else {
+				this._hidePanel( true );
+			}
+		} );
+
+		editor.ui.view.body.add( buttonView );
+		editor.ui.focusTracker.add( buttonView.element );
+
+		return buttonView;
+	}
+
+	/**
+	 * Returns list of element names that allow to display block button next to it.
+	 *
+	 * @private
+	 */
+	_getAllowedElements() {
+		const elements = [ 'p', 'li' ];
+
+		for ( const item of this.editor.config.get( 'heading.options' ) || [] ) {
+			if ( item.view ) {
+				elements.push( item.view );
+			}
+		}
+
+		return elements;
+	}
+
+	/**
+	 * Starts displaying button next to allowed elements.
+	 *
+	 * @private
+	 */
+	_enable() {
+		const editor = this.editor;
+		const view = editor.editing.view;
+		const viewDocument = view.document;
+		let targetElement, targetDomElement;
+
+		// Hides panel on a direct selection change.
+		this.listenTo( editor.model.document.selection, 'change:range', ( evt, data ) => {
+			if ( data.directChange ) {
+				this._hidePanel();
+			}
+		} );
+
+		this.listenTo( view, 'render', () => {
+			// Get selection parent container, block button will be attached to this element.
+			targetElement = getParentContainer( viewDocument.selection.getFirstPosition() );
+
+			const targetName = targetElement.name;
+
+			// Do not attach block button when target element is not on the white list.
+			if ( !this._allowedElements.includes( targetName ) ) {
+				this.buttonView.isVisible = false;
+
+				return;
+			}
+
+			// Get target DOM node.
+			targetDomElement = view.domConverter.mapViewToDom( targetElement );
+
+			// Show block button.
+			this.buttonView.isVisible = true;
+
+			// Attach block button to target DOM element.
+			this._attachButtonToElement( targetDomElement );
+
+			// When panel is opened then refresh it position to be properly aligned with block button.
+			if ( this.panelView.isVisible ) {
+				this._showPanel();
+			}
+		}, { priority: 'low' } );
+
+		// Keep button and panel position on window#resize.
+		this.listenTo( this.buttonView, 'change:isVisible', ( evt, name, isVisible ) => {
+			if ( isVisible ) {
+				this.buttonView.listenTo( window, 'resize', () => this._attachButtonToElement( targetDomElement ) );
+			} else {
+				this.buttonView.stopListening( window, 'resize' );
+
+				// Hide the panel when the button disappears.
+				this._hidePanel();
+			}
+		} );
+	}
+
+	/**
+	 * Stops displaying block button.
+	 *
+	 * @private
+	 */
+	_disable() {
+		this.stopListening( this.editor.model.document.selection, 'change:range' );
+		this.stopListening( this.editor.editing.view, 'render' );
+		this.stopListening( this.buttonView, 'change:isVisible' );
+		this.buttonView.isVisible = false;
+		this._hidePanel();
+	}
+
+	/**
+	 * Attaches #buttonView to the target block of content.
+	 *
+	 * @protected
+	 * @param {HTMLElement} targetElement Target element.
+	 */
+	_attachButtonToElement( targetElement ) {
+		const contentComputedStyles = window.getComputedStyle( targetElement );
+
+		const editableRect = new Rect( this.editor.ui.view.editableElement );
+		const contentPaddingTop = parseInt( contentComputedStyles.paddingTop );
+		const contentLineHeight = parseInt( contentComputedStyles.lineHeight );
+
+		const position = getOptimalPosition( {
+			element: this.buttonView.element,
+			target: targetElement,
+			positions: [
+				( contentRect, buttonRect ) => {
+					return {
+						top: contentRect.top + contentPaddingTop + ( ( contentLineHeight - buttonRect.height ) / 2 ),
+						left: editableRect.left
+					};
+				}
+			]
+		} );
+
+		this.buttonView.top = position.top;
+		this.buttonView.left = position.left;
+	}
+
+	/**
+	 * Shows toolbar attached to the block button.
+	 * When toolbar is already opened then just repositions it.
+	 *
+	 * @private
+	 */
+	_showPanel() {
+		this.panelView.pin( {
+			target: this.buttonView.element,
+			limiter: this.editor.ui.view.element
+		} );
+	}
+
+	/**
+	 * Hides toolbar.
+	 *
+	 * @private
+	 * @param {Boolean} [focusEditable=false] When `true` then editable will be focused after hiding panel.
+	 */
+	_hidePanel( focusEditable ) {
+		this.panelView.isVisible = false;
+
+		if ( focusEditable ) {
+			this.editor.editing.view.focus();
+		}
+	}
+}
+
+// Because the engine.view.writer.getParentContainer is not exported here is a copy.
+// See: https://github.com/ckeditor/ckeditor5-engine/issues/628
+function getParentContainer( position ) {
+	let parent = position.parent;
+
+	while ( !( parent instanceof ContainerElement ) ) {
+		parent = parent.parent;
+	}
+
+	return parent;
+}

+ 1 - 0
packages/ckeditor5-ui/src/toolbar/block/icons/pilcrow.svg

@@ -0,0 +1 @@
+<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg"><path d="M15.442 3H8.481a3.48 3.48 0 0 0 0 6.961v6.962h1.74V4.74h1.74v12.183h1.74V4.74h1.741V3z" fill="#000"/></svg>

+ 48 - 0
packages/ckeditor5-ui/src/toolbar/block/view/blockbuttonview.js

@@ -0,0 +1,48 @@
+/**
+ * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+import ButtonView from '../../../button/buttonview';
+import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
+
+const toPx = toUnit( 'px' );
+
+/**
+ * The block button view class.
+ *
+ * @extends {module:ui/button/buttonview~ButtonView}
+ */
+export default class BlockButtonView extends ButtonView {
+	/**
+	 * @inheritDoc
+	 */
+	constructor( locale ) {
+		super( locale );
+
+		const bind = this.bindTemplate;
+
+		/**
+		 * Top offset.
+		 *
+		 * @member {Number} #top
+		 */
+		this.set( 'top', 0 );
+
+		/**
+		 * Left offset.
+		 *
+		 * @member {Number} #left
+		 */
+		this.set( 'left', 0 );
+
+		this.extendTemplate( {
+			attributes: {
+				class: 'ltrs-toolbar-block-button',
+				style: {
+					top: bind.to( 'top', val => toPx( val ) ),
+					left: bind.to( 'left', val => toPx( val ) ),
+				}
+			}
+		} );
+	}
+}

+ 375 - 0
packages/ckeditor5-ui/tests/toolbar/block/blocktoolbar.js

@@ -0,0 +1,375 @@
+/**
+ * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+/* global document, window, Event */
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
+
+import BlockToolbar from '../../../src/toolbar/block/blocktoolbar';
+import ToolbarView from '../../../src/toolbar/toolbarview';
+import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
+import BlockButtonView from './../../../src/toolbar/block/view/blockbuttonview';
+
+import Heading from '@ckeditor/ckeditor5-heading/src/heading';
+import HeadingButtonsUI from '@ckeditor/ckeditor5-heading/src/headingbuttonsui';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbuttonui';
+import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
+import List from '@ckeditor/ckeditor5-list/src/list';
+
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+
+describe( 'BlockToolbar', () => {
+	let editor, element, blockToolbar;
+
+	beforeEach( () => {
+		element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		return ClassicTestEditor.create( element, {
+			plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote, List ],
+			blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
+		} ).then( newEditor => {
+			editor = newEditor;
+			blockToolbar = editor.plugins.get( BlockToolbar );
+		} );
+	} );
+
+	afterEach( () => {
+		element.remove();
+		return editor.destroy();
+	} );
+
+	it( 'should have pluginName property', () => {
+		expect( BlockToolbar.pluginName ).to.equal( 'BlockToolbar' );
+	} );
+
+	it( 'should register click observer', () => {
+		expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
+	} );
+
+	it( 'should initialize properly without Heading plugin', () => {
+		const element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		return ClassicTestEditor.create( element, {
+			plugins: [ BlockToolbar, Paragraph, ParagraphButtonUI, BlockQuote, List ],
+			blockToolbar: [ 'paragraph', 'blockQuote' ]
+		} ).then( editor => {
+			element.remove();
+			return editor.destroy();
+		} );
+	} );
+
+	describe( 'child views', () => {
+		describe( 'panelView', () => {
+			it( 'should create view instance', () => {
+				expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
+			} );
+
+			it( 'should be added to ui.view.body collection', () => {
+				expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
+			} );
+
+			it( 'should add panelView to ui.focusTracker', () => {
+				expect( editor.ui.focusTracker.isFocused ).to.false;
+
+				blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
+
+				expect( editor.ui.focusTracker.isFocused ).to.true;
+			} );
+
+			it( 'should close panelView after `Esc` press and focus view document', () => {
+				const spy = sinon.spy( editor.editing.view, 'focus' );
+
+				blockToolbar.panelView.isVisible = true;
+
+				blockToolbar.toolbarView.keystrokes.press( {
+					keyCode: keyCodes.esc,
+					preventDefault: () => {},
+					stopPropagation: () => {}
+				} );
+
+				expect( blockToolbar.panelView.isVisible ).to.false;
+				sinon.assert.calledOnce( spy );
+			} );
+
+			it( 'should close panelView on click outside the panel and not focus view document', () => {
+				const spy = sinon.spy();
+
+				editor.editing.view.on( 'focus', spy );
+				blockToolbar.panelView.isVisible = true;
+				document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
+
+				expect( blockToolbar.panelView.isVisible ).to.false;
+				sinon.assert.notCalled( spy );
+			} );
+
+			it( 'should not close panelView on click on panel element', () => {
+				blockToolbar.panelView.isVisible = true;
+				blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
+
+				expect( blockToolbar.panelView.isVisible ).to.true;
+			} );
+		} );
+
+		describe( 'toolbarView', () => {
+			it( 'should create view instance', () => {
+				expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
+			} );
+
+			it( 'should be added to panelView#content collection', () => {
+				expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
+			} );
+
+			it( 'should initialize toolbar items based on Editor#blockToolbar config', () => {
+				expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
+			} );
+
+			it( 'should hide panel after clicking on the button from toolbar', () => {
+				blockToolbar.buttonView.fire( 'execute' );
+
+				expect( blockToolbar.panelView.isVisible ).to.true;
+
+				blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
+
+				expect( blockToolbar.panelView.isVisible ).to.false;
+			} );
+		} );
+
+		describe( 'buttonView', () => {
+			it( 'should create view instance', () => {
+				expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
+			} );
+
+			it( 'should be added to editor ui.view.body collection', () => {
+				expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
+			} );
+
+			it( 'should add buttonView to ui.focusTracker', () => {
+				expect( editor.ui.focusTracker.isFocused ).to.false;
+
+				blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
+
+				expect( editor.ui.focusTracker.isFocused ).to.true;
+			} );
+
+			it( 'should pin panelView to the button on #execute event', () => {
+				expect( blockToolbar.panelView.isVisible ).to.false;
+
+				const spy = sinon.spy( blockToolbar.panelView, 'pin' );
+
+				blockToolbar.buttonView.fire( 'execute' );
+
+				expect( blockToolbar.panelView.isVisible ).to.true;
+				sinon.assert.calledWith( spy, {
+					target: blockToolbar.buttonView.element,
+					limiter: editor.ui.view.element
+				} );
+			} );
+
+			it( 'should hide panelView and focus editable on #execute event when panel was visible', () => {
+				blockToolbar.panelView.isVisible = true;
+				const spy = sinon.spy( editor.editing.view, 'focus' );
+
+				blockToolbar.buttonView.fire( 'execute' );
+
+				expect( blockToolbar.panelView.isVisible ).to.false;
+				sinon.assert.calledOnce( spy );
+			} );
+
+			it( 'should bind #isOn to panelView#isVisible', () => {
+				blockToolbar.panelView.isVisible = false;
+
+				expect( blockToolbar.buttonView.isOn ).to.false;
+
+				blockToolbar.panelView.isVisible = true;
+
+				expect( blockToolbar.buttonView.isOn ).to.true;
+			} );
+
+			it( 'should hide button tooltip when panelView is opened', () => {
+				blockToolbar.panelView.isVisible = false;
+
+				expect( blockToolbar.buttonView.tooltip ).to.true;
+
+				blockToolbar.panelView.isVisible = true;
+
+				expect( blockToolbar.buttonView.tooltip ).to.false;
+			} );
+		} );
+	} );
+
+	describe( 'allowed elements', () => {
+		it( 'should display button when selection is placed in a paragraph', () => {
+			setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should display button when selection is placed in a heading1', () => {
+			setData( editor.model, '<heading1>foo[]bar</heading1>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should display button when selection is placed in a heading2', () => {
+			setData( editor.model, '<heading2>foo[]bar</heading2>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should display button when selection is placed in a heading3', () => {
+			setData( editor.model, '<heading3>foo[]bar</heading3>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should display button when selection is placed in a list item', () => {
+			setData( editor.model, '<listItem type="numbered" indent="0">foo[]bar</listItem>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should display button when selection is placed in a allowed element in a blockQuote', () => {
+			setData( editor.model, '<blockQuote><paragraph>foo[]bar</paragraph></blockQuote>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+		} );
+
+		it( 'should not display button when selection is placed in not allowed element', () => {
+			editor.model.schema.register( 'table', { inheritAllFrom: '$block' } );
+			editor.conversion.elementToElement( { model: 'table', view: 'table' } );
+
+			setData( editor.model, '<table>foo[]bar</table>' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.false;
+		} );
+	} );
+
+	describe( 'attaching button to the content', () => {
+		it( 'should attach button to the left side of selected content and center with the first line on view#render', () => {
+			setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
+
+			const target = editor.ui.view.editableElement.querySelector( 'p' );
+
+			target.style.lineHeight = '20px';
+			target.style.paddingTop = '10px';
+
+			const editableRectSpy = sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
+				left: 100
+			} );
+
+			const targetRectSpy = sinon.stub( target, 'getBoundingClientRect' ).returns( {
+				top: 500,
+				left: 300
+			} );
+
+			const buttonRectSpy = sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
+				width: 100,
+				height: 100
+			} );
+
+			editor.editing.view.fire( 'render' );
+
+			expect( blockToolbar.buttonView.top ).to.equal( 470 );
+			expect( blockToolbar.buttonView.left ).to.equal( 100 );
+
+			editableRectSpy.restore();
+			targetRectSpy.restore();
+			buttonRectSpy.restore();
+		} );
+
+		it( 'should reposition panelView when is opened on view#render', () => {
+			blockToolbar.panelView.isVisible = false;
+
+			const spy = sinon.spy( blockToolbar.panelView, 'pin' );
+
+			editor.editing.view.fire( 'render' );
+
+			sinon.assert.notCalled( spy );
+
+			blockToolbar.panelView.isVisible = true;
+
+			editor.editing.view.fire( 'render' );
+
+			sinon.assert.calledWith( spy, {
+				target: blockToolbar.buttonView.element,
+				limiter: editor.ui.view.element
+			} );
+		} );
+
+		it( 'should hide opened panel on a selection direct change', () => {
+			blockToolbar.panelView.isVisible = true;
+
+			editor.model.document.selection.fire( 'change:range', { directChange: true } );
+
+			expect( blockToolbar.panelView.isVisible ).to.false;
+		} );
+
+		it( 'should not hide opened panel on a selection not direct change', () => {
+			blockToolbar.panelView.isVisible = true;
+
+			editor.model.document.selection.fire( 'change:range', { directChange: false } );
+
+			expect( blockToolbar.panelView.isVisible ).to.true;
+		} );
+
+		it( 'should hide button and stop attaching it when editor switch to readonly', () => {
+			setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
+
+			blockToolbar.panelView.isVisible = true;
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+			expect( blockToolbar.panelView.isVisible ).to.true;
+
+			editor.isReadOnly = true;
+
+			expect( blockToolbar.buttonView.isVisible ).to.false;
+			expect( blockToolbar.panelView.isVisible ).to.false;
+
+			editor.editing.view.fire( 'render' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.false;
+			expect( blockToolbar.panelView.isVisible ).to.false;
+
+			editor.isReadOnly = false;
+			editor.editing.view.fire( 'render' );
+
+			expect( blockToolbar.buttonView.isVisible ).to.true;
+			expect( blockToolbar.panelView.isVisible ).to.false;
+		} );
+
+		it( 'should update button position on browser resize only when button is visible', () => {
+			editor.model.schema.register( 'table', { inheritAllFrom: '$block' } );
+			editor.conversion.elementToElement( { model: 'table', view: 'table' } );
+
+			const spy = sinon.spy( blockToolbar, '_attachButtonToElement' );
+
+			setData( editor.model, '<table>fo[]o</table><paragraph>bar</paragraph>' );
+
+			window.dispatchEvent( new Event( 'resize' ) );
+
+			sinon.assert.notCalled( spy );
+
+			setData( editor.model, '<table>foo</table><paragraph>ba[]r</paragraph>' );
+
+			spy.reset();
+
+			window.dispatchEvent( new Event( 'resize' ) );
+
+			sinon.assert.called( spy );
+
+			setData( editor.model, '<table>fo[]o</table><paragraph>bar</paragraph>' );
+
+			spy.reset();
+
+			window.dispatchEvent( new Event( 'resize' ) );
+
+			sinon.assert.notCalled( spy );
+		} );
+	} );
+} );

+ 41 - 0
packages/ckeditor5-ui/tests/toolbar/block/view/blockbuttonview.js

@@ -0,0 +1,41 @@
+/**
+ * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+import BlockButtonView from '../../../../src/toolbar/block/view/blockbuttonview';
+
+describe( 'BlockButtonView', () => {
+	let view;
+
+	beforeEach( () => {
+		view = new BlockButtonView();
+
+		view.render();
+	} );
+
+	it( 'should create element from template', () => {
+		expect( view.element.classList.contains( 'ltrs-toolbar-block-button' ) ).to.true;
+	} );
+
+	describe( 'DOM binding', () => {
+		it( 'should react on `view#top` change', () => {
+			view.top = 0;
+
+			expect( view.element.style.top ).to.equal( '0px' );
+
+			view.top = 10;
+
+			expect( view.element.style.top ).to.equal( '10px' );
+		} );
+
+		it( 'should react on `view#left` change', () => {
+			view.left = 0;
+
+			expect( view.element.style.left ).to.equal( '0px' );
+
+			view.left = 10;
+
+			expect( view.element.style.left ).to.equal( '10px' );
+		} );
+	} );
+} );

+ 1 - 0
packages/ckeditor5-ui/theme/icons/pilcrow.svg

@@ -0,0 +1 @@
+<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg"><path d="M15.442 3H8.481a3.48 3.48 0 0 0 0 6.961v6.962h1.74V4.74h1.74v12.183h1.74V4.74h1.741V3z" fill="#000"/></svg>