simditor-html.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module unless amdModuleId is set
  4. define('simditor-html', ["jquery","simditor","html_beautify"], function (a0,b1,c2) {
  5. return (root['HTMLButton'] = factory(a0,b1,c2));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like environments that support module.exports,
  10. // like Node.
  11. module.exports = factory(require("jquery"),require("simditor"),require("js_beautify"));
  12. } else {
  13. root['SimditorHTML'] = factory(jQuery,Simditor,html_beautify);
  14. }
  15. }(this, function ($, Simditor, beautify) {
  16. var HTMLButton,
  17. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  18. hasProp = {}.hasOwnProperty,
  19. slice = [].slice;
  20. HTMLButton = (function(superClass) {
  21. extend(HTMLButton, superClass);
  22. function HTMLButton() {
  23. return HTMLButton.__super__.constructor.apply(this, arguments);
  24. }
  25. HTMLButton.prototype.name = 'html';
  26. HTMLButton.prototype.icon = 'html5';
  27. HTMLButton.prototype.needFocus = false;
  28. HTMLButton.prototype._init = function() {
  29. HTMLButton.__super__._init.call(this);
  30. this.editor.textarea.on('focus', (function(_this) {
  31. return function(e) {
  32. return _this.editor.el.addClass('focus').removeClass('error');
  33. };
  34. })(this));
  35. this.editor.textarea.on('blur', (function(_this) {
  36. return function(e) {
  37. _this.editor.el.removeClass('focus');
  38. return _this.editor.setValue(_this.editor.textarea.val());
  39. };
  40. })(this));
  41. return this.editor.textarea.on('input', (function(_this) {
  42. return function(e) {
  43. return _this._resizeTextarea();
  44. };
  45. })(this));
  46. };
  47. HTMLButton.prototype.status = function() {};
  48. HTMLButton.prototype.command = function() {
  49. var button, i, len, ref;
  50. this.editor.blur();
  51. this.editor.el.toggleClass('simditor-html');
  52. this.editor.htmlMode = this.editor.el.hasClass('simditor-html');
  53. if (this.editor.htmlMode) {
  54. this.editor.hidePopover();
  55. this.editor.textarea.val(this.beautifyHTML(this.editor.textarea.val()));
  56. this._resizeTextarea();
  57. } else {
  58. this.editor.setValue(this.editor.textarea.val());
  59. }
  60. ref = this.editor.toolbar.buttons;
  61. for (i = 0, len = ref.length; i < len; i++) {
  62. button = ref[i];
  63. if (button.name === 'html') {
  64. button.setActive(this.editor.htmlMode);
  65. } else {
  66. button.setDisabled(this.editor.htmlMode);
  67. }
  68. }
  69. return null;
  70. };
  71. HTMLButton.prototype.beautifyHTML = function() {
  72. var args;
  73. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  74. if (beautify.html) {
  75. return beautify.html.apply(beautify, args);
  76. } else {
  77. return beautify.apply(null, args);
  78. }
  79. };
  80. HTMLButton.prototype._resizeTextarea = function() {
  81. this._textareaPadding || (this._textareaPadding = this.editor.textarea.innerHeight() - this.editor.textarea.height());
  82. return this.editor.textarea.height(this.editor.textarea[0].scrollHeight - this._textareaPadding);
  83. };
  84. return HTMLButton;
  85. })(Simditor.Button);
  86. Simditor.Toolbar.addButton(HTMLButton);
  87. return HTMLButton;
  88. }));