-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
303 lines (238 loc) · 8.34 KB
/
index.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { FlexMessage, FlexImage, FlexBubble, FlexCarousel, FlexComponent, FlexBox, Action } from '@line/bot-sdk'
export class FlexMessageBuilder {
private _templateType: 'bubble' | 'carousel'
private _currentBubble: FlexBubble
private _flexMessage: FlexMessage
private _currentBlock: FlexBox
private _currentBlockName: 'header' | 'hero' | 'body' | 'footer'
private _currentFlexComponents: FlexComponent[]
public flexMessage(altText: string): FlexMessageBuilder {
this._flexMessage = {
type: 'flex',
altText,
contents: null as any
}
return this
}
public addCarousel(): FlexMessageBuilder {
this._templateType = 'carousel'
this._flexMessage.contents = {
type: 'carousel',
contents: []
}
return this
}
public addBubble(): FlexMessageBuilder {
this._currentBubble = {
type: 'bubble'
}
if (this._templateType === 'carousel') {
(this._flexMessage.contents as FlexCarousel).contents.push(this._currentBubble)
} else {
this._templateType = 'bubble'
this._flexMessage.contents = this._currentBubble
}
return this
}
public setDirection(directiion?: 'ltr' | 'rtl'): FlexMessageBuilder {
this._currentBubble.direction = directiion
return this
}
public addHeader(): FlexMessageBuilder {
this._currentFlexComponents = []
this._currentBlock = {
type: 'box',
layout: 'vertical',
contents: this._currentFlexComponents
}
this._currentBubble.header = this._currentBlock
this._currentBlockName = 'header'
return this
}
public addHero(flexImage: FlexImage): FlexMessageBuilder {
this._currentBubble.hero = flexImage
this._currentBlockName = 'hero'
return this
}
public addBody(): FlexMessageBuilder {
this._currentFlexComponents = []
this._currentBlock = {
type: 'box',
layout: 'vertical',
contents: this._currentFlexComponents
}
this._currentBubble.body = this._currentBlock
this._currentBlockName = 'body'
return this
}
public addFooter(): FlexMessageBuilder {
this._currentFlexComponents = []
this._currentBlock = {
type: 'box',
layout: 'vertical',
contents: this._currentFlexComponents
}
this._currentBubble.footer = this._currentBlock
this._currentBlockName = 'footer'
return this
}
public addComponents(...components: FlexComponent[]): FlexMessageBuilder {
this._currentFlexComponents.push(...components)
return this
}
private setAttribute(name: string, value?: any): FlexMessageBuilder {
if (value === null) {
delete this._currentBlock[name]
} else {
this._currentBlock[name] = value
}
return this
}
public setLayout(layout: 'horizontal' | 'vertical' | 'baseline'): FlexMessageBuilder {
return this.setAttribute('layout', layout)
}
public setSpacing(spacing: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'): FlexMessageBuilder {
return this.setAttribute('spacing', spacing)
}
public setMargin(margin: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'): FlexMessageBuilder {
return this.setAttribute('margin', margin)
}
public setActon(action: Action<{ label: string }>): FlexMessageBuilder {
return this.setAttribute('action', action)
}
public setFlex(flex: number): FlexMessageBuilder {
return this.setAttribute('flex', flex)
}
private setBlockStyle(blockName: string, styleName: 'backgroundColor' | 'separator' | 'separatorColor', value?: string | boolean): FlexMessageBuilder{
this._currentBubble.styles = this._currentBubble.styles || {}
if (!this._currentBubble.styles[blockName]) {
this._currentBubble.styles[blockName] = {[styleName]: value}
} else {
this._currentBubble.styles[blockName][styleName] = value
}
return this
}
public setStyleBackgroundColor(color?: string): FlexMessageBuilder {
return this.setBlockStyle(this._currentBlockName, 'backgroundColor', color)
}
public setStyleSeparator(hasSeparator?: boolean): FlexMessageBuilder {
return this.setBlockStyle(this._currentBlockName, 'separator', hasSeparator)
}
public setStyleSeparatorColor(color?: string): FlexMessageBuilder {
return this.setBlockStyle(this._currentBlockName, 'separatorColor', color)
}
public build(): FlexMessage {
return this._flexMessage
}
}
export class FlexComponentBuilder {
private _flexComponent: FlexComponent
private constructor(type: 'box' | 'button' | 'filler' | 'icon' | 'image' | 'separator' | 'spacer' | 'text') {
this._flexComponent = {
type
} as FlexComponent
return this
}
private setAttribute(name: string, value?: any): FlexComponentBuilder {
if (value === null) {
delete this._flexComponent[name]
} else {
this._flexComponent[name] = value
}
return this
}
public setLayout(layout?: 'horizontal' | 'vertical' | 'baseline'): FlexComponentBuilder {
return this.setAttribute('layout', layout)
}
public setFlex(flex?: number): FlexComponentBuilder {
return this.setAttribute('flex', flex)
}
public setSpacing(spacing?: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'): FlexComponentBuilder {
return this.setAttribute('spacing', spacing)
}
public setMargin(margin?: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'): FlexComponentBuilder {
return this.setAttribute('margin', margin)
}
public setAction(action?: Action): FlexComponentBuilder {
return this.setAttribute('action', action)
}
public setHeight(height?: 'sm' | 'md'): FlexComponentBuilder {
return this.setAttribute('height', height)
}
public setStyle(style?: 'link' | 'primary' | 'secondary'): FlexComponentBuilder {
return this.setAttribute('style', style)
}
public setColor(color?: string): FlexComponentBuilder {
return this.setAttribute('color', color)
}
public setGravity(gravity?: 'top' | 'bottom' | 'center'): FlexComponentBuilder {
return this.setAttribute('gravity', gravity)
}
public setSize(size?: 'xxs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | '3xl' | '4xl' | '5xl' | 'full'): FlexComponentBuilder {
return this.setAttribute('size', size)
}
public setUrl(url: string): FlexComponentBuilder {
return this.setAttribute('url', url)
}
public setAspectRatio(aspectRatio: '1:1' | '1.51:1' | '1.91:1' | '4:3' | '16:9' | '20:13' | '2:1' | '3:1' | '3:4' | '9:16' | '1:2' | '1:3'): FlexComponentBuilder {
return this.setAttribute('aspectRatio', aspectRatio)
}
public setAlign(align?: 'start' | 'end' | 'center'): FlexComponentBuilder {
return this.setAttribute('align', align)
}
public setAspectMode(aspectMode?: 'cover' | 'fit'): FlexComponentBuilder {
return this.setAttribute('aspectMode', aspectMode)
}
public setBackgroundColor(backgroundColor?: string): FlexComponentBuilder {
return this.setAttribute('backgroundColor', backgroundColor)
}
public setWrap(wrap?: boolean): FlexComponentBuilder {
return this.setAttribute('wrap', wrap)
}
public setMaxLines(maxLines?: number): FlexComponentBuilder {
return this.setAttribute('maxLines', maxLines)
}
public setWeight(weight?: 'regular' | 'bold'): FlexComponentBuilder {
return this.setAttribute('weight', weight)
}
public setGarvity(gravity?: 'top' | 'bottom' | 'center'): FlexComponentBuilder {
return this.setAttribute('gravity', gravity)
}
public setText(text?: string): FlexComponentBuilder {
return this.setAttribute('text', text)
}
public addContents(...flexComponents: FlexComponent[]): FlexComponentBuilder {
if (!this._flexComponent['contents']) {
this._flexComponent['contents'] = []
}
this._flexComponent['contents'].push(...flexComponents)
return this
}
public build(): FlexComponent {
return this._flexComponent
}
public static flexBox() {
return new FlexComponentBuilder('box')
}
public static flexButton() {
return new FlexComponentBuilder('button')
}
public static flexFiller() {
return new FlexComponentBuilder('filler')
}
public static flexIcon() {
return new FlexComponentBuilder('icon')
}
public static flexImage() {
return new FlexComponentBuilder('image')
}
public static flexSeparator() {
return new FlexComponentBuilder('separator')
}
public static flexSpacer() {
return new FlexComponentBuilder('spacer')
}
public static flexText() {
return new FlexComponentBuilder('text')
}
}