background.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = {}, styles = str.split(';');
  13. utils.each(styles, function (v) {
  14. var index = v.indexOf(':'),
  15. key = utils.trim(v.substr(0, index)).toLowerCase();
  16. key && (obj[key] = utils.trim(v.substr(index + 1) || ''));
  17. });
  18. return obj;
  19. }
  20. function setBackground(obj) {
  21. if (obj) {
  22. var styles = [];
  23. for (var name in obj) {
  24. if (obj.hasOwnProperty(name)) {
  25. styles.push(name + ":" + obj[name] + '; ');
  26. }
  27. }
  28. utils.cssRule(cssRuleId, styles.length ? ('body{' + styles.join("") + '}') : '', me.document);
  29. } else {
  30. utils.cssRule(cssRuleId, '', me.document)
  31. }
  32. }
  33. //重写editor.hasContent方法
  34. var orgFn = me.hasContents;
  35. me.hasContents = function(){
  36. if(me.queryCommandValue('background')){
  37. return true
  38. }
  39. return orgFn.apply(me,arguments);
  40. };
  41. return {
  42. bindEvents: {
  43. 'getAllHtml': function (type, headHtml) {
  44. var body = this.body,
  45. su = domUtils.getComputedStyle(body, "background-image"),
  46. url = "";
  47. if (su.indexOf(me.options.imagePath) > 0) {
  48. url = su.substring(su.indexOf(me.options.imagePath), su.length - 1).replace(/"|\(|\)/ig, "");
  49. } else {
  50. url = su != "none" ? su.replace(/url\("?|"?\)/ig, "") : "";
  51. }
  52. var html = '<style type="text/css">body{';
  53. var bgObj = {
  54. "background-color": domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  55. 'background-image': url ? 'url(' + url + ')' : '',
  56. 'background-repeat': domUtils.getComputedStyle(body, "background-repeat") || "",
  57. 'background-position': browser.ie ? (domUtils.getComputedStyle(body, "background-position-x") + " " + domUtils.getComputedStyle(body, "background-position-y")) : domUtils.getComputedStyle(body, "background-position"),
  58. 'height': domUtils.getComputedStyle(body, "height")
  59. };
  60. for (var name in bgObj) {
  61. if (bgObj.hasOwnProperty(name)) {
  62. html += name + ":" + bgObj[name] + "; ";
  63. }
  64. }
  65. html += '}</style> ';
  66. headHtml.push(html);
  67. },
  68. 'aftersetcontent': function () {
  69. if(isSetColored == false) setBackground();
  70. }
  71. },
  72. inputRule: function (root) {
  73. isSetColored = false;
  74. utils.each(root.getNodesByTagName('p'), function (p) {
  75. var styles = p.getAttr('data-background');
  76. if (styles) {
  77. isSetColored = true;
  78. setBackground(stringToObj(styles));
  79. p.parentNode.removeChild(p);
  80. }
  81. })
  82. },
  83. outputRule: function (root) {
  84. var me = this,
  85. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  86. if (styles) {
  87. root.appendChild(UE.uNode.createElement('<p style="display:none;" data-background="' + utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) + '"><br/></p>'));
  88. }
  89. },
  90. commands: {
  91. 'background': {
  92. execCommand: function (cmd, obj) {
  93. setBackground(obj);
  94. },
  95. queryCommandValue: function () {
  96. var me = this,
  97. styles = (utils.cssRule(cssRuleId, me.document) || '').replace(/[\n\r]+/g, '').match(reg);
  98. return styles ? stringToObj(styles[1]) : null;
  99. },
  100. notNeedUndo: true
  101. }
  102. }
  103. }
  104. });