-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
42ece87
to
c3c075b
Compare
There was a problem hiding this 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 XHRnull
(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.
plugins/filebrowser/plugin.js
Outdated
|
||
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 ) { |
There was a problem hiding this comment.
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.
plugins/filebrowser/plugin.js
Outdated
} else { | ||
var loader = editor.uploadRepository.create( fileInput.$.files[ 0 ] ); | ||
|
||
loader.on( 'uploaded', function( evt ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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. 🙂
…ig changes to docs related to the feature.
…nual test, code clean up.
plugins/filebrowser/plugin.js
Outdated
* | ||
* * `'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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo -> undefined.
plugins/filebrowser/plugin.js
Outdated
* config.filebrowserUploadMethod = 'xhr'; | ||
* | ||
* @since 4.8.1 | ||
* @cfg {Boolean} [filebrowserUploadMethod=undefined] |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<div id="output" style="background-color:lightgreen;"></div> | ||
|
||
<script> | ||
var xhr, requests; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variables.
plugins/filetools/plugin.js
Outdated
@@ -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. |
There was a problem hiding this comment.
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.
plugins/filetools/plugin.js
Outdated
* }; | ||
* | ||
* @since 4.8.1 | ||
* @cfg {Object} [xmlHttpRequestHeaders=null] |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
I'll take over this one from here. |
Corrections to XHR support for File Browser plugin uploads
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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
What changes did you make?
form submit
behaviour when files are uploadedCloses #643.