Giter Site home page Giter Site logo

zapkub / draft-js-richbuttons-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jasonphillips/draft-js-richbuttons-plugin

0.0 1.0 0.0 973 KB

A plugin for adding rich formatting controls - https://jasonphillips.github.io/draft-js-richbuttons-plugin/

JavaScript 99.94% HTML 0.06%

draft-js-richbuttons-plugin's Introduction

DraftJS RichButtons Plugin

npm version Build Status

This is a plugin for the draft-js-plugins-editor. This plugin allows you to add essential rich formatting buttons (inline and block styles) to your plugins-enabled editor.

Usage

First instantiate the plugin:

import createRichButtonsPlugin from 'draft-js-richbuttons-plugin';

const richButtonsPlugin = createRichButtonsPlugin();

Now get any desired components for inline or block formatting buttons from the instance:

/* import only the ones you need; all available shown */
const {   
  // inline buttons
  ItalicButton, BoldButton, MonospaceButton, UnderlineButton,
  // block buttons
  ParagraphButton, BlockquoteButton, CodeButton, OLButton, ULButton, H1Button, H2Button, H3Button, H4Button, H5Button, H6Button
} = richButtonsPlugin;

Render these where desired in your component:

<div className="myToolbar">
  <BoldButton/>
  <ItalicButton/>

  <H2Button/>
  <ULButton/>
  <OLButton/>
</div>

Rendering Your Own Buttons

The default buttons are intentionally plain, but will pass the needed props down to their child, allowing you to customize rendering to your needs.

Props passed to both inline and block buttons:

  • isActive (bool) - true if style / blocktype active in selection
  • label (string) - default label

Props unique to inline buttons:

  • toggleInlineStyle (func) - to be attached to your click event
  • inlineStyle (string) - the draft code for the style
  • onMouseDown (func) - attach this to the onMouseDown event of your custom controls; important for preventing focus from leaving the editor when toggling an inline style with a click

Props unique to block buttons:

  • toggleBlockType (func) - to be attached to your click event
  • blockType (string) - the draft code for the block type

Example:

/*
  Stateless component for inline style buttons, using the passed props as well as a custom prop "iconName"
*/
const MyIconButton = ({iconName, toggleInlineStyle, isActive, label, inlineStyle, onMouseDown, title}) =>
  <a onClick={toggleInlineStyle} onMouseDown={onMouseDown}>
    <span
      className={`fa fa-${iconName}`}
      title={title ? title : label}
      style={{ color: isActive ? '#000' : '#777' }}
    />
  </a>;

The above presentational component could then be used this way:

<BoldButton>
  <MyIconButton iconName="bold"/>
</BoldButton>
<ItalicButton>
  <MyIconButton iconName="italic" title="Italicize" />
</ItalicButton>

Version 2.x.x

The draft-js-plugins project is approaching a v2 milestone, currently in beta. Use the 2.x branch of this repository if moving to the lastest version of that project; otherwise, 1.x is stable for the current release of draft-js-plugins.

Key Bindings

The plugin automatically applies default key bindings from draft's RichUtils, including Tab / Shift-Tab behavior for nested lists.

License

MIT

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.