All Projects → HubSpot → Draft Convert

HubSpot / Draft Convert

Licence: apache-2.0
Extensibly serialize & deserialize Draft.js ContentState with HTML.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Draft Convert

Draft Extend
Build extensible Draft.js editors with configurable plugins and integrated serialization.
Stars: ✭ 109 (-73.8%)
Mutual labels:  rich-text-editor, draft-js
Braft Editor
美观易用的React富文本编辑器,基于draft-js开发
Stars: ✭ 4,310 (+936.06%)
Mutual labels:  rich-text-editor, draft-js
React Tapable Editor
A pluginable, intuitive medium/notion like rich text editor(currently in wip)
Stars: ✭ 170 (-59.13%)
Mutual labels:  rich-text-editor, draft-js
Megadraft
Megadraft is a Rich Text editor built on top of Facebook's Draft.JS featuring a nice default base of components and extensibility
Stars: ✭ 982 (+136.06%)
Mutual labels:  rich-text-editor, draft-js
Medium Draft
📝 A medium like Rich Text Editor built on draft-js with a focus on keyboard shortcuts.
Stars: ✭ 1,705 (+309.86%)
Mutual labels:  rich-text-editor, draft-js
Mui Rte
Material-UI Rich Text Editor and Viewer
Stars: ✭ 233 (-43.99%)
Mutual labels:  rich-text-editor, draft-js
React Text Selection Popover
Selection based Text UI made easy
Stars: ✭ 245 (-41.11%)
Mutual labels:  rich-text-editor, draft-js
docs-soap
Library to clean up the clipboard contents generated by google docs
Stars: ✭ 17 (-95.91%)
Mutual labels:  draft-js
Hricheditor
Android端富文本编辑器HEichEditor
Stars: ✭ 311 (-25.24%)
Mutual labels:  rich-text-editor
rtexteditorview
A simple WYSIWYG editor for Android
Stars: ✭ 51 (-87.74%)
Mutual labels:  rich-text-editor
typester
✒️ A WYSIWYG that gives you predictable and clean HTML
Stars: ✭ 29 (-93.03%)
Mutual labels:  rich-text-editor
React Native Draftjs Render
React Native render for draft.js model
Stars: ✭ 368 (-11.54%)
Mutual labels:  draft-js
Angular Froala
Angular.js bindings for Froala WYSIWYG HTML Rich Text Editor.
Stars: ✭ 299 (-28.12%)
Mutual labels:  rich-text-editor
rechord
Let's share your chord progressions!
Stars: ✭ 62 (-85.1%)
Mutual labels:  draft-js
Trumbowyg
A lightweight and amazing WYSIWYG JavaScript editor under 10kB
Stars: ✭ 3,664 (+780.77%)
Mutual labels:  rich-text-editor
draftjs-conductor
📝✨ Little Draft.js helpers to make rich text editors “just work”
Stars: ✭ 39 (-90.62%)
Mutual labels:  draft-js
Draft Js Plugins
React Plugin Architecture for Draft.js including Slack-Like Emojis, FB-Like Mentions and Stickers
Stars: ✭ 3,918 (+841.83%)
Mutual labels:  draft-js
TextEditor
Rich text editor for Blazor applications - Uses Quill JS
Stars: ✭ 156 (-62.5%)
Mutual labels:  rich-text-editor
Trix
A rich text editor for everyday writing
Stars: ✭ 16,546 (+3877.4%)
Mutual labels:  rich-text-editor
Re Editor
一个开箱即用的React富文本编辑器 🚀re-editor
Stars: ✭ 367 (-11.78%)
Mutual labels:  rich-text-editor

draft-convert

npm version License

Extensibly serialize & deserialize Draft.js content with HTML

See draft-extend for more on how to use draft-convert with plugins

Installation

npm install draft-convert --save or yarn add draft-convert

Jump to:

convertToHTML

Extensibly serialize Draft.js ContentState to HTML.

Basic usage:

const html = convertToHTML(editorState.getCurrentContent());

Advanced usage:

// convert to HTML with blue text, paragraphs, and links
const html = convertToHTML({
  styleToHTML: (style) => {
    if (style === 'BOLD') {
      return <span style={{color: 'blue'}} />;
    }
  },
  blockToHTML: (block) => {
    if (block.type === 'PARAGRAPH') {
      return <p />;
    }
  },
  entityToHTML: (entity, originalText) => {
    if (entity.type === 'LINK') {
      return <a href={entity.data.url}>{originalText}</a>;
    }
    return originalText;
  }
})(editorState.getCurrentContent());

// convert content state to HTML with functionality defined in the plugins applied
const html = compose(
    FirstPlugin,
    SecondPlugin,
    ThirdPlugin
)(convertToHTML)(editorState.getCurrentContent());

