From 4d3d3ea842ea212096a6d9fd36cf795a8d5aea12 Mon Sep 17 00:00:00 2001 From: maoznir Date: Fri, 26 Jun 2020 15:21:46 +0300 Subject: [PATCH 1/8] Refactor responsive image --- package.json | 2 +- src/Util/Util.js | 6 +- src/Util/closestAbove.js | 10 - src/Util/cloudinaryCoreUtils.js | 54 +++++ src/Util/debounce.js | 36 --- src/Util/firstDefined.js | 12 - src/Util/isElement.js | 28 --- src/Util/requestAnimationFrame.js | 37 --- .../CloudinaryComponent.js | 87 ++++--- src/components/Image/Image.js | 212 ++---------------- .../Transformation/Transformation.js | 1 + test/ContextTest.js | 12 +- test/ImageTest.js | 105 +++------ test/TransformationTest.js | 33 +-- test/Util.js | 31 --- 15 files changed, 186 insertions(+), 480 deletions(-) delete mode 100644 src/Util/closestAbove.js create mode 100644 src/Util/cloudinaryCoreUtils.js delete mode 100644 src/Util/debounce.js delete mode 100644 src/Util/firstDefined.js delete mode 100644 src/Util/isElement.js delete mode 100644 src/Util/requestAnimationFrame.js delete mode 100644 test/Util.js diff --git a/package.json b/package.json index 3d33c78..c6547a0 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test-dist": "TEST_SUBJECT=dist node_modules/.bin/mocha --require @babel/register test/.setup.js --recursive test", "test-lib": "TEST_SUBJECT=lib node_modules/.bin/mocha --require @babel/register test/.setup.js --recursive test", "prebuild": "node_modules/.bin/babel src --out-dir lib --copy-files ", - "build": "node_modules/.bin/webpack && npm run build-storybook && npm run bundlewatch", + "build": "node_modules/.bin/webpack && npm run bundlewatch", "bundlewatch": "bundlewatch --config ./bundlewatch.config.js", "storybook": "start-storybook -p 6006", "build-storybook": "del-cli docs && build-storybook -c .storybook -o docs" diff --git a/src/Util/Util.js b/src/Util/Util.js index 3c752c4..6ffd822 100644 --- a/src/Util/Util.js +++ b/src/Util/Util.js @@ -1,6 +1,2 @@ -export debounce from './debounce'; -export firstDefined from './firstDefined'; -export closestAbove from './closestAbove'; -export {requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame'; -export isElement from './isElement'; export extractCloudinaryProps from './extractCloudinaryProps'; +export * from './cloudinaryCoreUtils'; diff --git a/src/Util/closestAbove.js b/src/Util/closestAbove.js deleted file mode 100644 index 2b90439..0000000 --- a/src/Util/closestAbove.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Returns the first item in list that is greater or equal to the given value. - * @param {Array} list a sorted array of items - * @param {*} value - * @returns {*} - * @protected - */ -export default function closestAbove(list, value) { - return list.reduce((last, next) => value <= last ? last : next, undefined); -} diff --git a/src/Util/cloudinaryCoreUtils.js b/src/Util/cloudinaryCoreUtils.js new file mode 100644 index 0000000..5ac8e1b --- /dev/null +++ b/src/Util/cloudinaryCoreUtils.js @@ -0,0 +1,54 @@ +import {Cloudinary, Util} from "cloudinary-core"; + +/** + * Return object without null/undefined entries + * @param {*} obj + */ +const nonEmpty = (obj) => Object.entries(obj).reduce((a, [k, v]) => (v == null ? a : { ...a, [k]: v }), {}); + +/** + * Generated a configured Cloudinary object. + * @param extendedProps React props combined with custom Cloudinary configuration options + * @return {Cloudinary} configured using extendedProps + */ +const getConfiguredCloudinary = (extendedProps) => { + const { public_id, ...ops } = nonEmpty(extendedProps); // Remove null/undefined props + const options = Util.extractUrlParams(Util.withSnakeCaseKeys(ops)); + return Cloudinary.new(options); +}; + +const getTag = (props, tagType) => { + const { publicId, ...ops} = props; // Remove null/undefined props + const cld = getConfiguredCloudinary(props); + return cld[`${tagType}Tag`](publicId, ops); +}; + +/** + * Get a new tag initialized with given props + * @param {*} props + */ +const getImageTag = (props) => getTag(props, "image"); + +/** + * Get a new