draftjs

package module
v0.0.0-...-a5f1957 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 8 Imported by: 0

README

Draft.js Exporter

Go Report Card

Draft.js is a framework for building rich text editors. However, it does not support exporting documents at HTML. This package is designed to take the raw ContentState (output of convertToRaw) from Draft.js and convert it to HTML using Go. Mostly it useful for server-side rendering. I should note this package does not provide any input validation and assumes correct and safe input data.

Usage

func main() {

	// get your contentState JSON-string
	draftState := exampleDraftStateSource

	// make auxiliary variable
	contentState := draftjs.ContentState{}
	json.Unmarshal([]byte(draftState), &contentState) // don't forget error handling

	// prepare some config (HTML here)
	config := draftjs.DefaultConfig()

	// and just render content state to HTML-string
	s := draftjs.Render(&contentState, config)

	// that's it
	fmt.Println(s)
}

For RawContentState like this

{
  "entityMap": {
    "0": {
      "type": "LINK",
      "data": {
        "url": "https://medium.com/@rajaraodv/how-draft-js-represents-rich-text-data-eeabb5f25cf2#.ce9y2wyux"
      }
    }
  },
  "blocks": [
    {
      "text": "Rich text with link",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [
        {
          "offset": 0,
          "length": 4,
          "style": "BOLD"
        }, {
          "offset": 2,
          "length": 10,
          "style": "UNDERLINE"
        }, {
          "offset": 5,
          "length": 4,
          "style": "ITALIC"
        }, {
          "offset": 10,
          "length": 4,
          "style": "CODE"
        }
      ],
      "entityRanges": [{
          "offset": 15,
          "length": 4,
          "key": 0
       }]
}]}

It will give something like this but without indention:

<p>
	<strong>Ri</strong>
	<strong>
		<ins>ch</ins>
	</strong>
	<ins>
		<em>text</em>
	</ins>
	<ins>
		<code>wi</code>
	</ins>
	<code>th</code>
	<a href="https://medium.com/@rajaraodv/how-draft-js-represents-rich-text-data-eeabb5f25cf2#.ce9y2wyux" target="_blank">link</a>
</p>

That look like

Ri ch text wi th link

Setup

You'll need Golang installed first :o)

go get github.com/ejilay/draftjs

Testing

To test run the following command in project's root directory:

go test ./...

Documentation

Overview

draftjs exporter for go language

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBlockEndTag

func GetBlockEndTag(block *ContentBlock, config *Config) string

func GetBlockStartTag

func GetBlockStartTag(block *ContentBlock, config *Config) string

func GetBlockTag

func GetBlockTag(block *ContentBlock, config *Config) string

func GetBlockWrapperEndTag

func GetBlockWrapperEndTag(block *ContentBlock, config *Config) string

func GetBlockWrapperStartTag

func GetBlockWrapperStartTag(block *ContentBlock, config *Config) string

func GetBlockWrapperTag

func GetBlockWrapperTag(block *ContentBlock, config *Config) string

func GetBreakPoints

func GetBreakPoints(block *ContentBlock) ([]int, int)

func GetEntityDecorator

func GetEntityDecorator(content *ContentState, entityRange *EntityRange, config *Config) (Decorator, *Entity)

func GetEntityEndTag

func GetEntityEndTag(content *ContentState, entityRange *EntityRange, config *Config) string

func GetEntityStartTag

func GetEntityStartTag(content *ContentState, entityRange *EntityRange, config *Config) string

func GetMathJaxData

func GetMathJaxData(data string) string

func GetMaxHeightStyle

func GetMaxHeightStyle() string

func GetStyleEndTag

func GetStyleEndTag(style *InlineStyleRange, config *Config) string

func GetStyleStartTag

func GetStyleStartTag(style *InlineStyleRange, config *Config) string

func GetStylemapElement

func GetStylemapElement(style *InlineStyleRange, config *Config) string

func PerformInlineStylesAndEntities

func PerformInlineStylesAndEntities(content *ContentState, block *ContentBlock, config *Config, buf *bytes.Buffer)

