add-cms
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [3.1.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@3.0.3...decap-cms-media-library-cloudinary@3.1.0) (2025-06-26)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [3.0.3](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@3.0.2...decap-cms-media-library-cloudinary@3.0.3) (2024-03-21)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [3.0.2](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@3.0.2-beta.0...decap-cms-media-library-cloudinary@3.0.2) (2024-02-01)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [3.0.2-beta.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@3.0.1...decap-cms-media-library-cloudinary@3.0.2-beta.0) (2024-01-31)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [3.0.1](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@3.0.0...decap-cms-media-library-cloudinary@3.0.1) (2023-08-25)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- update peer dependencies ([#6886](https://github.com/decaporg/decap-cms/issues/6886)) ([e580ce5](https://github.com/decaporg/decap-cms/commit/e580ce52ce5f80fa040e8fbcab7fed0744f4f695))
|
||||
|
||||
# [3.0.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@1.4.0...decap-cms-media-library-cloudinary@3.0.0) (2023-08-18)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
# [1.4.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@1.4.0-beta.0...decap-cms-media-library-cloudinary@1.4.0) (2023-08-18)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
# 1.4.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))
|
||||
|
||||
## [1.3.11-beta.0](https://github.com/decaporg/decap-cms/compare/decap-cms-media-library-cloudinary@1.3.10...decap-cms-media-library-cloudinary@1.3.11-beta.0) (2023-07-27)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.10](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.9...decap-cms-media-library-cloudinary@1.3.10) (2021-02-23)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.9](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.8...decap-cms-media-library-cloudinary@1.3.9) (2021-02-10)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.8](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.7...decap-cms-media-library-cloudinary@1.3.8) (2020-09-15)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## 1.3.7 (2020-09-08)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([828bb16](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/828bb16415b8c22a34caa19c50c38b24ffe9ceae))
|
||||
|
||||
## 1.3.6 (2020-08-20)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([8262487](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/82624879ccbcb16610090041db28f00714d924c8))
|
||||
|
||||
## 1.3.5 (2020-07-27)
|
||||
|
||||
### Reverts
|
||||
|
||||
- Revert "chore(release): publish" ([118d50a](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/118d50a7a70295f25073e564b5161aa2b9883056))
|
||||
|
||||
## [1.3.4](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.3...decap-cms-media-library-cloudinary@1.3.4) (2019-09-26)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **media-library-cloudinary:** when 'multiple' is true, always return an array ([#2656](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/2656)) ([4c32ee4](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/4c32ee4))
|
||||
|
||||
## [1.3.3](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.2...decap-cms-media-library-cloudinary@1.3.3) (2019-07-24)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.2](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.2-beta.0...decap-cms-media-library-cloudinary@1.3.2) (2019-04-10)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.2-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.1...decap-cms-media-library-cloudinary@1.3.2-beta.0) (2019-04-05)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.1-beta.1...decap-cms-media-library-cloudinary@1.3.1) (2019-03-29)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
## [1.3.1-beta.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.1-beta.0...decap-cms-media-library-cloudinary@1.3.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-cloudinary/issues/2244)) ([6ffd13b](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/6ffd13b))
|
||||
|
||||
## [1.3.1-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.3.0...decap-cms-media-library-cloudinary@1.3.1-beta.0) (2019-03-25)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
# [1.3.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.2.0...decap-cms-media-library-cloudinary@1.3.0) (2019-03-22)
|
||||
|
||||
### Features
|
||||
|
||||
- add ES module builds ([#2215](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/2215)) ([d142b32](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/d142b32))
|
||||
|
||||
# [1.2.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.2.0-beta.0...decap-cms-media-library-cloudinary@1.2.0) (2019-03-22)
|
||||
|
||||
**Note:** Version bump only for package decap-cms-media-library-cloudinary
|
||||
|
||||
# [1.2.0-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.1.3-beta.0...decap-cms-media-library-cloudinary@1.2.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-cloudinary/issues/2141)) ([82cc794](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/82cc794))
|
||||
|
||||
## [1.1.3-beta.0](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.1.2...decap-cms-media-library-cloudinary@1.1.3-beta.0) (2019-03-15)
|
||||
|
||||
### Features
|
||||
|
||||
- upgrade to Emotion 10 ([#2166](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/2166)) ([ccef446](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/ccef446))
|
||||
|
||||
## [1.1.2](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.1.1...decap-cms-media-library-cloudinary@1.1.2) (2019-03-11)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **cloudinary:** allow multiple asset selection per field ([#2157](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/2157)) ([e297075](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/e297075))
|
||||
- **cloudinary:** update config object shape ([#2044](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/2044)) ([b42a9c6](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/b42a9c6))
|
||||
|
||||
## [1.1.1](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/compare/decap-cms-media-library-cloudinary@1.1.0...decap-cms-media-library-cloudinary@1.1.1) (2018-12-11)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **media-library-cloudinary:** fix options, add tests ([#1938](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/1938)) ([443f060](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/443f060))
|
||||
- **media-library-uploadcare:** fix bugs, add tests ([#1953](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/1953)) ([716ee62](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/716ee62))
|
||||
|
||||
# 1.1.0 (2018-12-04)
|
||||
|
||||
### Features
|
||||
|
||||
- add cloudinary support ([#1932](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/issues/1932)) ([1fc2f50](https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary/commit/1fc2f50))
|
||||
@@ -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-cloudinary/README.md)!
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "decap-cms-media-library-cloudinary",
|
||||
"description": "Cloudinary integration for Decap CMS",
|
||||
"version": "3.1.0",
|
||||
"repository": "https://github.com/decaporg/decap-cms/tree/main/packages/decap-cms-media-library-cloudinary",
|
||||
"bugs": "https://github.com/decaporg/decap-cms/issues",
|
||||
"module": "dist/esm/index.js",
|
||||
"main": "dist/decap-cms-media-library-cloudinary.js",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"decap-cms",
|
||||
"cloudinary",
|
||||
"image",
|
||||
"images",
|
||||
"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"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"decap-cms-lib-util": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
import cloudinary from '../index';
|
||||
|
||||
jest.mock('decap-cms-lib-util');
|
||||
|
||||
describe('cloudinary exports', () => {
|
||||
it('exports an object with expected properties', () => {
|
||||
expect(cloudinary).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"init": [Function],
|
||||
"name": "cloudinary",
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cloudinary media library', () => {
|
||||
let mediaLibrary;
|
||||
let cloudinaryConfig;
|
||||
let cloudinaryInsertHandler;
|
||||
|
||||
beforeEach(() => {
|
||||
/**
|
||||
* Mock of the Cloudinary library itself, which is otherwise created by
|
||||
* their script (which isn't actually run during testing).
|
||||
*/
|
||||
window.cloudinary = {
|
||||
createMediaLibrary: (config, { insertHandler }) => {
|
||||
cloudinaryConfig = config;
|
||||
cloudinaryInsertHandler = insertHandler;
|
||||
return mediaLibrary;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Mock of the object returned by the Cloudinary createMediaLibrary method.
|
||||
*/
|
||||
mediaLibrary = {
|
||||
show: jest.fn(),
|
||||
hide: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
const { loadScript } = require('decap-cms-lib-util');
|
||||
expect(loadScript).toHaveBeenCalledTimes(1);
|
||||
expect(loadScript).toHaveBeenCalledWith('https://media-library.cloudinary.com/global/all.js');
|
||||
});
|
||||
|
||||
describe('configuration', () => {
|
||||
const defaultCloudinaryConfig = {
|
||||
button_class: undefined,
|
||||
inline_container: undefined,
|
||||
insert_transformation: false,
|
||||
z_index: '99999',
|
||||
multiple: false,
|
||||
};
|
||||
|
||||
it('has defaults', async () => {
|
||||
await cloudinary.init();
|
||||
expect(cloudinaryConfig).toEqual(defaultCloudinaryConfig);
|
||||
});
|
||||
|
||||
it('does not allow enforced values to be overridden', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
button_class: 'foo',
|
||||
inline_container: 'foo',
|
||||
insert_transformation: 'foo',
|
||||
z_index: 0,
|
||||
},
|
||||
};
|
||||
await cloudinary.init({ options });
|
||||
expect(cloudinaryConfig).toEqual(defaultCloudinaryConfig);
|
||||
});
|
||||
|
||||
it('allows non-enforced defaults to be overridden', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
};
|
||||
await cloudinary.init({ options });
|
||||
expect(cloudinaryConfig).toEqual({ ...defaultCloudinaryConfig, ...options.config });
|
||||
});
|
||||
|
||||
it('allows unknown values', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
foo: 'bar',
|
||||
},
|
||||
};
|
||||
await cloudinary.init({ options });
|
||||
expect(cloudinaryConfig).toEqual({ ...defaultCloudinaryConfig, ...options.config });
|
||||
});
|
||||
});
|
||||
|
||||
describe('insertHandler', () => {
|
||||
let handleInsert;
|
||||
const asset = {
|
||||
url: 'http://foo.bar/image.jpg',
|
||||
secure_url: 'https://foo.bar/image.jpg',
|
||||
public_id: 'image',
|
||||
format: 'jpg',
|
||||
};
|
||||
const assetWithDerived = {
|
||||
...asset,
|
||||
derived: [
|
||||
{
|
||||
secure_url: 'https://derived.foo.bar/image.jpg',
|
||||
url: 'http://derived.foo.bar/image.jpg',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
handleInsert = jest.fn();
|
||||
});
|
||||
|
||||
it('calls insert function with single asset', async () => {
|
||||
await cloudinary.init({ handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(expect.any(String));
|
||||
});
|
||||
|
||||
it('calls insert function with multiple assets', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
};
|
||||
await cloudinary.init({ options, handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset, asset] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(expect.any(Array));
|
||||
});
|
||||
|
||||
it('calls insert function with array when only one asset is returned and config.multiple is true', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
};
|
||||
await cloudinary.init({ options, handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(expect.any(Array));
|
||||
});
|
||||
|
||||
it('calls insert function with secure url', async () => {
|
||||
await cloudinary.init({ handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(asset.secure_url);
|
||||
});
|
||||
|
||||
it('calls insert function with insecure url', async () => {
|
||||
const options = {
|
||||
use_secure_url: false,
|
||||
};
|
||||
await cloudinary.init({ options, handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(asset.url);
|
||||
});
|
||||
|
||||
it('supports derived assets', async () => {
|
||||
await cloudinary.init({ handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [assetWithDerived] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(assetWithDerived.derived[0].secure_url);
|
||||
});
|
||||
|
||||
it('ignores derived assets when use_transformations is false', async () => {
|
||||
const options = {
|
||||
use_transformations: false,
|
||||
};
|
||||
await cloudinary.init({ options, handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [assetWithDerived] });
|
||||
expect(handleInsert).toHaveBeenCalledWith(assetWithDerived.secure_url);
|
||||
});
|
||||
|
||||
it('supports outputting filename only', async () => {
|
||||
const options = {
|
||||
output_filename_only: true,
|
||||
};
|
||||
await cloudinary.init({ options, handleInsert });
|
||||
cloudinaryInsertHandler({ assets: [asset] });
|
||||
expect(handleInsert.mock.calls[0][0]).toMatchInlineSnapshot(`"image.jpg"`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('show method', () => {
|
||||
const defaultOptions = {
|
||||
config: {
|
||||
multiple: false,
|
||||
},
|
||||
};
|
||||
|
||||
it('calls cloudinary instance show method with default options', async () => {
|
||||
const integration = await cloudinary.init();
|
||||
integration.show();
|
||||
expect(mediaLibrary.show).toHaveBeenCalledWith(defaultOptions.config);
|
||||
});
|
||||
|
||||
it('accepts unknown configuration keys', async () => {
|
||||
const showOptions = {
|
||||
config: {
|
||||
...defaultOptions.config,
|
||||
foo: 'bar',
|
||||
},
|
||||
};
|
||||
const integration = await cloudinary.init();
|
||||
integration.show(showOptions);
|
||||
expect(mediaLibrary.show).toHaveBeenCalledWith(showOptions.config);
|
||||
});
|
||||
|
||||
it('receives global configuration for behavior only', async () => {
|
||||
const behaviorOptions = {
|
||||
default_transformations: [{ foo: 'bar' }],
|
||||
max_files: 2,
|
||||
multiple: true,
|
||||
};
|
||||
const nonBehaviorOptions = {
|
||||
api_key: 123,
|
||||
};
|
||||
const options = {
|
||||
config: {
|
||||
...behaviorOptions,
|
||||
...nonBehaviorOptions,
|
||||
},
|
||||
};
|
||||
const expectedOptions = {
|
||||
config: behaviorOptions,
|
||||
};
|
||||
const integration = await cloudinary.init({ options });
|
||||
integration.show();
|
||||
expect(mediaLibrary.show).toHaveBeenCalledWith(expectedOptions.config);
|
||||
});
|
||||
|
||||
it('allows global/default configuration to be overridden', async () => {
|
||||
const showOptions = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
};
|
||||
const integration = await cloudinary.init();
|
||||
integration.show(showOptions);
|
||||
expect(mediaLibrary.show).toHaveBeenCalledWith(showOptions.config);
|
||||
});
|
||||
|
||||
it('enforces multiple: false if allowMultiple is false', async () => {
|
||||
const options = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
};
|
||||
const showOptions = {
|
||||
config: {
|
||||
multiple: true,
|
||||
},
|
||||
allowMultiple: false,
|
||||
};
|
||||
const expectedOptions = {
|
||||
config: {
|
||||
multiple: false,
|
||||
},
|
||||
};
|
||||
const integration = await cloudinary.init(options);
|
||||
integration.show(showOptions);
|
||||
expect(mediaLibrary.show).toHaveBeenCalledWith(expectedOptions.config);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hide method', () => {
|
||||
it('calls cloudinary instance hide method', async () => {
|
||||
const integration = await cloudinary.init();
|
||||
integration.hide();
|
||||
expect(mediaLibrary.hide).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('enableStandalone method', () => {
|
||||
it('returns true', async () => {
|
||||
const integration = await cloudinary.init();
|
||||
expect(integration.enableStandalone()).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
import pick from 'lodash/pick';
|
||||
import { loadScript } from 'decap-cms-lib-util';
|
||||
|
||||
const defaultOptions = {
|
||||
use_secure_url: true,
|
||||
use_transformations: true,
|
||||
output_filename_only: false,
|
||||
};
|
||||
/**
|
||||
* This configuration hash cannot be overridden, as the values here are required
|
||||
* for the integration to work properly.
|
||||
*/
|
||||
const enforcedConfig = {
|
||||
button_class: undefined,
|
||||
inline_container: undefined,
|
||||
insert_transformation: false,
|
||||
z_index: '99999',
|
||||
};
|
||||
|
||||
const defaultConfig = {
|
||||
multiple: false,
|
||||
};
|
||||
|
||||
function getAssetUrl(asset, { use_secure_url, use_transformations, output_filename_only }) {
|
||||
/**
|
||||
* Allow output of the file name only, in which case the rest of the url (including)
|
||||
* transformations) can be handled by the static site generator.
|
||||
*/
|
||||
if (output_filename_only) {
|
||||
return `${asset.public_id}.${asset.format}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get url from `derived` property if it exists. This property contains the
|
||||
* transformed version of image if transformations have been applied.
|
||||
*/
|
||||
const urlObject = asset.derived && use_transformations ? asset.derived[0] : asset;
|
||||
|
||||
/**
|
||||
* Retrieve the `https` variant of the image url if the `useSecureUrl` option
|
||||
* is set to `true` (this is the default setting).
|
||||
*/
|
||||
const urlKey = use_secure_url ? 'secure_url' : 'url';
|
||||
|
||||
return urlObject[urlKey];
|
||||
}
|
||||
|
||||
async function init({ options = {}, handleInsert } = {}) {
|
||||
/**
|
||||
* Configuration is specific to Cloudinary, while options are specific to this
|
||||
* integration.
|
||||
*/
|
||||
const { config: providedConfig = {}, ...integrationOptions } = options;
|
||||
const resolvedOptions = { ...defaultOptions, ...integrationOptions };
|
||||
const cloudinaryConfig = { ...defaultConfig, ...providedConfig, ...enforcedConfig };
|
||||
const cloudinaryBehaviorConfigKeys = ['default_transformations', 'max_files', 'multiple'];
|
||||
const cloudinaryBehaviorConfig = pick(cloudinaryConfig, cloudinaryBehaviorConfigKeys);
|
||||
|
||||
await loadScript('https://media-library.cloudinary.com/global/all.js');
|
||||
|
||||
function insertHandler(data) {
|
||||
const assets = data.assets.map(asset => getAssetUrl(asset, resolvedOptions));
|
||||
handleInsert(providedConfig.multiple || assets.length > 1 ? assets : assets[0]);
|
||||
}
|
||||
|
||||
const mediaLibrary = window.cloudinary.createMediaLibrary(cloudinaryConfig, { insertHandler });
|
||||
|
||||
return {
|
||||
show: ({ config: instanceConfig = {}, allowMultiple } = {}) => {
|
||||
/**
|
||||
* Ensure multiple selection is not available if the field is configured
|
||||
* to disallow it.
|
||||
*/
|
||||
if (allowMultiple === false) {
|
||||
instanceConfig.multiple = false;
|
||||
}
|
||||
return mediaLibrary.show({ ...cloudinaryBehaviorConfig, ...instanceConfig });
|
||||
},
|
||||
hide: () => mediaLibrary.hide(),
|
||||
enableStandalone: () => true,
|
||||
};
|
||||
}
|
||||
|
||||
const cloudinaryMediaLibrary = { name: 'cloudinary', init };
|
||||
|
||||
export const DecapCmsMediaLibraryCloudinary = cloudinaryMediaLibrary;
|
||||
export default cloudinaryMediaLibrary;
|
||||
@@ -0,0 +1,3 @@
|
||||
const { getConfig } = require('../../scripts/webpack.js');
|
||||
|
||||
module.exports = getConfig();
|
||||
Reference in New Issue
Block a user