on e53ebfc help sign in
elk/libraries/Text.elm
148 lines 3.3 kB history raw
1module Text where
3{-| Functions for displaying text
5# Creating Text
6@docs toText
8# Creating Elements
9@docs leftAligned, rightAligned, centered, justified
11# Links and Style
12@docs link, Style, style, Line, defaultStyle
14# Convenience Functions
16There are two convenience functions for creating an `Element` which can be
17useful when debugging or prototyping:
19@docs plainText, asText
21There are also a bunch of functions to set parts of a `Style` individually:
23@docs typeface, monospace, height, color, bold, italic, line
25-}
27import Basics (..)
28import String
29import Color (Color, black)
30import Graphics.Element (Element, Three, Pos, ElementPrim, Properties)
31import Maybe (Maybe, Nothing)
32import JavaScript (JSString)
33import Native.Text
35data Text = Text
37data Line = Under | Over | Through
39{-| Representation of all the ways you can style `Text`.
40-}
41type Style =
42 { typeface : [String]
43 , height : Maybe Float
44 , color : Color
45 , bold : Bool
46 , italic : Bool
47 , line : Maybe Line
48 }
50{-| Plain black text. It uses the browsers default typeface and text height.
51No decorations are used:
53 { typeface = []
54 , height = Nothing
55 , color = black
56 , bold = False
57 , italic = False
58 , line = Nothing
59 }
60-}
61defaultStyle : Style
62defaultStyle =
63 { typeface = []
64 , height = Nothing
65 , color = black
66 , bold = False
67 , italic = False
68 , line = Nothing
69 }
71{-| Convert a string into text which can be styled and displayed. -}
72toText : String -> Text
73toText = Native.Text.toText
75{-| Set the style of some text.
76-}
77style : Style -> Text -> Text
78style = Native.Text.style
80{-| Provide a list of prefered typefaces for some text.
82 ["helvetica","arial","sans-serif"]
84Not everyone has access to the same typefaces, so rendering will use the first
85typeface in the list that is found on the user's computer. If there are no
86matches, it will use their default typeface. Works the same as the CSS
87font-family property.
88-}
89typeface : [String] -> Text -> Text
90typeface = Native.Text.typeface
92{-| Switch to a monospace typeface. Good for code snippets. -}
93monospace : Text -> Text
94monospace = Native.Text.monospace
96{-| Create a link.
98 link "http://elm-lang.org" (toText "Elm Website")
99-}
100link : String -> Text -> Text
101link = Native.Text.link
103height : Float -> Text -> Text
104height = Native.Text.height
106color : Color -> Text -> Text
107color = Native.Text.color
109bold : Text -> Text
110bold = Native.Text.bold
112italic : Text -> Text
113italic = Native.Text.italic
115line : Line -> Text -> Text
116line = Native.Text.line
118leftAligned : Text -> Element
119leftAligned = Native.Text.leftAligned
121rightAligned : Text -> Element
122rightAligned = Native.Text.rightAligned
124centered : Text -> Element
125centered = Native.Text.centered
127justified : Text -> Element
128justified = Native.Text.justified
130{-| Display a plain string. -}
131plainText : String -> Element
132plainText str =
133 leftAligned (toText str)
135{-| for internal use only -}
136markdown : Element
137markdown = Native.Text.markdown
139{-| Convert anything to its textual representation and make it displayable in
140the browser:
142 asText == text . monospace . show
144Excellent for debugging.
145-}
146asText : a -> Element
147asText value =
148 leftAligned (monospace (toText (String.show value)))