Bläddra i källkod

Merge pull request #34 from ckeditor/t/33

Feature: Introduced ParagraphButtonUI plugin. Closes #33.
Piotr Jasiun 7 år sedan
förälder
incheckning
430e263ca0

+ 1 - 0
packages/ckeditor5-paragraph/package.json

@@ -9,6 +9,7 @@
   "dependencies": {
     "@ckeditor/ckeditor5-core": "^1.0.0-beta.2",
     "@ckeditor/ckeditor5-engine": "^1.0.0-beta.2",
+    "@ckeditor/ckeditor5-ui": "^1.0.0-beta.2",
     "@ckeditor/ckeditor5-utils": "^1.0.0-beta.2"
   },
   "devDependencies": {

+ 52 - 0
packages/ckeditor5-paragraph/src/paragraphbuttonui.js

@@ -0,0 +1,52 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module paragraph/paragraphbuttonui
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
+import icon from '../theme/icons/paragraph.svg';
+
+/**
+ * Class creates UI component representing paragraph button. It can be used together with
+ * {@link module:heading/headingbuttonsui~HeadingButtonsUI} to replace heading dropdown used in toolbars.
+ *
+ * This plugin is not loaded automatically with {@link module:paragraph/paragraph~Paragraph paragraph plugin}. It must
+ * be added manually in order to use `paragraph` component.
+ *
+ *		ClassicEditor
+ *			.create( {
+ *				plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
+ * 				toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
+ *			} )
+ *			.then( ... )
+ *			.catch( ... );
+ *
+ */
+export default class ParagraphButtonUI extends Plugin {
+	init() {
+		const editor = this.editor;
+		const t = editor.t;
+
+		editor.ui.componentFactory.add( 'paragraph', locale => {
+			const view = new ButtonView( locale );
+			const command = editor.commands.get( 'paragraph' );
+
+			view.label = t( 'Paragraph' );
+			view.icon = icon;
+			view.tooltip = true;
+			view.bind( 'isEnabled' ).to( command );
+			view.bind( 'isOn' ).to( command, 'value' );
+
+			view.on( 'execute', () => {
+				editor.execute( 'paragraph' );
+			} );
+
+			return view;
+		} );
+	}
+}

+ 84 - 0
packages/ckeditor5-paragraph/tests/paragraphbuttonui.js

@@ -0,0 +1,84 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals document */
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import Paragraph from '../src/paragraph';
+import Heading from '@ckeditor/ckeditor5-heading/src/heading';
+import ParagraphButtonUI from '../src/paragraphbuttonui';
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
+import icon from '../theme/icons/paragraph.svg';
+
+describe( 'HeadingButtonUI', () => {
+	let editorElement, editor;
+
+	describe( 'default config', () => {
+		beforeEach( () => {
+			editorElement = document.createElement( 'div' );
+			document.body.appendChild( editorElement );
+
+			return ClassicTestEditor
+				.create( editorElement, {
+					plugins: [ Paragraph, ParagraphButtonUI, Heading ],
+					toolbar: [ 'paragraph' ]
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+					setData( editor.model, '<paragraph>f{}oo</paragraph>' );
+				} );
+		} );
+
+		afterEach( () => {
+			editorElement.remove();
+
+			return editor.destroy();
+		} );
+
+		it( 'should define default buttons', () => {
+			const factory = editor.ui.componentFactory;
+
+			expect( factory.create( 'paragraph' ) ).to.be.instanceOf( ButtonView );
+		} );
+
+		it( 'should intialize buttons with correct data', () => {
+			const paragraphButton = editor.ui.componentFactory.create( 'paragraph' );
+
+			expect( paragraphButton.label ).to.equal( 'Paragraph' );
+			expect( paragraphButton.icon ).to.equal( icon );
+			expect( paragraphButton.tooltip ).to.equal( true );
+		} );
+
+		it( 'should bind button to command', () => {
+			const paragraphButton = editor.ui.componentFactory.create( 'paragraph' );
+			const paragraphCommand = editor.commands.get( 'paragraph' );
+
+			expect( paragraphCommand.isEnabled ).to.be.true;
+			expect( paragraphButton.isEnabled ).to.be.true;
+
+			paragraphCommand.isEnabled = false;
+			expect( paragraphButton.isEnabled ).to.be.false;
+
+			expect( paragraphCommand.value ).to.be.true;
+			expect( paragraphButton.isOn ).to.be.true;
+
+			setData( editor.model, '<heading2>f{}oo</heading2>' );
+
+			expect( paragraphCommand.value ).to.be.false;
+			expect( paragraphButton.isOn ).to.be.false;
+		} );
+
+		it( 'should bind button execute to command execute', () => {
+			const pararaphButton = editor.ui.componentFactory.create( 'paragraph' );
+			const executeCommandSpy = sinon.spy( editor, 'execute' );
+
+			pararaphButton.fire( 'execute' );
+
+			sinon.assert.calledOnce( executeCommandSpy );
+			sinon.assert.calledWithExactly( executeCommandSpy, 'paragraph' );
+		} );
+	} );
+} );

+ 1 - 0
packages/ckeditor5-paragraph/theme/icons/paragraph.svg

@@ -0,0 +1 @@
+<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><g fill="#000"><path d="M7.5 5.4V10l.1.2H9c1 0 2-.3 2.5-.7.5-.4 1-1 1-2 0-1.6-1.4-2.3-3.5-2.3H7.6l-.1.2zM6.1 17a1 1 0 0 1-.8-.3 1 1 0 0 1-.3-.8V4.4l.3-.9c.2-.2.5-.3.8-.3L9 3c2 0 3.4.4 4.3 1.1 1 .8 1.4 1.9 1.4 3.3 0 1.6-.4 2.8-1.3 3.6-1 .8-2.3 1.2-4 1.2L7.5 12l-.1.2v3.6c0 .3-.1.6-.3.8a1 1 0 0 1-.8.3H6z"/></g></g></svg>