8
0
فهرست منبع

Introduced TableWidgetToolbar.

Maciej Bukowski 7 سال پیش
والد
کامیت
0344d001ed
2فایلهای تغییر یافته به همراه213 افزوده شده و 0 حذف شده
  1. 44 0
      packages/ckeditor5-table/src/tablewidgettoolbar.js
  2. 169 0
      packages/ckeditor5-table/tests/tablewidgettoolbar.js

+ 44 - 0
packages/ckeditor5-table/src/tablewidgettoolbar.js

@@ -0,0 +1,44 @@
+/**
+ * Copyright (c) 2016 - 2018, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import { isTableWidgetSelected } from '@ckeditor/ckeditor5-table/src/utils';
+import WidgetToolbar from '@ckeditor/ckeditor5-widget/src/widgettoolbar';
+
+/**
+ * The table widget toolbar class. It creates a table toolbar that shows up when the table widget is selected.
+ *
+ * The toolbar uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}.
+ *
+ * @extends module:core/plugin~Plugin
+ */
+export default class TableWidgetToolbar extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ WidgetToolbar ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'TableWidgetToolbar';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	afterInit() {
+		const editor = this.editor;
+		const widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
+
+		widgetToolbar.add( {
+			toolbarItems: editor.config.get( 'table.widgetToolbar' ) || [],
+			isSelected: isTableWidgetSelected,
+		} );
+	}
+}
+

+ 169 - 0
packages/ckeditor5-table/tests/tablewidgettoolbar.js

@@ -0,0 +1,169 @@
+/**
+ * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
+ */
+
+/* global document */
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import Table from '@ckeditor/ckeditor5-table/src/table';
+import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import TableWidgetToolbar from '../src/tablewidgettoolbar';
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import View from '@ckeditor/ckeditor5-ui/src/view';
+
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+
+describe( 'TableWidgetToolbar', () => {
+	let editor, element, tableWidgetToolbar, balloon, toolbar, model;
+
+	testUtils.createSinonSandbox();
+
+	beforeEach( () => {
+		element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		return ClassicTestEditor.create( element, {
+			plugins: [ Paragraph, Table, TableToolbar, TableWidgetToolbar ],
+			table: {
+				widgetToolbar: []
+			}
+		} ).then( _editor => {
+			editor = _editor;
+			tableWidgetToolbar = editor.plugins.get( TableWidgetToolbar );
+			toolbar = tableWidgetToolbar._toolbar;
+			balloon = editor.plugins.get( 'ContextualBalloon' );
+			model = editor.model;
+		} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy()
+			.then( () => element.remove() );
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( TableWidgetToolbar ) ).to.be.instanceOf( TableWidgetToolbar );
+	} );
+
+	// it( 'should have only one item - comment', () => {
+	// 	expect( toolbar.items ).to.have.length( 1 );
+	// 	expect( toolbar.items.get( 0 ).label ).to.equal( 'Inline comment' );
+	// } );
+
+	it( 'should set proper CSS classes', () => {
+		const spy = sinon.spy( balloon, 'add' );
+
+		editor.ui.focusTracker.isFocused = true;
+
+		setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
+
+		sinon.assert.calledWithMatch( spy, {
+			view: toolbar,
+			balloonClassName: 'ck-toolbar-container'
+		} );
+	} );
+
+	describe( 'integration with the editor focus', () => {
+		it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
+			editor.ui.focusTracker.isFocused = true;
+
+			setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
+
+			editor.ui.focusTracker.isFocused = false;
+			expect( balloon.visibleView ).to.be.null;
+
+			editor.ui.focusTracker.isFocused = true;
+			expect( balloon.visibleView ).to.equal( toolbar );
+		} );
+
+		it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
+			editor.ui.focusTracker.isFocused = false;
+
+			setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
+
+			editor.ui.focusTracker.isFocused = true;
+			expect( balloon.visibleView ).to.equal( toolbar );
+
+			editor.ui.focusTracker.isFocused = false;
+			expect( balloon.visibleView ).to.be.null;
+		} );
+	} );
+
+	describe( 'integration with the editor selection', () => {
+		beforeEach( () => {
+			editor.ui.focusTracker.isFocused = true;
+		} );
+
+		it( 'should show the toolbar on ui#update when the table widget is selected', () => {
+			setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
+
+			expect( balloon.visibleView ).to.be.null;
+
+			editor.ui.fire( 'update' );
+
+			expect( balloon.visibleView ).to.be.null;
+
+			editor.model.change( writer => {
+				// Select the [<table></table>]
+				writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
+			} );
+
+			expect( balloon.visibleView ).to.equal( toolbar );
+
+			// Make sure successive change does not throw, e.g. attempting
+			// to insert the toolbar twice.
+			editor.ui.fire( 'update' );
+			expect( balloon.visibleView ).to.equal( toolbar );
+		} );
+
+		it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
+			setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
+
+			expect( balloon.visibleView ).to.be.null;
+
+			editor.ui.fire( 'update' );
+
+			expect( balloon.visibleView ).to.be.null;
+		} );
+
+		it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
+			setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
+
+			expect( balloon.visibleView ).to.equal( toolbar );
+
+			const lastView = new View();
+			lastView.element = document.createElement( 'div' );
+
+			balloon.add( {
+				view: lastView,
+				position: {
+					target: document.body
+				}
+			} );
+
+			expect( balloon.visibleView ).to.equal( lastView );
+
+			editor.ui.fire( 'update' );
+
+			expect( balloon.visibleView ).to.equal( lastView );
+		} );
+
+		it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
+			setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
+			expect( balloon.visibleView ).to.equal( toolbar );
+
+			model.change( writer => {
+				// Select the <paragraph>[...]</paragraph>
+				writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
+			} );
+
+			expect( balloon.visibleView ).to.be.null;
+
+			// Make sure successive change does not throw, e.g. attempting
+			// to remove the toolbar twice.
+			editor.ui.fire( 'update' );
+			expect( balloon.visibleView ).to.be.null;
+		} );
+	} );
+} );