Extjs如何动态更改htmleditor的背景颜色

问题描述 投票:0回答:2

`我在htmleditor中添加了一个自定义按钮,它将改变预览区域的背景颜色。我已经尝试了所有东西,但似乎无法得到它

Ext.onReady(function () {
   Ext.tip.QuickTipManager.init(); // enable tooltips
 new Ext.panel.Panel({
    title: 'HTML Editor',
    renderTo: Ext.getBody(),
    width: 550,
    height: 250,
    frame: true,
    layout: 'fit',
    items: {
        xtype: 'htmleditor',
        enableColors: false,
        enableAlignments: false,
        listeners:{
                render:function(){
                        this.getToolbar().add({
                                xtype:'button',
                                scope: this,
                                tooltip:'Set background color',                             
                                iconCls : 'btn-charttheme',
                                menu : {
                                    xtype : 'colormenu',
                                    listeners : {
                                        select :function(picker,selColor) {

                                            }
                                        }
                                    }
                                });
                        }
                    }
              }
});`

`

extjs extjs4
2个回答
0
投票

我希望我可以使用this solution,但它看起来只适用于Ext JS 3,除非我做错了什么。我开始和编辑的textareaEl一起探索并想出一个非常丑陋的解决方案......主要是因为他们在引擎盖下使用了iframe。这是我的代码:

Ext.onReady(function () {
  Ext.application({
    name: 'Fiddle',
    launch: function () {
      Ext.tip.QuickTipManager.init(); // enable tooltips
      var myEditor = Ext.create('Ext.form.field.HtmlEditor', {
        enableColors: false,
        enableAlignments: false,
        listeners: {
          render: onRenderEditor
        }
      });
      function onRenderEditor(editor) {
        this.getToolbar().add({
          xtype: 'button',
          scope: this,
          tooltip: 'Set background color',
          iconCls: 'btn-charttheme',
          menu: {
            xtype: 'colormenu',
            listeners: {
              select: function (picker, selColor) {
                if (editor.iframeEl) {
                /* This is very hacky... we're getting the textareaEl, which
                 * was provided to us, and getting its next sibling, which is
                 * the iframe... and then we're probing the iframe for the
                 * body and changing its background-color to the selected hex */
                  var iframe = editor.iframeEl.dom;
                  if (iframe) {
                    var doc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
                    if (doc && doc.body && doc.body.style) {
                      doc.body.style['background-color'] = '#' + selColor;
                      /*txtTextarea = Ext.fly(rb).down('textarea');
                       txtTextarea.dom.style.color = 'yellow';
                       txtTextarea.dom.style.cssText += 'background: olive !important';*/
                    }
                  }
                }
              }
            }
          }
        });
      }
      new Ext.panel.Panel({
        title: 'HTML Editor',
        renderTo: Ext.getBody(),
        width: 550,
        height: 250,
        frame: true,
        layout: 'fit',
        items: [myEditor]
      });
    }
  });
});

就像我说的那样,我不喜欢这个解决方案,但它是一个解决方案......我很想听听正确的方法......我试着搞乱CSS类,但没有产生任何东西。


0
投票

后来我找到了另一种解决方案。您可以使用HTMLeditor类上提供的getEditorBody()方法来获取预览区域,然后对其进行动态设置。

在ExtJS 6中:

{
    xtype: 'htmleditor',
    listeners: {
        initialize: 'onEditorInitialize'
    }
}

onEditorInitialize: function (editor) {
    const bodyArea = editor.getEditorBody();
    bodyArea.style.backgroundColor = '#333';
}
© www.soinside.com 2019 - 2024. All rights reserved.