func Render

func Render(contentState *ContentState, config *Config) string

Render renders Draft.js content state to string with config

func RenderPlainText

func RenderPlainText(contentState *ContentState) string

func RenderPlainTextToBuffer

func RenderPlainTextToBuffer(contentState *ContentState, buffer *bytes.Buffer)

func RenderWithBuf

func RenderWithBuf(contentState *ContentState, config *Config, buf *bytes.Buffer)

RenderWithBuf renders Draft.js content state to buffer with config

func SetDefaultBlocks

func SetDefaultBlocks(config *Config)

func SetDefaultDecorators

func SetDefaultDecorators(config *Config)

func SetDefaultStyles

func SetDefaultStyles(config *Config)

Types

type AudioDecorator

type AudioDecorator struct {
}

func (*AudioDecorator) RenderBeginning

func (decorator *AudioDecorator) RenderBeginning(data map[string]string) string

func (*AudioDecorator) RenderEnding

func (decorator *AudioDecorator) RenderEnding(data map[string]string) string

type BlockAudioDecorator

type BlockAudioDecorator struct {
}

func (*BlockAudioDecorator) RenderBeginning

func (decorator *BlockAudioDecorator) RenderBeginning(data map[string]string) string

func (*BlockAudioDecorator) RenderEnding

func (decorator *BlockAudioDecorator) RenderEnding(data map[string]string) string

type BlockImageDecorator

type BlockImageDecorator struct {
}

func (*BlockImageDecorator) RenderBeginning

func (decorator *BlockImageDecorator) RenderBeginning(data map[string]string) string

func (*BlockImageDecorator) RenderEnding

func (decorator *BlockImageDecorator) RenderEnding(data map[string]string) string

type BlockIterator

type BlockIterator struct {
	// contains filtered or unexported fields
}

func NewBlockIterator

func NewBlockIterator(contentState *ContentState) *BlockIterator

func (*BlockIterator) HasNext

func (bi *BlockIterator) HasNext() bool

func (BlockIterator) NextBlock

func (bi BlockIterator) NextBlock() *ContentBlock

func (*BlockIterator) StepNext

func (bi *BlockIterator) StepNext() *BlockIterator

type BlockMathJaxDecorator

type BlockMathJaxDecorator struct {
}

func (*BlockMathJaxDecorator) RenderBeginning

func (decorator *BlockMathJaxDecorator) RenderBeginning(data map[string]string) string

func (*BlockMathJaxDecorator) RenderEnding

func (decorator *BlockMathJaxDecorator) RenderEnding(data map[string]string) string

type Cache

type Cache map[string]CacheElement

func NewCache

func NewCache() Cache

type CacheElement

type CacheElement map[string]string

type Config

type Config struct {
	// contains filtered or unexported fields
}

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig Makes new config and fills it with default HTML elements

func (*Config) GetBlockMapElement

func (config *Config) GetBlockMapElement(descriptorType string) *Descriptor

func (*Config) GetEntityDecorator

func (config *Config) GetEntityDecorator(descriptorType string) *Descriptor

func (*Config) GetFromCache

func (config *Config) GetFromCache(key1, key2 string) (string, bool)

func (*Config) GetStyleMapElement

func (config *Config) GetStyleMapElement(descriptorType string) *Descriptor

func (*Config) Precache

func (config *Config) Precache()

func (*Config) PrecacheBlocks

func (config *Config) PrecacheBlocks()

func (*Config) PrecacheStyles

func (config *Config) PrecacheStyles()

func (*Config) SetBlockMapElement

func (config *Config) SetBlockMapElement(descriptor *Descriptor)

func (*Config) SetEntityDecorator

func (config *Config) SetEntityDecorator(descriptor *Descriptor)

func (*Config) SetStyleMapElement

func (config *Config) SetStyleMapElement(descriptor *Descriptor)

func (*Config) SetToCache

func (config *Config) SetToCache(key1, key2, value string)

type ContentBlock

