plugin.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Created with JetBrains PhpStorm.
  3. * User: campaign
  4. * Date: 10/8/13
  5. * Time: 6:15 PM
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. UE.plugin = (function() {
  9. var _plugins = {};
  10. return {
  11. register: function(pluginName, fn, oldOptionName, afterDisabled) {
  12. if (oldOptionName && utils.isFunction(oldOptionName)) {
  13. afterDisabled = oldOptionName;
  14. oldOptionName = null;
  15. }
  16. _plugins[pluginName] = {
  17. optionName: oldOptionName || pluginName,
  18. execFn: fn,
  19. //当插件被禁用时执行
  20. afterDisabled: afterDisabled
  21. };
  22. },
  23. load: function(editor) {
  24. utils.each(_plugins, function(plugin) {
  25. var _export = plugin.execFn.call(editor);
  26. if (editor.options[plugin.optionName] !== false) {
  27. if (_export) {
  28. //后边需要再做扩展
  29. utils.each(_export, function(v, k) {
  30. switch (k.toLowerCase()) {
  31. case "shortcutkey":
  32. editor.addshortcutkey(v);
  33. break;
  34. case "bindevents":
  35. utils.each(v, function(fn, eventName) {
  36. editor.addListener(eventName, fn);
  37. });
  38. break;
  39. case "bindmultievents":
  40. utils.each(utils.isArray(v) ? v : [v], function(event) {
  41. var types = utils.trim(event.type).split(/\s+/);
  42. utils.each(types, function(eventName) {
  43. editor.addListener(eventName, event.handler);
  44. });
  45. });
  46. break;
  47. case "commands":
  48. utils.each(v, function(execFn, execName) {
  49. editor.commands[execName] = execFn;
  50. });
  51. break;
  52. case "outputrule":
  53. editor.addOutputRule(v);
  54. break;
  55. case "inputrule":
  56. editor.addInputRule(v);
  57. break;
  58. case "defaultoptions":
  59. editor.setOpt(v);
  60. }
  61. });
  62. }
  63. } else if (plugin.afterDisabled) {
  64. plugin.afterDisabled.call(editor);
  65. }
  66. });
  67. //向下兼容
  68. utils.each(UE.plugins, function(plugin) {
  69. plugin.call(editor);
  70. });
  71. },
  72. run: function(pluginName, editor) {
  73. var plugin = _plugins[pluginName];
  74. if (plugin) {
  75. plugin.exeFn.call(editor);
  76. }
  77. }
  78. };
  79. })();