on d97fe19 help sign in
158 lines 5.1 kB history raw
1Elm.Native.Text = {};
2Elm.Native.Text.make = function(elm) {
3 elm.Native = elm.Native || {};
4 elm.Native.Text = elm.Native.Text || {};
5 if (elm.Native.Text.values) return elm.Native.Text.values;
7 var JS = Elm.JavaScript.make(elm);
8 var Utils = Elm.Native.Utils.make(elm);
9 var Color = Elm.Native.Color.make(elm);
10 var Element = Elm.Graphics.Element.make(elm);
11 var show = Elm.Native.Show.make(elm).show;
13 function makeSpaces(s) {
14 if (s.length == 0) { return s; }
15 var arr = s.split('');
16 if (arr[0] == ' ') { arr[0] = " " }
17 for (var i = arr.length; --i; ) {
18 if (arr[i][0] == ' ' && arr[i-1] == ' ') {
19 arr[i-1] = arr[i-1] + arr[i];
20 arr[i] = '';
21 }
22 }
23 for (var i = arr.length; i--; ) {
24 if (arr[i].length > 1 && arr[i][0] == ' ') {
25 var spaces = arr[i].split('');
26 for (var j = spaces.length - 2; j >= 0; j -= 2) {
27 spaces[j] = ' ';
28 }
29 arr[i] = spaces.join('');
30 }
31 }
32 arr = arr.join('');
33 if (arr[arr.length-1] === " ") {
34 return arr.slice(0,-1) + ' ';
35 }
36 return arr;
37 }
39 function properEscape(str) {
40 if (str.length == 0) return str;
41 str = str //.replace(/&/g, "&")
42 .replace(/"/g, '"')
43 .replace(/'/g, "'")
44 .replace(/</g, "&#60;")
45 .replace(/>/g, "&#62;")
46 .replace(/\n/g, "<br/>");
47 var arr = str.split('<br/>');
48 for (var i = arr.length; i--; ) {
49 arr[i] = makeSpaces(arr[i]);
50 }
51 return arr.join('<br/>');
52 }
54 function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
56 function addTag(tag) {
57 return function(text) {
58 return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
59 }
60 }
62 function addStyle(style, value, text) {
63 return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
64 }
66 function typeface(name, text) {
67 return addStyle('font-family', JS.fromString(name), text);
68 }
69 function monospace(text) {
70 return addStyle('font-family', 'monospace', text);
71 }
72 function size(px, text) { return addStyle('font-size', px + 'px', text) }
73 var header = addTag('h1');
74 function height(h, text) { return addStyle('font-size', h+'px', text) }
75 function italic(text) { return addStyle('font-style', 'italic', text) }
76 var bold = addTag('b');
78 function extract(c) {
79 if (c._3 === 1) { return 'rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')'; }
80 return 'rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')';
81 }
82 function color(c, text) {
83 return addStyle('color', extract(c), text);
84 }
85 function underline(text) { return addStyle('text-decoration', 'underline', text) }
86 function overline(text) { return addStyle('text-decoration', 'overline', text) }
87 function strikeThrough(text) {
88 return addStyle('text-decoration', 'line-through', text);
89 }
90 function link(href, text) {
91 return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
92 }
94 function position(align) {
95 return function(text) {
96 var raw = {
97 ctor:'RawHtml',
98 html: '<span style="text-align:' + align + ';">' + text + '</span>',
99 guid: '0',
100 args: [],
101 };
102 var pos = A2(Utils.htmlHeight, 0, raw);
103 return A3(Element.newElement, pos._0, pos._1, raw);
104 }
105 }
107 function markdown(text, guid) {
108 var raw = {
109 ctor:'RawHtml',
110 html: text,
111 guid: guid,
112 args: Array.prototype.slice.call(arguments, 2).map(function(arg) {
113 if (arg.props && arg.element) {
114 arg.isElement = true;
115 return arg;
116 } else if (!arg.isText) {
117 return Utils.txt('<code>' + show(arg) + '</code>');
118 }
119 return arg;
120 }),
121 };
122 var pos = A2(Utils.htmlHeight, 0, raw);
123 return A3(Element.newElement, pos._0, pos._1, raw);
124 }
126 function asText(v) {
127 return position('left')(monospace(toText(show(v))));
128 }
130 function plainText(v) {
131 return position('left')(toText(v));
132 }
134 return elm.Native.Text.values = {
135 toText: toText,
137 header : header,
138 height : F2(height),
139 italic : italic,
140 bold : bold,
141 underline : underline,
142 overline : overline,
143 strikeThrough : strikeThrough,
144 monospace : monospace,
145 typeface : F2(typeface),
146 color : F2(color),
147 link : F2(link),
149 justified : position('justify'),
150 centered : position('center'),
151 righted : position('right'),
152 text : position('left'),
153 plainText : plainText,
154 markdown : markdown,
156 asText : asText,
157 };
158};