on 63243ca help sign in
148 lines 4.4 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 height(px, text) {
57 return { style: 'font-size:' + px + 'px;', text:text }
58 }
59 function typeface(name, text) {
60 return { style: 'font-family:' + name + ';', text:text }
61 }
62 function monospace(text) {
63 return { style: 'font-family:monospace;', text:text }
64 }
65 function italic(text) {
66 return { style: 'font-style:italic;', text:text }
67 }
68 function bold(text) {
69 return { style: 'font-weight:bold;', text:text }
70 }
71 function link(href, text) {
72 return { href: toText(href), text:text };
73 }
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 };
82 }
84 function color(c, text) {
85 var color = (c._3 === 1)
86 ? ('rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')')
87 : ('rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')');
88 return { style: 'color:' + color + ';', text:text };
89 }
91 function position(align) {
92 function create(text) {
93 var raw = {
94 ctor:'RawHtml',
95 html: Utils.makeText('text-align:' + align + ';', text),
96 guid: null,
97 args: [],
98 };
99 var pos = A2(Utils.htmlHeight, 0, raw);
100 return A3(Element.newElement, pos._0, pos._1, raw);
101 }
102 return create;
103 }
105 function markdown(text, guid) {
106 var raw = {
107 ctor:'RawHtml',
108 html: text,
109 guid: guid,
110 args: [],
111 };
112 var pos = A2(Utils.htmlHeight, 0, raw);
113 return A3(Element.newElement, pos._0, pos._1, raw);
114 }
116 var text = position('left');
117 function asText(v) {
118 return text(monospace(toText(show(v))));
119 }
121 function plainText(v) {
122 return text(toText(v));
123 }
125 return elm.Native.Text.values = {
126 toText: toText,
128 height : F2(height),
129 italic : italic,
130 bold : bold,
131 underline : underline,
132 overline : overline,
133 strikeThrough : strikeThrough,
134 monospace : monospace,
135 typeface : F2(typeface),
136 color : F2(color),
137 link : F2(link),
139 justified : position('justify'),
140 centered : position('center'),
141 righted : position('right'),
142 text : text,
143 plainText : plainText,
144 markdown : markdown,
146 asText : asText,
147 };
148};