Skip to content
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

Improve perfs #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@

1.2.7 / 2024-07-21
==================

* Use board uid and getElementById
* Improve performances
* Fix bug if missing linked item

1.2.6 / 2024-03-09
==================

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-sync-board",
"version": "1.2.6",
"version": "1.2.7",
"description": "React-sync-board is a board for react",
"type": "module",
"files": [
Expand Down
6 changes: 5 additions & 1 deletion src/lib/board/Items/Item.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { memo } from "react";

import ResizeHandler from "./ResizeHandler";
import { css } from "goober";
import deepEqual from "fast-deep-equal";

import ResizeHandler from "./ResizeHandler";
import useMainStore from "../store/main";

const itemClass = css`
display: inline-block;
transition: transform 150ms;
Expand Down Expand Up @@ -184,6 +186,7 @@ const Item = ({
showResizeHandle = true,
}) => {
const itemWrapperRef = React.useRef(null);
const [uid] = useMainStore((state) => [state.config.uid]);

const {
component: Component = () => null,
Expand Down Expand Up @@ -244,6 +247,7 @@ const Item = ({
<div
style={{ transform: `rotate(${rotation}deg` }}
data-id={id}
id={`${uid}__${id}`}
className={className}
>
<div
Expand Down
8 changes: 8 additions & 0 deletions src/lib/board/Items/useGetSelectedItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import useMainStore from "../store/main";

const useGetSelectedItems = () => {
const [getSelection] = useMainStore((state) => [state.getSelection]);
return getSelection;
};

export default useGetSelectedItems;
17 changes: 7 additions & 10 deletions src/lib/board/Items/useItemActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ const useItemActions = () => {
getLinkedItems(getStoreItems(), getItemIds(), itemIds),
posDelta
);

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

const putItemsOnTop = React.useCallback(
Expand All @@ -144,12 +142,12 @@ const useItemActions = () => {

const stickOnGrid = React.useCallback(
(itemIds, { type: globalType, size: globalSize } = {}) => {
const { boardWrapper } = getConfiguration();
const { uid } = getConfiguration();

batchUpdateItems(
itemIds,
(item) => {
const elem = getItemElem(boardWrapper, item.id);
const elem = getItemElem(uid, item.id);

if (!elem) {
return;
Expand Down Expand Up @@ -292,10 +290,9 @@ const useItemActions = () => {
});

insertItems(itemsWithPosition, beforeId);

updateItemExtent();
placeItems(itemsToInsert.map(({ id }) => id));
},
[getCenter, insertItems, updateItemExtent]
[getCenter, insertItems, placeItems]
);

const pushItem = React.useCallback(
Expand Down Expand Up @@ -351,11 +348,11 @@ const useItemActions = () => {
if (hasClass(target, "passthrough")) {
// Get current value
const itemList = getItemIds();
const { boardWrapper } = getConfiguration();
const { uid } = getConfiguration();

// Found element under the cursor
const elements = itemList.reduce((prev, itemId) => {
const elem = getItemElem(boardWrapper, itemId);
const elem = getItemElem(uid, itemId);
const itemRect = elem.getBoundingClientRect();
if (isPointInsideRect({ x: clientX, y: clientY }, itemRect)) {
prev.unshift(elem);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/board/Selection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ const BoundingBox = () => {
// Update selection bounding box
const updateBox = React.useCallback(() => {
const currentSelectedItems = getSelection();
const { boardWrapperRect, boardWrapper } = getConfiguration();
const { boardWrapperRect, uid } = getConfiguration();

if (currentSelectedItems.length === 0) {
setSelectionBox(null);
return;
}

const boundingBox = getItemsBoundingBox(currentSelectedItems, boardWrapper);
const boundingBox = getItemsBoundingBox(currentSelectedItems, uid);

if (!boundingBox) {
setSelectionBox(null);
Expand Down
10 changes: 8 additions & 2 deletions src/lib/board/useDim.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ const useDim = () => {
const updateItemExtent = React.useCallback(() => {
// Update item extent
const items = getItemList();
const { boardWrapper, boardSize } = getConfiguration();
const { boardSize, uid } = getConfiguration();

const newRes = items.reduce(
(boundingBox, item) => {
const elem = getItemElem(boardWrapper, item.id);
const elem = getItemElem(uid, item.id);

if (elem) {
boundingBox.left = Math.min(item.x, boundingBox.left);
Expand Down Expand Up @@ -392,6 +392,12 @@ const useDim = () => {
200
);

React.useEffect(() => {
window.debugUpdateExtent = () => updateItemExtent();
window.debugDisplayExtent = () =>
console.log(getConfiguration().itemExtent);
}, [getConfiguration, updateItemExtent]);

return {
setDim: setDimSafe,
rotateBoard,
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { default as useWire } from "@/hooks/useWire";
export { default as useItems } from "@/board/Items/useItems";
export { default as useDebouncedItems } from "@/board/Items/useDebouncedItems";
export { default as useSelectedItems } from "@/board/Items/useSelectedItems";
export { default as useGetSelectedItems } from "@/board/Items/useGetSelectedItems";
export { default as useItemActions } from "@/board/Items/useItemActions";
export { default as useItemInteraction } from "@/board/Items/useItemInteraction";
export { default as useAvailableActions } from "@/board/Items/useAvailableActions";
Expand Down
34 changes: 19 additions & 15 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,17 @@ export const isItemInsideElement = (itemElement, otherElem) => {
return result;
};

export const getItemElem = (wrapper, itemId) => {
export const getItemElem = (uid, itemId) => {
try {
const elems = wrapper.getElementsByClassName(`item ${itemId}`);
const elem = elems[0];
const elem = document.getElementById(`${uid}__${itemId}`);
if (!elem) {
console.error(`Missing item ${itemId}`);
}
return elem;
} catch (e) {
console.error(
`Error while getting item with id ${itemId} with wrapper`,
wrapper
`Error while getting item with id ${itemId} inside wrapper`,
uid
);
return undefined;
}
Expand All @@ -153,9 +152,9 @@ export const getIdFromElem = (elem) => {
return value;
};

export const getItemsBoundingBox = (itemIds, wrapper = document) => {
export const getItemsBoundingBox = (itemIds, uid) => {
const result = itemIds.reduce((prev, itemId) => {
const elem = getItemElem(wrapper, itemId);
const elem = getItemElem(uid, itemId);

if (!elem) {
if (!prev) {
Expand Down Expand Up @@ -212,14 +211,19 @@ const getLinkedItemsRecursive = (itemMap, itemIds, alreadyMet = null) => {
return [];
} else {
alreadyMet.add(itemId);
return [
itemId,
...getLinkedItemsRecursive(
itemMap,
itemMap[itemId].linkedItems,
alreadyMet
),
];
if (itemMap[itemId]) {
// If the item has been removed but not from linked list
return [
itemId,
...getLinkedItemsRecursive(
itemMap,
itemMap[itemId].linkedItems,
alreadyMet
),
];
} else {
return [];
}
}
})
.flat();
Expand Down
Loading