@@ -2,27 +2,47 @@ import Events from '../Events.js';
22import { Command } from '../command.js' ;
33import { createUniqueId } from '../entity.js' ;
44
5+ /**
6+ * Command to remove a component from an entity
7+ * @param editor Editor
8+ * @param payload Object containing entity (element or ID string) and component
9+ * @constructor
10+ */
511export class ComponentRemoveCommand extends Command {
6- constructor ( editor , payload ) {
12+ constructor ( editor , payload = null ) {
713 super ( editor ) ;
814
915 this . type = 'componentremove' ;
1016 this . name = 'Remove Component' ;
1117 this . updatable = false ;
1218
13- const entity = payload . entity ;
14- if ( ! entity . id ) {
15- entity . id = createUniqueId ( ) ;
19+ if ( payload !== null ) {
20+ // Handle case where entity is passed as ID string when used with multi command
21+ let entity ;
22+ if ( typeof payload . entity === 'string' ) {
23+ entity = document . querySelector ( `#${ payload . entity } :not(a-mixin)` ) ;
24+ if ( ! entity ) {
25+ console . error ( 'Entity not found with ID:' , payload . entity ) ;
26+ return ;
27+ }
28+ this . entityId = payload . entity ;
29+ } else {
30+ entity = payload . entity ;
31+ if ( ! entity . id ) {
32+ entity . id = createUniqueId ( ) ;
33+ }
34+ this . entityId = entity . id ;
35+ }
36+
37+ this . component = payload . component ;
38+
39+ const component =
40+ entity . components [ payload . component ] ??
41+ AFRAME . components [ payload . component ] ;
42+ this . value = component . isSingleProperty
43+ ? component . schema . stringify ( entity . getAttribute ( payload . component ) )
44+ : structuredClone ( entity . getDOMAttribute ( payload . component ) ) ;
1645 }
17- this . entityId = entity . id ;
18- this . component = payload . component ;
19-
20- const component =
21- entity . components [ payload . component ] ??
22- AFRAME . components [ payload . component ] ;
23- this . value = component . isSingleProperty
24- ? component . schema . stringify ( entity . getAttribute ( payload . component ) )
25- : structuredClone ( entity . getDOMAttribute ( payload . component ) ) ;
2646 }
2747
2848 execute ( nextCommandCallback ) {
@@ -49,4 +69,19 @@ export class ComponentRemoveCommand extends Command {
4969 nextCommandCallback ?. ( entity ) ;
5070 }
5171 }
72+
73+ toJSON ( ) {
74+ const output = super . toJSON ( this ) ;
75+ output . entityId = this . entityId ;
76+ output . component = this . component ;
77+ output . value = this . value ;
78+ return output ;
79+ }
80+
81+ fromJSON ( json ) {
82+ super . fromJSON ( json ) ;
83+ this . entityId = json . entityId ;
84+ this . component = json . component ;
85+ this . value = json . value ;
86+ }
5287}
0 commit comments