RemoveScriptCommand.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function RemoveScriptCommand( editor, object, script ) {
  9. Command.call( this, editor );
  10. this.type = 'RemoveScriptCommand';
  11. this.name = 'Remove Script';
  12. this.object = object;
  13. this.script = script;
  14. if ( this.object && this.script ) {
  15. this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  16. }
  17. }
  18. RemoveScriptCommand.prototype = {
  19. execute: function () {
  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: function () {
  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: function () {
  34. var output = Command.prototype.toJSON.call( this );
  35. output.objectUuid = this.object.uuid;
  36. output.script = this.script;
  37. output.index = this.index;
  38. return output;
  39. },
  40. fromJSON: function ( json ) {
  41. Command.prototype.fromJSON.call( this, json );
  42. this.script = json.script;
  43. this.index = json.index;
  44. this.object = this.editor.objectByUuid( json.objectUuid );
  45. }
  46. };
  47. export { RemoveScriptCommand };