Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I beautify(formatting) code after load #146

Open
Mostafasaffari opened this issue Sep 10, 2018 · 2 comments
Open

how can I beautify(formatting) code after load #146

Mostafasaffari opened this issue Sep 10, 2018 · 2 comments

Comments

@Mostafasaffari
Copy link

Hi
how can I beautify code? like this

@scniro
Copy link

scniro commented Oct 16, 2018

@Mostafasaffari check out react-codemirror2

I actually do this in the demo (code here)

@ssadev
Copy link

ssadev commented Jul 11, 2020

Use react-codemirror2. Here how i solve this:

import React, { Component } from "react";
// import CodeMirror from "react-codemirror";
import { Controlled as CodeMirror } from "react-codemirror2";
require("codemirror/theme/dracula.css");
require("codemirror/mode/xml/xml");
require("codemirror/mode/markdown/markdown");
let hBeautify = require("js-beautify").html;

export class HtmlEditor extends Component {
  state = {
    code: "null",
  };
  updateCode = (e) => {
    this.setState({ code: e });
    console.log("object");
  };
  autoFormatSelection = () => {
    let code = this.state.code;
    // console.log(code);
    let formatedHTML = hBeautify(code, { indent_size: 2 });
    console.log(formatedHTML);
    this.setState({ code: formatedHTML });
  };
  render() {
    let options = {
      height: "150px",
      lineNumbers: true,
      tabSize: 2,
      mode: "xml",
      theme: "dracula",
      extraKeys: { "Shift-Tab": this.autoFormatSelection },
    };
    return (
      <div className="col-4 bg-light p-0">
        <div className="card text-white bg-dark rounded-0 h-100">
          <div className="card-header">
            HTML
            <a
              className="btn btn-outline-light float-right btn-sm"
              href="#"
              data-toggle="modal"
              data-target="#codeconfig"
            >
              <i className="fa fa-cog" />
            </a>
          </div>
          <div className="card-body p-0">
            <CodeMirror
              value={this.state.code}
              options={options}
              onBeforeChange={(editor, data, value) => {
                this.setState({ code: value });
              }}
              onChange={(editor, data, value) => {
                this.setState({ code: value });
                console.log("object");
              }}
            />
          </div>
        </div>
      </div>
    );
  }
}

export default HtmlEditor;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants