command_manager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var MultiHashHandler = require("../keyboard/hash_handler").MultiHashHandler;
  5. var EventEmitter = require("../lib/event_emitter").EventEmitter;
  6. /**
  7. * @class CommandManager
  8. *
  9. **/
  10. /**
  11. * new CommandManager(platform, commands)
  12. * @param {String} platform Identifier for the platform; must be either `"mac"` or `"win"`
  13. * @param {Array} commands A list of commands
  14. *
  15. **/
  16. var CommandManager = function(platform, commands) {
  17. MultiHashHandler.call(this, commands, platform);
  18. this.byName = this.commands;
  19. this.setDefaultHandler("exec", function(e) {
  20. return e.command.exec(e.editor, e.args || {});
  21. });
  22. };
  23. oop.inherits(CommandManager, MultiHashHandler);
  24. (function() {
  25. oop.implement(this, EventEmitter);
  26. this.exec = function(command, editor, args) {
  27. if (Array.isArray(command)) {
  28. for (var i = command.length; i--; ) {
  29. if (this.exec(command[i], editor, args)) return true;
  30. }
  31. return false;
  32. }
  33. if (typeof command === "string")
  34. command = this.commands[command];
  35. if (!command)
  36. return false;
  37. if (editor && editor.$readOnly && !command.readOnly)
  38. return false;
  39. var e = {editor: editor, command: command, args: args};
  40. e.returnValue = this._emit("exec", e);
  41. this._signal("afterExec", e);
  42. return e.returnValue === false ? false : true;
  43. };
  44. this.toggleRecording = function(editor) {
  45. if (this.$inReplay)
  46. return;
  47. editor && editor._emit("changeStatus");
  48. if (this.recording) {
  49. this.macro.pop();
  50. this.removeEventListener("exec", this.$addCommandToMacro);
  51. if (!this.macro.length)
  52. this.macro = this.oldMacro;
  53. return this.recording = false;
  54. }
  55. if (!this.$addCommandToMacro) {
  56. this.$addCommandToMacro = function(e) {
  57. this.macro.push([e.command, e.args]);
  58. }.bind(this);
  59. }
  60. this.oldMacro = this.macro;
  61. this.macro = [];
  62. this.on("exec", this.$addCommandToMacro);
  63. return this.recording = true;
  64. };
  65. this.replay = function(editor) {
  66. if (this.$inReplay || !this.macro)
  67. return;
  68. if (this.recording)
  69. return this.toggleRecording(editor);
  70. try {
  71. this.$inReplay = true;
  72. this.macro.forEach(function(x) {
  73. if (typeof x == "string")
  74. this.exec(x, editor);
  75. else
  76. this.exec(x[0], editor, x[1]);
  77. }, this);
  78. } finally {
  79. this.$inReplay = false;
  80. }
  81. };
  82. this.trimMacro = function(m) {
  83. return m.map(function(x){
  84. if (typeof x[0] != "string")
  85. x[0] = x[0].name;
  86. if (!x[1])
  87. x = x[0];
  88. return x;
  89. });
  90. };
  91. }).call(CommandManager.prototype);
  92. exports.CommandManager = CommandManager;
  93. });