background.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * 背景插件,为UEditor提供设置背景功能
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. UE.plugin.register("background", function() {
  7. var me = this,
  8. cssRuleId = "editor_background",
  9. isSetColored,
  10. reg = new RegExp("body[\\s]*\\{(.+)\\}", "i");
  11. function stringToObj(str) {
  12. var obj = {},
  13. styles = str.split(";");
  14. utils.each(styles, function(v) {
  15. var index = v.indexOf(":"),
  16. key = utils.trim(v.substr(0, index)).toLowerCase();
  17. key && (obj[key] = utils.trim(v.substr(index + 1) || ""));
  18. });
  19. return obj;
  20. }
  21. function setBackground(obj) {
  22. if (obj) {
  23. var styles = [];
  24. for (var name in obj) {
  25. if (obj.hasOwnProperty(name)) {
  26. styles.push(name + ":" + obj[name] + "; ");
  27. }
  28. }
  29. utils.cssRule(
  30. cssRuleId,
  31. styles.length ? "body{" + styles.join("") + "}" : "",
  32. me.document
  33. );
  34. } else {
  35. utils.cssRule(cssRuleId, "", me.document);
  36. }
  37. }
  38. //重写editor.hasContent方法
  39. var orgFn = me.hasContents;
  40. me.hasContents = function() {
  41. if (me.queryCommandValue("background")) {
  42. return true;
  43. }
  44. return orgFn.apply(me, arguments);
  45. };
  46. return {
  47. bindEvents: {
  48. getAllHtml: function(type, headHtml) {
  49. var body = this.body,
  50. su = domUtils.getComputedStyle(body, "background-image"),
  51. url = "";
  52. if (su.indexOf(me.options.imagePath) > 0) {
  53. url = su
  54. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  55. .replace(/"|\(|\)/gi, "");
  56. } else {
  57. url = su != "none" ? su.replace(/url\("?|"?\)/gi, "") : "";
  58. }
  59. var html = '<style type="text/css">body{';
  60. var bgObj = {
  61. "background-color":
  62. domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  63. "background-image": url ? "url(" + url + ")" : "",
  64. "background-repeat":
  65. domUtils.getComputedStyle(body, "background-repeat") || "",
  66. "background-position": browser.ie
  67. ? domUtils.getComputedStyle(body, "background-position-x") +
  68. " " +
  69. domUtils.getComputedStyle(body, "background-position-y")
  70. : domUtils.getComputedStyle(body, "background-position"),
  71. height: domUtils.getComputedStyle(body, "height")
  72. };
  73. for (var name in bgObj) {
  74. if (bgObj.hasOwnProperty(name)) {
  75. html += name + ":" + bgObj[name] + "; ";
  76. }
  77. }
  78. html += "}</style> ";
  79. headHtml.push(html);
  80. },
  81. aftersetcontent: function() {
  82. if (isSetColored == false) setBackground();
  83. }
  84. },
  85. inputRule: function(root) {
  86. isSetColored = false;
  87. utils.each(root.getNodesByTagName("p"), function(p) {
  88. var styles = p.getAttr("data-background");
  89. if (styles) {
  90. isSetColored = true;
  91. setBackground(stringToObj(styles));
  92. p.parentNode.removeChild(p);
  93. }
  94. });
  95. },
  96. outputRule: function(root) {
  97. var me = this,
  98. styles = (utils.cssRule(cssRuleId, me.document) || "")
  99. .replace(/[\n\r]+/g, "")
  100. .match(reg);
  101. if (styles) {
  102. root.appendChild(
  103. UE.uNode.createElement(
  104. '<p style="display:none;" data-background="' +
  105. utils.trim(styles[1].replace(/"/g, "").replace(/[\s]+/g, " ")) +
  106. '"><br/></p>'
  107. )
  108. );
  109. }
  110. },
  111. commands: {
  112. background: {
  113. execCommand: function(cmd, obj) {
  114. setBackground(obj);
  115. },
  116. queryCommandValue: function() {
  117. var me = this,
  118. styles = (utils.cssRule(cssRuleId, me.document) || "")
  119. .replace(/[\n\r]+/g, "")
  120. .match(reg);
  121. return styles ? stringToObj(styles[1]) : null;
  122. },
  123. notNeedUndo: true
  124. }
  125. }
  126. };
  127. });