forked from ejilay/draftjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
80 lines (65 loc) · 2.3 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// draftjs exporter for go language
package draftjs
import (
"bytes"
)
func renderBlocks(contentState *ContentState, config *Config, blockIterator *BlockIterator, buf *bytes.Buffer) {
var wrapperedBlock *ContentBlock
wrapperTag := ""
for blockIterator.block != nil {
if wrapperTag != GetBlockWrapperTag(blockIterator.block, config) {
wrapperTag = GetBlockWrapperTag(blockIterator.block, config)
buf.WriteString(GetBlockWrapperEndTag(wrapperedBlock, config))
buf.WriteString(GetBlockWrapperStartTag(blockIterator.block, config))
}
wrapperedBlock = blockIterator.block
currentBlock := blockIterator.block
buf.WriteString(GetBlockStartTag(currentBlock, config))
PerformInlineStylesAndEntities(contentState, currentBlock, config, buf)
if blockIterator.HasNext() && blockIterator.NextBlock().Depth > blockIterator.block.Depth {
renderBlocks(contentState, config, blockIterator.StepNext(), buf)
}
buf.WriteString(GetBlockEndTag(currentBlock, config))
if blockIterator.HasNext() && blockIterator.NextBlock().Depth < currentBlock.Depth {
break
}
blockIterator.StepNext()
}
if wrapperTag != "" && wrapperedBlock != nil {
buf.WriteString(GetBlockWrapperEndTag(wrapperedBlock, config))
}
}
// Render renders Draft.js content state to string with config
func Render(contentState *ContentState, config *Config) string {
var buf bytes.Buffer
if config == nil {
config = NewDefaultConfig()
}
config.Precache()
RenderWithBuf(contentState, config, &buf)
return buf.String()
}
// RenderWithBuf renders Draft.js content state to buffer with config
func RenderWithBuf(contentState *ContentState, config *Config, buf *bytes.Buffer) {
renderBlocks(contentState, config, NewBlockIterator(contentState), buf)
}
// Interface implementation
func (contentState *ContentState) String() string {
return Render(contentState, nil)
}
func RenderPlainText(contentState *ContentState) string {
var buffer bytes.Buffer
textLen := len(contentState.Blocks)
for _, block := range contentState.Blocks {
textLen += len(block.Text)
}
buffer.Grow(textLen)
RenderPlainTextToBuffer(contentState, &buffer)
return buffer.String()
}
func RenderPlainTextToBuffer(contentState *ContentState, buffer *bytes.Buffer) {
for _, block := range contentState.Blocks {
buffer.WriteString(block.Text)
buffer.WriteString("\n")
}
}