draftjs

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 6 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 added in v1.0.1

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

func GetBlockStartTag added in v1.0.1

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

func GetBlockTag added in v1.0.1

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

func GetBlockWrapperEndTag added in v1.0.1

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

func GetBlockWrapperStartTag added in v1.0.1

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

func GetBlockWrapperTag added in v1.0.1

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

func GetBreakPoints

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

func GetEntityDecorator added in v1.0.1

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

func GetEntityEndTag added in v1.0.1

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

func GetEntityStartTag added in v1.0.1

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

func GetStyleEndTag added in v1.0.1

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

func GetStyleStartTag added in v1.0.1

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

func GetStylemapElement added in v1.0.1

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

func PerformInlineStylesAndEntities added in v1.0.1

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 RenderWithBuf added in v1.0.1

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

RenderWithBuf renders Draft.js content state to buffer with config

func SetDefaultBlocks added in v1.0.1

func SetDefaultBlocks(config *Config)

func SetDefaultDecorators added in v1.0.1

func SetDefaultDecorators(config *Config)

func SetDefaultStyles added in v1.0.1

func SetDefaultStyles(config *Config)

Types

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 Cache added in v1.0.1

type Cache map[string]CacheElement

func NewCache added in v1.0.1

func NewCache() Cache

type CacheElement added in v1.0.1

type CacheElement map[string]string

type Config

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

func NewDefaultConfig added in v1.0.1

func NewDefaultConfig() *Config

NewDefaultConfig Makes new config and fills it with default HTML elements

func (*Config) GetBlockMapElement added in v1.0.1

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

func (*Config) GetEntityDecorator added in v1.0.1

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

func (*Config) GetFromCache added in v1.0.1

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

func (*Config) GetStyleMapElement added in v1.0.1

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

func (*Config) Precache added in v1.0.1

func (config *Config) Precache()

func (*Config) PrecacheBlocks added in v1.0.1

func (config *Config) PrecacheBlocks()

func (*Config) PrecacheStyles added in v1.0.1

func (config *Config) PrecacheStyles()

func (*Config) SetBlockMapElement added in v1.0.1

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

func (*Config) SetEntityDecorator added in v1.0.1

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

func (*Config) SetStyleMapElement added in v1.0.1

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

func (*Config) SetToCache added in v1.0.1

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 added in v1.0.1

func (contentState *ContentState) String() string

Interface implementation

type Decorator added in v1.0.1

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

type Descriptor added in v1.0.1

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

func GetDescriptorFromMap added in v1.0.1

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 added in v1.0.1

type ImageDecorator struct {
}

func (*ImageDecorator) RenderBeginning added in v1.0.1

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

func (*ImageDecorator) RenderEnding added in v1.0.1

func (decorator *ImageDecorator) 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 added in v1.0.1

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

func (*LinkDecorator) RenderEnding added in v1.0.1

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