enhancement(media-library-uploadcare): add integration settings to control url operations (#2670)
This commit is contained in:
parent
5636f5f54c
commit
574477adca
@ -51,9 +51,10 @@ describe('uploadcare media library', () => {
|
|||||||
/**
|
/**
|
||||||
* Mock to manually call the close dialog registered callback.
|
* Mock to manually call the close dialog registered callback.
|
||||||
*/
|
*/
|
||||||
simulateCloseDialog = result =>
|
simulateCloseDialog = (result, files) =>
|
||||||
openDialogCallback({
|
openDialogCallback({
|
||||||
promise: () => Promise.resolve(result),
|
promise: () => Promise.resolve(result),
|
||||||
|
...(files ? { files: () => files.map(file => Promise.resolve(file)) } : {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -90,7 +91,7 @@ Object {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('configuration', () => {
|
describe('widget configuration', () => {
|
||||||
const options = {
|
const options = {
|
||||||
config: {
|
config: {
|
||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
@ -179,13 +180,106 @@ Object {
|
|||||||
options.config.multiple = true;
|
options.config.multiple = true;
|
||||||
const { result, cdnUrl } = generateMockUrl({ count: 3, cdnUrl: true });
|
const { result, cdnUrl } = generateMockUrl({ count: 3, cdnUrl: true });
|
||||||
const mockDialogCloseResult = { cdnUrl, count: 3 };
|
const mockDialogCloseResult = { cdnUrl, count: 3 };
|
||||||
|
const mockDialogCloseFiles = result.map((cdnUrl, idx) => ({
|
||||||
|
cdnUrl,
|
||||||
|
isImage: true,
|
||||||
|
name: `test${idx}.png`,
|
||||||
|
}));
|
||||||
const integration = await uploadcareMediaLibrary.init({ options, handleInsert });
|
const integration = await uploadcareMediaLibrary.init({ options, handleInsert });
|
||||||
await integration.show();
|
await integration.show();
|
||||||
await simulateCloseDialog(mockDialogCloseResult);
|
await simulateCloseDialog(mockDialogCloseResult, mockDialogCloseFiles);
|
||||||
expect(handleInsert).toHaveBeenCalledWith(result);
|
expect(handleInsert).toHaveBeenCalledWith(result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('settings', () => {
|
||||||
|
describe('defaultOperations', () => {
|
||||||
|
it('should append specified string to the url', async () => {
|
||||||
|
const options = {
|
||||||
|
config: {
|
||||||
|
publicKey: TEST_PUBLIC_KEY,
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
defaultOperations: '/preview/',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const url = generateMockUrl();
|
||||||
|
const mockResult = { cdnUrl: url, isImage: true };
|
||||||
|
const integration = await uploadcareMediaLibrary.init({
|
||||||
|
options,
|
||||||
|
handleInsert,
|
||||||
|
});
|
||||||
|
await integration.show();
|
||||||
|
await simulateCloseDialog(mockResult);
|
||||||
|
expect(handleInsert).toHaveBeenCalledWith(url + '-/preview/');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work along with `autoFilename` setting enabled', async () => {
|
||||||
|
const options = {
|
||||||
|
config: {
|
||||||
|
publicKey: TEST_PUBLIC_KEY,
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
autoFilename: true,
|
||||||
|
defaultOperations: '/preview/',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const url = generateMockUrl();
|
||||||
|
const mockResult = { cdnUrl: url, isImage: true, name: 'test.png' };
|
||||||
|
const integration = await uploadcareMediaLibrary.init({
|
||||||
|
options,
|
||||||
|
handleInsert,
|
||||||
|
});
|
||||||
|
await integration.show();
|
||||||
|
await simulateCloseDialog(mockResult);
|
||||||
|
expect(handleInsert).toHaveBeenCalledWith(url + '-/preview/test.png');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should overwrite filename with `autoFilename` setting enabled', async () => {
|
||||||
|
const options = {
|
||||||
|
config: {
|
||||||
|
publicKey: TEST_PUBLIC_KEY,
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
autoFilename: true,
|
||||||
|
defaultOperations: '/preview/another_name.png',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const url = generateMockUrl();
|
||||||
|
const mockResult = { cdnUrl: url, isImage: true, name: 'test.png' };
|
||||||
|
const integration = await uploadcareMediaLibrary.init({
|
||||||
|
options,
|
||||||
|
handleInsert,
|
||||||
|
});
|
||||||
|
await integration.show();
|
||||||
|
await simulateCloseDialog(mockResult);
|
||||||
|
expect(handleInsert).toHaveBeenCalledWith(url + '-/preview/another_name.png');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('autoFilename', () => {
|
||||||
|
it('should append filename to the url', async () => {
|
||||||
|
const options = {
|
||||||
|
config: {
|
||||||
|
publicKey: TEST_PUBLIC_KEY,
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
autoFilename: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const url = generateMockUrl();
|
||||||
|
const mockResult = { cdnUrl: url, isImage: true, name: 'test.png' };
|
||||||
|
const integration = await uploadcareMediaLibrary.init({
|
||||||
|
options,
|
||||||
|
handleInsert,
|
||||||
|
});
|
||||||
|
await integration.show();
|
||||||
|
await simulateCloseDialog(mockResult);
|
||||||
|
expect(handleInsert).toHaveBeenCalledWith(url + 'test.png');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('enableStandalone method', () => {
|
describe('enableStandalone method', () => {
|
||||||
it('returns false', async () => {
|
it('returns false', async () => {
|
||||||
const integration = await uploadcareMediaLibrary.init();
|
const integration = await uploadcareMediaLibrary.init();
|
||||||
|
@ -73,24 +73,47 @@ function getFile(url) {
|
|||||||
* Open the standalone dialog. A single instance is created and destroyed for
|
* Open the standalone dialog. A single instance is created and destroyed for
|
||||||
* each use.
|
* each use.
|
||||||
*/
|
*/
|
||||||
function openDialog(files, config, handleInsert) {
|
function openDialog({ files, config, handleInsert, settings = {} }) {
|
||||||
uploadcare.openDialog(files, config).done(({ promise }) =>
|
if (settings.defaultOperations && !settings.defaultOperations.startsWith('/')) {
|
||||||
promise().then(({ cdnUrl, count }) => {
|
console.warn(
|
||||||
if (config.multiple) {
|
'Uploadcare default operations should start with `/`. Example: `/preview/-/resize/100x100/image.png`',
|
||||||
const urls = Array.from({ length: count }, (val, idx) => `${cdnUrl}nth/${idx}/`);
|
);
|
||||||
handleInsert(urls);
|
}
|
||||||
|
|
||||||
|
const buildUrl = fileInfo => {
|
||||||
|
const { cdnUrl, name, isImage } = fileInfo;
|
||||||
|
|
||||||
|
let url =
|
||||||
|
isImage && settings.defaultOperations ? `${cdnUrl}-${settings.defaultOperations}` : cdnUrl;
|
||||||
|
const filenameDefined = !url.endsWith('/');
|
||||||
|
|
||||||
|
if (!filenameDefined && settings.autoFilename) {
|
||||||
|
url = url + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
|
uploadcare.openDialog(files, config).done(({ promise, files }) => {
|
||||||
|
const isGroup = Boolean(files);
|
||||||
|
|
||||||
|
return promise().then(info => {
|
||||||
|
if (isGroup) {
|
||||||
|
return Promise.all(
|
||||||
|
files().map(promise => promise.then(fileInfo => buildUrl(fileInfo))),
|
||||||
|
).then(urls => handleInsert(urls));
|
||||||
} else {
|
} else {
|
||||||
handleInsert(cdnUrl);
|
handleInsert(buildUrl(info));
|
||||||
}
|
}
|
||||||
}),
|
});
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialization function will only run once, returns an API object for Netlify
|
* Initialization function will only run once, returns an API object for Netlify
|
||||||
* CMS to call methods on.
|
* CMS to call methods on.
|
||||||
*/
|
*/
|
||||||
async function init({ options = { config: {} }, handleInsert } = {}) {
|
async function init({ options = { config: {}, settings: {} }, handleInsert } = {}) {
|
||||||
const { publicKey, ...globalConfig } = options.config;
|
const { publicKey, ...globalConfig } = options.config;
|
||||||
const baseConfig = { ...defaultConfig, ...globalConfig };
|
const baseConfig = { ...defaultConfig, ...globalConfig };
|
||||||
|
|
||||||
@ -118,9 +141,21 @@ async function init({ options = { config: {} }, handleInsert } = {}) {
|
|||||||
* from the Uploadcare library will have a `state` method.
|
* from the Uploadcare library will have a `state` method.
|
||||||
*/
|
*/
|
||||||
if (files && !files.state) {
|
if (files && !files.state) {
|
||||||
return files.then(result => openDialog(result, resolvedConfig, handleInsert));
|
return files.then(result =>
|
||||||
|
openDialog({
|
||||||
|
files: result,
|
||||||
|
config: resolvedConfig,
|
||||||
|
settings: options.settings,
|
||||||
|
handleInsert,
|
||||||
|
}),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return openDialog(files, resolvedConfig, handleInsert);
|
return openDialog({
|
||||||
|
files,
|
||||||
|
config: resolvedConfig,
|
||||||
|
settings: options.settings,
|
||||||
|
handleInsert,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -78,3 +78,25 @@ For example:
|
|||||||
multiple: true
|
multiple: true
|
||||||
previewStep: false
|
previewStep: false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Integration settings
|
||||||
|
|
||||||
|
There are several settings that control the behavior of integration with the widget.
|
||||||
|
|
||||||
|
* `autoFilename` (`boolean`) - specify whether to add a filename to the end of the url.
|
||||||
|
Example: `http://ucarecdn.com/:uuid/filename.png`
|
||||||
|
* `defaultOperations` (`string`) - specify a string added at the end of the url.
|
||||||
|
This could be useful to apply a set of CDN operations to each image,
|
||||||
|
for example resizing or compression. All the possible operations are listed
|
||||||
|
[here](https://uploadcare.com/docs/api_reference/cdn/).
|
||||||
|
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
media_library:
|
||||||
|
name: uploadcare
|
||||||
|
config:
|
||||||
|
publicKey: demopublickey
|
||||||
|
settings:
|
||||||
|
autoFilename: true
|
||||||
|
defaultOperations: '/resize/800x600/'
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user