使用 CKEditor 动态主体颜色

发布于 2024-09-25 08:03:15 字数 867 浏览 3 评论 0原文

我在 CKEditor 上遇到一个问题想要解决。我使用 jQuery 颜色选择器将背景颜色添加到 DIV 标签。然后我允许用户使用 CKEditor 编辑 Div 标签内容。但是,我注意到没有一种简单的方法可以获取 div 标签的背景颜色,然后在编辑器加载时将其设置为 CKEditor 的背景颜色。

我已经阅读了 bodyClass 和 bodyId,但不认为这些可以解决我的问题。我没有类元素,但有一个内联样式声明,就像

<div class="tp-header" style="background-color:#CCCCCC;">content</div>

我调用 CKEditor 一样,如下所示:

var editorId = 'editor1';
var instance = CKEDITOR.instances[editorId];
var color = $('.' + headerElementClass).css('background-color');
if (instance) { CKEDITOR.remove(instance); }
$('#' + editorId).ckeditor({ toolbar: 'BasicHtml', height: '100px', width: '500px', fullPage: false, bodyClass : 'background-color:' + color });
$('#' + editorId).val($('.' + headerElementClass).html());

注意 bodyClass 的失败使用。有什么办法可以做到这一点吗?我在网站上四处寻找答案,但没有找到。我希望这里有人能给出答案。

I have a situation on CKEditor that I would like to resolve. I use a jQuery color picker to add background color to a DIV tag. Then I allow the user to edit the Div tag contents using CKEditor. However, I noticed that there isn't a simple way to take the div tag's background color and then make that as the background color of the CKEditor when the editor loads up.

I have read up on bodyClass and bodyId and do not think that these solve my problem. I do not have a class element but an inline style declaration like

<div class="tp-header" style="background-color:#CCCCCC;">content</div>

I invoke the CKEditor as follows:

var editorId = 'editor1';
var instance = CKEDITOR.instances[editorId];
var color = $('.' + headerElementClass).css('background-color');
if (instance) { CKEDITOR.remove(instance); }
$('#' + editorId).ckeditor({ toolbar: 'BasicHtml', height: '100px', width: '500px', fullPage: false, bodyClass : 'background-color:' + color });
$('#' + editorId).val($('.' + headerElementClass).html());

Notice the failed usage of bodyClass. Is there any way to do this? I have scourged around the site for answers but couldn't find one. I hope someone here has the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-10-02 08:03:16

我正在思考这个问题,并想出了一个更简单的解决方案。
我没有使用 CKEditor jQuery 适配器,因此您可能需要修改它以适合您的情况。

我确实使用标准 JavaScript 集成方法对其进行了测试。

快速概述:
设置变量。
创建编辑器实例。

插入此“addCss”函数调用:

CKEDITOR.instances[editorId].addCss( 'body { background-color: '+color+'; }' );

这就是它的全部内容。以下是基于您的代码的示例:

// I added the "id" attribute:
<div id="editor1" class="tp-header" style="background-color:#CCCCCC;">content</div>

// Declare the variables, I added "headerElementClass".
var headerElementClass = "tp-header";
var color = $('.' + headerElementClass).css('background-color');
var editorId = 'editor1';

// Create the instance.
var instanceOne = CKEDITOR.replace( editorId,
{
  toolbar: 'Basic',
  height: '100px',
  width: '500px',
  fullPage: false,
  customConfig : 'yourCustomConfigFileIfUsed.js'
 });

// Insert the "addCss" function call:
instanceOne.addCss( 'body { background-color: '+color+'; }' );

如果您愿意,可以将 addCss 函数调用移至您的配置文件(将其放置在 editorConfig 函数之外)。

祝你好运,


离开更复杂的方法,有人可能会发现这些概念很有用。

您可以使用( bodyClass: 'nameOfClass' ),然后为该类的背景颜色属性分配一个值。但这很困难,因为你有动态背景颜色。

要动态分配背景颜色,您可以执行以下操作:
从您的代码开始并继续使用 jQuery:

