add-cms
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
## [3.0.2](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@3.0.1...decap-cms-media-library-uploadcare@3.0.2) (2024-03-21)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [3.0.1](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@3.0.1-beta.0...decap-cms-media-library-uploadcare@3.0.1) (2024-02-01)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [3.0.1-beta.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@3.0.0...decap-cms-media-library-uploadcare@3.0.1-beta.0) (2024-01-31)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
# [3.0.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@0.6.0...decap-cms-media-library-uploadcare@3.0.0) (2023-08-18)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
# [0.6.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@0.6.0-beta.0...decap-cms-media-library-uploadcare@0.6.0) (2023-08-18)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
# 0.6.0-beta.0 (2023-08-18)
|
||||
|
||||
### Features
|
||||
|
||||
- rename packages ([#6863](https://github.com/decaporg/decap-cms/issues/6863)) ([d515e7b](https://github.com/decaporg/decap-cms/commit/d515e7bd33216a775d96887b08c4f7b1962941bb))
|
||||
|
||||
## [0.5.11-beta.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-uploadcare@0.5.10...decap-cms-media-library-uploadcare@0.5.11-beta.0) (2023-07-27)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.10](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.9...decap-cms-media-library-uploadcare@0.5.10) (2021-02-10)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.9](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.8...decap-cms-media-library-uploadcare@0.5.9) (2020-09-15)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## 0.5.8 (2020-09-08)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([828bb16](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/828bb16415b8c22a34caa19c50c38b24ffe9ceae))
|
||||
|
||||
## 0.5.7 (2020-08-20)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([8262487](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/82624879ccbcb16610090041db28f00714d924c8))
|
||||
|
||||
## 0.5.6 (2020-07-27)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([118d50a](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/118d50a7a70295f25073e564b5161aa2b9883056))
|
||||
|
||||
## [0.5.5](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.4...decap-cms-media-library-uploadcare@0.5.5) (2019-11-07)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.4](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.3...decap-cms-media-library-uploadcare@0.5.4) (2019-09-26)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.3](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.2...decap-cms-media-library-uploadcare@0.5.3) (2019-07-24)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.2](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.2-beta.0...decap-cms-media-library-uploadcare@0.5.2) (2019-04-10)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.2-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.1...decap-cms-media-library-uploadcare@0.5.2-beta.0) (2019-04-05)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.1-beta.1...decap-cms-media-library-uploadcare@0.5.1) (2019-03-29)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.5.1-beta.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.1-beta.0...decap-cms-media-library-uploadcare@0.5.1-beta.1) (2019-03-26)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- export on decap-cms and maps on esm ([#2244](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/2244)) ([6ffd13b](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/6ffd13b))
|
||||
|
||||
## [0.5.1-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.5.0...decap-cms-media-library-uploadcare@0.5.1-beta.0) (2019-03-25)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
# [0.5.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.4.0...decap-cms-media-library-uploadcare@0.5.0) (2019-03-22)
|
||||
|
||||
### Features
|
||||
|
||||
- add ES module builds ([#2215](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/2215)) ([d142b32](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/d142b32))
|
||||
|
||||
# [0.4.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.4.0-beta.0...decap-cms-media-library-uploadcare@0.4.0) (2019-03-22)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
# [0.4.0-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.5-beta.0...decap-cms-media-library-uploadcare@0.4.0-beta.0) (2019-03-21)
|
||||
|
||||
### Features
|
||||
|
||||
- provide usable UMD builds for all packages ([#2141](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/2141)) ([82cc794](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/82cc794))
|
||||
|
||||
## [0.3.5-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.4...decap-cms-media-library-uploadcare@0.3.5-beta.0) (2019-03-15)
|
||||
|
||||
### Features
|
||||
|
||||
- upgrade to Emotion 10 ([#2166](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/2166)) ([ccef446](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/ccef446))
|
||||
|
||||
## [0.3.4](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.3...decap-cms-media-library-uploadcare@0.3.4) (2019-02-26)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.3.3](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.2...decap-cms-media-library-uploadcare@0.3.3) (2019-02-09)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.3.2](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.1...decap-cms-media-library-uploadcare@0.3.2) (2019-02-08)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-uploadcare
|
||||
|
||||
## [0.3.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.3.0...decap-cms-media-library-uploadcare@0.3.1) (2018-12-11)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **media-library-uploadcare:** fix bugs, add tests ([#1953](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/1953)) ([716ee62](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/716ee62))
|
||||
|
||||
# [0.3.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.2.1...decap-cms-media-library-uploadcare@0.3.0) (2018-12-04)
|
||||
|
||||
### Features
|
||||
|
||||
- add cloudinary support ([#1932](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/1932)) ([1fc2f50](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/1fc2f50))
|
||||
|
||||
## [0.2.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/compare/decap-cms-media-library-uploadcare@0.2.0...decap-cms-media-library-uploadcare@0.2.1) (2018-11-12)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **uploadcare:** allow to be Uploadcare to be used in list widget ([#1774](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/1774)) ([deaac3d](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/deaac3d))
|
||||
|
||||
<a name="0.2.0"></a>
|
||||
|
||||
# 0.2.0 (2018-09-06)
|
||||
|
||||
### Features
|
||||
|
||||
- **media:** add external media library support, Uploadcare integration ([#1602](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/1602)) ([0596904](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/0596904))
|
||||
|
||||
<a name="0.2.0"></a>
|
||||
|
||||
# 0.2.0 (2018-09-06)
|
||||
|
||||
### Features
|
||||
|
||||
- **media:** add external media library support, Uploadcare integration ([#1602](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/issues/1602)) ([0596904](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare/commit/0596904))
|
||||
@@ -0,0 +1,9 @@
|
||||
# Docs coming soon!
|
||||
|
||||
Decap CMS was converted from a single npm package to a "monorepo" of over 20 packages.
|
||||
We haven't created a README for this package yet, but you can:
|
||||
|
||||
1. Check out the [main readme](https://github.com/decaporg/decap-cms/#readme) or the [documentation
|
||||
site](https://www.decapcms.org) for more info.
|
||||
2. Reach out to the [community chat](https://decapcms.org/chat/) if you need help.
|
||||
3. Help out and [write the readme yourself](https://github.com/decaporg/decap-cms/edit/main/packages/decap-cms-media-library-uploadcare/README.md)!
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "decap-cms-media-library-uploadcare",
|
||||
"description": "Uploadcare integration for Decap CMS",
|
||||
"version": "3.0.2",
|
||||
"repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-uploadcare",
|
||||
"bugs": "https://github.com/decaporg/decap-cms/issues",
|
||||
"module": "dist/esm/index.js",
|
||||
"main": "dist/decap-cms-media-library-uploadcare.js",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"decap-cms",
|
||||
"uploadcare",
|
||||
"media",
|
||||
"assets",
|
||||
"files",
|
||||
"uploads"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"scripts": {
|
||||
"develop": "npm run build:esm -- --watch",
|
||||
"build": "cross-env NODE_ENV=production webpack",
|
||||
"build:esm": "cross-env NODE_ENV=esm babel src --out-dir dist/esm --ignore \"**/__tests__\" --root-mode upward"
|
||||
},
|
||||
"dependencies": {
|
||||
"uploadcare-widget": "^3.7.0",
|
||||
"uploadcare-widget-tab-effects": "^1.4.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import uploadcare from 'uploadcare-widget';
|
||||
import uploadcareTabEffects from 'uploadcare-widget-tab-effects';
|
||||
|
||||
import uploadcareMediaLibrary from '../index';
|
||||
|
||||
function generateMockUrl({ count = 1, cdnUrl } = {}) {
|
||||
const baseUrl = 'https://ucarecdn.com';
|
||||
const url = `${baseUrl}/${uuid()}~${count}/`;
|
||||
const result =
|
||||
count === 1 ? `${url}nth/0/` : Array.from({ length: count }, (val, idx) => `${url}nth/${idx}/`);
|
||||
if (cdnUrl) {
|
||||
return { result, cdnUrl: url };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
let openDialogCallback;
|
||||
|
||||
/**
|
||||
* Mock of the uploadcare widget object itself.
|
||||
*/
|
||||
jest.mock('uploadcare-widget', () => ({
|
||||
registerTab: jest.fn(),
|
||||
openDialog: jest.fn(() => ({
|
||||
done: jest.fn(cb => {
|
||||
openDialogCallback = cb;
|
||||
}),
|
||||
})),
|
||||
fileFrom: jest.fn((type, url) =>
|
||||
Promise.resolve({
|
||||
testFileUrl: url,
|
||||
}),
|
||||
),
|
||||
loadFileGroup: () => ({
|
||||
done: cb => cb(),
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('uploadcare media library', () => {
|
||||
let handleInsert;
|
||||
let simulateCloseDialog;
|
||||
const TEST_PUBLIC_KEY = 123;
|
||||
const defaultConfig = {
|
||||
imagesOnly: false,
|
||||
multiple: false,
|
||||
previewStep: true,
|
||||
integration: 'DecapCMS-Uploadcare-MediaLibrary',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
/**
|
||||
* Mock to manually call the close dialog registered callback.
|
||||
*/
|
||||
simulateCloseDialog = (result, files) =>
|
||||
openDialogCallback({
|
||||
promise: () => Promise.resolve(result),
|
||||
...(files ? { files: () => files.map(file => Promise.resolve(file)) } : {}),
|
||||
});
|
||||
|
||||
/**
|
||||
* Spy to serve as the Decap CMS insertion handler.
|
||||
*/
|
||||
handleInsert = jest.fn();
|
||||
});
|
||||
|
||||
it('exports an object with expected properties', () => {
|
||||
expect(uploadcareMediaLibrary).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"init": [Function],
|
||||
"name": "uploadcare",
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
describe('initialization', () => {
|
||||
it('sets global required configuration', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
publicKey: TEST_PUBLIC_KEY,
|
||||
},
|
||||
};
|
||||
await uploadcareMediaLibrary.init({ options });
|
||||
expect(window.UPLOADCARE_LIVE).toEqual(false);
|
||||
expect(window.UPLOADCARE_MANUAL_START).toEqual(true);
|
||||
expect(window.UPLOADCARE_PUBLIC_KEY).toEqual(TEST_PUBLIC_KEY);
|
||||
});
|
||||
|
||||
it('registers the effects tab', async () => {
|
||||
await uploadcareMediaLibrary.init();
|
||||
expect(uploadcare.registerTab).toHaveBeenCalledWith('preview', uploadcareTabEffects);
|
||||
});
|
||||
});
|
||||
|
||||
describe('widget configuration', () => {
|
||||
const options = {
|
||||
config: {
|
||||
foo: 'bar',
|
||||
},
|
||||
};
|
||||
|
||||
it('has defaults', async () => {
|
||||
const integration = await uploadcareMediaLibrary.init();
|
||||
await integration.show();
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, defaultConfig);
|
||||
});
|
||||
|
||||
it('can be defined globally', async () => {
|
||||
const expectedConfig = {
|
||||
...defaultConfig,
|
||||
...options.config,
|
||||
};
|
||||
const integration = await uploadcareMediaLibrary.init({ options });
|
||||
await integration.show();
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
||||
});
|
||||
|
||||
it('can be defined per field', async () => {
|
||||
const expectedConfig = {
|
||||
...defaultConfig,
|
||||
...options.config,
|
||||
};
|
||||
const integration = await uploadcareMediaLibrary.init();
|
||||
await integration.show({ config: options.config });
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
||||
});
|
||||
});
|
||||
|
||||
describe('show method', () => {
|
||||
const options = {
|
||||
config: {
|
||||
foo: 'bar',
|
||||
},
|
||||
};
|
||||
|
||||
it('accepts imagesOnly as standalone property', async () => {
|
||||
const expectedConfig = {
|
||||
...defaultConfig,
|
||||
...options.config,
|
||||
imagesOnly: true,
|
||||
};
|
||||
const integration = await uploadcareMediaLibrary.init();
|
||||
await integration.show({ config: options.config, imagesOnly: true });
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
||||
});
|
||||
|
||||
it('allows multiple selection if allowMultiple is not false', async () => {
|
||||
options.config.multiple = true;
|
||||
const expectedConfig = {
|
||||
...defaultConfig,
|
||||
...options.config,
|
||||
multiple: true,
|
||||
};
|
||||
const integration = await uploadcareMediaLibrary.init({ options });
|
||||
await integration.show({ config: options.config });
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
||||
});
|
||||
|
||||
it('disallows multiple selection if allowMultiple is false', async () => {
|
||||
options.config.multiple = true;
|
||||
const expectedConfig = {
|
||||
...defaultConfig,
|
||||
...options.config,
|
||||
multiple: false,
|
||||
};
|
||||
const integration = await uploadcareMediaLibrary.init({ options });
|
||||
await integration.show({ config: options.config, allowMultiple: false });
|
||||
expect(uploadcare.openDialog).toHaveBeenCalledWith(null, expectedConfig);
|
||||
});
|
||||
|
||||
it('passes selected image url to handleInsert', async () => {
|
||||
const url = generateMockUrl();
|
||||
const mockResult = { cdnUrl: url };
|
||||
const integration = await uploadcareMediaLibrary.init({ handleInsert });
|
||||
await integration.show();
|
||||
await simulateCloseDialog(mockResult);
|
||||
expect(handleInsert).toHaveBeenCalledWith(url);
|
||||
});
|
||||
|
||||
it('passes multiple selected image urls to handleInsert', async () => {
|
||||
options.config.multiple = true;
|
||||
const { result, cdnUrl } = generateMockUrl({ count: 3, cdnUrl: true });
|
||||
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 });
|
||||
await integration.show();
|
||||
await simulateCloseDialog(mockDialogCloseResult, mockDialogCloseFiles);
|
||||
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', () => {
|
||||
it('returns false', async () => {
|
||||
const integration = await uploadcareMediaLibrary.init();
|
||||
expect(integration.enableStandalone()).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
import uploadcare from 'uploadcare-widget';
|
||||
import uploadcareTabEffects from 'uploadcare-widget-tab-effects';
|
||||
import { Iterable } from 'immutable';
|
||||
|
||||
window.UPLOADCARE_LIVE = false;
|
||||
window.UPLOADCARE_MANUAL_START = true;
|
||||
|
||||
const USER_AGENT = 'DecapCMS-Uploadcare-MediaLibrary';
|
||||
const CDN_BASE_URL = 'https://ucarecdn.com';
|
||||
|
||||
/**
|
||||
* Default Uploadcare widget configuration, can be overridden via config.yml.
|
||||
*/
|
||||
const defaultConfig = {
|
||||
previewStep: true,
|
||||
integration: USER_AGENT,
|
||||
};
|
||||
|
||||
/**
|
||||
* Determine whether an array of urls represents an unaltered set of Uploadcare
|
||||
* group urls. If they've been changed or any are missing, a new group will need
|
||||
* to be created to represent the current values.
|
||||
*/
|
||||
function isFileGroup(files) {
|
||||
const basePatternString = `~${files.length}/nth/`;
|
||||
|
||||
function mapExpression(val, idx) {
|
||||
return new RegExp(`${basePatternString}${idx}/$`);
|
||||
}
|
||||
|
||||
const expressions = Array.from({ length: files.length }, mapExpression);
|
||||
return expressions.every(exp => files.some(url => exp.test(url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fileGroupInfo object wrapped in a promise-like object.
|
||||
*/
|
||||
function getFileGroup(files) {
|
||||
/**
|
||||
* Capture the group id from the first file in the files array.
|
||||
*/
|
||||
const groupId = new RegExp(`^.+/([^/]+~${files.length})/nth/`).exec(files[0])[1];
|
||||
|
||||
/**
|
||||
* The `openDialog` method handles the jQuery promise object returned by
|
||||
* `fileFrom`, but requires the promise returned by `loadFileGroup` to provide
|
||||
* the result of it's `done` method.
|
||||
*/
|
||||
return new Promise(resolve => uploadcare.loadFileGroup(groupId).done(group => resolve(group)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a url or array/List of urls to Uploadcare file objects wrapped in
|
||||
* promises, or Uploadcare groups when possible. Output is wrapped in a promise
|
||||
* because the value we're returning may be a promise that we created.
|
||||
*/
|
||||
function getFiles(value) {
|
||||
if (Array.isArray(value) || Iterable.isIterable(value)) {
|
||||
const arr = Array.isArray(value) ? value : value.toJS();
|
||||
return isFileGroup(arr) ? getFileGroup(arr) : Promise.all(arr.map(val => getFile(val)));
|
||||
}
|
||||
return value && typeof value === 'string' ? getFile(value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a single url to an Uploadcare file object wrapped in a promise-like
|
||||
* object. Group urls that get passed here were not a part of a complete and
|
||||
* untouched group, so they'll be uploaded as new images (only way to do it).
|
||||
*/
|
||||
function getFile(url) {
|
||||
const groupPattern = /~\d+\/nth\/\d+\//;
|
||||
const uploaded = url.startsWith(CDN_BASE_URL) && !groupPattern.test(url);
|
||||
return uploadcare.fileFrom(uploaded ? 'uploaded' : 'url', url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the standalone dialog. A single instance is created and destroyed for
|
||||
* each use.
|
||||
*/
|
||||
function openDialog({ files, config, handleInsert, settings = {} }) {
|
||||
if (settings.defaultOperations && !settings.defaultOperations.startsWith('/')) {
|
||||
console.warn(
|
||||
'Uploadcare default operations should start with `/`. Example: `/preview/-/resize/100x100/image.png`',
|
||||
);
|
||||
}
|
||||
|
||||
function 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 {
|
||||
handleInsert(buildUrl(info));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialization function will only run once, returns an API object for Decap
|
||||
* CMS to call methods on.
|
||||
*/
|
||||
async function init({ options = { config: {}, settings: {} }, handleInsert } = {}) {
|
||||
const { publicKey, ...globalConfig } = options.config;
|
||||
const baseConfig = { ...defaultConfig, ...globalConfig };
|
||||
|
||||
window.UPLOADCARE_PUBLIC_KEY = publicKey;
|
||||
|
||||
/**
|
||||
* Register the effects tab by default because the effects tab is awesome. Can
|
||||
* be disabled via config.
|
||||
*/
|
||||
uploadcare.registerTab('preview', uploadcareTabEffects);
|
||||
|
||||
return {
|
||||
/**
|
||||
* On show, create a new widget, cache it in the widgets object, and open.
|
||||
* No hide method is provided because the widget doesn't provide it.
|
||||
*/
|
||||
show: ({ value, config: instanceConfig = {}, allowMultiple, imagesOnly = false } = {}) => {
|
||||
const config = { ...baseConfig, imagesOnly, ...instanceConfig };
|
||||
const multiple = allowMultiple === false ? false : !!config.multiple;
|
||||
const resolvedConfig = { ...config, multiple };
|
||||
const files = getFiles(value);
|
||||
|
||||
/**
|
||||
* Resolve the promise only if it's ours. Only the jQuery promise objects
|
||||
* from the Uploadcare library will have a `state` method.
|
||||
*/
|
||||
if (files && !files.state) {
|
||||
return files.then(result =>
|
||||
openDialog({
|
||||
files: result,
|
||||
config: resolvedConfig,
|
||||
settings: options.settings,
|
||||
handleInsert,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
return openDialog({
|
||||
files,
|
||||
config: resolvedConfig,
|
||||
settings: options.settings,
|
||||
handleInsert,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Uploadcare doesn't provide a "media library" widget for viewing and
|
||||
* selecting existing files, so we return `false` here so Decap CMS only
|
||||
* opens the Uploadcare widget when called from an editor control. This
|
||||
* results in the "Media" button in the global nav being hidden.
|
||||
*/
|
||||
enableStandalone: () => false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The object that will be registered only needs a (default) name and `init`
|
||||
* method. The `init` method returns the API object.
|
||||
*/
|
||||
const uploadcareMediaLibrary = { name: 'uploadcare', init };
|
||||
|
||||
export const DecapCmsMediaLibraryUploadcare = uploadcareMediaLibrary;
|
||||
export default uploadcareMediaLibrary;
|
||||
@@ -0,0 +1,3 @@
|
||||
const { getConfig } = require('../../scripts/webpack.js');
|
||||
|
||||
module.exports = getConfig();
|
||||
Reference in New Issue
Block a user