Coder Social home page Coder Social logo

jkhaui / draft-js-code-editor-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from withspectrum/draft-js-code-editor-plugin

0.0 1.0 0.0 226 KB

Add IDE-like behaviours to code blocks in your DraftJS editors

License: MIT License

JavaScript 100.00%

draft-js-code-editor-plugin's Introduction

draft-js-code-editor-plugin

Add IDE-like behaviours to code blocks in your DraftJS editor. Meant to be used with draft-js-plugins.

Note: If you're not using draft-js-plugins you can also use the lower-level draft-js-code library.

Functionality

  • Insert indentation on tab
  • Preserve indentation of current line when pressing enter
  • Remove indentation correctly with backspace
  • More to come!

Usage

First, install the plugin:

npm install --save draft-js-code-editor-plugin

Then pass it to the plugins prop of the draft-js-plugins editor:

import React, { Component  } from 'react';
import Editor from 'draft-js-plugins-editor';
import createCodeEditorPlugin from 'draft-js-code-editor-plugin';
import { EditorState  } from 'draft-js';

export default class DemoEditor extends Component {
  state = {
    editorState: EditorState.createEmpty(),
    plugins: [createCodeEditorPlugin()]      
  };

  onChange = (editorState) => {
    this.setState({
      editorState,          
    });
  };

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        plugins={this.state.plugins}
      />
    );  
  }
}

Add code block syntax highlighting

Using the draft-js-prism-plugin you can easily add syntax highlighting support to your code blocks!

// Install prismjs and draft-js-prism-plugin
import Prism from 'prismjs';
import createPrismPlugin from 'draft-js-prism-plugin';

class Editor extends Component {
  state = {
    plugins: [
    // Add the Prism plugin to the plugins array 
      createPrismPlugin({
        prism: Prism
      }),
      createCodeEditorPlugin()
    ]
  };
}

License

Licensed under the MIT License, Copyright Šī¸ 2017 Space Program Inc. See LICENSE.md for more information.

draft-js-code-editor-plugin's People

Contributors

brianlovin avatar depfu[bot] avatar mxstbr avatar

Watchers

 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.