Bladeren bron

Merge pull request #8 from ckeditor/t/3

Other: CKEditor should set CKFinder language when opening. Closes #3.
Piotrek Koszuliński 7 jaren geleden
bovenliggende
commit
5736ba3bdf

+ 8 - 4
packages/ckeditor5-ckfinder/src/ckfindercommand.js

@@ -34,9 +34,7 @@ export default class CKFinderCommand extends Command {
 		this.stopListening( this.editor.model.document, 'change' );
 
 		// Lower this command listener priority to be sure that refresh() will be called after link & image refresh.
-		this.listenTo( this.editor.model.document, 'change', () => {
-			this.refresh();
-		}, { priority: 'low' } );
+		this.listenTo( this.editor.model.document, 'change', () => this.refresh(), { priority: 'low' } );
 	}
 
 	/**
@@ -46,7 +44,8 @@ export default class CKFinderCommand extends Command {
 		const imageCommand = this.editor.commands.get( 'imageUpload' );
 		const linkCommand = this.editor.commands.get( 'link' );
 
-		this.isEnabled = ( !imageCommand || !linkCommand ) ? false : ( imageCommand.isEnabled || linkCommand.isEnabled );
+		// The CKFinder command is enabled when one of image or link command is enabled.
+		this.isEnabled = imageCommand && linkCommand && ( imageCommand.isEnabled || linkCommand.isEnabled );
 	}
 
 	/**
@@ -65,6 +64,11 @@ export default class CKFinderCommand extends Command {
 
 		options.chooseFiles = true;
 
+		// Pass the lang code to the CKFinder if not defined by user.
+		if ( !options.language ) {
+			options.language = editor.locale.language;
+		}
+
 		// The onInit method allows to extend CKFinder's behavior. It is used to attach event listeners to file choosing related events.
 		options.onInit = finder => {
 			finder.on( 'files:choose', evt => {

+ 57 - 5
packages/ckeditor5-ckfinder/tests/ckfindercommand.js

@@ -34,9 +34,6 @@ describe( 'CKFinderCommand', () => {
 
 				command = new CKFinderCommand( editor );
 
-				const schema = model.schema;
-				schema.extend( 'image' );
-
 				setModelData( model, '<paragraph>f[o]o</paragraph>' );
 			} );
 	} );
@@ -134,7 +131,7 @@ describe( 'CKFinderCommand', () => {
 			expect( finderMock ).to.have.property( 'file:choose:resizedImage' );
 		} );
 
-		it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
+		it( 'should use CKFinder.modal() as default CKFinder opener method', () => {
 			const spy = sinon.spy( window.CKFinder, 'modal' );
 
 			command.execute();
@@ -142,7 +139,7 @@ describe( 'CKFinderCommand', () => {
 			sinon.assert.calledOnce( spy );
 		} );
 
-		it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
+		it( 'should use CKFinder.popup() when ckfinder.openerMethod is set to it', () => {
 			const spy = sinon.spy( window.CKFinder, 'popup' );
 
 			editor.config.set( 'ckfinder.openerMethod', 'popup' );
@@ -197,6 +194,61 @@ describe( 'CKFinderCommand', () => {
 			expect( openerMethodOptions ).to.have.property( 'connectorPath', connectorPath );
 		} );
 
+		it( 'should pass editor default language to the CKFinder instance', () => {
+			const spy = sinon.spy( window.CKFinder, 'modal' );
+			command.execute();
+
+			const openerMethodOptions = spy.args[ 0 ][ 0 ];
+
+			expect( openerMethodOptions ).to.have.property( 'language', 'en' );
+		} );
+
+		it( 'should pass editor language set by configuration to the CKFinder instance', () => {
+			const finderMock = {
+				on: ( eventName, callback ) => {
+					finderMock[ eventName ] = callback;
+				}
+			};
+
+			return VirtualTestEditor
+				.create( {
+					plugins: [ Paragraph, ImageEditing, ImageUploadEditing, LinkEditing, Notification ],
+					language: 'pl'
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+
+					window.CKFinder = {
+						modal: config => {
+							config.onInit( finderMock );
+						}
+					};
+
+					command = new CKFinderCommand( editor );
+
+					setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
+					const spy = sinon.spy( window.CKFinder, 'modal' );
+
+					command.execute();
+
+					const openerMethodOptions = spy.args[ 0 ][ 0 ];
+
+					expect( openerMethodOptions ).to.have.property( 'language', 'pl' );
+				} );
+		} );
+
+		it( 'should not pass editor language if it is set in ckfinder.options', () => {
+			const spy = sinon.spy( window.CKFinder, 'modal' );
+
+			editor.config.set( 'ckfinder.options.language', 'pl' );
+
+			command.execute();
+
+			const openerMethodOptions = spy.args[ 0 ][ 0 ];
+
+			expect( openerMethodOptions ).to.have.property( 'language', 'pl' );
+		} );
+
 		it( 'should insert multiple chosen images as image widget', () => {
 			const url1 = 'foo/bar1.jpg';
 			const url2 = 'foo/bar2.jpg';