var editorId = 'editor1';
var instance = CKEDITOR.instances[editorId];
var color = $('.' + headerElementClass).css('background-color');

// Create a unique body id for this instance "editor1" ( bodyIdForeditor1 )
var idForBody = 'bodyIdFor' + editorId;

if (instance) { CKEDITOR.remove(instance); }

// Use bodyId instead of the original bodyClass assignment
$('#' + editorId).ckeditor({ 
  toolbar: 'BasicHtml', 
  height: '100px', 
  width: '500px', 
  fullPage: false, 
  bodyId : idForBody 
});

$('#' + editorId).val($('.' + headerElementClass).html());

// After both the document and editor instance are ready, 
// assign the background color to the body

// Wait for the document ready event
$(document).ready(function(){

  // Wait for the instanceReady event to fire for this (editor1) instance
  CKEDITOR.instances.editor1.on( 'instanceReady', 
    function( instanceReadyEventObj )
    {
      var currentEditorInstance = instanceReadyEventObj.editor;
      var iframeDoc=null;

      // Create a function because these steps will be repeated
      function setIframeBackground()
      {
        // The CKEditor content iframe doesn't have a Name, Id or Class
        // So, we'll assign an ID to the iframe 
        // it's inside a table data cell that does have an Id.
        // The Id of the data cell is "cke_contents_editor1" 
        // Note that the instance name is the last part of the Id
        // I'll follow this convention and use an Id of "cke_contents_iframe_editor1" 

        $("#cke_contents_editor1 iframe").attr("id", "cke_contents_iframe_editor1");

        // Now use the iframe Id to get the iframe document object
        // We'll need this to set the context and access items inside the iframe

        $('#cke_iframe_editor1').each( 
          function(){ iframeDoc=this.contentWindow.document;}
        );

        // Finally we can access the iframe body and set the background color.
        // We set the Id of the body when we created the instance (bodyId : idForBody).
        // We use the iframe document object (iframeDoc) to set the context.
        // We use the "color" variable created earlier

        $('#' + idForBody, iframeDoc).css("background-color", color);
      }

      // Call the function to set the color when the editor instance first loads
      setIframeBackground();

      // When the user switches to "source" view mode, the iframe is destroyed
      // So we need to set the color again when they switch back to "wysiwyg" mode

      // Watch for the "mode" event and check if we're in "wysiwyg" mode
      currentEditorInstance.on( 'mode', function()
      {
        if(currentEditorInstance.mode == 'wysiwyg')
          setIframeBackground();
      });
    }
  );
});

Be Well,

I was thinking about this and I came up with a much simpler solution.
I'm not using the CKEditor jQuery adapter, so you may need to modify it to fit your situation.

I did test it using the standard JavaScript integration approach.

Quick Overview:
Set the variables.
Create the editor instance.

Insert this "addCss" function call:

CKEDITOR.instances[editorId].addCss( 'body { background-color: '+color+'; }' );

That's all there is to it. Here's a sample based on your code:

// I added the "id" attribute:
<div id="editor1" class="tp-header" style="background-color:#CCCCCC;">content</div>

// Declare the variables, I added "headerElementClass".
var headerElementClass = "tp-header";
var color = $('.' + headerElementClass).css('background-color');
var editorId = 'editor1';

// Create the instance.
var instanceOne = CKEDITOR.replace( editorId,
{
  toolbar: 'Basic',
  height: '100px',
  width: '500px',
  fullPage: false,
  customConfig : 'yourCustomConfigFileIfUsed.js'
 });

// Insert the "addCss" function call:
instanceOne.addCss( 'body { background-color: '+color+'; }' );

The addCss function call can be moved to your config file if you prefer (place it outside the editorConfig function).

Be Well,
Joe


Leaving the more complicated approach, someone might find the concepts useful.

You could use ( bodyClass: 'nameOfClass' ), then assign a value to the background-color property of that class. But that's difficult because you have a dynamic background color.

To assign the background color dynamically you could do something like this:
Starting with your code and continuing the use of jQuery:

var editorId = 'editor1';
var instance = CKEDITOR.instances[editorId];
var color = $('.' + headerElementClass).css('background-color');

// Create a unique body id for this instance "editor1" ( bodyIdForeditor1 )
var idForBody = 'bodyIdFor' + editorId;

if (instance) { CKEDITOR.remove(instance); }

// Use bodyId instead of the original bodyClass assignment
$('#' + editorId).ckeditor({ 
  toolbar: 'BasicHtml', 
  height: '100px', 
  width: '500px', 
  fullPage: false, 
  bodyId : idForBody 
});

$('#' + editorId).val($('.' + headerElementClass).html());

// After both the document and editor instance are ready, 
// assign the background color to the body

// Wait for the document ready event
$(document).ready(function(){

  // Wait for the instanceReady event to fire for this (editor1) instance
  CKEDITOR.instances.editor1.on( 'instanceReady', 
    function( instanceReadyEventObj )
    {
      var currentEditorInstance = instanceReadyEventObj.editor;
      var iframeDoc=null;

      // Create a function because these steps will be repeated
      function setIframeBackground()
      {
        // The CKEditor content iframe doesn't have a Name, Id or Class
        // So, we'll assign an ID to the iframe 
        // it's inside a table data cell that does have an Id.
        // The Id of the data cell is "cke_contents_editor1" 
        // Note that the instance name is the last part of the Id
        // I'll follow this convention and use an Id of "cke_contents_iframe_editor1" 

        $("#cke_contents_editor1 iframe").attr("id", "cke_contents_iframe_editor1");

        // Now use the iframe Id to get the iframe document object
        // We'll need this to set the context and access items inside the iframe

        $('#cke_iframe_editor1').each( 
          function(){ iframeDoc=this.contentWindow.document;}
        );

        // Finally we can access the iframe body and set the background color.
        // We set the Id of the body when we created the instance (bodyId : idForBody).
        // We use the iframe document object (iframeDoc) to set the context.
        // We use the "color" variable created earlier

        $('#' + idForBody, iframeDoc).css("background-color", color);
      }

      // Call the function to set the color when the editor instance first loads
      setIframeBackground();

      // When the user switches to "source" view mode, the iframe is destroyed
      // So we need to set the color again when they switch back to "wysiwyg" mode

      // Watch for the "mode" event and check if we're in "wysiwyg" mode
      currentEditorInstance.on( 'mode', function()
      {
        if(currentEditorInstance.mode == 'wysiwyg')
          setIframeBackground();
      });
    }
  );
});

Be Well,
Joe

只等公子 2024-10-02 08:03:16

codewaggle 的答案是一个很好的答案,但是如果您想在编辑器的 元素上设置内联样式,您也可以这样做,使用:

editor.document.getBody().setStyle()

editor.document.getBody().setStyles()

但是,您需要每次重做一次调用 editor.setData() 后以及用户切换回所见即所得模式(从源模式)后的时间,因为这些事情重新创建了编辑器 iframe。为此,请使用一个函数设置样式,例如 setEditorStyle首先检查 editor.mode==='wysiwyg' (否则 editor.document 为 null),然后将该函数添加为 instanceReadymode 事件的事件侦听器;如果您曾经调用过 setData() 并且不想随后手动调用它,也许还有 contentDom 事件。

请参阅此处这里

codewaggle's answer is a good one, but if you want to set inline styles on the editor's <body> element, you can do that too, using:

editor.document.getBody().setStyle()

or

editor.document.getBody().setStyles()

However, you'll need to redo this every time after calling editor.setData() and after the user switches back to wysiwyg mode (from source mode), because these things re-create the editor iframe. To do all that, set your styles using a function, say setEditorStyle, in which you check first that editor.mode==='wysiwyg' (editor.document is null otherwise), then add that function as an event listener for the instanceReady and mode events; and perhaps also the contentDom event if you ever call setData() and don't want to call it manually afterwards.

See some other StackOverflow answers here and here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文