-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Variants Editor #177
Draft
suffle
wants to merge
4
commits into
Flowpack:main
Choose a base branch
from
suffle:feature/variants-editor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: Variants Editor #177
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Resources/Private/JavaScript/asset-variants/src/components/ImageCrop.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { FC } from 'react'; | ||
import ReactCrop, { PercentCrop, PixelCrop } from 'react-image-crop'; | ||
|
||
interface ImageCropPros { | ||
aspectRatio: number; | ||
currentCrop: any; | ||
onCropChange(crop: PixelCrop, percentageCrop: PercentCrop): void; | ||
onCropComplete(crop: PixelCrop, percentageCrop: PercentCrop): void; | ||
originalPreviewUrl: string; | ||
} | ||
|
||
const ImageCrop: FC<ImageCropPros> = ({ | ||
aspectRatio, | ||
currentCrop, | ||
onCropChange, | ||
onCropComplete, | ||
originalPreviewUrl, | ||
}: ImageCropPros) => { | ||
return ( | ||
<ReactCrop | ||
aspect={aspectRatio} | ||
crop={currentCrop} | ||
onChange={onCropChange} | ||
onComplete={onCropComplete} | ||
keepSelection={true} | ||
> | ||
<img src={originalPreviewUrl} /> | ||
</ReactCrop> | ||
); | ||
}; | ||
|
||
export default React.memo(ImageCrop); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Resources/Private/JavaScript/asset-variants/src/components/VariantModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React, { FC, useEffect, useRef, useState } from 'react'; | ||
import { useCallback } from 'react'; | ||
import { useRecoilState, useSetRecoilState } from 'recoil'; | ||
import 'react-image-crop/dist/ReactCrop.css'; | ||
import { Button, Dialog } from '@neos-project/react-ui-components'; | ||
|
||
import { useIntl, createUseMediaUiStyles, MediaUiTheme } from '@media-ui/core/src'; | ||
import { useSelectedAsset } from '@media-ui/core/src/hooks'; | ||
|
||
import assetVariantModalState from '../state/assetVariantModalState'; | ||
import useSelectedAssetVariant from '../hooks/useSelectedAssetVariant'; | ||
import selectedVariantIdState from '../state/selectedVariantIdState'; | ||
import ImageCrop from './ImageCrop'; | ||
import { PixelCrop } from 'react-image-crop'; | ||
|
||
const useStyles = createUseMediaUiStyles((theme: MediaUiTheme) => ({ | ||
cropContainer: { | ||
display: 'flex', | ||
justifyContent: 'center', | ||
}, | ||
})); | ||
|
||
const VariantModal: FC = () => { | ||
const classes = useStyles(); | ||
const { translate } = useIntl(); | ||
const [isOpen, setIsOpen] = useRecoilState(assetVariantModalState); | ||
const setSelectedVariantId = useSetRecoilState(selectedVariantIdState); | ||
const asset = useSelectedAsset(); | ||
const assetVariant = useSelectedAssetVariant(); | ||
const [currentCrop, setCurrentCrop] = useState<PixelCrop>(); | ||
const [hasChanged, setHasChanged] = useState(false); | ||
const aspectRatio = useRef<number>(); | ||
const handleRequestClose = useCallback(() => { | ||
setSelectedVariantId(undefined); | ||
setIsOpen(false); | ||
}, [setIsOpen, setSelectedVariantId]); | ||
const handleCropComplete = (pxCrop: PixelCrop) => { | ||
const savedCropInformation = assetVariant.cropInformation; | ||
setHasChanged( | ||
savedCropInformation?.x !== pxCrop.x || | ||
savedCropInformation?.y !== pxCrop.y || | ||
savedCropInformation?.width !== pxCrop.width || | ||
savedCropInformation?.height !== pxCrop.height | ||
); | ||
}; | ||
|
||
const cropHasChanged = () => { | ||
const savedCropInformation = assetVariant.cropInformation; | ||
return ( | ||
savedCropInformation?.x !== currentCrop.x || | ||
savedCropInformation?.y !== currentCrop.y || | ||
savedCropInformation?.width !== currentCrop.width || | ||
savedCropInformation?.height !== currentCrop.height | ||
); | ||
}; | ||
|
||
const handleSave = () => { | ||
console.log('saving', currentCrop); | ||
}; | ||
|
||
useEffect(() => { | ||
setCurrentCrop({ unit: 'px', ...assetVariant.cropInformation }); | ||
aspectRatio.current = assetVariant.width / assetVariant.height; | ||
}, [assetVariant]); | ||
|
||
return ( | ||
<Dialog | ||
isOpen={isOpen} | ||
title={translate( | ||
'assetVariantModal.title', | ||
`Variant details for ${assetVariant.presetIdentifier}: ${assetVariant.variantName}`, | ||
{ | ||
assetVariant: assetVariant, | ||
} | ||
)} | ||
onRequestClose={handleRequestClose} | ||
style="wide" | ||
actions={[ | ||
<Button key="cancel" style="neutral" hoverStyle="darken" onClick={handleRequestClose}> | ||
{translate('assetVariantModal.cancel', 'Cancel')} | ||
</Button>, | ||
<Button key="save" style="success" hoverStyle="success" disabled={!hasChanged} onClick={handleSave}> | ||
{translate('assetVariantModal.save', 'Save')} | ||
</Button>, | ||
]} | ||
> | ||
<div className={classes.cropContainer}> | ||
{assetVariant ? ( | ||
<ImageCrop | ||
currentCrop={currentCrop} | ||
onCropChange={setCurrentCrop} | ||
onCropComplete={handleCropComplete} | ||
originalPreviewUrl={asset.previewUrl} | ||
aspectRatio={aspectRatio.current} | ||
/> | ||
) : ( | ||
<span>{translate('assetVariantModal.loading', 'Loading...')}</span> | ||
)} | ||
</div> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default React.memo(VariantModal); |
20 changes: 20 additions & 0 deletions
20
Resources/Private/JavaScript/asset-variants/src/hooks/useSelectVariant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useCallback } from 'react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
|
||
import selectedVariantIdState from '../state/selectedVariantIdState'; | ||
import assetVariantModalState from '../state/assetVariantModalState'; | ||
|
||
const useSelectVariant = () => { | ||
const setSelectedVariantId = useSetRecoilState(selectedVariantIdState); | ||
const setAssetVariantModalState = useSetRecoilState(assetVariantModalState); | ||
|
||
return useCallback( | ||
(variantId: string) => { | ||
if (!variantId) return; | ||
setSelectedVariantId(variantId); | ||
setAssetVariantModalState(true); | ||
}, | ||
[setSelectedVariantId, setAssetVariantModalState] | ||
); | ||
}; | ||
export default useSelectVariant; |
14 changes: 14 additions & 0 deletions
14
Resources/Private/JavaScript/asset-variants/src/hooks/useSelectedAssetVariant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
import { useSelectedAsset } from '@media-ui/core/src/hooks'; | ||
import AssetVariant from '../interfaces/AssetVariant'; | ||
import selectedVariantIdState from '../state/selectedVariantIdState'; | ||
import useAssetVariants from './useAssetVariants'; | ||
|
||
export default function useSelectedAssetVariant(): AssetVariant { | ||
const selectedVariantId = useRecoilValue(selectedVariantIdState); | ||
const selectedAsset = useSelectedAsset(); | ||
const assetVariants = useAssetVariants({ assetId: selectedAsset.id, assetSourceId: selectedAsset.assetSource.id }); | ||
|
||
return assetVariants.variants?.find((variant) => variant.id === selectedVariantId); | ||
} |
34 changes: 34 additions & 0 deletions
34
Resources/Private/JavaScript/asset-variants/src/hooks/useUpdateVariant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// import { useMutation } from '@apollo/client'; | ||
|
||
// import { Asset } from '@media-ui/core/src/interfaces'; | ||
|
||
// import { REPLACE_ASSET } from '../mutations'; | ||
// import { FileUploadResult } from '../interfaces'; | ||
|
||
// export interface AssetReplacementOptions { | ||
// generateRedirects: boolean; | ||
// keepOriginalFilename: boolean; | ||
// } | ||
|
||
// interface ReplaceAssetProps { | ||
// asset: Asset; | ||
// file: File; | ||
// options: AssetReplacementOptions; | ||
// } | ||
|
||
// export default function useReplaceAsset() { | ||
// const [action, { error, data, loading }] = useMutation<{ replaceAsset: FileUploadResult }>(REPLACE_ASSET); | ||
|
||
// const replaceAsset = ({ asset, file, options }: ReplaceAssetProps) => { | ||
// return action({ | ||
// variables: { | ||
// id: asset.id, | ||
// assetSourceId: asset.assetSource.id, | ||
// file, | ||
// options, | ||
// }, | ||
// }); | ||
// }; | ||
|
||
// return { replaceAsset, uploadState: data?.replaceAsset || null, error, loading }; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export { default as ASSET_VARIANTS } from './queries/assetVariants'; | ||
export { default as CROP_INFORMATION_FRAGMENT } from './fragments/cropInformation'; | ||
|
||
export { default as VariantModal } from './components/VariantModal'; | ||
export { default as VariantsInspector } from './components/VariantsInspector'; | ||
|
||
export { default as assetVariantModalState } from './state/assetVariantModalState'; |
9 changes: 9 additions & 0 deletions
9
Resources/Private/JavaScript/asset-variants/src/state/assetVariantModalState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { atom, useSetRecoilState } from 'recoil'; | ||
import selectedVariantIdState from './selectedVariantIdState'; | ||
|
||
const assetVariantModalState = atom({ | ||
key: 'assetVariantModalState', | ||
default: false, | ||
}); | ||
|
||
export default assetVariantModalState; |
8 changes: 8 additions & 0 deletions
8
Resources/Private/JavaScript/asset-variants/src/state/selectedVariantIdState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { atom } from 'recoil'; | ||
|
||
const selectedVariantIdState = atom<string>({ | ||
key: 'selectedVariantIdState', | ||
default: null, | ||
}); | ||
|
||
export default selectedVariantIdState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you wondering why there can exist variants without an preset identifier in a Neos installation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess thats because custom crops (e.g. in Fusion) are also stored as variants and have no preset identifier?
I took that code from the current media browser version, and I am wondering why a variant with preset identifier should not be croppable, and more important: why does checking for an existing crop area ensure that the image is croppable. If I have a 16:9 image and a 16:9 preset, technically there is no need for a crop area over the whole image, but I should still be able to choose a smaller area