Skip to content

Commit 98be5c9

Browse files
committedJul 5, 2018
Add Text element
1 parent 234aa60 commit 98be5c9

8 files changed

+126
-1
lines changed
 

‎lib/body_element.dart

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract class BodyElement {
2+
String get type;
3+
Map<String, dynamic> toMap();
4+
}

‎lib/canvas.dart

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
import 'body_element.dart';
2+
13
class Canvas {
24
String backgroundColor;
35
List<String> bodyElementIds;
6+
List<BodyElement> bodyElements;
47
bool isHidden = false;
58
bool isPublished = false;
69
String name;
710

11+
Canvas({
12+
this.backgroundColor,
13+
this.bodyElementIds,
14+
this.isHidden,
15+
this.isPublished,
16+
this.name,
17+
});
18+
819
Map<String, dynamic> toMap() {
920
return {
1021
'background_color': backgroundColor,

‎lib/facebook_canvas.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
library facebook_canvas;
22

3-
export 'canvas.dart';
3+
export 'body_element.dart';
4+
export 'inline_style.dart';
5+
export 'rich_text.dart';
6+
export 'text.dart';
7+
export 'canvas.dart';

‎lib/inline_style.dart

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class InlineStyle {
2+
int length;
3+
int offset;
4+
String style;
5+
6+
InlineStyle({
7+
this.length,
8+
this.offset,
9+
this.style,
10+
});
11+
12+
Map<String, dynamic> toMap() {
13+
return {
14+
'length': length,
15+
'offset': offset,
16+
'style': style,
17+
};
18+
}
19+
}

‎lib/rich_text.dart

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'inline_style.dart';
2+
3+
class RichText {
4+
List<InlineStyle> inlineStyles;
5+
String plainText;
6+
7+
RichText(this.plainText, [this.inlineStyles = const []]);
8+
9+
Map<String, dynamic> toMap() {
10+
return {
11+
'inline_styles':
12+
inlineStyles.map((inlineStyle) => inlineStyle?.toMap()).toList(),
13+
'plain_text': plainText,
14+
};
15+
}
16+
}

‎lib/text.dart

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'rich_text.dart';
2+
import 'body_element.dart';
3+
4+
enum TextAlignment {
5+
left,
6+
center,
7+
right,
8+
}
9+
10+
class Text implements BodyElement {
11+
String type = 'canvas_text';
12+
13+
String backgroundColor;
14+
double bottomPadding;
15+
String fontFamily;
16+
int fontSize;
17+
double lineHeight;
18+
String name;
19+
RichText richText;
20+
TextAlignment textAlignment;
21+
String textColor;
22+
double topPadding;
23+
24+
Text({
25+
this.backgroundColor,
26+
this.bottomPadding,
27+
this.fontFamily,
28+
this.fontSize,
29+
this.lineHeight,
30+
this.name,
31+
this.richText,
32+
this.textAlignment = TextAlignment.center,
33+
this.textColor,
34+
this.topPadding,
35+
});
36+
37+
Map<String, dynamic> toMap() {
38+
return {
39+
'background_color': backgroundColor,
40+
'bottom_padding': bottomPadding,
41+
'font_family': fontFamily,
42+
'font_size': fontSize,
43+
'line_height': lineHeight,
44+
'name': name,
45+
'rich_text': richText?.toMap(),
46+
'text_alignment': textAlignment
47+
.toString()
48+
.substring('TextAlignment.'.length)
49+
.toUpperCase(),
50+
'text_color': textColor,
51+
'top_padding': topPadding,
52+
};
53+
}
54+
}

‎pubspec.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
name: facebook_canvas
22
version: 0.1.0
33
description: Facebook Canvas elements in Dart.
4+
5+
dev_dependencies:
6+
test:

‎test/text_test.dart

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:test/test.dart';
2+
3+
import 'package:facebook_canvas/text.dart';
4+
5+
void main() {
6+
test('.toMap()', () {
7+
final text = new Text();
8+
text.textAlignment = TextAlignment.center;
9+
10+
final map = text.toMap();
11+
12+
expect(map['text_alignment'], equals('CENTER'));
13+
});
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.