From d2b3d42669ffd73295a81de6f9806d5218b3c5d5 Mon Sep 17 00:00:00 2001 From: Kieran Farr Date: Mon, 3 Feb 2025 23:53:01 -0800 Subject: [PATCH] add AddComponent --- .../components/components/AddComponent.js | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/editor/components/components/AddComponent.js diff --git a/src/editor/components/components/AddComponent.js b/src/editor/components/components/AddComponent.js new file mode 100644 index 000000000..32f858c83 --- /dev/null +++ b/src/editor/components/components/AddComponent.js @@ -0,0 +1,102 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import Select from 'react-select'; + +export default class AddComponent extends React.Component { + static propTypes = { + entity: PropTypes.object + }; + + constructor(props) { + super(props); + this.state = { value: null }; + } + + /** + * Add blank component. + * If component is instanced, generate an ID. + */ + addComponent = (value) => { + this.setState({ value: null }); + + let componentName = value.value; + + var entity = this.props.entity; + + if (AFRAME.components[componentName].multiple) { + const id = prompt( + `Provide an ID for this component (e.g., 'foo' for ${componentName}__foo).` + ); + componentName = id ? `${componentName}__${id}` : componentName; + } + + AFRAME.INSPECTOR.execute('componentadd', { + entity, + component: componentName, + value: '' + }); + }; + + /** + * Component dropdown options. + */ + getComponentsOptions() { + const usedComponents = Object.keys(this.props.entity.components); + var commonOptions = Object.keys(AFRAME.components) + .filter(function (componentName) { + return ( + componentName.startsWith('street-generated-') && // Added filter for street-generated- prefix + (AFRAME.components[componentName].multiple || + usedComponents.indexOf(componentName) === -1) + ); + }) + .sort() + .map(function (value) { + return { value: value, label: value, origin: 'loaded' }; + }); + + this.options = commonOptions; + this.options = this.options.sort(function (a, b) { + return a.label === b.label ? 0 : a.label < b.label ? -1 : 1; + }); + } + + renderOption(option) { + var bullet = ( + + ); + return ( + + {option.label} {option.origin === 'loaded' ? bullet : ''} + + ); + } + + render() { + const entity = this.props.entity; + if (!entity) { + return
; + } + + this.getComponentsOptions(); + + return ( +
+

COMPONENTS

+