Sfoglia il codice sorgente

Automatically adding alternate text button to ImageToolbar if one is present.

Szymon Kupś 8 anni fa
parent
commit
e4d8a4965b

+ 7 - 0
packages/ckeditor5-image/src/imagealternatetext/imagealternatetext.js

@@ -38,6 +38,13 @@ export default class ImageAlternateText extends Plugin {
 	init() {
 		this._createButton();
 
+		// Push button to default image toolbar if one exists.
+		const defaultImageToolbarConfig = this.editor.config.get( 'image.defaultToolbar' );
+
+		if ( defaultImageToolbarConfig ) {
+			defaultImageToolbarConfig.push( 'imageAlternateText' );
+		}
+
 		return this._createBalloonPanel().then( panel => {
 			/**
 			 * Balloon panel containing alternate text change form.

+ 18 - 0
packages/ckeditor5-image/tests/imagealternatetext/imagealternatetext.js

@@ -91,6 +91,24 @@ describe( 'ImageAlternateText', () => {
 			sinon.assert.calledOnce( spy );
 			expect( plugin.form.lebeledInput.value ).equals( '' );
 		} );
+
+		it( 'should not add button to default image toolbar if image toolbar is not present', () => {
+			expect( editor.config.get( 'image.defaultToolbar' ) ).to.be.undefined;
+		} );
+
+		it( 'should add button to default image toolbar if toolbar is present', () => {
+			const editorElement = global.document.createElement( 'div' );
+			global.document.body.appendChild( editorElement );
+
+			return ClassicTestEditor.create( editorElement, {
+				plugins: [ ImageAlternateText, ImageToolbar ]
+			} )
+			.then( newEditor => {
+				expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'imageAlternateText' ] );
+
+				newEditor.destroy();
+			} );
+		} );
 	} );
 
 	describe( 'balloon panel form', () => {

+ 11 - 1
packages/ckeditor5-image/tests/imagetoolbar.js

@@ -43,7 +43,17 @@ describe( 'ImageToolbar', () => {
 	} );
 
 	it( 'should initialize image.defaultToolbar to an empty array', () => {
-		expect( editor.config.get( 'image.defaultToolbar' ) ).to.eql( [] );
+		const editorElement = global.document.createElement( 'div' );
+		global.document.body.appendChild( editorElement );
+
+		return ClassicEditor.create( editorElement, {
+			plugins: [ ImageToolbar ],
+		} )
+		.then( editor => {
+			expect( editor.config.get( 'image.defaultToolbar' ) ).to.eql( [] );
+
+			return editor.destroy();
+		} );
 	} );
 
 	it( 'should not initialize if there is no configuration', () => {

+ 1 - 4
packages/ckeditor5-image/tests/manual/alternatetext.js

@@ -17,10 +17,7 @@ import ImageToolbar from '../../src/imagetoolbar';
 
 ClassicEditor.create( document.querySelector( '#editor' ), {
 	plugins: [ EnterPlugin, TypingPlugin, ParagraphPlugin, HeadingPlugin, ImagePlugin, UndoPlugin, ClipboardPlugin, ImageToolbar ],
-	toolbar: [ 'headings', 'undo', 'redo' ],
-	image: {
-		toolbar: [ 'imageAlternateText' ]
-	}
+	toolbar: [ 'headings', 'undo', 'redo' ]
 } )
 .then( editor => {
 	window.editor = editor;