Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XHR as possible uploading mechanism in filebrowser plugin #1352

Merged
merged 28 commits into from
Dec 22, 2017
Merged

Conversation

msamsel
Copy link
Contributor

@msamsel msamsel commented Dec 15, 2017

What is the purpose of this pull request?

New feature

Does your PR contain necessary tests?

All patches which change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.

This PR contains

  • Unit tests
  • Manual tests

What changes did you make?

  • change way how filebrowser upload files - prefer using XHR
  • allow on providing custom headers to XHR used when files are uploaded
  • add config flag which will force to use form submit behaviour when files are uploaded

Closes #643.

@msamsel msamsel changed the title Use XHR as default mechanism of uploading in filebrowser plugin Use XHR as default uploading mechanism in filebrowser plugin Dec 15, 2017
@msamsel msamsel force-pushed the t/tp3117 branch 3 times, most recently from 42ece87 to c3c075b Compare December 18, 2017 10:28
@mlewand mlewand self-requested a review December 18, 2017 14:34
Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revisit configuration:

We could add this feature (sending file browser stuff over XHR) in a minor release (4.8.1), but we can't change the default behavior in a minor release.

That's why, for now, the default behavior should be to send <form> using element, as it was. But it should be possible to change this by setting a configuration option to 'xhr'.

Another task will be to use xhr by default in a next major release - 4.9, while still allowing the config variable to enforce old <form> mode.

So that requires us to change the config a bit, it should work the following way:

filebrowser_forceFormSubmit should be renamed to filebrowserUploadMethod. API docs should explain that for wide browser support CKEditor provides multiple uploading strategies and this option can allow you to force usage of a given strategy.

The default strat. is a form strategy.

This config can take following values:

  • 'form' - files are upload by sending traditional HTML <form>
  • 'xhr' - files are uploaded using XHR
  • null (default value) - the default upload method applies

It also must include @since tag


There are no API docs for config.xmlHttpRequestHeaders option. Please don't forget about @since tag.


if ( uploadFile.call( sender, evt ) ) {
var fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement();
// Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117).
if ( editor.config.filebrowser_forceFormSubmit || !CKEDITOR.fileTools.isFileUploadSupported ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will break if no filetools plugin is added. Following exception will be thrown:

uploadxhr:105 Uncaught TypeError: Cannot read property 'isFileUploadSupported' of undefined
    at Object.<anonymous> (uploadxhr:105)
    at Object.listenerFirer (event.js:144)
    at Object.CKEDITOR.event.fire (event.js:290)
    at Object.<anonymous> (editor.js:625)
    at doCallback (scriptloader.js:70)
    at Array.checkLoaded (scriptloader.js:84)
    at onLoad (scriptloader.js:98)
    at scriptloader.js:135

Just remove filetools plugin from uploadxhr manual test and follow the steps in the description. You'll see that it will throw this error.

} else {
var loader = editor.uploadRepository.create( fileInput.$.files[ 0 ] );

loader.on( 'uploaded', function( evt ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to provide an error handling. E.g. user is connected to the wifi, and he losts the connection, upload returns 404.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify it to showing an alert. I'll save you some time, and here is the snippet for it:

loader.on( 'error', function( evt ) {
	alert( this.message );
} );

however you still need to enable the upload button (note that it gets disabled).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a separate unit and manual test for the above. You could just stub window.alert and then ensure it was called with an error message.

Remember to set config.language to 'en' in the unit test, otherwise person opening on a different locale could have alert called with message other than expected. 🙂

@msamsel msamsel changed the title Use XHR as default uploading mechanism in filebrowser plugin Use XHR as possible uploading mechanism in filebrowser plugin Dec 20, 2017
@msamsel msamsel requested a review from mlewand December 20, 2017 11:49
*
* * `'xhr'` - XMLHttpRequest is used to upload file
* * `'form'` - Form submit is used to upload file
* * `undefiend` - when configuration option is not specified `'form'` configuration is used
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo -> undefined.

* config.filebrowserUploadMethod = 'xhr';
*
* @since 4.8.1
* @cfg {Boolean} [filebrowserUploadMethod=undefined]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong type. This is not a bool but a string. Also I have asked for a null value rather than undefined - I'll correct that.

<div id="output" style="background-color:lightgreen;"></div>

<script>
var xhr, requests;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not specify multiple variables in a single line.

if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
bender.ignore();
} else {
// Mock XHR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing dot.

var output = document.getElementById( 'output' );
var outputString = CKEDITOR.tools.array.reduce( requests, function( acc, item ) {
var line = '';
for ( header in item.requestHeaders ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be much simplified, you could just add JSON.stringify( item.requestHeaders ) 🙂

I was able to get:

image

instead of

image

Which seems to be a bit more readable.

<div id="output" style="background-color:lightgreen;"></div>

<script>
var xhr, requests;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variables.

@@ -885,3 +911,16 @@
* @cfg {String} [fileTools_defaultFileName='']
* @member CKEDITOR.config
*/

/**
* Additional headers of XMLHttpRequest used during file upload with plugins: {@link CKEDITOR.fileTools} and filebrowser.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These headers are not added only for file uploading but any request made by filetools.

* };
*
* @since 4.8.1
* @cfg {Object} [xmlHttpRequestHeaders=null]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config property should be prefixed with fileTools_ just like the other filetools config property.

Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistakenly gave a wrong review result for the previous one, sorry.

@mlewand
Copy link
Contributor

mlewand commented Dec 20, 2017

I'll take over this one from here.

Copy link
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mlewand mlewand merged commit b849b9a into master Dec 22, 2017
@mlewand mlewand deleted the t/tp3117 branch December 22, 2017 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants