@rolster/react-components 18.23.6 → 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,11 +3328,22 @@ 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
- const [selection, setSelection] = useState(props.rateSelection ?? 60);
3346
+ const [selection, setSelection] = useState(props.selection ?? 60);
3334
3347
  const [labels, setLabels] = useState({
3335
3348
  actionRestore: reactI18n('editorImageActionRestore'),
3336
3349
  actionSelect: reactI18n('editorImageActionSelect')
@@ -3346,6 +3359,9 @@ function RlsImageEditor(props) {
3346
3359
  const refPicture = useRef(null);
3347
3360
  const image = useRef(new Image());
3348
3361
  const originalImage = useRef();
3362
+ const ratio = useMemo(() => {
3363
+ return props.ratio || '1:1';
3364
+ }, [props.ratio]);
3349
3365
  const getCropProperties = useCallback(() => {
3350
3366
  return {
3351
3367
  height: simpleThreeRule(refImage.current.offsetHeight, image.current.height, refSelection.current.offsetHeight),
@@ -3371,7 +3387,7 @@ function RlsImageEditor(props) {
3371
3387
  refOverlayLeft.current.style.width = `calc(${left}px)`;
3372
3388
  }, []);
3373
3389
  const refreshSelectionFromWidth = useCallback((rateSelection) => {
3374
- const ratioFactor = getRatioFactor(props.ratio || '1:1');
3390
+ const ratioFactor = getRatioFactor(ratio);
3375
3391
  const _ratio = rateSelection * ratioFactor;
3376
3392
  const offsetWidth = refImage.current?.offsetWidth || 0;
3377
3393
  const offsetHeight = refImage.current?.offsetHeight || 0;
@@ -3382,22 +3398,24 @@ function RlsImageEditor(props) {
3382
3398
  width = height / ratioFactor;
3383
3399
  }
3384
3400
  return { height, width };
3385
- }, [props.ratio]);
3401
+ }, [ratio]);
3386
3402
  const refreshSelectionFromHeight = useCallback((rateSelection) => {
3387
- const ratioFactor = getRatioFactor(props.ratio || '1:1');
3388
- const _ratio = rateSelection * ratioFactor;
3403
+ const ratioFactor = getRatioFactor(ratio);
3404
+ const _ratio = rateSelection / ratioFactor;
3389
3405
  const offsetWidth = refImage.current?.offsetWidth || 0;
3390
3406
  const offsetHeight = refImage.current?.offsetHeight || 0;
3391
3407
  let height = (offsetHeight * rateSelection) / 100;
3392
3408
  let width = (offsetHeight * _ratio) / 100;
3393
3409
  if (width > offsetWidth) {
3394
3410
  width = offsetWidth;
3395
- height = width / ratioFactor;
3411
+ height = width * ratioFactor;
3396
3412
  }
3397
3413
  return { height, width };
3398
- }, [props.ratio]);
3414
+ }, [ratio]);
3399
3415
  const refreshSelectionStyle = useCallback((rateSelection) => {
3400
- if (image.current.width > 0 && image.current.height > 0) {
3416
+ if (refSelection.current &&
3417
+ image.current.width > 0 &&
3418
+ image.current.height > 0) {
3401
3419
  const { height, width } = image.current.width >= image.current.height
3402
3420
  ? refreshSelectionFromHeight(rateSelection)
3403
3421
  : refreshSelectionFromWidth(rateSelection);
@@ -3405,17 +3423,17 @@ function RlsImageEditor(props) {
3405
3423
  refSelection.current.style.height = `${height}px`;
3406
3424
  if (refSelection.current.offsetLeft + width >
3407
3425
  refImage.current.offsetWidth) {
3408
- const selectionLeft = refImage.current.offsetWidth - width;
3409
- refSelection.current.style.left = `${selectionLeft}px`;
3426
+ const left = refImage.current.offsetWidth - width;
3427
+ refSelection.current.style.left = `${left < 0 ? 0 : left}px`;
3410
3428
  }
3411
3429
  if (refSelection.current.offsetTop + height >
3412
3430
  refImage.current.offsetHeight) {
3413
- const selectionTop = refImage.current.offsetHeight - height;
3414
- refSelection.current.style.top = `${selectionTop}px`;
3431
+ const top = refImage.current.offsetHeight - height;
3432
+ refSelection.current.style.top = `${top < 0 ? 0 : top}px`;
3415
3433
  }
3416
3434
  refreshOverlaysStyle();
3417
3435
  }
3418
- }, [props.ratio]);
3436
+ }, [ratio]);
3419
3437
  const setImageStyle = useCallback((width, height) => {
3420
3438
  refImage.current.style.width = width;
3421
3439
  refImage.current.style.height = height;
@@ -3424,7 +3442,7 @@ function RlsImageEditor(props) {
3424
3442
  }, []);
3425
3443
  const refreshImageStyle = useCallback(() => {
3426
3444
  if (image.current.width <= 0 || image.current.height <= 0) {
3427
- return setImageStyle('0%', '0%');
3445
+ return setImageStyle('0px', '0px');
3428
3446
  }
3429
3447
  const height = (refBody.current.offsetWidth * image.current.height) /
3430
3448
  image.current.width;
@@ -3434,16 +3452,17 @@ function RlsImageEditor(props) {
3434
3452
  const width = (refBody.current.offsetHeight * image.current.width) /
3435
3453
  image.current.height;
3436
3454
  return setImageStyle(`${width}px`, '100%');
3437
- }, []);
3455
+ }, [ratio]);
3438
3456
  useEffect(() => {
3439
3457
  image.current.onload = () => {
3440
3458
  const context = refCanvas.current.getContext('2d', {
3441
3459
  willReadFrequently: true
3442
3460
  });
3443
- refCanvas.current.width = image.current.width;
3444
- refCanvas.current.height = image.current.height;
3445
- context?.drawImage(image.current, 0, 0, image.current.width, image.current.height);
3446
- 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);
3447
3466
  refreshImageStyle();
3448
3467
  refreshSelectionStyle(selection);
3449
3468
  };
@@ -3464,7 +3483,7 @@ function RlsImageEditor(props) {
3464
3483
  }, [props.src]);
3465
3484
  useEffect(() => {
3466
3485
  refreshSelectionStyle(selection);
3467
- }, [selection]);
3486
+ }, [ratio, selection]);
3468
3487
  const onResizeElement = useCallback(() => {
3469
3488
  refreshSelectionStyle(selection);
3470
3489
  }, [selection]);
