Skip to content

Commit

Permalink
Merge pull request #8 from toeverything/fix/kanban-reorder-card
Browse files Browse the repository at this point in the history
Fix(kanban): kanban reorder
  • Loading branch information
mitsuhatu authored Aug 1, 2022
2 parents 873ded9 + 4fb498c commit 090cba0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DndableItems } from './type';
import type {
KanbanCard,
KanbanGroup,
RecastItem,
} from '@toeverything/components/editor-core';
import { isEqual } from '@toeverything/utils';

Expand Down Expand Up @@ -33,15 +34,44 @@ const findContainer = (id: string, items: DndableItems) => {
);
};

type FindMoveInfo = (params: {
id: string;
activeContainer: string;
overContainer: string;
kanban: KanbanGroup[];
}) => {
targetCard: RecastItem;
targetGroup: KanbanGroup | null;
};

const findMoveInfo: FindMoveInfo = ({
id,
activeContainer,
overContainer,
kanban,
}) => {
const activeGroup = kanban.find(group => group.id === activeContainer);
const overGroup = kanban.find(group => group.id === overContainer);
const activityCard = activeGroup.items.find(item => item.id === id);

return {
targetCard: activityCard.block,
targetGroup: overGroup,
};
};

/**
* Find the sibling node after the dragging of the moved node ends
* @param cards
* @param currentCardId
*/
const findSibling = (cards: KanbanCard[], currentCardId: string) => {
const findSibling = (
cards: KanbanCard[],
currentCardId: string
): [string, string, number] => {
const index = cards.findIndex(card => card.id === currentCardId);

return [cards[index - 1]?.id ?? null, cards[index + 1]?.id ?? null];
return [cards[index - 1]?.id ?? null, cards[index + 1]?.id ?? null, index];
};

/**
Expand Down Expand Up @@ -84,4 +114,5 @@ export {
findSibling,
pickIdFromCards,
shouldUpdate,
findMoveInfo,
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
findSibling,
pickIdFromCards,
shouldUpdate,
findMoveInfo,
} from '../helper';
import type {
CollisionDetection,
Expand All @@ -19,11 +20,13 @@ import type {
DragEndEvent,
} from '@dnd-kit/core';
import type { DndableItems, UseDndableRes } from '../type';
import { useKanban } from '@toeverything/components/editor-core';

export const useDndable = (
dndableItems: DndableItems,
dndableContainerIds: string[]
): UseDndableRes => {
const { kanban, moveCard } = useKanban();
const [items, setItems] = useState(dndableItems);
const [containerIds, setContainerIds] = useState(dndableContainerIds);
const [active, setActive] = useState(null);
Expand Down Expand Up @@ -259,16 +262,14 @@ export const useDndable = (
).filter(Boolean),
};

const activeItems = items[activeContainer];
const activeItem = activeItems.find(
item => item.id === activeId
);
const [beforeId, afterId] = findSibling(
items[overContainer],
activeId
);
const { targetCard } = findMoveInfo({
id: activeId,
activeContainer,
overContainer,
kanban,
});

activeItem?.moveTo(overContainer, beforeId, afterId);
moveCard(targetCard, null, overIndex);

return data;
});
Expand Down

0 comments on commit 090cba0

Please sign in to comment.