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

Commit b5092a4

Browse files
authored
Merge pull request #98 from ckeditor/t/97
Feature: Implemented the responsive image support in the `SimpleUploadAdapter`. Closes #97.
2 parents 83d552f + 4e2f287 commit b5092a4

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

docs/features/simple-upload-adapter.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,32 @@ The [`responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpReq
8787

8888
### Successful upload
8989

90-
If the upload is successful, the server should return an object containing the `url` property which points out to the uploaded image on the server:
90+
If the upload is successful, the server should either return:
9191

92-
```json
93-
{
94-
"url": "https://example.com/images/foo.jpg"
95-
}
96-
```
92+
* an object containing the `url` property which points out to the uploaded image on the server:
93+
94+
```json
95+
{
96+
"url": "https://example.com/images/foo.jpg"
97+
}
98+
```
99+
100+
* an object with the `urls` property, if you want to use [responsive images](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) and the server supports it:
101+
102+
```json
103+
{
104+
"urls": {
105+
"default": "https://example.com/images/foo.jpg",
106+
"800": "https://example.com/images/foo-800.jpg",
107+
"1024": "https://example.com/images/foo-1024.jpg",
108+
"1920": "https://example.com/images/foo-1920.jpg"
109+
}
110+
}
111+
```
112+
113+
The `"default"` URL will be used in the `src` attribute of the image in the editor content. Other URLs will be used in the `srcset` attribute allowing the web browser to select the best one for the geometry of the viewport.
97114

98-
That image URL is essential because it will be used:
115+
URL(s) in the server response are used:
99116

100117
* to display the image during the editing (as seen by the user in the editor),
101118
* in the editor content {@link builds/guides/integration/saving-data saved to the databse}.

src/adapters/simpleuploadadapter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ class Adapter {
176176
return reject( response && response.error && response.error.message ? response.error.message : genericErrorText );
177177
}
178178

179-
resolve( {
180-
default: response.url
181-
} );
179+
resolve( response.url ? { default: response.url } : response.urls );
182180
} );
183181

184182
// Upload progress when it is supported.

tests/adapters/simpleuploadadapter.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ describe( 'SimpleUploadAdapter', () => {
136136
} );
137137
} );
138138

139-
it( 'should call url from config', () => {
140-
let request;
139+
it( 'should send a request to config#uploadUrl', () => {
141140
const validResponse = {
142141
url: 'http://example.com/images/image.jpeg'
143142
};
@@ -146,7 +145,7 @@ describe( 'SimpleUploadAdapter', () => {
146145

147146
return loader.file
148147
.then( () => {
149-
request = sinonXHR.requests[ 0 ];
148+
const request = sinonXHR.requests[ 0 ];
150149
request.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( validResponse ) );
151150

152151
expect( request.url ).to.equal( 'http://example.com' );
@@ -159,7 +158,31 @@ describe( 'SimpleUploadAdapter', () => {
159158
} );
160159
} );
161160

162-
it( 'should modify headers of uploading request if specified', () => {
161+
it( 'should support responsive image URLs returned in the server response', () => {
162+
const validResponse = {
163+
urls: {
164+
default: 'http://example.com/images/image.jpeg',
165+
'120': 'http://example.com/images/image-120.jpeg',
166+
'240': 'http://example.com/images/image-240.jpeg'
167+
}
168+
};
169+
170+
const uploadPromise = adapter.upload();
171+
172+
return loader.file
173+
.then( () => {
174+
sinonXHR.requests[ 0 ].respond( 200, {
175+
'Content-Type': 'application/json'
176+
}, JSON.stringify( validResponse ) );
177+
178+
return uploadPromise;
179+
} )
180+
.then( uploadResponse => {
181+
expect( uploadResponse ).to.deep.equal( validResponse.urls );
182+
} );
183+
} );
184+
185+
it( 'should use config#headers in the request (when specified)', () => {
163186
const editorElement = document.createElement( 'div' );
164187
document.body.appendChild( editorElement );
165188

0 commit comments

Comments
 (0)