Skip to content

Commit

Permalink
fix(composer): One instance of sub models allowed per instance of the…
Browse files Browse the repository at this point in the history
… model (#366)
  • Loading branch information
jwills-jdubs committed Nov 15, 2022
1 parent 15f75cb commit 24f3914
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Object3D, Event } from 'three';
import { useEditorState, useSceneDocument } from '../../../../../../store';
import useMaterialEffect from '../../../../../../hooks/useMaterialEffect';
import SubModelTree from '..';
import { KnownComponentType } from '../../../../../../interfaces';

jest.mock('../../../../../../utils/mathUtils', () => ({
generateUUID: jest.fn(() => '40B59050-EBAE-497F-A366-201E775341DD'), // Hard code UUID for predictable snapshots.
Expand All @@ -23,7 +24,7 @@ jest.mock('../../../../../../store', () => ({
...jest.requireActual('../../../../../../store'),
useSceneDocument: jest.fn(() => ({
appendSceneNodeInternal: jest.fn(),
document: { nodeMap: {} },
getSceneNodeByRef: jest.fn(),
})),
useEditorState: jest.fn(() => ({ setSceneNodeObject3DMapping: jest.fn() })),
}));
Expand Down Expand Up @@ -139,6 +140,7 @@ describe('SubModelTree', () => {

it('should append node onCreate', () => {
const appendSceneNodeInternal = jest.fn();
const getSceneNodeByRef = jest.fn();
const setSceneNodeObject3DMapping = jest.fn();
const object = {
name: 'RootObject',
Expand All @@ -153,7 +155,7 @@ describe('SubModelTree', () => {

(useSceneDocument as jest.Mock).mockImplementation(() => ({
appendSceneNodeInternal,
document: { nodeMap: {} },
getSceneNodeByRef,
}));

(useEditorState as jest.Mock).mockImplementation(() => ({ setSceneNodeObject3DMapping }));
Expand All @@ -173,6 +175,22 @@ describe('SubModelTree', () => {

it('should not append duplicate node onCreate', () => {
const appendSceneNodeInternal = jest.fn();

const nodes = [
{
childRefs: ['childRef'],
},
{
ref: 'childRef',
components: [
{
type: KnownComponentType.SubModelRef,
selector: 'RootObject',
},
],
},
];
const getSceneNodeByRef = jest.fn().mockReturnValueOnce(nodes[0]).mockReturnValueOnce(nodes[1]);
const setSceneNodeObject3DMapping = jest.fn();
const object = {
name: 'RootObject',
Expand All @@ -187,15 +205,7 @@ describe('SubModelTree', () => {

(useSceneDocument as jest.Mock).mockImplementation(() => ({
appendSceneNodeInternal,
document: {
nodeMap: {
RootObject: {
properties: {
subModelId: 'RootObject',
},
},
},
},
getSceneNodeByRef,
}));

(useEditorState as jest.Mock).mockImplementation(() => ({ setSceneNodeObject3DMapping }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ Array [
],
"name": "RootObject",
"parentRef": "112",
"properties": Object {
"subModelId": "RootObject",
},
"properties": Object {},
"ref": "112#undefined",
"transform": Object {
"position": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import Tree, { TreeItem } from '../../../../Tree';
import { ISubModelRefComponent, KnownComponentType } from '../../../../../interfaces';
import { ISceneComponentInternal, ISceneNodeInternal, useEditorState, useSceneDocument } from '../../../../../store';
import { useSceneComposerId } from '../../../../../common/sceneComposerIdContext';
import './SubModelTree.scss';
import { findComponentByType } from '../../../../../utils/nodeUtils';

import './SubModelTree.scss';
import TreeItemLabel from './SubModelTreeItemLabel';

export interface SubModelTreeProps {
Expand All @@ -27,7 +28,7 @@ const SubModelTree: FC<SubModelTreeProps> = ({
visible: defaultVisible = true,
}) => {
const sceneComposerId = useSceneComposerId();
const { appendSceneNodeInternal, document } = useSceneDocument(sceneComposerId);
const { appendSceneNodeInternal, getSceneNodeByRef } = useSceneDocument(sceneComposerId);
const { setSceneNodeObject3DMapping } = useEditorState(sceneComposerId);
const [expanded, setExpanded] = useState(defaultExpanded);
const [visible, setVisible] = useState(defaultVisible);
Expand Down Expand Up @@ -55,11 +56,16 @@ const SubModelTree: FC<SubModelTreeProps> = ({

const onCreate = useCallback(() => {
// Prevent duplicates
const duplicates = Object.entries(document.nodeMap).filter(
([name, node]) => node.properties?.subModelId === object3D.name,
);

if (duplicates.length > 0) {
const duplicates = getSceneNodeByRef(parentRef)?.childRefs.filter((childRef) => {
const child = getSceneNodeByRef(childRef);
const childSubModelComponent = findComponentByType(
child,
KnownComponentType.SubModelRef,
) as ISubModelRefComponent;
return childSubModelComponent?.selector === object3D.name;
});

if (duplicates && duplicates.length > 0) {
return;
}

Expand All @@ -85,14 +91,12 @@ const SubModelTree: FC<SubModelTreeProps> = ({
snapToFloor: false,
},
childRefs: [],
properties: {
subModelId: object3D.name,
},
properties: {},
} as ISceneNodeInternal;

appendSceneNodeInternal(node);
setSceneNodeObject3DMapping(nodeRef, object3D); // Cache Reference
}, [object3D, document.nodeMap]);
}, [object3D]);

const onHover = useCallback((e) => {
e.preventDefault();
Expand Down

0 comments on commit 24f3914

Please sign in to comment.