on master help sign in
Change the internal representation of Text
82e9acfEvan Czaplicki
libraries/Native/List.js+17−3
@@ -53,8 +53,15 @@ Elm.Native.List.make = function(elm) {
53 }
53 }
54
54
55 function append(xs,ys) {
55 function append(xs,ys) {
56+ // append Text
57+ if (xs.text || ys.text) {
56− if (xs.isText) return Utils.txt(xs.concat(ys));
58+ return Utils.txt(Utils.makeText('',xs) + Utils.makeText('',ys));
59+ }
60+
61+ // append Strings
57− if (typeof xs === "string") return xs.concat(ys);
62+ if (typeof xs === "string") return xs + ys;
63+
64+ // append Lists
58 if (xs.ctor === '[]') { return ys; }
65 if (xs.ctor === '[]') { return ys; }
59 var root = Cons(xs._0, Nil);
66 var root = Cons(xs._0, Nil);
60 var curr = root;
67 var curr = root;
@@ -253,7 +260,14 @@ Elm.Native.List.make = function(elm) {
253 }
260 }
254
261
255 function join(sep, xss) {
262 function join(sep, xss) {
263+ if (sep.text) {
264+ sep = Utils.makeText('',sep);
265+ xss = toArray(xss);
266+ for (var i = xss.length; i--; ) {
267+ xss[i] = Utils.makeText('',xss[i]);
268+ }
256− if (sep.isText) return Utils.txt(toArray(xss).join(sep));
269+ return Utils.txt(xss.join(sep));
270+ }
257 if (typeof sep === 'string') return toArray(xss).join(sep);
271 if (typeof sep === 'string') return toArray(xss).join(sep);
258 if (xss.ctor === '[]') return Nil;
272 if (xss.ctor === '[]') return Nil;
259 var s = toArray(sep);
273 var s = toArray(sep);
libraries/Native/Text.js+31−33
@@ -53,55 +53,53 @@ Elm.Native.Text.make = function(elm) {
53
53
54 function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
54 function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
55
55
56− function addTag(tag) {
56+ function height(px, text) {
57− return function(text) {
57+ return { style: 'font-size:' + px + 'px;', text:text }
58− return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
59− }
60− }
61−
62− function addStyle(style, value, text) {
63− return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
64 }
58 }
65−
66 function typeface(name, text) {
59 function typeface(name, text) {
67− return addStyle('font-family', JS.fromString(name), text);
60+ return { style: 'font-family:' + name + ';', text:text }
68 }
61 }
69 function monospace(text) {
62 function monospace(text) {
70− return addStyle('font-family', 'monospace', text);
63+ return { style: 'font-family:monospace;', text:text }
71 }
64 }
72− function size(px, text) { return addStyle('font-size', px + 'px', text) }
65+ function italic(text) {
73− var header = addTag('h1');
66+ return { style: 'font-style:italic;', text:text }
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');
77−
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 }
67 }
82− function color(c, text) {
68+ function bold(text) {
83− return addStyle('color', extract(c), 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 };
84 }
79 }
85− function underline(text) { return addStyle('text-decoration', 'underline', text) }
86− function overline(text) { return addStyle('text-decoration', 'overline', text) }
87 function strikeThrough(text) {
80 function strikeThrough(text) {
88− return addStyle('text-decoration', 'line-through', text);
81+ return { line: ' line-through', text:text };
89 }
82 }
83+
90− function link(href, text) {
84+ function color(c, text) {
91− return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
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 };
92 }
89 }
93
90
94 function position(align) {
91 function position(align) {
95− return function(text) {
92+ function create(text) {
96 var raw = {
93 var raw = {
97 ctor:'RawHtml',
94 ctor:'RawHtml',
98− html: '<span style="text-align:' + align + ';">' + text + '</span>',
95+ html: Utils.makeText('text-align:' + align + ';', text),
99 guid: null,
96 guid: null,
100 args: [],
97 args: [],
101 };
98 };
102 var pos = A2(Utils.htmlHeight, 0, raw);
99 var pos = A2(Utils.htmlHeight, 0, raw);
103 return A3(Element.newElement, pos._0, pos._1, raw);
100 return A3(Element.newElement, pos._0, pos._1, raw);
104 }
101 }
102+ return create;
105 }
103 }
106
104
107 function markdown(text, guid) {
105 function markdown(text, guid) {
@@ -123,18 +121,18 @@ Elm.Native.Text.make = function(elm) {
123 return A3(Element.newElement, pos._0, pos._1, raw);
121 return A3(Element.newElement, pos._0, pos._1, raw);
124 }
122 }
125
123
124+ var text = position('left');
126 function asText(v) {
125 function asText(v) {
127− return position('left')(monospace(toText(show(v))));
126+ return text(monospace(toText(show(v))));
128 }
127 }
129
128
130 function plainText(v) {
129 function plainText(v) {
131− return position('left')(toText(v));
130+ return text(toText(v));
132 }
131 }
133
132
134 return elm.Native.Text.values = {
133 return elm.Native.Text.values = {
135 toText: toText,
134 toText: toText,
136
135
137− header : header,
138 height : F2(height),
136 height : F2(height),
139 italic : italic,
137 italic : italic,
140 bold : bold,
138 bold : bold,
@@ -149,7 +147,7 @@ Elm.Native.Text.make = function(elm) {
149 justified : position('justify'),
147 justified : position('justify'),
150 centered : position('center'),
148 centered : position('center'),
151 righted : position('right'),
149 righted : position('right'),
152− text : position('left'),
150+ text : text,
153 plainText : plainText,
151 plainText : plainText,
154 markdown : markdown,
152 markdown : markdown,
155
153
libraries/Native/Utils.js+29−1
@@ -68,10 +68,37 @@ Elm.Native.Utils.make = function(elm) {
68
68
69 function txt(str) {
69 function txt(str) {
70 var t = new String(str);
70 var t = new String(str);
71− t.isText = true;
71+ t.text = true;
72 return t;
72 return t;
73 }
73 }
74
74
75+ function makeText(style, text) {
76+ var style = style;
77+ var line = '';
78+ var href = '';
79+ while (true) {
80+ if (text.line) {
81+ line += text.line;
82+ text = text.text;
83+ continue;
84+ }
85+ if (text.style) {
86+ style += text.style;
87+ text = text.text;
88+ continue;
89+ }
90+ if (text.href) {
91+ href = text.href;
92+ text = text.text;
93+ continue;
94+ }
95+ if (href) text = '<a href="' + href + '">' + text + '</a>';
96+ if (line) style += 'text-decoration:' + line + ';';
97+ if (style) text = '<span style="' + style + '">' + text + '</span>';
98+ return text;
99+ }
100+ }
101+
75 var count = 0;
102 var count = 0;
76 function guid(_) { return count++ }
103 function guid(_) { return count++ }
77
104
@@ -186,6 +213,7 @@ Elm.Native.Utils.make = function(elm) {
186 Tuple2:Tuple2,
213 Tuple2:Tuple2,
187 chr:chr,
214 chr:chr,
188 txt:txt,
215 txt:txt,
216+ makeText:makeText,
189 copy: copy,
217 copy: copy,
190 remove: remove,
218 remove: remove,
191 replace: replace,
219 replace: replace,
libraries/Text.elm+1−5
@@ -12,7 +12,7 @@ module Text where
12@docs color, typeface, height, link
12@docs color, typeface, height, link
13
13
14# Simple Formatting
14# Simple Formatting
15−@docs monospace, header, bold, italic, underline, overline, strikeThrough
15+@docs monospace, bold, italic, underline, overline, strikeThrough
16
16
17-}
17-}
18
18
@@ -43,10 +43,6 @@ typeface = Native.Text.typeface
43monospace : Text -> Text
43monospace : Text -> Text
44monospace = Native.Text.monospace
44monospace = Native.Text.monospace
45
45
46−{-| Make text big and noticable. -}
47−header : Text -> Text
48−header = Native.Text.header
49−
50{-| Create a link. -}
46{-| Create a link. -}
51link : String -> Text -> Text
47link : String -> Text -> Text
52link = Native.Text.link
48link = Native.Text.link