GithubHelp home page GithubHelp logo

hungdl / draft-js-code Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samypesse/draft-js-code

0.0 2.0 0.0 664 KB

Collection of utilities to make code blocks edition easy in DraftJS

Home Page: http://samypesse.github.io/draft-js-code/

License: Apache License 2.0

CSS 6.29% HTML 1.97% JavaScript 91.73%

draft-js-code's Introduction

draft-js-code

NPM version Build Status

draft-js-code is a collection of utilities to make code blocks edition easy in DraftJS. It works well with draft-js-prism.

Demo: samypesse.github.io/draft-js-code/

Features

  • Insert indent when pressing TAB
  • Insert new line in block with right indentation when pressing ENTER
  • Remove indentation when pressing DELETE
  • Pressing TAB on last line, split the block
  • Exit code blocks when pressing ENTER + Command

Installation

$ npm install draft-js-code --save

API

CodeUtils.hasSelectionInBlock(editorState)

Returns true if user is editing a code block. You should call this method to encapsulate all other methods when limiting code edition behaviour to code-block.

CodeUtils.handleKeyCommand(editorState, command)

Handle key command for code blocks, returns a new EditorState or null.

CodeUtils.handleTab(e, editorState)

Handle user pressing tab, to insert indentation, it returns a new EditorState.

CodeUtils.handleReturn(e, editorState)

Handle user pressing return, to insert a new line inside the code block, it returns a new EditorState.

Usage

var React = require('react');
var ReactDOM = require('react-dom');
var Draft = require('draft-js');
var CodeUtils = require('draft-js-code');

var Editor = React.createClass({
    getInitialState: function() {
        return {
            editorState: Draft.EditorState.createEmpty()
        };
    },

    onChange: function(editorState) {
        this.setState({
            editorState: editorState
        });
    },

    handleKeyCommand: function(command) {
        var newState;

        if (CodeUtils.hasSelectionInBlock(editorState)) {
            newState = CodeUtils.handleKeyCommand(editorState, command);
        }

        if (!newState) {
            newState = Draft.RichUtils.handleKeyCommand(editorState, command);
        }

        if (newState) {
            this.onChange(newState);
            return true;
        }
        return false;
    },

    keyBindingFn: function(e) {
        var editorState = this.state.editorState;
        var command;

        if (CodeUtils.hasSelectionInBlock(editorState)) {
            command = CodeUtils.getKeyBinding(e);
        }
        if (command) {
            return command;
        }

        return Draft.getDefaultKeyBinding(e);
    },

    handleReturn: function(e) {
        var editorState = this.state.editorState;

        if (!CodeUtils.hasSelectionInBlock(editorState)) {
            return;
        }

        this.onChange(
            CodeUtils.handleReturn(e, editorState)
        );
        return true;
    },

    handleTab: function(e) {
        var editorState = this.state.editorState;

        if (!CodeUtils.hasSelectionInBlock(editorState)) {
            return;
        }

        this.onChange(
            CodeUtils.handleTab(e, editorState)
        );
    },

    render: function() {
        return <Draft.Editor
            editorState={this.state.editorState}
            onChange={this.onChange}
            keyBindingFn={this.keyBindingFn}
            handleKeyCommand={this.handleKeyCommand}
            handleReturn={this.handleReturn}
            onTab={this.handleTab}
        />;
    }
});

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.