on 080eb08 help sign in
150 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),
96 align: align,
97 guid : null,
98 args : [],
99 };
100 var pos = A2(Utils.htmlHeight, 0, raw);
101 return A3(Element.newElement, pos._0, pos._1, raw);
102 }
103 return create;
104 }
106 function markdown(text, guid) {
107 var raw = {
108 ctor:'RawHtml',
109 html: text,
110 align: null,
111 guid: guid,
112 args: [],
113 };
114 var pos = A2(Utils.htmlHeight, 0, raw);
115 return A3(Element.newElement, pos._0, pos._1, raw);
116 }
118 var text = position('left');
119 function asText(v) {
120 return text(monospace(toText(show(v))));
121 }
123 function plainText(v) {
124 return text(toText(v));
125 }
127 return elm.Native.Text.values = {
128 toText: toText,
130 height : F2(height),
131 italic : italic,
132 bold : bold,
133 underline : underline,
134 overline : overline,
135 strikeThrough : strikeThrough,
136 monospace : monospace,
137 typeface : F2(typeface),
138 color : F2(color),
139 link : F2(link),
141 justified : position('justify'),
142 centered : position('center'),
143 righted : position('right'),
144 text : text,
145 plainText : plainText,
146 markdown : markdown,
148 asText : asText,
149 };
150};