RemoveScriptCommand.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param script javascript object
  6. * @constructor
  7. */
  8. class RemoveScriptCommand extends Command {
  9. constructor( editor, object = null, script = '' ) {
  10. super( editor );
  11. this.type = 'RemoveScriptCommand';
  12. this.name = editor.strings.getKey( 'command/RemoveScript' );
  13. this.object = object;
  14. this.script = script;
  15. if ( this.object !== null && this.script !== '' ) {
  16. this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  17. }
  18. }
  19. execute() {
  20. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  21. if ( this.index !== - 1 ) {
  22. this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );
  23. }
  24. this.editor.signals.scriptRemoved.dispatch( this.script );
  25. }
  26. undo() {
  27. if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
  28. this.editor.scripts[ this.object.uuid ] = [];
  29. }
  30. this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );
  31. this.editor.signals.scriptAdded.dispatch( this.script );
  32. }
  33. toJSON() {
  34. const output = super.toJSON( this );
  35. output.objectUuid = this.object.uuid;
  36. output.script = this.script;
  37. output.index = this.index;
  38. return output;
  39. }
  40. fromJSON( json ) {
  41. super.fromJSON( json );
  42. this.script = json.script;
  43. this.index = json.index;
  44. this.object = this.editor.objectByUuid( json.objectUuid );
  45. }
  46. }
  47. export { RemoveScriptCommand };