|
| 1 | +import { Component, ReactNode } from "react"; |
| 2 | + |
| 3 | +import { IoAddSharp, IoCloseOutline } from "react-icons/io5"; |
| 4 | + |
| 5 | +import { Node } from "babylonjs"; |
| 6 | + |
| 7 | +import { showAlert, showPrompt } from "../../../../ui/dialog"; |
| 8 | +import { Button } from "../../../../ui/shadcn/ui/button"; |
| 9 | + |
| 10 | +import { EditorInspectorStringField } from "../fields/string"; |
| 11 | +import { EditorInspectorSectionField } from "../fields/section"; |
| 12 | + |
| 13 | +export interface ICustomMetadataInspectorProps { |
| 14 | + object: Node; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Custom metadata inspector component that allows users to add/edit/remove key-value pairs |
| 19 | + * for custom metadata on nodes. |
| 20 | + */ |
| 21 | +export class CustomMetadataInspector extends Component<ICustomMetadataInspectorProps> { |
| 22 | + public render(): ReactNode { |
| 23 | + |
| 24 | + const keys = Object.keys(this.props.object.metadata?.customMetadata ?? {}); |
| 25 | + |
| 26 | + return ( |
| 27 | + <EditorInspectorSectionField title="Metadata"> |
| 28 | + {keys.length === 0 && ( |
| 29 | + <div className="text-center text-white/50 py-2"> |
| 30 | + No metadata found. Click "Add" to create a new key-value pair. |
| 31 | + </div> |
| 32 | + )} |
| 33 | + |
| 34 | + {keys.map((key, index) => ( |
| 35 | + <div key={index} className="flex items-center gap-2"> |
| 36 | + <div className="w-full"> |
| 37 | + <EditorInspectorStringField |
| 38 | + label={key} |
| 39 | + object={this.props.object.metadata?.customMetadata ?? {}} |
| 40 | + property={key} |
| 41 | + onChange={() => this.forceUpdate()} |
| 42 | + /> |
| 43 | + </div> |
| 44 | + |
| 45 | + <Button |
| 46 | + variant="secondary" |
| 47 | + className="p-2" |
| 48 | + onClick={() => this._handleRemoveKey(key)} |
| 49 | + > |
| 50 | + <IoCloseOutline className="w-4 h-4" /> |
| 51 | + </Button> |
| 52 | + </div> |
| 53 | + ))} |
| 54 | + |
| 55 | + <Button |
| 56 | + variant="secondary" |
| 57 | + className="flex items-center gap-2 w-full" |
| 58 | + onClick={() => this._handleAddKey()} |
| 59 | + > |
| 60 | + <IoAddSharp className="w-6 h-6" /> Add |
| 61 | + </Button> |
| 62 | + </EditorInspectorSectionField> |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + private async _handleAddKey(): Promise<void> { |
| 67 | + const key = await showPrompt("Add Custom Metadata", "Enter the key name for the new metadata:"); |
| 68 | + |
| 69 | + if (!key) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Validate key name |
| 74 | + if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) { |
| 75 | + showAlert("Invalid Key Name", "Key must start with a letter or underscore and contain only letters, numbers, and underscores."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Ensure metadata exists |
| 80 | + if (!this.props.object.metadata) { |
| 81 | + this.props.object.metadata = {}; |
| 82 | + } |
| 83 | + |
| 84 | + // Ensure customMetadata exists |
| 85 | + if (!this.props.object.metadata.customMetadata) { |
| 86 | + this.props.object.metadata.customMetadata = {}; |
| 87 | + } |
| 88 | + |
| 89 | + // Check if key already exists |
| 90 | + if (key in this.props.object.metadata.customMetadata) { |
| 91 | + showAlert("Duplicate Key", `Key "${key}" already exists.`); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // Add new key with empty string value |
| 96 | + this.props.object.metadata.customMetadata[key] = ""; |
| 97 | + |
| 98 | + this.forceUpdate(); |
| 99 | + } |
| 100 | + |
| 101 | + private _handleRemoveKey(key: string): void { |
| 102 | + if (!this.props.object.metadata?.customMetadata) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + delete this.props.object.metadata.customMetadata[key]; |
| 107 | + |
| 108 | + // Clean up empty customMetadata object |
| 109 | + if (Object.keys(this.props.object.metadata.customMetadata).length === 0) { |
| 110 | + delete this.props.object.metadata.customMetadata; |
| 111 | + } |
| 112 | + |
| 113 | + this.forceUpdate(); |
| 114 | + } |
| 115 | +} |
0 commit comments