[英]Extjs how to change background color of htmleditor dynamically
`I have added a custom button in htmleditor which will change the background color of the preview area.I have tried everything but can't seem to get it `我在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) {
}
}
}
});
}
}
}
});`
` `
I was hoping I could use this solution , but it looks like that only worked in Ext JS 3, unless I was doing something wrong. 我希望可以使用此解决方案 ,但是除非我做错了,否则它似乎只能在Ext JS 3中使用。 I started poking around with the editor's
textareaEl
and came up with a very ugly solution... mainly because they're using an iframe under the hood. 我开始四处浏览编辑器的
textareaEl
并提出了一个非常丑陋的解决方案……主要是因为他们在后台使用iframe。 Here's my code: 这是我的代码:
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]
});
}
});
});
Like I said, I don't like this solution, but it's a solution... I'd love to hear the proper way... I tried messing around with CSS classes, but didn't produce anything there. 就像我说的那样,我不喜欢这种解决方案,但是它是一种解决方案。我很想听听正确的方法……我尝试弄乱CSS类,但是在那里什么也没产生。
I found another solution to this for posterity. 我为后代找到了另一种解决方案。 You can use the getEditorBody() method available on the HTMLeditor class to get the preview area, then dynamically style it.
您可以使用HTMLeditor类上可用的getEditorBody()方法来获取预览区域,然后对其进行动态样式设置。
In ExtJS 6: 在ExtJS 6中:
{
xtype: 'htmleditor',
listeners: {
initialize: 'onEditorInitialize'
}
}
onEditorInitialize: function (editor) {
const bodyArea = editor.getEditorBody();
bodyArea.style.backgroundColor = '#333';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.