@@ -3477,7 +3496,7 @@ function RlsImageEditor(props) {
3477
3496
  const onCropImage = useCallback(() => {
3478
3497
  const cropProps = getCropProperties();
3479
3498
  const width = props.maxWidth || cropProps.width;
3480
- const height = width * getRatioFactor(props.ratio || '1:1');
3499
+ const height = width * getRatioFactor(ratio);
3481
3500
  refPicture.current.width = width;
3482
3501
  refPicture.current.height = height;
3483
3502
  const context = refPicture.current.getContext('2d');
@@ -3497,7 +3516,7 @@ function RlsImageEditor(props) {
3497
3516
  }
3498
3517
  }, props.mimeType || 'image/jpeg', props.quality || 1);
3499
3518
  }, [
3500
- props.ratio,
3519
+ ratio,
3501
3520
  props.mimeType,
3502
3521
  props.onValue,
3503
3522
  props.formControl,
@@ -3510,7 +3529,7 @@ function RlsImageEditor(props) {
3510
3529
  context?.putImageData(originalImage.current, 0, 0);
3511
3530
  }
3512
3531
  }, []);
3513
- 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 })] }));
3514
3533
  }
3515
3534
 
3516
3535
  function RlsImageEditorModal(props) {
@@ -3564,7 +3583,7 @@ function useImageEditorController(options) {
3564
3583
  setSrcEditor(undefined);
3565
3584
  }, []);
3566
3585
  const RlsImageEditorChooser = useMemo(() => {
3567
- return (srcEditor && (jsxRuntimeExports.jsx(RlsImageEditorModal, { visible: true, src: srcEditor, formControl: options.formControl, maxWidth: options.maxWidth, quality: options.quality, ratio: options.ratio, onValue: onEditorValue, children: jsxRuntimeExports.jsx(RlsButton, { type: "flat", rlsTheme: "danger", onClick: onCancel, disabled: options.disabled, children: labels.actionCancel }) })));
3586
+ return (srcEditor && (jsxRuntimeExports.jsx(RlsImageEditorModal, { visible: true, src: srcEditor, formControl: options.formControl, maxWidth: options.maxWidth, quality: options.quality, ratio: options.ratio, selection: options.selection, onValue: onEditorValue, children: jsxRuntimeExports.jsx(RlsButton, { type: "flat", rlsTheme: "danger", onClick: onCancel, disabled: options.disabled, children: labels.actionCancel }) })));
3568
3587
  }, [
3569
3588
  srcEditor,
3570
3589
  labels,