Skip to content

Commit cffe4f8

Browse files
In getComponentClipboardRepresentation filter out properties that are the same as their default value, and modify the propertyRowDefined class calculation to the same to have the property in white if it's different from the default value (#816)
1 parent 769fa2d commit cffe4f8

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/components/components/PropertyRow.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Vec4Widget from '../widgets/Vec4Widget';
1313
import Vec3Widget from '../widgets/Vec3Widget';
1414
import Vec2Widget from '../widgets/Vec2Widget';
1515
import { updateEntity } from '../../lib/entity';
16+
import { equal } from '../../lib/utils';
1617

1718
export default class PropertyRow extends React.Component {
1819
static propTypes = {
@@ -129,6 +130,30 @@ export default class PropertyRow extends React.Component {
129130
}
130131
}
131132

133+
isPropertyDefined() {
134+
const props = this.props;
135+
let definedValue;
136+
let defaultValue;
137+
// getDOMAttribute returns null if the component doesn't exist, and
138+
// in the case of a multi-properties component it returns undefined
139+
// if it exists but has the default values.
140+
if (props.isSingle) {
141+
definedValue = props.entity.getDOMAttribute(props.componentname);
142+
if (definedValue === null) return false;
143+
defaultValue =
144+
props.entity.components[props.componentname].schema.default;
145+
return !equal(definedValue, defaultValue);
146+
} else {
147+
definedValue = (props.entity.getDOMAttribute(props.componentname) || {})[
148+
props.name
149+
];
150+
if (definedValue === undefined) return false;
151+
defaultValue =
152+
props.entity.components[props.componentname].schema[props.name].default;
153+
return !equal(definedValue, defaultValue);
154+
}
155+
}
156+
132157
render() {
133158
const props = this.props;
134159
const value =
@@ -140,10 +165,7 @@ export default class PropertyRow extends React.Component {
140165

141166
const className = clsx({
142167
propertyRow: true,
143-
propertyRowDefined: props.isSingle
144-
? !!props.entity.getDOMAttribute(props.componentname)
145-
: props.name in
146-
(props.entity.getDOMAttribute(props.componentname) || {})
168+
propertyRowDefined: this.isPropertyDefined()
147169
});
148170

149171
return (

src/lib/entity.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,27 @@ function getUniqueId(baseId) {
503503

504504
export function getComponentClipboardRepresentation(entity, componentName) {
505505
entity.flushToDOM();
506-
const data = entity.getDOMAttribute(componentName);
506+
let data = entity.getDOMAttribute(componentName);
507507
if (!data) {
508508
return componentName;
509509
}
510510

511-
const schema = entity.components[componentName].schema;
512-
const attributes = stringifyComponentValue(schema, data);
513-
return `${componentName}="${attributes}"`;
511+
const component = entity.components[componentName];
512+
const schema = component.schema;
513+
// If multi-properties component, filter out properties that are the same as their default value
514+
if (!isSingleProperty(schema)) {
515+
data = { ...data };
516+
517+
for (const [propertyName, value] of Object.entries(data)) {
518+
const defaultValue = getDefaultValue(component, propertyName);
519+
if (equal(value, defaultValue)) {
520+
delete data[propertyName];
521+
}
522+
}
523+
}
524+
525+
const properties = stringifyComponentValue(schema, data);
526+
return `${componentName}="${properties}"`;
514527
}
515528

516529
/**

0 commit comments

Comments
 (0)