Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #45 from ckeditor/t/35
Browse files Browse the repository at this point in the history
Feature: Implemented a tip in the form that helps users discover the auto embedding. Closes #35.
  • Loading branch information
pomek authored Oct 29, 2018
2 parents a1b1b8d + 0240af5 commit ebdec7e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions lang/contexts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"media widget": "Label for the media widget.",
"Media URL": "Label for the URL input in the Media Embed URL editing balloon.",
"Paste the URL into the content to embed faster.": "The tip displayed next to the media URL input informing about an easier way of embedding.",
"The URL must not be empty.": "An error message that informs about an empty value in the URL input.",
"This media URL is not supported.": "An error message that informs about unsupported media URL.",
"Insert media": "Toolbar button tooltip for the Media Embed feature."
Expand Down
2 changes: 1 addition & 1 deletion src/mediaembedui.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class MediaEmbedUI extends Plugin {
}
} );

dropdown.on( 'change:isOpen', () => form.resetErrors() );
dropdown.on( 'change:isOpen', () => form.resetFormStatus() );
dropdown.on( 'cancel', () => closeUI() );

function closeUI() {
Expand Down
22 changes: 17 additions & 5 deletions src/ui/mediaformview.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export default class MediaFormView extends View {
* @returns {Boolean}
*/
isValid() {
this.resetErrors();
this.resetFormStatus();

for ( const validator of this._validators ) {
const errorText = validator( this );
Expand All @@ -236,10 +236,14 @@ export default class MediaFormView extends View {
}

/**
* Cleans up the errors in all form fields. See {@link #isValid}.
* Cleans up the supplementary error and information text of the {@link #urlInputView}
* bringing them back to the state when the form has been displayed for the first time.
*
* See {@link #isValid}.
*/
resetErrors() {
this.urlInputView.errorText = false;
resetFormStatus() {
this.urlInputView.errorText = null;
this.urlInputView.infoText = null;
}

/**
Expand All @@ -252,9 +256,17 @@ export default class MediaFormView extends View {
const t = this.locale.t;

const labeledInput = new LabeledInputView( this.locale, InputTextView );
const inputView = labeledInput.inputView;

labeledInput.label = t( 'Media URL' );
labeledInput.inputView.placeholder = 'https://example.com';
inputView.placeholder = 'https://example.com';

inputView.on( 'input', () => {
// Display the #infoText only when there's some value to hide it when the input
// is empty, e.g. user used backspace to clean it up.
labeledInput.infoText = inputView.element.value ?
t( 'Paste the URL into the content to embed faster.' ) : null;
} );

return labeledInput;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/mediaembedui.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ describe( 'MediaEmbedUI', () => {
} );

describe( '#change:isOpen event', () => {
it( 'resets form errors', () => {
const spy = sinon.spy( form, 'resetErrors' );
it( 'resets form status', () => {
const spy = sinon.spy( form, 'resetFormStatus' );

dropdown.fire( 'change:isOpen' );

Expand Down
32 changes: 26 additions & 6 deletions tests/ui/mediaformview.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe( 'MediaFormView', () => {
it( 'has placeholder', () => {
expect( view.urlInputView.inputView.placeholder ).to.equal( 'https://example.com' );
} );

it( 'displays the info text when upon #input when the field has a value', () => {
view.urlInputView.inputView.element.value = 'foo';
view.urlInputView.inputView.fire( 'input' );

expect( view.urlInputView.infoText ).to.match( /^Paste the URL/ );

view.urlInputView.inputView.element.value = '';
view.urlInputView.inputView.fire( 'input' );

expect( view.urlInputView.infoText ).to.be.null;
} );
} );

describe( 'template', () => {
Expand Down Expand Up @@ -244,8 +256,8 @@ describe( 'MediaFormView', () => {
} );

describe( 'isValid()', () => {
it( 'calls resetErrors()', () => {
const spy = sinon.spy( view, 'resetErrors' );
it( 'calls resetFormStatus()', () => {
const spy = sinon.spy( view, 'resetFormStatus' );

view.isValid();

Expand Down Expand Up @@ -277,17 +289,25 @@ describe( 'MediaFormView', () => {
sinon.assert.calledOnce( val1 );
sinon.assert.calledOnce( val2 );

expect( view.urlInputView.errorText ).to.be.false;
expect( view.urlInputView.errorText ).to.be.null;
} );
} );

describe( 'resetErrors()', () => {
describe( 'resetFormStatus()', () => {
it( 'resets urlInputView#errorText', () => {
view.urlInputView.errorText = 'foo';

view.resetErrors();
view.resetFormStatus();

expect( view.urlInputView.errorText ).to.be.null;
} );

it( 'resets urlInputView#infoText', () => {
view.urlInputView.infoText = 'foo';

view.resetFormStatus();

expect( view.urlInputView.errorText ).to.be.false;
expect( view.urlInputView.infoText ).to.be.null;
} );
} );
} );

0 comments on commit ebdec7e

Please sign in to comment.