Skip to content

Commit

Permalink
dobra jest z focusami
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Feb 7, 2024
1 parent 8c1625d commit 47ec024
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 @@ -38,28 +38,47 @@ type FieldProps = Pick<GrafFieldProps, "node"> & {
};

export const Field: React.FC<FieldProps> = ({ node }) => {
const { parentTypes, setSelectedNodeId, getParentOfField, focusMode } =
useTreesState();
const {
parentTypes,
setSelectedNodeId,
setSelectedNodeIdThatWillBeAdded,
getParentOfField,
focusMode,
} = useTreesState();
const { printPreviewActive } = useRelationsState();
const { setTypeRelatedNodesToFocusedNode, filteredFocusedNodes } =
useRelationNodesState();
const {
setTypeRelatedNodesToFocusedNode,
filteredFocusedNodes,
filteredTypeRelatedToFocusedNode,
} = useRelationNodesState();
const nodeClick = (n: ParserField) => {
const parent = getParentOfField(n);
if (parent) {
const isFocus = !!(focusMode && filteredFocusedNodes);
if (isFocus) {
setTypeRelatedNodesToFocusedNode(parent);
if (
!filteredFocusedNodes
.concat(filteredTypeRelatedToFocusedNode)
.find((ffn) => ffn.id === parent.id)
) {
setSelectedNodeIdThatWillBeAdded({
source: "relation",
value: {
id: parent.id,
name: parent.name,
},
});
return;
}
}
setSelectedNodeId({
source: "relation",
value: {
id: parent.id,
name: parent.name,
},
});
const isFocus = !!(focusMode && filteredFocusedNodes);
if (isFocus) {
setTypeRelatedNodesToFocusedNode(parent);
if (!filteredFocusedNodes.find((ffn) => ffn.id === parent.id)) {
return;
}
}
}
};
return (
Expand Down
13 changes: 13 additions & 0 deletions packages/editor/src/state/containers/trees/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ const useTreesStateContainer = createContainer(() => {
},
[_setSelectedNodeId, allNodes, selectedNodeId?.value?.id, focusMode]
);
const setSelectedNodeIdThatWillBeAdded = useCallback(
(_selectedNodeId: SelectedNodeId) => {
const nodeId = _selectedNodeId?.value?.id;
if (
nodeId !== selectedNodeId?.value?.id ||
_selectedNodeId?.justCreated !== selectedNodeId?.justCreated
) {
return _setSelectedNodeId(_selectedNodeId);
}
},
[_setSelectedNodeId, allNodes, selectedNodeId?.value?.id, focusMode]
);
const generateTreeFromSchema = async (schema: PassedSchema) => {
if (!schema.code && !schema.libraries) {
setTree({ nodes: [] }, true);
Expand Down Expand Up @@ -494,6 +506,7 @@ const useTreesStateContainer = createContainer(() => {
setSnapshots,
selectedNodeId,
setSelectedNodeId,
setSelectedNodeIdThatWillBeAdded,
queryNode,
getParentOfField,
activeNode,
Expand Down

0 comments on commit 47ec024

Please sign in to comment.