• Stars
    star
    285
  • Rank 145,115 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An collection of useful utility functions for DraftJS.

DraftJS Utils

An collection of useful utility functions for DraftJS.

I have been using DraftJS in few of my projects. DraftJS is very nice library for creating editors. I wrote a couple of utility functions for myself which I can re-use across my projects. They are well tested. I am open-sourcing them so that others can also leverage. Many of the functions described use ImmutableJS.

Installing

npm install draftjs-utils

Methods

Method Name Parameters Return Type Description
1 getSelectedBlocksMap EditorState OrderedMap The function will return an Immutable OrderedMap of currently selected Blocks. The key is key of Block and value is ContentBlock.
2 getSelectedBlocksList EditorState List The function will return an Immutable List of currently selected Blocks. The data type of returned objects is ContentBlock.
3 getSelectedBlock EditorState ContentBlock The function will return first of currently selected Blocks, this is more useful when we expect user to select only one Block. The data type of returned object is ContentBlock.
3 getBlockBeforeSelectedBlock EditorState ContentBlock The function will return block just before the selected block(s). The data type of returned object is ContentBlock.
5 getAllBlocks EditorState List The function will return all the Blocks of the editor. The data type of returned objects is ContentBlock.
6 getSelectedBlocksType EditorState string The function will return the type of currently selected Blocks. The type is a simple string. It will return undefined if not all selected Blocks have same type.
7 removeSelectedBlocksStyle EditorState EditorState The function will reset the type of selected Blocks to unstyled.
8 getSelectionText EditorState string The function will return plain text of current selection.
9 addLineBreakRemovingSelection EditorState EditorState The function will replace currently selected text with a \n.
10 insertNewUnstyledBlock EditorState EditorState The function will add a new unstyled Block and copy current selection to it.
11 clearEditorContent EditorState EditorState The function will clear all content from the Editor.
12 getSelectionInlineStyle EditorState object The function will return inline style applicable to current selection. The function will return only those styles that are applicable to whole selection.
13 setBlockData EditorState, object EditorState The function will add block level meta-data.
14 getSelectedBlocksMetadata EditorState Map The function will return map of block data of current block.
15 blockRenderMap Map The function will return map of block types Block Type -> HTML Tag.
16 getSelectionEntity EditorState Entity The function will return the Entity of current selection. Entity can not span multiple Blocks, method will check only first selected Block.
17 getEntityRange EditorState, entityKey object The function will return the range of given Entity in currently selected Block. Entity can not span multiple Blocks, method will check only first selected Block.
18 handleNewLine EditorState EditorState, Event The function will handle newline event in editor gracefully, it will insert \n for soft-new lines and remove selected text if any.
19 isListBlock ContentBlock boolean The function will return true is type of block is 'unordered-list-item' or 'ordered-list-item'.
20 changeDepth EditorState , adjustment, maxDepth EditorState Change the depth of selected Blocks by adjustment if its less than maxdepth.
21 getSelectionCustomInlineStyle EditorState , Array (of styles) object Function will return Map of custom inline styles applicable to current selection.
22 toggleCustomInlineStyle EditorState , string (styleType), string(styleValue) EditorState Toggle application of custom inline style to current selection.
23 removeAllInlineStyles EditorState EditorState The function will remove all inline styles of current selection.

Demo

For a complete example, check out https://github.com/jpuri/react-draft-wysiwyg repo.

License

MIT.

More Repositories

1

react-draft-wysiwyg

A Wysiwyg editor build on top of ReactJS and DraftJS. https://jpuri.github.io/react-draft-wysiwyg
JavaScript
6,358
star
2

draftjs-to-html

Library for converting Draftjs editor content state to HTML
JavaScript
315
star
3

html-to-draftjs

HTML
158
star
4

react-range-slider

React range slider component.
JavaScript
115
star
5

draftjs-to-markdown

JavaScript
62
star
6

GraphQL-Typescript-Starter

React Node GraphQL TypeScript - Starter Kit
TypeScript
44
star
7

react-component-stores

A simplified and powerful state management library for React applications.
JavaScript
21
star
8

react-simplified-boilerplate

A simplified boilerplate for my React projects
JavaScript
7
star
9

rustoli

Programming problems solved using Rust programming language
Rust
5
star
10

defi_yield_farming

DEFI yield farming dapp using chainlink price feeds
JavaScript
5
star
11

reason-places

A single page web application build using ReasonML
JavaScript
5
star
12

Solidity-Examples

Example solidity contracts
Solidity
3
star
13

solom

Articles about solidity
3
star
14

reason-tictactoe

Simple game of Tic Tac Toe made using ReasonML
OCaml
3
star
15

Election-DAPP

Election is simple DAPP which allow users to cast votes. It has some rules built on top of it like a user can cast only 1 vote, etc.
JavaScript
2
star
16

Runup

Runup token is implementation of ERC-20 token ground up (without using any library).
JavaScript
2
star
17

DaddaNFT

NFT minting
JavaScript
2
star
18

token_price_link

Obtaining latest token prices using chainlink blockchain
JavaScript
2
star
19

VR

VR and 2D Experiments
JavaScript
2
star
20

hub-poc

HTML
1
star
21

Learning-Reason

1
star
22

react-prosemirror

JavaScript
1
star
23

sanity-gatsby-blog

Blog with Gatsby
JavaScript
1
star
24

humble-site

CSS
1
star