on master help sign in
Revamp the Text library so that there is an explicit Text.Style record

The Style record is needed to allow formatting of text fields!

commit9723d2d41c833a97002ba74a1e3c9c48f9ca5156 authorEvan Czaplicki <info@elm-lang.org> dateSat 1 Mar 2014, 17:26 (13 years ago) parentcc3c870
3 files changed+114−82
libraries/Native/Text.js+60−37 libraries/Native/Utils.js−7 libraries/Text.elm+54−38
libraries/Native/Text.js+60−37
@@ -4,10 +4,9 @@ Elm.Native.Text.make = function(elm) {
44 elm.Native.Text = elm.Native.Text || {};
55 if (elm.Native.Text.values) return elm.Native.Text.values;
66
7− var JS = Elm.JavaScript.make(elm);
8− var Utils = Elm.Native.Utils.make(elm);
9− var Color = Elm.Native.Color.make(elm);
107 var Element = Elm.Graphics.Element.make(elm);
8+ var List = Elm.Native.List.make(elm);
9+ var Utils = Elm.Native.Utils.make(elm);
1110 var show = Elm.Native.Show.make(elm).show;
1211
1312 function makeSpaces(s) {
@@ -51,13 +50,58 @@ Elm.Native.Text.make = function(elm) {
5150 return arr.join('<br/>');
5251 }
5352
54− function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
53+ function toText(str) { return Utils.txt(properEscape(str)); }
54+
55+ // conversions from Elm values to CSS
56+ function toTypefaces(list) {
57+ var typefaces = List.toArray(list);
58+ for (var i = typefaces.length; i--; ) {
59+ var typeface = typefaces[i];
60+ if (typeface.contains(' ')) {
61+ typefaces[i] = "'" + typeface + "'";
62+ }
63+ }
64+ return typefaces.join(',');
65+ }
66+ function toLine(line) {
67+ var ctor = line.ctor;
68+ var decoration = ctor === 'Under' ? 'underline' :
69+ ctor === 'Over' ? 'overline' : 'line-through';
70+ return 'text-decoration:' + decoration + ';';
71+ }
72+ function toColor(c) {
73+ var color = (c._3 === 1)
74+ ? ('rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')')
75+ : ('rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')');
76+ return 'color:' + color + ';';
77+ }
5578
79+ // setting styles of Text
80+ function style(style, text) {
81+ var newText = '<span style="' + toColor(style.color);
82+ if (style.typeface.ctor !== '[]') {
83+ newText += 'font-family:' + toTypefaces(style.typeface) + ';'
84+ }
85+ if (style.height.ctor !== "Nothing") {
86+ newText += 'font-size:' + style.height._0 + 'px;';
87+ }
88+ if (style.bold) {
89+ newText += 'font-weight:bold;';
90+ }
91+ if (style.italic) {
92+ newText += 'font-style:italic;';
93+ }
94+ if (style.line.ctor !== 'Nothing') {
95+ newText += toLine(style.line._0);
96+ }
97+ newText += '">' + Utils.makeText(text) + '</span>'
98+ return Utils.txt(newText);
99+ }
56100 function height(px, text) {
57101 return { style: 'font-size:' + px + 'px;', text:text }
58102 }
59− function typeface(name, text) {
60− return { style: 'font-family:' + name + ';', text:text }
103+ function typeface(names, text) {
104+ return { style: 'font-family:' + toTypefaces(names) + ';', text:text }
61105 }
62106 function monospace(text) {
63107 return { style: 'font-family:monospace;', text:text }
@@ -71,14 +115,8 @@ Elm.Native.Text.make = function(elm) {
71115 function link(href, text) {
72116 return { href: toText(href), text:text };
73117 }
74− function underline(text) {
75− return { line: ' underline', text:text };
76− }
77− function overline(text) {
78− return { line: ' overline', text:text };
79− }
80− function strikeThrough(text) {
81− return { line: ' line-through', text:text };
118+ function line(line, text) {
119+ return { style: toLine(line), text:text };
82120 }
83121
84122 function color(c, text) {
@@ -88,8 +126,8 @@ Elm.Native.Text.make = function(elm) {
88126 return { style: 'color:' + color + ';', text:text };
89127 }
90128
91− function position(align) {
92− function create(text) {
129+ function block(align) {
130+ return function(text) {
93131 var raw = {
94132 ctor :'RawHtml',
95133 html : Utils.makeText(text),
@@ -100,7 +138,6 @@ Elm.Native.Text.make = function(elm) {
100138 var pos = A2(Utils.htmlHeight, 0, raw);
101139 return A3(Element.newElement, pos._0, pos._1, raw);
102140 }
103− return create;
104141 }
105142
106143 function markdown(text, guid) {
@@ -115,36 +152,22 @@ Elm.Native.Text.make = function(elm) {
115152 return A3(Element.newElement, pos._0, pos._1, raw);
116153 }
117154
118− var text = position('left');
119− function asText(v) {
120− return text(monospace(toText(show(v))));
121− }
122−
123− function plainText(v) {
124− return text(toText(v));
125− }
126−
127155 return elm.Native.Text.values = {
128156 toText: toText,
129157
130158 height : F2(height),
131159 italic : italic,
132160 bold : bold,
133− underline : underline,
134− overline : overline,
135− strikeThrough : strikeThrough,
161+ line : F2(line),
136162 monospace : monospace,
137163 typeface : F2(typeface),
138164 color : F2(color),
139165 link : F2(link),
140166
141− justified : position('justify'),
142− centered : position('center'),
143− righted : position('right'),
144− text : text,
145− plainText : plainText,
146− markdown : markdown,
147−
148− asText : asText,
167+ leftAligned : block('left'),
168+ rightAligned : block('right'),
169+ centered : block('center'),
170+ justified : block('justify'),
171+ markdown : markdown,
149172 };
150173};
libraries/Native/Utils.js−7
@@ -76,14 +76,8 @@ Elm.Native.Utils.make = function(elm) {
7676
7777 function makeText(text) {
7878 var style = '';
79− var line = '';
8079 var href = '';
8180 while (true) {
82− if (text.line) {
83− line += text.line;
84− text = text.text;
85− continue;
86− }
8781 if (text.style) {
8882 style += text.style;
8983 text = text.text;
@@ -95,7 +89,6 @@ Elm.Native.Utils.make = function(elm) {
9589 continue;
9690 }
9791 if (href) text = '<a href="' + href + '">' + text + '</a>';
98− if (line) style += 'text-decoration:' + line + ';';
9992 if (style) text = '<span style="' + style + '">' + text + '</span>';
10093 return text;
10194 }
libraries/Text.elm+54−38
@@ -6,13 +6,21 @@ module Text where
66@docs toText
77
88# Creating Elements
9−@docs plainText, asText, text, centered, justified, righted
9+@docs leftAligned, rightAligned, centered, justified
1010
11−# Formatting
12−@docs color, typeface, height, link
11+# Style and Links
12+@docs Style, style, Line, link
1313
14−# Simple Formatting
15−@docs monospace, bold, italic, underline, overline, strikeThrough
14+# Convenience Functions
15+
16+There are two convenience functions for creating an `Element` which can be
17+useful when debugging or prototyping:
18+
19+@docs plainText, asText
20+
21+There are also a bunch of functions to set parts of a `Style` individually:
22+
23+@docs typeface, monospace, height, color, bold, italic, line
1624
1725-}
1826
@@ -25,75 +33,82 @@ import Native.Text
2533
2634data Text = Text
2735
36+data Line = Under | Over | Through
37+
38+{-| Representation of all the ways you can style `Text`.
39+-}
40+type Style =
41+ { typeface : [String]
42+ , height : Maybe Float
43+ , color : Color
44+ , bold : Bool
45+ , italic : Bool
46+ , line : Maybe Line
47+ }
48+
2849{-| Convert a string into text which can be styled and displayed. -}
2950toText : String -> Text
3051toText = Native.Text.toText
3152
32−{-| Set the typeface of some text. The first argument should be a comma
33−separated listing of the desired typefaces:
53+{-| Set the style of some text.
54+-}
55+style : Style -> Text -> Text
56+style = Native.Text.style
57+
58+{-| Provide a list of prefered typefaces for some text.
3459
35− "helvetica, arial, sans-serif"
60+ ["helvetica","arial","sans-serif"]
3661
37−Works the same as the CSS font-family property.
62+Not everyone has access to the same typefaces, so rendering will use the first
63+typeface in the list that is found on the user's computer. If there are no
64+matches, it will use their default typeface. Works the same as the CSS
65+font-family property.
3866-}
39−typeface : String -> Text -> Text
67+typeface : [String] -> Text -> Text
4068typeface = Native.Text.typeface
4169
4270{-| Switch to a monospace typeface. Good for code snippets. -}
4371monospace : Text -> Text
4472monospace = Native.Text.monospace
4573
46−{-| Create a link. -}
74+{-| Create a link.
75+
76+ link "http://elm-lang.org" (toText "Elm Website")
77+-}
4778link : String -> Text -> Text
4879link = Native.Text.link
4980
50−{-| Set the height of text in pixels. -}
5181height : Float -> Text -> Text
5282height = Native.Text.height
5383
54−{-| Set the color of a string. -}
5584color : Color -> Text -> Text
5685color = Native.Text.color
5786
58−{-| Make a string bold. -}
5987bold : Text -> Text
6088bold = Native.Text.bold
6189
62−{-| Italicize a string. -}
6390italic : Text -> Text
6491italic = Native.Text.italic
6592
66−{-| Draw a line above a string. -}
67−overline : Text -> Text
68−overline = Native.Text.overline
93+line : Line -> Text -> Text
94+line = Native.Text.line
6995
70−{-| Underline a string. -}
71−underline : Text -> Text
72−underline = Native.Text.underline
96+leftAligned : Text -> Element
97+leftAligned = Native.Text.leftAligned
7398
74−{-| Draw a line through a string. -}
75−strikeThrough : Text -> Text
76−strikeThrough = Native.Text.strikeThrough
77−
78−{-| Display justified, styled text. -}
79−justified : Text -> Element
80−justified = Native.Text.justified
99+rightAligned : Text -> Element
100+rightAligned = Native.Text.rightAligned
81101
82−{-| Display centered, styled text. -}
83102centered : Text -> Element
84103centered = Native.Text.centered
85104
86−{-| Display right justified, styled text. -}
87−righted : Text -> Element
88−righted = Native.Text.righted
89−
90−{-| Display styled text. -}
91−text : Text -> Element
92−text = Native.Text.text
105+justified : Text -> Element
106+justified = Native.Text.justified
93107
94108{-| Display a plain string. -}
95109plainText : String -> Element
96−plainText = Native.Text.plainText
110+plainText str =
111+ leftAligned (toText str)
97112
98113{-| for internal use only -}
99114markdown : Element
@@ -107,4 +122,5 @@ the browser:
107122Excellent for debugging.
108123-}
109124asText : a -> Element
110−asText = Native.Text.asText
125+asText value =
126+ leftAligned (monospace (toText (show value)))