on e53ebfc help sign in
165 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 Color = Elm.Native.Color.make(elm);
8 var Element = Elm.Graphics.Element.make(elm);
9 var List = Elm.Native.List.make(elm);
10 var Utils = Elm.Native.Utils.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(str)); }
56 // conversions from Elm values to CSS
57 function toTypefaces(list) {
58 var typefaces = List.toArray(list);
59 for (var i = typefaces.length; i--; ) {
60 var typeface = typefaces[i];
61 if (typeface.contains(' ')) {
62 typefaces[i] = "'" + typeface + "'";
63 }
64 }
65 return typefaces.join(',');
66 }
67 function toLine(line) {
68 var ctor = line.ctor;
69 var decoration = ctor === 'Under' ? 'underline' :
70 ctor === 'Over' ? 'overline' : 'line-through';
71 return 'text-decoration:' + decoration + ';';
72 }
74 // setting styles of Text
75 function style(style, text) {
76 var newText = '<span style="color:' + toCss(style.color) + ';'
77 if (style.typeface.ctor !== '[]') {
78 newText += 'font-family:' + toTypefaces(style.typeface) + ';'
79 }
80 if (style.height.ctor !== "Nothing") {
81 newText += 'font-size:' + style.height._0 + 'px;';
82 }
83 if (style.bold) {
84 newText += 'font-weight:bold;';
85 }
86 if (style.italic) {
87 newText += 'font-style:italic;';
88 }
89 if (style.line.ctor !== 'Nothing') {
90 newText += toLine(style.line._0);
91 }
92 newText += '">' + Utils.makeText(text) + '</span>'
93 return Utils.txt(newText);
94 }
95 function height(px, text) {
96 return { style: 'font-size:' + px + 'px;', text:text }
97 }
98 function typeface(names, text) {
99 return { style: 'font-family:' + toTypefaces(names) + ';', text:text }
100 }
101 function monospace(text) {
102 return { style: 'font-family:monospace;', text:text }
103 }
104 function italic(text) {
105 return { style: 'font-style:italic;', text:text }
106 }
107 function bold(text) {
108 return { style: 'font-weight:bold;', text:text }
109 }
110 function link(href, text) {
111 return { href: toText(href), text:text };
112 }
113 function line(line, text) {
114 return { style: toLine(line), text:text };
115 }
117 function color(color, text) {
118 return { style: 'color:' + toCss(color) + ';', text:text };
119 }
121 function block(align) {
122 return function(text) {
123 var raw = {
124 ctor :'RawHtml',
125 html : Utils.makeText(text),
126 align: align,
127 guid : null,
128 args : [],
129 };
130 var pos = A2(Utils.htmlHeight, 0, raw);
131 return A3(Element.newElement, pos._0, pos._1, raw);
132 }
133 }
135 function markdown(text, guid) {
136 var raw = {
137 ctor:'RawHtml',
138 html: text,
139 align: null,
140 guid: guid,
141 args: [],
142 };
143 var pos = A2(Utils.htmlHeight, 0, raw);
144 return A3(Element.newElement, pos._0, pos._1, raw);
145 }
147 return elm.Native.Text.values = {
148 toText: toText,
150 height : F2(height),
151 italic : italic,
152 bold : bold,
153 line : F2(line),
154 monospace : monospace,
155 typeface : F2(typeface),
156 color : F2(color),
157 link : F2(link),
159 leftAligned : block('left'),
160 rightAligned : block('right'),
161 centered : block('center'),
162 justified : block('justify'),
163 markdown : markdown,
164 };
165};