styleToHTML, blockToHtml, and entityToHTML are functions that take Draft content data and may return a ReactElement or an object of shape {start, end} defining strings for the beginning and end tags of the style, block, or entity. entityToHTML may return either a string with or without HTML if the use case demands it. blockToHTML also may return an optional empty property to handle alternative behavior for empty blocks. To use this along with a ReactElement return value an object of shape {element: ReactElement, empty: ReactElement} may be returned. If no additional functionality is necessary convertToHTML can be invoked with just a ContentState to serialize using just the default Draft functionality. convertToHTML can be passed as an argument to a plugin to modularly augment its functionality.

Legacy alternative conversion options styleToHTML and blockToHTML options may be plain objects keyed by style or block type with values of ReactElement s or {start, end} objects. These objects will eventually be removed in favor of the functions described above.

Type info:

type ContentStateConverter = (contentState: ContentState) => string

type Tag =
  ReactElement |
  {start: string, end: string, empty?: string} |
  {element: ReactElement, empty?: ReactElement}

type RawEntity = {
    type: string,
    mutability: DraftEntityMutability,
    data: Object
}

type RawBlock = {
    type: string,
    depth: number,
    data?: object,
    text: string
}

type convertToHTML = ContentStateConverter | ({
    styleToHTML?: (style: string) => Tag,
    blockToHTML?: (block: RawBlock) => Tag),
    entityToHTML?: (entity: RawEntity, originalText: string) => Tag | string
}) => ContentStateConverter

convertFromHTML

Extensibly deserialize HTML to Draft.js ContentState.

Basic usage:

const editorState = EditorState.createWithContent(convertFromHTML(html));

Advanced usage:

// convert HTML to ContentState with blue text, links, and at-mentions
const contentState = convertFromHTML({
    htmlToStyle: (nodeName, node, currentStyle) => {
        if (nodeName === 'span' && node.style.color === 'blue') {
            return currentStyle.add('BLUE');
        } else {
            return currentStyle;
        }
    },
    htmlToEntity: (nodeName, node, createEntity) => {
        if (nodeName === 'a') {
            return createEntity(
                'LINK',
                'MUTABLE',
                {url: node.href}
            )
        }
    },
    textToEntity: (text, createEntity) => {
        const result = [];
        text.replace(/\@(\w+)/g, (match, name, offset) => {
            const entityKey = createEntity(
                'AT-MENTION',
                'IMMUTABLE',
                {name}
            );
            result.push({
                entity: entityKey,
                offset,
                length: match.length,
                result: match
            });
        });
        return result;
    },
    htmlToBlock: (nodeName, node) => {
        if (nodeName === 'blockquote') {
            return {
                type: 'blockquote',
                data: {}
            };
        }
    }
})(html);

// convert HTML to ContentState with functionality defined in the draft-extend plugins applied
const fromHTML = compose(
    FirstPlugin,
    SecondPlugin,
    ThirdPlugin
)(convertFromHTML);
const contentState = fromHTML(html);

If no additional functionality is necessary convertToHTML can be invoked with just an HTML string to deserialize using just the default Draft functionality. Any convertFromHTML can be passed as an argument to a plugin to modularly augment its functionality. A flat option may be provided to force nested block elements to split into flat, separate blocks. For example, the HTML input <p>line one<br />linetwo</p> will produce two unstyled blocks in flat mode.

Type info:

type HTMLConverter = (html: string, {flat: ?boolean}, DOMBuilder: ?Function, generateKey: ?Function) => ContentState

type EntityKey = string

type convertFromHTML = HTMLConverter | ({
    htmlToStyle: ?(nodeName: string, node: Node) => DraftInlineStyle,
    htmlToBlock: ?(nodeName: string, node: Node) => ?(DraftBlockType | {type: DraftBlockType, data: object} | false),
    htmlToEntity: ?(
        nodeName: string,
        node: Node,
        createEntity: (type: string, mutability: string, data: object) => EntityKey,
        getEntity: (key: EntityKey) => Entity,
        mergeEntityData: (key: string, data: object) => void,
        replaceEntityData: (key: string, data: object) => void
    ): ?EntityKey,
    textToEntity: ?(
        text: string,
        createEntity: (type: string, mutability: string, data: object) => EntityKey,
        getEntity: (key: EntityKey) => Entity,
        mergeEntityData: (key: string, data: object) => void,
        replaceEntityData: (key: string, data: object) => void
    ) => Array<{entity: EntityKey, offset: number, length: number, result: ?string}>
}) => HTMLConverter

Middleware functions

Any conversion option for convertToHTML or convertFromHTML may also accept a middleware function of shape (next) => (…args) => result , where …args are the normal configuration function paramaters and result is the expected return type for that function. These functions can transform results of the default conversion included in convertToHTML or convertFromHTML by leveraging the result of next(...args). These middleware functions are most useful when passed as the result of composition of draft-extend plugins. If you choose to use them independently, a __isMiddleware property must be set to true on the function for draft-convert to properly handle it.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].