-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from Flowpack/feature/190-dnd-collections
FEATURE: Enable drag & drop for asset collections
- Loading branch information
Showing
12 changed files
with
209 additions
and
16 deletions.
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
22 changes: 22 additions & 0 deletions
22
Resources/Private/JavaScript/asset-collections/src/helpers/collectionPath.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 |
---|---|---|
@@ -1,13 +1,35 @@ | ||
export function collectionPath(collection: AssetCollection, collections: AssetCollection[]) { | ||
const path: { title: string; id: string }[] = []; | ||
const idsInPath = []; | ||
|
||
// Build the absolute path from the given collection to the root | ||
let parentCollection = collection; | ||
while (parentCollection) { | ||
if (idsInPath.includes(parentCollection.id)) { | ||
throw new Error('Circular reference detected in collection path'); | ||
} | ||
path.push({ title: parentCollection.title, id: parentCollection.id }); | ||
idsInPath.push(parentCollection.id); | ||
parentCollection = parentCollection.parent | ||
? collections.find(({ id }) => id === parentCollection.parent.id) | ||
: null; | ||
} | ||
return path.reverse(); | ||
} | ||
|
||
export function isChildOfCollection( | ||
collection: AssetCollection, | ||
parentId: AssetCollectionId, | ||
collections: AssetCollection[] | ||
): boolean { | ||
let parentCollection = collection; | ||
while (parentCollection) { | ||
if (parentCollection.id === parentId) { | ||
return true; | ||
} | ||
parentCollection = parentCollection.parent | ||
? collections.find(({ id }) => id === parentCollection.parent.id) | ||
: null; | ||
} | ||
return false; | ||
} |
122 changes: 122 additions & 0 deletions
122
...rces/Private/JavaScript/asset-collections/src/provider/AssetCollectionTreeDndProvider.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,122 @@ | ||
import React, { useCallback, useState, createContext, useContext } from 'react'; | ||
import { DndProvider } from 'react-dnd'; | ||
import HTML5Backend from 'react-dnd-html5-backend'; | ||
|
||
import { useIntl, useNotify } from '@media-ui/core'; | ||
|
||
import { useSetAssetCollectionParent } from '../hooks/useSetAssetCollectionParent'; | ||
import useAssetCollectionsQuery from '../hooks/useAssetCollectionsQuery'; | ||
import { UNASSIGNED_COLLECTION_ID } from '../hooks/useAssetCollectionQuery'; | ||
import { isChildOfCollection } from '../helpers/collectionPath'; | ||
|
||
interface AssetCollectionTreeDndProviderProps { | ||
children: React.ReactElement; | ||
} | ||
|
||
interface AssetCollectionTreeDndProviderValues { | ||
currentlyDraggedNodes: string[]; | ||
handleDrag: (assetCollectionId: string) => void; | ||
handeEndDrag: () => void; | ||
handleDrop: (targetAssetCollectionId: string, position: number) => void; | ||
acceptsDraggedNode: (assetCollectionId: AssetCollectionId, mode: 'into' | 'after') => boolean; | ||
} | ||
|
||
export const AssetCollectionDndContext = createContext(null); | ||
export const useAssetCollectionDnd = (): AssetCollectionTreeDndProviderValues => useContext(AssetCollectionDndContext); | ||
|
||
export function AssetCollectionTreeDndProvider({ children }: AssetCollectionTreeDndProviderProps) { | ||
const { translate } = useIntl(); | ||
const Notify = useNotify(); | ||
const { assetCollections } = useAssetCollectionsQuery(); | ||
const [currentlyDraggedNodes, setCurrentlyDraggedNodes] = useState<string[]>([]); | ||
const { setAssetCollectionParent } = useSetAssetCollectionParent(); | ||
|
||
const handleDrag = useCallback( | ||
(assetCollectionId: string) => { | ||
setCurrentlyDraggedNodes([assetCollectionId]); | ||
}, | ||
[setCurrentlyDraggedNodes] | ||
); | ||
|
||
const handeEndDrag = useCallback(() => { | ||
setCurrentlyDraggedNodes([]); | ||
}, [setCurrentlyDraggedNodes]); | ||
|
||
const handleDrop = useCallback( | ||
(targetAssetCollectionId: string, position: 'before' | 'into') => { | ||
const targetAssetCollection = assetCollections.find(({ id }) => id === targetAssetCollectionId); | ||
const draggedAssetCollections = currentlyDraggedNodes.map((draggedId) => | ||
assetCollections.find(({ id }) => id === draggedId) | ||
); | ||
|
||
const targetAssetCollectionParent = targetAssetCollection.parent?.id | ||
? assetCollections.find(({ id }) => id === targetAssetCollection.parent?.id) | ||
: null; | ||
const targetParentCollection = position === 'into' ? targetAssetCollection : targetAssetCollectionParent; | ||
|
||
draggedAssetCollections.forEach((draggedAssetCollection: AssetCollection) => { | ||
if (targetParentCollection?.id !== draggedAssetCollection.parent?.id) { | ||
setAssetCollectionParent({ | ||
assetCollection: draggedAssetCollection, | ||
parent: targetParentCollection, | ||
}) | ||
.then(() => { | ||
Notify.ok( | ||
translate( | ||
'ParentCollectionSelectBox.setParent.success', | ||
'The parent collection has been set' | ||
) | ||
); | ||
}) | ||
.catch(({ message }) => { | ||
Notify.error( | ||
translate( | ||
'ParentCollectionSelectBox.setParent.error', | ||
'Error while setting the parent collection' | ||
), | ||
message | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
setCurrentlyDraggedNodes([]); | ||
}, | ||
[Notify, assetCollections, currentlyDraggedNodes, setAssetCollectionParent, setCurrentlyDraggedNodes, translate] | ||
); | ||
|
||
const acceptsDraggedNode = useCallback( | ||
(assetCollectionId: AssetCollectionId, mode: 'into' | 'after') => { | ||
if (currentlyDraggedNodes.length === 0 || currentlyDraggedNodes.includes(assetCollectionId)) return false; | ||
|
||
// TODO: Also check current assetSource.readonly property | ||
const canBeInsertedInto = assetCollectionId && assetCollectionId !== UNASSIGNED_COLLECTION_ID; | ||
const canBeInsertedAlongside = assetCollectionId && assetCollectionId !== UNASSIGNED_COLLECTION_ID; | ||
const canBeInserted = mode === 'into' ? canBeInsertedInto : canBeInsertedAlongside; | ||
if (!canBeInserted) return false; | ||
|
||
const assetCollection = assetCollections.find(({ id }) => id === assetCollectionId); | ||
const createsRecursion = currentlyDraggedNodes.some((draggedAssetCollectionId) => { | ||
return isChildOfCollection(assetCollection, draggedAssetCollectionId, assetCollections); | ||
}); | ||
return !createsRecursion; | ||
}, | ||
[assetCollections, currentlyDraggedNodes] | ||
); | ||
|
||
return ( | ||
<DndProvider backend={HTML5Backend}> | ||
<AssetCollectionDndContext.Provider | ||
value={{ | ||
currentlyDraggedNodes, | ||
handleDrag, | ||
handeEndDrag, | ||
handleDrop, | ||
acceptsDraggedNode, | ||
}} | ||
> | ||
{children} | ||
</AssetCollectionDndContext.Provider> | ||
</DndProvider> | ||
); | ||
} |
4 changes: 3 additions & 1 deletion
4
Resources/Private/JavaScript/asset-collections/typings/AssetCollection.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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,6 @@ export default function loadIconLibrary() { | |
faWeightHanging, | ||
faFilter, | ||
faSearch, | ||
faBroom, | ||
faBroom | ||
); | ||
} |
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