-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
formatted.js
41 lines (34 loc) · 1.09 KB
/
formatted.js
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
// @flow
export class FormattedSection {
text: string;
scale: number | null;
fontStack: string | null;
constructor(text: string, scale: number | null, fontStack: string | null) {
this.text = text;
this.scale = scale;
this.fontStack = fontStack;
}
}
export default class Formatted {
sections: Array<FormattedSection>;
constructor(sections: Array<FormattedSection>) {
this.sections = sections;
}
static fromString(unformatted: string): Formatted {
return new Formatted([new FormattedSection(unformatted, null, null)]);
}
toString(): string {
return this.sections.map(section => section.text).join('');
}
serialize() {
const serialized = ["format"];
for (const section of this.sections) {
serialized.push(section.text);
const fontStack = section.fontStack ?
["literal", section.fontStack.split(',')] :
null;
serialized.push({ "text-font": fontStack, "font-scale": section.scale });
}
return serialized;
}
}