type ContentBlock struct {
	Key               string              `json:"key"`
	Type              string              `json:"type"`
	Text              string              `json:"text"`
	Depth             int                 `json:"depth"`
	InlineStyleRanges []*InlineStyleRange `json:"inlineStyleRanges"`
	EntityRanges      []*EntityRange      `json:"entityRanges"`
	Data              interface{}         `json:"data"`
}

ContentBlock https://github.com/facebook/draft-js/blob/master/src/model/encoding/RawDraftContentBlock.js

type ContentState

type ContentState struct {
	Blocks    []*ContentBlock    `json:"blocks"`
	EntityMap map[string]*Entity `json:"entityMap"`
}

ContentState https://github.com/facebook/draft-js/blob/master/src/model/encoding/RawDraftContentState.js

func (*ContentState) String

func (contentState *ContentState) String() string

Interface implementation

type Decorator

type Decorator interface {
	RenderBeginning(data map[string]string) string
	RenderEnding(data map[string]string) string
}

type Descriptor

type Descriptor struct {
	Type      string
	Element   string
	Wrapper   string
	Decorator Decorator
}

func GetDescriptorFromMap

func GetDescriptorFromMap(key string, sourceMap map[string]*Descriptor) *Descriptor

type Entity

type Entity struct {
	Type       string            `json:"type"`
	Mutability string            `json:"mutability"`
	Data       map[string]string `json:"data"`
}

Entity https://github.com/facebook/draft-js/blob/master/src/model/encoding/RawDraftEntity.js

type EntityRange

type EntityRange struct {
	Key int `json:"key"`
	Range
}

EntityRange https://github.com/facebook/draft-js/blob/master/src/model/encoding/EntityRange.js

func GetEntityForRange

func GetEntityForRange(r *Range, block *ContentBlock) []*EntityRange

type ImageDecorator

type ImageDecorator struct {
}

func (*ImageDecorator) RenderBeginning

func (decorator *ImageDecorator) RenderBeginning(data map[string]string) string

func (*ImageDecorator) RenderEnding

func (decorator *ImageDecorator) RenderEnding(data map[string]string) string

type InlineAudioDecorator

type InlineAudioDecorator struct {
}

func (*InlineAudioDecorator) RenderBeginning

func (decorator *InlineAudioDecorator) RenderBeginning(data map[string]string) string

func (*InlineAudioDecorator) RenderEnding

func (decorator *InlineAudioDecorator) RenderEnding(data map[string]string) string

type InlineImageDecorator

type InlineImageDecorator struct {
}

func (*InlineImageDecorator) RenderBeginning

func (decorator *InlineImageDecorator) RenderBeginning(data map[string]string) string

func (*InlineImageDecorator) RenderEnding

func (decorator *InlineImageDecorator) RenderEnding(data map[string]string) string

type InlineMathJaxDecorator

type InlineMathJaxDecorator struct {
}

func (*InlineMathJaxDecorator) RenderBeginning

func (decorator *InlineMathJaxDecorator) RenderBeginning(data map[string]string) string

func (*InlineMathJaxDecorator) RenderEnding

func (decorator *InlineMathJaxDecorator) RenderEnding(data map[string]string) string

type InlineStyleRange

type InlineStyleRange struct {
	Style string `json:"style"`
	Range
}

InlineStyleRange https://github.com/facebook/draft-js/blob/master/src/model/encoding/InlineStyleRange.js

func GetStyleForRange

func GetStyleForRange(r *Range, block *ContentBlock) []*InlineStyleRange

type LinkDecorator

type LinkDecorator struct {
}

func (*LinkDecorator) RenderBeginning

func (decorator *LinkDecorator) RenderBeginning(data map[string]string) string

func (*LinkDecorator) RenderEnding

func (decorator *LinkDecorator) RenderEnding(data map[string]string) string

type Range

type Range struct {
	Offset int `json:"offset"`
	Length int `json:"length"`
}

func GetRanges

func GetRanges(block *ContentBlock) ([]*Range, bool)

bool == fullstring (no styles)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL