Skip to content

Commit

Permalink
[BulkEditPhotos] Add preview size controls (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhatib authored Dec 14, 2022
1 parent 4185c13 commit b2ff593
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
45 changes: 37 additions & 8 deletions src/BulkEditPhotos/BulkEditPhotos.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PhotoIcon } from '@heroicons/react/24/solid';
import JSZip from 'jszip';
import { doc } from 'prettier';
import { useCallback, useEffect, useRef, useState } from 'react';
Expand Down Expand Up @@ -39,6 +40,7 @@ const BulkEditPhotos = (): JSX.Element => {
const [opacity, setOpacity] = useState(100);
const [saturation, setSaturation] = useState(100);
const [sepia, setSepia] = useState(0);
const [previewSize, setPreviewSize] = useState(250);

const onCrop = useCallback(() => {
const croppedCanvases = cropperRefs.current.map((cropper) => {
Expand Down Expand Up @@ -130,14 +132,39 @@ const BulkEditPhotos = (): JSX.Element => {
<div className="flex flex-grow m-3 lg:ml-0">
{/* Sidebar */}
<div className="flex flex-col p-3 bg-gray-800 w-96">
<div className="h-10 w-48">
<UploadButton
onDrop={onDrop}
accept="image/*"
fullSized={false}
>
<span className="text-base">Add More Images</span>
</UploadButton>
<div className="flex w-full">
<div className="h-10 w-48">
<UploadButton
onDrop={onDrop}
accept="image/*"
fullSized={false}
>
<span className="text-base">Add More Images</span>
</UploadButton>
</div>
<div className="flex-grow" />
<div className="flex items-center">
<div>
<button
className="h-5 self-end bg-green-500 text-white px-1 hover:bg-green-700 mr-2"
onClick={() => {
setPreviewSize((previewSize) => previewSize / 1.2);
}}
>
<PhotoIcon className="w-3" />
</button>
</div>{' '}
<div>
<button
className="h-8 self-end bg-green-500 text-white px-1 hover:bg-green-700"
onClick={() => {
setPreviewSize((previewSize) => previewSize * 1.2);
}}
>
<PhotoIcon className="w-5" />
</button>
</div>{' '}
</div>
</div>
<div className="flex flex-col">
{/* Crops */}
Expand Down Expand Up @@ -545,6 +572,7 @@ const BulkEditPhotos = (): JSX.Element => {
image={image}
ref={(ref) => registerPhotoCropperRef(ref, index)}
onCrop={onCrop}
previewSize={previewSize}
/>
</div>
);
Expand Down Expand Up @@ -580,6 +608,7 @@ const BulkEditPhotos = (): JSX.Element => {
sepia={sepia}
opacity={opacity}
blur={blur}
previewSize={previewSize}
/>
)}
</div>
Expand Down
10 changes: 8 additions & 2 deletions src/images/ImageCanvasEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface ImageCanvasEditorProps {
sepia?: number;
opacity?: number;
blur?: number;
previewSize?: number;
}

export interface CanvasEditorRef {
Expand Down Expand Up @@ -51,6 +52,7 @@ export const ImageCanvasEditor = forwardRef<
sepia = 0,
opacity = 100,
blur = 0,
previewSize = 100,
},
ref
): JSX.Element {
Expand Down Expand Up @@ -102,10 +104,14 @@ export const ImageCanvasEditor = forwardRef<
{srcCanvas ? (
<canvas
ref={canvasRef}
className="h-40 inline-block pointer-events-none"
style={{ height: previewSize }}
className="inline-block pointer-events-none"
/>
) : (
<div className="h-40 w-32 flex justify-center items-center">
<div
style={{ height: previewSize }}
className="w-32 flex justify-center items-center"
>
<div className="w-9 transform scale-75">
<GridLoader
color={'#BFDBFE'}
Expand Down
10 changes: 7 additions & 3 deletions src/images/PhotoCropper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Props {
aspect?: number;
image: ImageData;
onCrop?: () => void;
previewSize?: number;
}

function centerAspectCrop(
Expand Down Expand Up @@ -43,7 +44,7 @@ export interface PhotoCropperRef {
}

const PhotoCropper = forwardRef<PhotoCropperRef, Props>(function (
{ image, onCrop, aspect },
{ image, onCrop, aspect, previewSize = 250 },
ref
): JSX.Element {
const [crop, setCrop] = useState<Crop>();
Expand Down Expand Up @@ -154,8 +155,11 @@ const PhotoCropper = forwardRef<PhotoCropperRef, Props>(function (
ref={imgRef}
alt="Crop me"
src={image.url}
style={{ transform: `scale(${scale}) rotate(${rotate}deg)` }}
className="h-40 block pointer-events-none"
style={{
transform: `scale(${scale}) rotate(${rotate}deg)`,
height: previewSize,
}}
className="block pointer-events-none"
/>
</ReactCrop>
{/* Toolbar */}
Expand Down

0 comments on commit b2ff593

Please sign in to comment.