简体   繁体   English

如何在CodeMirror中呈现“结果”视图?

[英]How to render the “result” view in CodeMirror?

I have created a component in react which allows to edit two states: codeHtml and codeCss in two CodeMirror editor views: 我在react中创建了一个组件,该组件允许在两个CodeMirror编辑器视图中编辑两个状态:codeHtml和codeCss:

import React from "react";
import CodeMirror from "react-codemirror";
require('codemirror/mode/htmlembedded/htmlembedded');
require('codemirror/mode/css/css');

export default class HtmlComponent extends React.Component {
  constructor(props) {
    super();
    this.state = {
        codeHtml:'<div class="comeClass">some test div text</div>',
        codeCss:'.someClass{color:"red"}',
    }

  }

    updateHtmlCode(newCode) {
        this.setState({
            codeHtml: newCode,
        });
    }
    updateCssCode(newCode) {
        this.setState({
            codeCss: newCode,
        });
    }
    render() {
        console.log(this.state);

        return(
            <div>
            HTML editor:
            <CodeMirror value={this.state.codeHtml} onChange={this.updateHtmlCode.bind(this)} options={{mode:"htmlembedded",lineNumbers: true}} />
            CSS editor:
            <CodeMirror value={this.state.codeCss} onChange={this.updateCssCode.bind(this)} options={{mode:"css",lineNumbers: true}} />
            </div>
        ) 
    }

}

在此处输入图片说明

I need to render the resulting DOM as third view but I don´t find anything in the documentation: https://codemirror.net/doc/manual.html 我需要将生成的DOM渲染为第三种视图,但是我在文档中找不到任何内容: https : //codemirror.net/doc/manual.html

The point of using CodeMirror was to avoid having to deal with the logic of expanding the whole websites CSS file.... 使用CodeMirror的目的是避免必须处理扩展整个网站CSS文件的逻辑。

Is the a CodeMirror way to do it? 是使用CodeMirror的方法吗?

AN easy way of rendering the resulting html code is: 呈现结果html代码的一种简单方法是:

   render() {
    console.log(this.state);
    const hCode = this.state.codeHtml;
    const cCode = this.state.codeCss;

    return(
        <div>
        HTML editor:
        <CodeMirror value={this.state.codeHtml} onChange={this.updateHtmlCode.bind(this)} options={{mode:"htmlembedded",lineNumbers: true}} />
        CSS editor:
        <CodeMirror value={this.state.codeCss} onChange={this.updateCssCode.bind(this)} options={{mode:"css",lineNumbers: true}} />
        <div dangerouslySetInnerHTML={{__html:hCode}}></div>
        </div>
    ) 
}

在此处输入图片说明

But is there an easy way to apply the css to the resulting html? 但是,有没有一种简单的方法可以将CSS应用于生成的html?

Solution is to add the html code to an iframe and then use js or jquery to attach & update the style tag in the iframe 's header: 解决方案是将html代码添加到iframe ,然后使用jsjqueryiframe的标头中附加和更新style标签:

updateStyleTagOnView(cCode){
    const head = jQuery("#iframe").contents().find("head");
    const css = '<style type="text/css">' + cCode + '</style>';
    jQuery(head[0]).empty().append(css);    
}

render() {

    const hCode = this.state.codeHtml;
    const cCode = this.state.codeCss;
    const mCode = this.state.mixedCode;

    this.updateStyleTagOnView(cCode)

    return(
        <div>
        HTML editor:
        <CodeMirror value={this.state.codeHtml} onChange={this.updateHtmlCode.bind(this)} options={{mode:"htmlembedded",lineNumbers: true}} />
        CSS editor:
        <CodeMirror value={this.state.codeCss} onChange={this.updateCssCode.bind(this)} options={{mode:"css",lineNumbers: true}} />
        CSS editor:
        <CodeMirror value={this.state.mixedCode} onChange={this.updateMixedCode.bind(this)} options={{mode:"css",lineNumbers: true}} />

        <iframe id="iframe" srcDoc={hCode}>
          <p>Your browser does not support iframes.</p>
        </iframe>
        </div>
    ) 
}

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

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