on dcbe2fc help sign in
135 lines 4.3 kB history raw
1Elm.Native.Text = {};
2Elm.Native.Text.make = function(elm) {
3 elm.Native = elm.Native || {};
4 if (elm.Native.Text) return elm.Native.Text;
6 var JS = Elm.JavaScript.make(elm);
7 var htmlHeight = Elm.Native.Utils.make(elm).htmlHeight;
8 var Color = Elm.Native.Color.make(elm);
9 var Element = Elm.Graphics.Element.make(elm);
10 var show = Elm.Native.Show.make(elm).show;
12 function makeSpaces(s) {
13 if (s.length == 0) { return s; }
14 var arr = s.split('');
15 if (arr[0] == ' ') { arr[0] = " " }
16 for (var i = arr.length; --i; ) {
17 if (arr[i][0] == ' ' && arr[i-1] == ' ') {
18 arr[i-1] = arr[i-1] + arr[i];
19 arr[i] = '';
20 }
21 }
22 for (var i = arr.length; i--; ) {
23 if (arr[i].length > 1 && arr[i][0] == ' ') {
24 var spaces = arr[i].split('');
25 for (var j = spaces.length - 2; j >= 0; j -= 2) {
26 spaces[j] = ' ';
27 }
28 arr[i] = spaces.join('');
29 }
30 }
31 arr = arr.join('');
32 if (arr[arr.length-1] === " ") {
33 return arr.slice(0,-1) + ' ';
34 }
35 return arr;
36 }
38 function properEscape(str) {
39 if (str.length == 0) return str;
40 str = str //.replace(/&/g, "&")
41 .replace(/"/g, /*"*/ '"')
42 .replace(/'/g, /*'*/ "'")
43 .replace(/</g, "&#60;")
44 .replace(/>/g, "&#62;")
45 .replace(/\n/g, "<br/>");
46 var arr = str.split('<br/>');
47 for (var i = arr.length; i--; ) {
48 arr[i] = makeSpaces(arr[i]);
49 }
50 return arr.join('<br/>');
51 }
53 function toText(str) { return properEscape(JS.fromString(str)); }
55 function addTag(tag) { return function(text) {
56 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
57 }
58 }
60 function addStyle(style, value, text) {
61 return "<span style='" + style + ":" + value + "'>" + text + "</span>";
62 }
64 function typeface(name, text) {
65 return addStyle('font-family', JS.fromString(name), text);
66 }
67 function monospace(text) {
68 return addStyle('font-family', 'monospace', text);
69 }
70 function size(px, text) { return addStyle('font-size', px + 'px', text) }
71 var header = addTag('h1');
72 function height(h, text) { return addStyle('font-size', h+'px', text) }
73 function italic(text) { return addStyle('font-style', 'italic', text) }
74 var bold = addTag('b');
76 function extract(c) {
77 if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
78 return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
79 }
80 function color(c, text) {
81 return addStyle('color', extract(c), text);
82 }
83 function underline(text) { return addStyle('text-decoration', 'underline', text) }
84 function overline(text) { return addStyle('text-decoration', 'overline', text) }
85 function strikeThrough(text) {
86 return addStyle('text-decoration', 'line-through', text);
87 }
88 function link(href, text) {
89 return "<a href='" + toText(href) + "'>" + text + "</a>";
90 }
92 function position(pos) {
93 return function(text) {
94 var e = {ctor:'RawHtml',
95 _0: '<div style="padding:0;margin:0;text-align:' +
96 pos + '">' + text + '</div>'
97 };
98 var p = A2(htmlHeight, 0, text);
99 return A3(Element.newElement, p._0, p._1, e);
100 }
101 }
103 function asText(v) {
104 return position('left')(monospace(toText(show(v))));
105 }
107 function plainText(v) {
108 return position('left')(toText(v));
109 }
111 return elm.Native.Text = {
112 toText: toText,
114 header : header,
115 height : F2(height),
116 italic : italic,
117 bold : bold,
118 underline : underline,
119 overline : overline,
120 strikeThrough : strikeThrough,
121 monospace : monospace,
122 typeface : F2(typeface),
123 color : F2(color),
124 link : F2(link),
126 justified : position('justify'),
127 centered : position('center'),
128 righted : position('right'),
129 text : position('left'),
130 plainText : plainText,
132 asText : asText
133 };
135};