Przeglądaj źródła

Merge pull request #136 from ckeditor/t/135

Feature: `LinkFormView` controls should enter the read-only mode when `LinkCommand` and `UnlinkCommand` are disabled. Closes #135.
Aleksander Nowodzinski 8 lat temu
rodzic
commit
837122f1ee

+ 8 - 1
packages/ckeditor5-link/src/link.js

@@ -87,8 +87,15 @@ export default class Link extends Plugin {
 	_createForm() {
 		const editor = this.editor;
 		const formView = new LinkFormView( editor.locale );
+		const linkCommand = editor.commands.get( 'link' );
+		const unlinkCommand = editor.commands.get( 'unlink' );
+
+		formView.urlInputView.bind( 'value' ).to( linkCommand, 'value' );
 
-		formView.urlInputView.bind( 'value' ).to( editor.commands.get( 'link' ), 'value' );
+		// Form elements should be read-only when corresponding commands are disabled.
+		formView.urlInputView.bind( 'isReadOnly' ).to( linkCommand, 'isEnabled', value => !value );
+		formView.saveButtonView.bind( 'isEnabled' ).to( linkCommand );
+		formView.unlinkButtonView.bind( 'isEnabled' ).to( unlinkCommand );
 
 		// Execute link command after clicking on formView `Save` button.
 		this.listenTo( formView, 'submit', () => {

+ 22 - 0
packages/ckeditor5-link/tests/link.js

@@ -175,6 +175,28 @@ describe( 'Link', () => {
 			sinon.assert.calledOnce( spy );
 		} );
 
+		it( 'should disable #formView elements when link and unlink commands are disabled', () => {
+			setModelData( editor.document, '<paragraph>f[o]o</paragraph>' );
+
+			linkFeature._showPanel();
+
+			editor.commands.get( 'link' ).isEnabled = true;
+			editor.commands.get( 'unlink' ).isEnabled = true;
+
+			expect( formView.urlInputView.isReadOnly ).to.false;
+			expect( formView.saveButtonView.isEnabled ).to.true;
+			expect( formView.unlinkButtonView.isEnabled ).to.true;
+			expect( formView.cancelButtonView.isEnabled ).to.true;
+
+			editor.commands.get( 'link' ).isEnabled = false;
+			editor.commands.get( 'unlink' ).isEnabled = false;
+
+			expect( formView.urlInputView.isReadOnly ).to.true;
+			expect( formView.saveButtonView.isEnabled ).to.false;
+			expect( formView.unlinkButtonView.isEnabled ).to.false;
+			expect( formView.cancelButtonView.isEnabled ).to.true;
+		} );
+
 		// https://github.com/ckeditor/ckeditor5-link/issues/53
 		it( 'should set formView.unlinkButtonView#isVisible depending on the selection in a link or not', () => {
 			setModelData( editor.document, '<paragraph>f[]oo</paragraph>' );