-
Notifications
You must be signed in to change notification settings - Fork 15
/
block_formats.go
196 lines (160 loc) · 4.5 KB
/
block_formats.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package quill
// paragraph
type textFormat struct{}
func (*textFormat) Fmt() *Format {
return &Format{
Val: "p",
Place: Tag,
Block: true,
}
}
func (*textFormat) HasFormat(o *Op) bool {
return o.Type == "text"
}
// block quote
type blockQuoteFormat struct{}
func (*blockQuoteFormat) Fmt() *Format {
return &Format{
Val: "blockquote",
Place: Tag,
Block: true,
}
}
func (*blockQuoteFormat) HasFormat(o *Op) bool {
return o.HasAttr("blockquote")
}
// header
type headerFormat struct {
level string // the string "1", "2", "3", ...
}
func (hf *headerFormat) Fmt() *Format {
return &Format{
Val: "h" + hf.level,
Place: Tag,
Block: true,
}
}
func (hf *headerFormat) HasFormat(o *Op) bool {
return o.Attrs["header"] == hf.level
}
// list
type listFormat struct {
lType string // either "ul" or "ol"
indent uint8 // the number of nested
}
func (lf *listFormat) Fmt() *Format {
return &Format{
Val: "li",
Place: Tag,
Block: true,
}
}
func (lf *listFormat) HasFormat(o *Op) bool {
return o.HasAttr("list")
}
// listFormat implements the FormatWrapper interface.
func (lf *listFormat) Wrap() (string, string) {
return "<" + lf.lType + ">", "</" + lf.lType + ">"
}
// listFormat implements the FormatWrapper interface.
func (lf *listFormat) Open(open []*Format, o *Op) bool {
// If there is a list of this type already open, no need to open another.
for i := range open {
if open[i].Place == Tag && open[i].Val == "<"+lf.lType+">" {
return false
}
}
return true
}
// listFormat implements the FormatWrapper interface.
func (lf *listFormat) Close(open []*Format, o *Op, doingBlock bool) bool {
if !doingBlock { // The closing wrap is written only when we know what kind of block this will be.
return false
}
t := o.Attrs["list"] // The type of the current list item (ordered or bullet).
return !o.HasAttr("list") || (t == "ordered" && lf.lType != "ol") || (t == "bullet" && lf.lType != "ul")
// Currently, the way Quill.js renders nested lists isn't very satisfactory. But we'll stay consistent with how
// it appears to users for now. The code below is mostly correct for a better way to render nested lists.
//if !o.HasAttr("list") { // If the block is not a list item at all, close the list block.
// return true
//}
//t := o.Attrs["list"] // The type of the current list item (ordered or bullet).
// ind := indentDepths[o.Attrs["indent"]] // The indent of the current list item.
// Close the list block only if both (a) the current list item is staying at the same indent level or is at a
// lower indent level and (b) the type of the list is different from the type of the previous.
// return ind <= lf.indent && ((t == "ordered" && lf.lType != "ol") || (t == "bullet" && lf.lType != "ul"))
}
// indentDepths gives either the indent amount of a list or 0 if there is no indenting.
var indentDepths = map[string]uint8{
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
}
// text alignment
type alignFormat struct {
val string
}
func (af *alignFormat) Fmt() *Format {
return &Format{
Val: "align-" + af.val,
Place: Class,
Block: true,
}
}
func (af *alignFormat) HasFormat(o *Op) bool {
return o.Attrs["align"] == af.val
}
type indentFormat struct {
in string
}
func (inf *indentFormat) Fmt() *Format {
return &Format{
Val: "indent-" + inf.in,
Place: Class,
Block: true,
}
}
func (inf *indentFormat) HasFormat(o *Op) bool {
return o.Attrs["indent"] == inf.in
}
// code block
type codeBlockFormat struct {
o *Op
}
func (cf *codeBlockFormat) Fmt() *Format {
return &Format{
Place: Tag,
Block: true,
}
}
func (cf *codeBlockFormat) HasFormat(o *Op) bool {
return false // Only a wrapper.
}
// codeBlockFormat implements the FormatWrapper interface.
func (*codeBlockFormat) Wrap() (string, string) {
return "<pre>", "\n</pre>"
}
// codeBlockFormat implements the FormatWrapper interface.
func (*codeBlockFormat) Open(open []*Format, _ *Op) bool {
// If there is a code block already open, no need to open another.
for i := range open {
if open[i].Place == Tag && open[i].Val == "<pre>" {
return false
}
}
return true
}
// codeBlockFormat implements the FormatWrapper interface.
func (cf *codeBlockFormat) Close(_ []*Format, o *Op, doingBlock bool) bool {
if doingBlock && !o.HasAttr("code-block") {
return true
}
// We are simply adding another line to the code block. The previous line should end with
// a "\n" though all instances of "\n" are stripped out by the split from the start.
if !doingBlock {
o.Data = "\n" + o.Data
}
return false
}