Skip to content

Commit

Permalink
Get linked item is now recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Jan 15, 2024
1 parent 38a2432 commit 4273ea7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/lib/board/Items/ItemList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ItemList = () => {
top: `${boardSize / 2}px`,
left: `${boardSize / 2}px`,
display: "inline-block",
lineHeight: 0,
});

return itemList.map((itemId) => (
Expand Down
31 changes: 14 additions & 17 deletions src/lib/board/Items/useItemActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
insideClass,
hasClass,
snapToGrid,
getLinkedItems,
} from "@/utils";

import useItemInteraction from "./useItemInteraction";
Expand Down Expand Up @@ -105,18 +106,14 @@ const useItemActions = () => {

const moveItems = React.useCallback(
(itemIds, posDelta) => {
const prevMap = getStoreItems();

const itemIdsWithLinkedItems = itemIds
.map((itemId) => {
return [itemId, ...(prevMap[itemId].linkedItems || [])];
})
.flat();
moveStoreItems(
getLinkedItems(getStoreItems(), getItemIds(), itemIds),
posDelta
);

moveStoreItems(itemIdsWithLinkedItems, posDelta);
updateItemExtent();
},
[getStoreItems, moveStoreItems, updateItemExtent]
[getItemIds, getStoreItems, moveStoreItems, updateItemExtent]
);

const putItemsOnTop = React.useCallback(
Expand Down Expand Up @@ -168,16 +165,15 @@ const useItemActions = () => {

const placeItems = React.useCallback(
(itemIds, gridConfig) => {
const prevMap = getStoreItems();

// Put moved items on top
const itemIdsWithLinkedItems = itemIds
.map((itemId) => {
return [itemId, ...(prevMap[itemId].linkedItems || [])];
})
.flat();
// Put all moved items on top
const itemIdsWithLinkedItems = getLinkedItems(
getStoreItems(),
getItemIds(),
itemIds
);

putItemsOnTop(itemIdsWithLinkedItems);

// Remove moving state
batchUpdateItems(itemIdsWithLinkedItems, (item) => {
const newItem = { ...item };
Expand All @@ -192,6 +188,7 @@ const useItemActions = () => {
[
batchUpdateItems,
callPlaceInteractions,
getItemIds,
getStoreItems,
putItemsOnTop,
stickOnGrid,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/board/Items/useItemInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const useItemInteraction = (interaction) => {
);

const call = React.useCallback(
(items) => {
callInteractions(interaction, items);
(itemIds) => {
callInteractions(interaction, itemIds);
},
[callInteractions, interaction]
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/board/store/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const itemInteractions = (set, get) => ({
interactions: { ...state.interactions, [interaction]: nextInteraction },
};
}),
callInteractions: (interaction, items) => {
callInteractions: (interaction, itemIds) => {
if (!get().interactions[interaction]) return;
get().interactions[interaction].forEach((callback) => {
callback(items);
callback(itemIds);
});
},
});
Expand Down
32 changes: 32 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,38 @@ export const getItemsBoundingBox = (itemIds, wrapper = document) => {
return result;
};

const getLinkedItemsRecursive = (itemMap, itemIds, alreadyMet = null) => {
if (alreadyMet === null) {
alreadyMet = new Set();
}

if (!itemIds || itemIds.length === 0) {
return [];
}
return itemIds
.map((itemId) => {
if (alreadyMet.has(itemId)) {
return [];
} else {
alreadyMet.add(itemId);
return [
itemId,
...getLinkedItemsRecursive(
itemMap,
itemMap[itemId].linkedItems,
alreadyMet
),
];
}
})
.flat();
};

export const getLinkedItems = (itemMap, orderedItemIds, itemIds) => {
const linkedItems = new Set(getLinkedItemsRecursive(itemMap, itemIds));
return orderedItemIds.filter((itemId) => linkedItems.has(itemId));
};

export const snapToGrid = (
{ x, y, width, height },
{ type = "grid", size = 1, offset = { x: 0, y: 0 } }
Expand Down

0 comments on commit 4273ea7

Please sign in to comment.