-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Inspectorv2] Add metadata properties #16975
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
Merged
sebavan
merged 9 commits into
BabylonJS:master
from
docEdub:250805-inspector-v2-add-metadata
Aug 13, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4e6df3
Add partial support for inspector v2 metadata properties
docEdub 414ad6f
Merge branch 'master' into 250805-inspector-v2-add-metadata
docEdub 8ad5d49
Add `createTestMetadata()` to inspector-v2 test scene
docEdub 3ebd9b6
Get metadata UI to update on switch toggle, and add "Update"/"Clear" …
docEdub b12ccdc
Add "Populate glTF extras" button, and cleanup
docEdub ed9da3c
Get metadata textarea styling working
docEdub 62c75f3
Merge branch 'master' into 250805-inspector-v2-add-metadata
docEdub e7ae687
Fix lint
docEdub 2f59879
Use `BoundProperty` for "Prevent Object corruption" and "Pretty JSON"…
docEdub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
packages/dev/inspector-v2/src/components/properties/metadataProperties.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import type { FunctionComponent } from "react"; | ||
|
||
import type { Nullable } from "core/types"; | ||
import { SwitchPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/switchPropertyLine"; | ||
import { TextAreaPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/textAreaPropertyLine"; | ||
import { TextPropertyLine } from "shared-ui-components/fluent/hoc/propertyLines/textPropertyLine"; | ||
import { BoundProperty } from "./boundProperty"; | ||
|
||
enum MetadataTypes { | ||
NULL = "null", | ||
STRING = "string", | ||
OBJECT = "Object", | ||
JSON = "JSON", | ||
} | ||
|
||
/** | ||
* Checks if the input is a string. | ||
* @param input - any input to check | ||
* @returns boolean - true if the input is a string, false otherwise | ||
*/ | ||
function IsString(input: any): boolean { | ||
return typeof input === "string" || input instanceof String; | ||
} | ||
|
||
/** | ||
* Parses a string and returns a JSON object if the string is valid JSON, otherwise returns null | ||
* @param string - any string | ||
* @returns JSON object or null if the string is not valid JSON | ||
*/ | ||
// function ParseString(string: string): JSON | null { | ||
// try { | ||
// return JSON.parse(string); | ||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// } catch (error) { | ||
// return null; | ||
// } | ||
// } | ||
|
||
/** | ||
* Checks recursively for functions on an object and returns `false` if any are found. | ||
* @param o any object, string or number | ||
* @returns boolean | ||
*/ | ||
function ObjectCanSafelyStringify(o: object | string | number | boolean): boolean { | ||
if (typeof o === "function") { | ||
return false; | ||
} | ||
if (o === null || o === true || o === false || typeof o === "number" || IsString(o)) { | ||
return true; | ||
} | ||
|
||
if (typeof o === "object") { | ||
if (Object.values(o).length === 0) { | ||
return true; | ||
} | ||
return Object.values(o as Record<string, any>).every((value) => ObjectCanSafelyStringify(value)); | ||
} | ||
|
||
if (Array.isArray(o)) { | ||
return o.every((value) => ObjectCanSafelyStringify(value)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export interface IMetadataContainer { | ||
metadata: any; | ||
} | ||
|
||
class MetadataUtils { | ||
private _editedMetadata: Nullable<string> = null; | ||
|
||
static PrettyJSON = false; | ||
|
||
static PreventObjectCorruption = true; | ||
|
||
constructor(public readonly entity: IMetadataContainer) {} | ||
|
||
get editedMetadata(): string { | ||
if (!this._editedMetadata) { | ||
this._editedMetadata = this.parsedMetadata; | ||
} | ||
return this._editedMetadata; | ||
} | ||
|
||
set editedMetadata(value: string) { | ||
this._editedMetadata = value; | ||
} | ||
|
||
get entityType(): MetadataTypes { | ||
const metadata = this.entity.metadata; | ||
|
||
if (IsString(metadata)) { | ||
return MetadataTypes.STRING; | ||
} | ||
if (metadata === null) { | ||
return MetadataTypes.NULL; | ||
} | ||
if (!ObjectCanSafelyStringify(metadata)) { | ||
return MetadataTypes.OBJECT; | ||
} | ||
|
||
return MetadataTypes.JSON; | ||
} | ||
|
||
/** | ||
* @returns whether the entity's metadata can be parsed as JSON. | ||
*/ | ||
get isParsable(): boolean { | ||
if (!this.entity.metadata) { | ||
return false; | ||
} | ||
|
||
try { | ||
return !!JSON.parse(JSON.stringify(this.entity.metadata)); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
get isReadonly(): boolean { | ||
return this.entityType === MetadataTypes.OBJECT && MetadataUtils.PreventObjectCorruption; | ||
} | ||
|
||
get parsedMetadata(): string { | ||
const metadata = this.entity.metadata; | ||
|
||
if (this.isParsable) { | ||
return JSON.stringify(metadata, undefined, MetadataUtils.PrettyJSON ? 2 : undefined); | ||
} | ||
|
||
if (IsString(metadata)) { | ||
return metadata; | ||
} | ||
|
||
return String(metadata); | ||
} | ||
} | ||
|
||
/** | ||
* Component to display metadata properties of an entity. | ||
* @param props - The properties for the component. | ||
* @returns A React component that displays metadata properties. | ||
*/ | ||
export const MetadataProperties: FunctionComponent<{ entity: IMetadataContainer }> = (props) => { | ||
const { entity } = props; | ||
|
||
const metadataUtils = new MetadataUtils(entity); | ||
|
||
return ( | ||
<> | ||
<BoundProperty component={TextPropertyLine} label={"Property type"} target={metadataUtils} propertyKey="entityType" /> | ||
<BoundProperty component={SwitchPropertyLine} label={"Prevent Object corruption"} target={MetadataUtils} propertyKey="PreventObjectCorruption" /> | ||
<BoundProperty component={SwitchPropertyLine} label={"Pretty JSON"} target={MetadataUtils} propertyKey="PrettyJSON" /> | ||
<BoundProperty disabled={metadataUtils.isReadonly} component={TextAreaPropertyLine} label={"Data"} target={metadataUtils} propertyKey="editedMetadata" /> | ||
{/* TODO: Update component when settings change. Toggling PrettyJSON and PreventObjectCorruption should update the text area, but they don't right now. */} | ||
docEdub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{/* TODO: Add buttons. See metadataPropertyGridComponent.tsx for v1 implementation. */} | ||
</> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/dev/inspector-v2/src/services/panes/properties/metadataPropertiesService.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { IMetadataContainer } from "../../../components/properties/metadataProperties"; | ||
import type { ServiceDefinition } from "../../../modularity/serviceDefinition"; | ||
import type { IPropertiesService } from "./propertiesService"; | ||
|
||
import { MetadataProperties } from "../../../components/properties/metadataProperties"; | ||
import { PropertiesServiceIdentity } from "./propertiesService"; | ||
|
||
function IsMetadataContainer(entity: unknown): entity is IMetadataContainer { | ||
return (entity as IMetadataContainer).metadata !== undefined; | ||
} | ||
|
||
export const MetadataPropertiesServiceDefinition: ServiceDefinition<[], [IPropertiesService]> = { | ||
friendlyName: "Metadata Properties", | ||
consumes: [PropertiesServiceIdentity], | ||
factory: (propertiesService) => { | ||
const contentRegistration = propertiesService.addSectionContent({ | ||
key: "Metadata Properties", | ||
// TransformNode and Bone don't share a common base class, but both have the same transform related properties. | ||
predicate: (entity: unknown) => IsMetadataContainer(entity), | ||
content: [ | ||
{ | ||
section: "Metadata", | ||
component: ({ context }) => <MetadataProperties entity={context} />, | ||
}, | ||
], | ||
}); | ||
|
||
return { | ||
dispose: () => { | ||
contentRegistration.dispose(); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.