Giter Club home page Giter Club logo

preact-codemirror's Introduction

preact-codemirror

Simple and lightweight CodeMirror component for Preact; for ⚛️ Preact

Usage

languages themes

const { h, Component, render } = require("preact");
import CodeMirror from "preact-codemirror";

// import language
import "codemirror/mode/sql/sql.js";

// import theme
import "codemirror/theme/dracula.css";

function loadStyleSheet(url) {
  var sheet = document.createElement("link");
  sheet.rel = "stylesheet";
  sheet.href = url;
  sheet.type = "text/css";
  document.head.appendChild(sheet);
  var _timer;

  // TODO: handle failure
  return new Promise(function(resolve) {
    sheet.onload = resolve;
    sheet.addEventListener("load", resolve);
    sheet.onreadystatechange = function() {
      if (sheet.readyState === "loaded" || sheet.readyState === "complete") {
        resolve();
      }
    };

    _timer = setInterval(function() {
      try {
        for (var i = 0; i < document.styleSheets.length; i++)
          if (document.styleSheets[i].href === sheet.href) resolve();
      } catch (e) {
        /* the stylesheet wasn't loaded */
      }
    }, 250);
  }).then(function() {
    clearInterval(_timer);
    return sheet;
  });
}

class Demo extends Component {
  state = {
    currentTheme: "mbo",
    themes: [
      "3024-day",
      "3024-night",
      "abcdef",
      "ambiance-mobile",
      "ambiance",
      "base16-dark",
      "base16-light",
      "bespin",
      "blackboard",
      "cobalt",
      "colorforth",
      "darcula",
      "dracula",
      "duotone-dark",
      "duotone-light",
      "eclipse",
      "elegant",
      "erlang-dark",
      "gruvbox-dark",
      "hopscotch",
      "icecoder",
      "idea",
      "isotope",
      "lesser-dark",
      "liquibyte",
      "lucario",
      "material",
      "mbo",
      "mdn-like",
      "midnight",
      "monokai",
      "neat",
      "neo",
      "night",
      "oceanic-next",
      "panda-syntax",
      "paraiso-dark",
      "paraiso-light",
      "pastel-on-dark",
      "railscasts",
      "rubyblue",
      "seti",
      "shadowfox",
      "solarized",
      "ssms",
      "the-matrix",
      "tomorrow-night-bright",
      "tomorrow-night-eighties",
      "ttcn",
      "twilight",
      "vibrant-ink",
      "xq-dark",
      "xq-light",
      "yeti",
      "zenburn"
    ],
    code: ` 
SELECT 'Customer' As Type, 
       FirstName + ' ' + LastName AS ContactName, 
       City, Country, Phone
  FROM Customer
UNION
SELECT 'Supplier', 
       ContactName, City, Country, Phone
  FROM Supplier
`
  };
  componentWillMount() {
    loadStyleSheet(
      `https://codemirror.net/theme/${this.state.currentTheme}.css`
    );
  }
  themeChange = e => {
    this.setState(
      {
        currentTheme: e.target.value
      },
      () => {
        loadStyleSheet(`https://codemirror.net/theme/${e.target.value}.css`);
      }
    );
  };
  render(props, state) {
    return (
      <div>
        <select
          onChange={this.themeChange}
          style={{
            width: "100%",
            border: "1px solid #ccc",
            height: "30px"
          }}
        >
          {state.themes.map(item => (
            <option value={item} selected={state.currentTheme == item}>
              {item}
            </option>
          ))}
        </select>
        <CodeMirror
          code={state.code.trim()}
          config={{
            mode: "sql",
            theme: state.currentTheme,
            styleActiveLine: true,
            lineNumbers: true
          }}
          instance={instance => {
            instance.on("change", () => {
              console.log(instance.getValue());
            });
          }}
        />
      </div>
    );
  }
}

render(<Demo />, document.body);

Edit mzn9j60m48

Install

$ npm install --save preact-codemirror
<script src="https://unpkg.com/[email protected]/dist/preact-codemirror.min.js"></script>

Properties

code

Type: String

config

Type: Object

instance

Type: Function

License

MIT © Ahmet Şimşek

preact-codemirror's People

Contributors

indatawetrust avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.