简体   繁体   English

在TinyMCE或CKEditor中运行时更改内容CSS

[英]Changing content CSS on the run in TinyMCE or CKEditor

I have a form in which I want to edit a HTML template. 我有一个要编辑HTML模板的表单。 It has 2 textareas, one for the HTML and another one for the CSS. 它有2个文本区域,一个用于HTML,另一个用于CSS。

I'd like to use either TinyMCE or CKEditor for the HTML textarea. 我想将TinyMCE或CKEditor用于HTML文本区域。

Is there any way to change the content CSS in either of them to match the CSS in the CSS textarea on the run, so when I change the CSS it is automatically loaded into the editor? 有什么方法可以更改其中任何一个的内容CSS,使其与运行中的CSS textarea中的CSS相匹配,因此,当我更改CSS时,它会自动加载到编辑器中吗?

Thanks. 谢谢。

I have no experience with CKEditor, but i know that it is possible with TinyMce. 我没有CKEditor的经验,但我知道TinyMce是可能的。 What you need to do is to write an own plugin which will provide the necessary functionality. 您需要做的是编写一个自己的插件,该插件将提供必要的功能。

OnNodeChange in the 2nd textarea (the one with your css) you need to update the head of the first editors iframe. 您需要在第二个文本区域(带有CSS的文本区域)中的OnNodeChange更新第一个编辑器iframe的头部。 This code snippet to be executed on a special action (for example onNodeChange) should point you into the right direction: 在特殊操作(例如onNodeChange)上执行的以下代码片段应为您指明正确的方向:

var DEBUG = false;
var css_code = tinymce.editors[1].getContent(); // get content of 2nd editorinstance on page (your css)


iframe_id = tinymce.editors[0].id+'_ifr';

with(document.getElementById(iframe_id).contentWindow){
    var h=document.getElementsByTagName("head");
    if (!h.length) {
        if (DEBUG) console.log('length of h is null');
        return;
    }
    var newStyleSheet=document.createElement("style");
    newStyleSheet.type="text/css";
    h[0].appendChild(newStyleSheet);
    try{
        if (typeof newStyleSheet.styleSheet !== "undefined") {
        newStyleSheet.styleSheet.cssText = css_code;
    }
    else {
        newStyleSheet.appendChild(document.createTextNode(css_code));
        newStyleSheet.innerHTML=css_code;
    }
}

Be aware that this code will add a new style sheet everytime it is called - yielding in increasing the editor iframes head. 请注意,这段代码每次被调用时都会添加一个新的样式表-从而增加了编辑器iframe头。 So i think best practice is to clean up the last inserted style before appliing the new one. 因此,我认为最佳实践是在应用新样式之前清理最后插入的样式。 Removing the last Node of the head shozld be sufficient. 除去头部的最后一个节点就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM