Skip to content

Commit 2e0933d

Browse files
committed
Revert code
1 parent 6a7b35a commit 2e0933d

11 files changed

+144
-56
lines changed

.DS_Store

6 KB
Binary file not shown.

cli/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"os"
87

9-
"github.com/ejilay/draftjs"
8+
"github.com/manabie-com/draftjs"
109
"github.com/urfave/cli"
1110
)
1211

@@ -35,13 +34,14 @@ func main() {
3534
return err
3635
}
3736
defer f.Close()
37+
// from stringify json
38+
3839
if err := json.NewDecoder(f).Decode(&contentState); err != nil {
3940
return err
4041
}
4142
} else {
4243
draftState := c.Args().First()
4344
if err := json.Unmarshal([]byte(draftState), &contentState); err != nil {
44-
fmt.Println(err)
4545
return err
4646
}
4747
}
@@ -52,12 +52,12 @@ func main() {
5252

5353
config := draftjs.NewDefaultConfig()
5454
s := draftjs.Render(&contentState, config)
55-
fmt.Println(s)
55+
fmt.Println("html>>>", s)
5656
return nil
5757
}
5858

5959
err := app.Run(os.Args)
6060
if err != nil {
61-
log.Fatal(err)
61+
panic(err)
6262
}
6363
}

config.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ func SetDefaultBlocks(config *Config) {
117117
descriptor.Element = "blockquote"
118118
config.SetBlockMapElement(descriptor)
119119

120-
descriptor = new(Descriptor)
121-
descriptor.Type = "atomic"
122-
descriptor.Element = "figure"
123-
config.SetBlockMapElement(descriptor)
120+
// descriptor = new(Descriptor)
121+
// descriptor.Type = "atomic"
122+
// descriptor.Element = "figure"
123+
// config.SetBlockMapElement(descriptor)
124124
}
125125

126126
func SetDefaultStyles(config *Config) {
@@ -171,19 +171,34 @@ func SetDefaultDecorators(config *Config) {
171171
descriptor.Decorator = new(ImageDecorator)
172172
config.SetEntityDecorator(descriptor)
173173

174+
descriptor = new(Descriptor)
175+
descriptor.Type = "AUDIO"
176+
descriptor.Decorator = new(AudioDecorator)
177+
config.SetEntityDecorator(descriptor)
178+
174179
descriptor = new(Descriptor)
175180
descriptor.Type = "BLOCK_IMAGE"
176-
descriptor.Decorator = new(ImageDecoratorV2)
181+
descriptor.Decorator = new(BlockImageDecorator)
182+
config.SetEntityDecorator(descriptor)
183+
184+
descriptor = new(Descriptor)
185+
descriptor.Type = "INLINE_IMAGE"
186+
descriptor.Decorator = new(InlineImageDecorator)
177187
config.SetEntityDecorator(descriptor)
178188

179189
descriptor = new(Descriptor)
180190
descriptor.Type = "BLOCK_AUDIO"
181-
descriptor.Decorator = new(AudioDecorator)
191+
descriptor.Decorator = new(BlockAudioDecorator)
192+
config.SetEntityDecorator(descriptor)
193+
194+
descriptor = new(Descriptor)
195+
descriptor.Type = "INLINE_AUDIO"
196+
descriptor.Decorator = new(InlineAudioDecorator)
182197
config.SetEntityDecorator(descriptor)
183198

184199
descriptor = new(Descriptor)
185200
descriptor.Type = "BLOCK_MATHJAX"
186-
descriptor.Decorator = new(MathJaxDecorator)
201+
descriptor.Decorator = new(BlockMathJaxDecorator)
187202
config.SetEntityDecorator(descriptor)
188203

189204
descriptor = new(Descriptor)

decorators.go

+50-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package draftjs
22

33
import (
44
"fmt"
5-
"os"
65
)
76

87
type Decorator interface {
@@ -25,11 +24,8 @@ type ImageDecorator struct {
2524
}
2625

2726
func (decorator *ImageDecorator) RenderBeginning(data map[string]string) string {
28-
maxHeight := os.Getenv("IMAGE_MAX_HEIGHT")
29-
maxHeightStr := ""
30-
if maxHeight != "" {
31-
maxHeightStr = fmt.Sprintf(" style=\"max-height:%spx;\"", maxHeight)
32-
}
27+
maxHeightStr := GetMaxHeightStyle()
28+
3329
if alt, ok := data["alt"]; ok {
3430
return fmt.Sprintf("<img%s src=\"%s\" alt=\"%s\">", data["data"], maxHeightStr, alt)
3531
}
@@ -40,20 +36,60 @@ func (decorator *ImageDecorator) RenderEnding(data map[string]string) string {
4036
return "</img>"
4137
}
4238

43-
type ImageDecoratorV2 struct {
39+
type BlockImageDecorator struct {
4440
}
4541

46-
func (decorator *ImageDecoratorV2) RenderBeginning(data map[string]string) string {
42+
func (decorator *BlockImageDecorator) RenderBeginning(data map[string]string) string {
43+
maxHeightStr := GetMaxHeightStyle()
4744
if alt, ok := data["alt"]; ok {
48-
return fmt.Sprintf("<img src=%s alt=\"%s\">", data["data"], alt)
45+
return fmt.Sprintf("<figure><img%s src=\"%s\" alt=\"%s\">", maxHeightStr, data["data"], alt)
4946
}
50-
return fmt.Sprintf("<img src=%s>", data["data"])
47+
return fmt.Sprintf("<figure><img%s src=\"%s\">", maxHeightStr, data["data"])
48+
}
49+
50+
func (decorator *BlockImageDecorator) RenderEnding(data map[string]string) string {
51+
return "</img></figure>"
5152
}
5253

53-
func (decorator *ImageDecoratorV2) RenderEnding(data map[string]string) string {
54+
type InlineImageDecorator struct {
55+
}
56+
57+
func (decorator *InlineImageDecorator) RenderBeginning(data map[string]string) string {
58+
maxHeightStr := GetMaxHeightStyle()
59+
60+
if alt, ok := data["alt"]; ok {
61+
return fmt.Sprintf("<img%s src=%s alt=\"%s\">", maxHeightStr, data["data"], alt)
62+
}
63+
return fmt.Sprintf("<img%s src=%s>", maxHeightStr, data["data"])
64+
}
65+
66+
func (decorator *InlineImageDecorator) RenderEnding(data map[string]string) string {
5467
return "</img>"
5568
}
5669

70+
type BlockAudioDecorator struct {
71+
}
72+
73+
func (decorator *BlockAudioDecorator) RenderBeginning(data map[string]string) string {
74+
return fmt.Sprintf("<figure><audio controls><source src=\"%s\" type=\"audio/mpeg\">", data["data"])
75+
}
76+
77+
func (decorator *BlockAudioDecorator) RenderEnding(data map[string]string) string {
78+
return "</audio></figure>"
79+
}
80+
81+
type InlineAudioDecorator struct {
82+
}
83+
84+
func (decorator *InlineAudioDecorator) RenderBeginning(data map[string]string) string {
85+
return fmt.Sprintf("<audio controls><source src=\"%s\" type=\"audio/mpeg\">", data["data"])
86+
}
87+
88+
func (decorator *InlineAudioDecorator) RenderEnding(data map[string]string) string {
89+
90+
return "</audio>"
91+
}
92+
5793
type AudioDecorator struct {
5894
}
5995

@@ -65,14 +101,14 @@ func (decorator *AudioDecorator) RenderEnding(data map[string]string) string {
65101
return "</audio>"
66102
}
67103

68-
type MathJaxDecorator struct {
104+
type BlockMathJaxDecorator struct {
69105
}
70106

71-
func (decorator *MathJaxDecorator) RenderBeginning(data map[string]string) string {
107+
func (decorator *BlockMathJaxDecorator) RenderBeginning(data map[string]string) string {
72108
return fmt.Sprintf("\\[%s\\]", data["data"])
73109
}
74110

75-
func (decorator *MathJaxDecorator) RenderEnding(data map[string]string) string {
111+
func (decorator *BlockMathJaxDecorator) RenderEnding(data map[string]string) string {
76112
return ""
77113
}
78114

example/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/ejilay/draftjs"
8-
"github.com/ejilay/draftjs/tests"
7+
"github.com/manabie-com/draftjs"
8+
"github.com/manabie-com/draftjs/tests"
99
)
1010

1111
func main() {

exporter.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package draftjs
33

44
import (
55
"bytes"
6-
"strings"
76
)
87

98
func renderBlocks(contentState *ContentState, config *Config, blockIterator *BlockIterator, buf *bytes.Buffer) {
@@ -49,7 +48,7 @@ func Render(contentState *ContentState, config *Config) string {
4948

5049
RenderWithBuf(contentState, config, &buf)
5150
var bufString = buf.String()
52-
bufString = strings.Replace(bufString, "<div></div>", "<br>", -1)
51+
// bufString = strings.Replace(bufString, "<div></div>", "<br>", -1)
5352
return bufString
5453
}
5554

go.mod

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
module github.com/ejilay/draftjs
1+
module github.com/manabie-com/draftjs
22

33
go 1.18
4+
5+
require github.com/urfave/cli v1.22.14
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
9+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
10+
)

go.sum

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
8+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
11+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
12+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
13+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
14+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
15+
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
16+
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
19+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

helpers.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package draftjs
33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"sort"
78
"strconv"
89
"text/template"
@@ -370,3 +371,12 @@ func invalidRange(r *Range, maxLength int) bool {
370371
}
371372
return false
372373
}
374+
375+
func GetMaxHeightStyle() string {
376+
maxHeight := os.Getenv("IMAGE_MAX_HEIGHT")
377+
maxHeightStr := ""
378+
if maxHeight != "" {
379+
maxHeightStr = fmt.Sprintf(" style=\"max-height:%spx;\"", maxHeight)
380+
}
381+
return maxHeightStr
382+
}

tests/export_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"encoding/json"
66
"testing"
77

8-
"github.com/ejilay/draftjs"
8+
"github.com/manabie-com/draftjs"
99
)
1010

1111
func TestRender(t *testing.T) {
1212
var (
1313
contentState draftjs.ContentState
1414
err error
1515
)
16+
1617
config := draftjs.NewDefaultConfig()
1718
for _, test := range GetTestsTable() {
1819
if err = json.Unmarshal([]byte(test.State), &contentState); err != nil {

0 commit comments

Comments
 (0)