@rolster/react-components 18.23.7 → 18.23.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/index.js CHANGED
@@ -3310,6 +3310,8 @@ function useResize(props) {
3310
3310
  }, []);
3311
3311
  }
3312
3312
 
3313
+ const minValueSlider = 50;
3314
+ const maxValueSlider = 100;
3313
3315
  function getRatioFactor(ratio) {
3314
3316
  switch (ratio) {
3315
3317
  case '16:9':
@@ -3326,8 +3328,19 @@ function getRatioFactor(ratio) {
3326
3328
  return 1;
3327
3329
  }
3328
3330
  }
3329
- function simpleThreeRule(value1, value2, proportion) {
3330
- return (value2 * proportion) / value1;
3331
+ function simpleThreeRule(a1, b1, a2) {
3332
+ return (b1 * a2) / a1;
3333
+ }
3334
+ function calculateImgDimension(image, dimension) {
3335
+ let { height, width } = image;
3336
+ if (height > width) {
3337
+ height = height > dimension ? dimension : height;
3338
+ width = simpleThreeRule(image.height, height, width);
3339
+ return { height, width };
3340
+ }
3341
+ width = width > dimension ? dimension : width;
3342
+ height = simpleThreeRule(image.width, width, height);
3343
+ return { height, width };
3331
3344
  }
3332
3345
  function RlsImageEditor(props) {
3333
3346
  const [selection, setSelection] = useState(props.selection ?? 60);
@@ -3395,12 +3408,14 @@ function RlsImageEditor(props) {
3395
3408
  let width = (offsetHeight * _ratio) / 100;
3396
3409
  if (width > offsetWidth) {
3397
3410
  width = offsetWidth;
3398
- height = width / ratioFactor;
3411
+ height = width * ratioFactor;
3399
3412
  }
3400
3413
  return { height, width };
3401
3414
  }, [ratio]);
3402
3415
  const refreshSelectionStyle = useCallback((rateSelection) => {
3403
- if (image.current.width > 0 && image.current.height > 0) {
3416
+ if (refSelection.current &&
3417
+ image.current.width > 0 &&
3418
+ image.current.height > 0) {
3404
3419
  const { height, width } = image.current.width >= image.current.height
3405
3420
  ? refreshSelectionFromHeight(rateSelection)
3406
3421
  : refreshSelectionFromWidth(rateSelection);
@@ -3408,13 +3423,13 @@ function RlsImageEditor(props) {
3408
3423
  refSelection.current.style.height = `${height}px`;
3409
3424
  if (refSelection.current.offsetLeft + width >
3410
3425
  refImage.current.offsetWidth) {
3411
- const selectionLeft = refImage.current.offsetWidth - width;
3412
- refSelection.current.style.left = `${selectionLeft}px`;
3426
+ const left = refImage.current.offsetWidth - width;
3427
+ refSelection.current.style.left = `${left < 0 ? 0 : left}px`;
3413
3428
  }
3414
3429
  if (refSelection.current.offsetTop + height >
3415
3430
  refImage.current.offsetHeight) {
3416
- const selectionTop = refImage.current.offsetHeight - height;
3417
- refSelection.current.style.top = `${selectionTop}px`;
3431
+ const top = refImage.current.offsetHeight - height;
3432
+ refSelection.current.style.top = `${top < 0 ? 0 : top}px`;
3418
3433
  }
3419
3434
  refreshOverlaysStyle();
3420
3435
  }
@@ -3427,7 +3442,7 @@ function RlsImageEditor(props) {
3427
3442
  }, []);
3428
3443
  const refreshImageStyle = useCallback(() => {
3429
3444
  if (image.current.width <= 0 || image.current.height <= 0) {
3430
- return setImageStyle('0%', '0%');
3445
+ return setImageStyle('0px', '0px');
3431
3446
  }
3432
3447
  const height = (refBody.current.offsetWidth * image.current.height) /
3433
3448
  image.current.width;
@@ -3437,16 +3452,17 @@ function RlsImageEditor(props) {
3437
3452
  const width = (refBody.current.offsetHeight * image.current.width) /
3438
3453
  image.current.height;
3439
3454
  return setImageStyle(`${width}px`, '100%');
3440
- }, []);
3455
+ }, [ratio]);
3441
3456
  useEffect(() => {
3442
3457
  image.current.onload = () => {
3443
3458
  const context = refCanvas.current.getContext('2d', {
3444
3459
  willReadFrequently: true
3445
3460
  });
3446
- refCanvas.current.width = image.current.width;
3447
- refCanvas.current.height = image.current.height;
3448
- context?.drawImage(image.current, 0, 0, image.current.width, image.current.height);
3449
- originalImage.current = context?.getImageData(0, 0, refCanvas.current.width, refCanvas.current.height);
3461
+ const { height, width } = calculateImgDimension(image.current, props.maxDimension || 500);
3462
+ refCanvas.current.width = width;
3463
+ refCanvas.current.height = height;
3464
+ context?.drawImage(image.current, 0, 0, width, height);
3465
+ originalImage.current = context?.getImageData(0, 0, width, height);
3450
3466
  refreshImageStyle();
3451
3467
  refreshSelectionStyle(selection);
3452
3468
  };
@@ -3467,7 +3483,7 @@ function RlsImageEditor(props) {
3467
3483
  }, [props.src]);
3468
3484
  useEffect(() => {
3469
3485
  refreshSelectionStyle(selection);
3470
- }, [selection]);
3486
+ }, [ratio, selection]);
3471
3487
  const onResizeElement = useCallback(() => {
3472
3488
  refreshSelectionStyle(selection);
3473
3489
  }, [selection]);
@@ -3513,7 +3529,7 @@ function RlsImageEditor(props) {
3513
3529
  context?.putImageData(originalImage.current, 0, 0);
3514
3530
  }
3515
3531
  }, []);
3516
- return (jsxRuntimeExports.jsxs("div", { className: "rls-image-editor", children: [jsxRuntimeExports.jsx("div", { ref: refBody, className: "rls-image-editor__body", children: jsxRuntimeExports.jsxs("div", { ref: refImage, className: "rls-image-editor__body__image", children: [jsxRuntimeExports.jsx("div", { ref: refSelection, className: "rls-image-editor__body__selection" }), jsxRuntimeExports.jsx("div", { ref: refOverlayTop, className: "rls-image-editor__body__overlay--top" }), jsxRuntimeExports.jsx("div", { ref: refOverlayRight, className: "rls-image-editor__body__overlay--right" }), jsxRuntimeExports.jsx("div", { ref: refOverlayBottom, className: "rls-image-editor__body__overlay--bottom" }), jsxRuntimeExports.jsx("div", { ref: refOverlayLeft, className: "rls-image-editor__body__overlay--left" }), jsxRuntimeExports.jsx("canvas", { ref: refCanvas })] }) }), jsxRuntimeExports.jsxs("div", { className: "rls-image-editor__footer", children: [jsxRuntimeExports.jsx("div", { className: "rls-image-editor__sliders", children: jsxRuntimeExports.jsx(RlsSlider, { prefixIcon: "external-link", value: selection, minValue: 50, maxValue: 100, onValue: setSelection, disabled: props.disabled }) }), jsxRuntimeExports.jsxs("div", { className: "rls-image-editor__actions", children: [props.children, jsxRuntimeExports.jsx(RlsButton, { type: "classic", prefixIcon: "refresh", onClick: onRestore, disabled: props.disabled, children: labels.actionRestore }), jsxRuntimeExports.jsx(RlsButton, { type: "raised", prefixIcon: "crop", onClick: onCropImage, disabled: props.disabled, children: labels.actionSelect })] })] }), jsxRuntimeExports.jsx("canvas", { ref: refPicture })] }));
3532
+ return (jsxRuntimeExports.jsxs("div", { className: "rls-image-editor", children: [jsxRuntimeExports.jsx("div", { ref: refBody, className: "rls-image-editor__body", children: jsxRuntimeExports.jsxs("div", { ref: refImage, className: "rls-image-editor__body__image", children: [jsxRuntimeExports.jsx("div", { ref: refSelection, className: "rls-image-editor__body__selection" }), jsxRuntimeExports.jsx("div", { ref: refOverlayTop, className: "rls-image-editor__body__overlay--top" }), jsxRuntimeExports.jsx("div", { ref: refOverlayRight, className: "rls-image-editor__body__overlay--right" }), jsxRuntimeExports.jsx("div", { ref: refOverlayBottom, className: "rls-image-editor__body__overlay--bottom" }), jsxRuntimeExports.jsx("div", { ref: refOverlayLeft, className: "rls-image-editor__body__overlay--left" }), jsxRuntimeExports.jsx("canvas", { ref: refCanvas })] }) }), jsxRuntimeExports.jsxs("div", { className: "rls-image-editor__footer", children: [jsxRuntimeExports.jsx("div", { className: "rls-image-editor__sliders", children: jsxRuntimeExports.jsx(RlsSlider, { prefixIcon: "external-link", value: selection, minValue: minValueSlider, maxValue: maxValueSlider, onValue: setSelection, disabled: props.disabled }) }), jsxRuntimeExports.jsxs("div", { className: "rls-image-editor__actions", children: [props.children, jsxRuntimeExports.jsx(RlsButton, { type: "classic", prefixIcon: "refresh", onClick: onRestore, disabled: props.disabled, children: labels.actionRestore }), jsxRuntimeExports.jsx(RlsButton, { type: "raised", prefixIcon: "crop", onClick: onCropImage, disabled: props.disabled, children: labels.actionSelect })] })] }), jsxRuntimeExports.jsx("canvas", { ref: refPicture })] }));
3517
3533
  }
3518
3534
 
3519
3535
  function RlsImageEditorModal(props) {