MultiCmdsCommand.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param cmdArray array containing command objects
  5. * @constructor
  6. */
  7. class MultiCmdsCommand extends Command {
  8. constructor( editor, cmdArray = [] ) {
  9. super( editor );
  10. this.type = 'MultiCmdsCommand';
  11. this.name = editor.strings.getKey( 'command/MultiCmds' );
  12. this.cmdArray = cmdArray;
  13. }
  14. execute() {
  15. this.editor.signals.sceneGraphChanged.active = false;
  16. for ( let i = 0; i < this.cmdArray.length; i ++ ) {
  17. this.cmdArray[ i ].execute();
  18. }
  19. this.editor.signals.sceneGraphChanged.active = true;
  20. this.editor.signals.sceneGraphChanged.dispatch();
  21. }
  22. undo() {
  23. this.editor.signals.sceneGraphChanged.active = false;
  24. for ( let i = this.cmdArray.length - 1; i >= 0; i -- ) {
  25. this.cmdArray[ i ].undo();
  26. }
  27. this.editor.signals.sceneGraphChanged.active = true;
  28. this.editor.signals.sceneGraphChanged.dispatch();
  29. }
  30. toJSON() {
  31. const output = super.toJSON( this );
  32. const cmds = [];
  33. for ( let i = 0; i < this.cmdArray.length; i ++ ) {
  34. cmds.push( this.cmdArray[ i ].toJSON() );
  35. }
  36. output.cmds = cmds;
  37. return output;
  38. }
  39. fromJSON( json ) {
  40. super.fromJSON( json );
  41. const cmds = json.cmds;
  42. for ( let i = 0; i < cmds.length; i ++ ) {
  43. const cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  44. cmd.fromJSON( cmds[ i ] );
  45. this.cmdArray.push( cmd );
  46. }
  47. }
  48. }
  49. export { MultiCmdsCommand };