elm/libraries/Native/Text.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

Elm.Native.Text = {};
Elm.Native.Text.make = function(elm) {
2013-09-01 19:55:11 +00:00
elm.Native = elm.Native || {};
elm.Native.Text = elm.Native.Text || {};
if (elm.Native.Text.values) return elm.Native.Text.values;
2013-09-01 19:55:11 +00:00
var JS = Elm.JavaScript.make(elm);
var Utils = Elm.Native.Utils.make(elm);
var Color = Elm.Native.Color.make(elm);
var Element = Elm.Graphics.Element.make(elm);
var show = Elm.Native.Show.make(elm).show;
2013-09-01 19:55:11 +00:00
function makeSpaces(s) {
if (s.length == 0) { return s; }
var arr = s.split('');
if (arr[0] == ' ') { arr[0] = " " }
for (var i = arr.length; --i; ) {
if (arr[i][0] == ' ' && arr[i-1] == ' ') {
arr[i-1] = arr[i-1] + arr[i];
arr[i] = '';
}
}
for (var i = arr.length; i--; ) {
if (arr[i].length > 1 && arr[i][0] == ' ') {
var spaces = arr[i].split('');
for (var j = spaces.length - 2; j >= 0; j -= 2) {
spaces[j] = ' ';
}
arr[i] = spaces.join('');
}
}
arr = arr.join('');
if (arr[arr.length-1] === " ") {
return arr.slice(0,-1) + ' ';
}
return arr;
}
2013-09-01 19:55:11 +00:00
function properEscape(str) {
if (str.length == 0) return str;
str = str //.replace(/&/g, "&")
.replace(/"/g, '"')
.replace(/'/g, "'")
2013-09-01 19:55:11 +00:00
.replace(/</g, "&#60;")
.replace(/>/g, "&#62;")
.replace(/\n/g, "<br/>");
var arr = str.split('<br/>');
for (var i = arr.length; i--; ) {
arr[i] = makeSpaces(arr[i]);
}
2013-09-01 19:55:11 +00:00
return arr.join('<br/>');
}
2013-09-01 19:55:11 +00:00
function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
2013-09-01 19:55:11 +00:00
function height(px, text) {
return { style: 'font-size:' + px + 'px;', text:text }
}
2013-09-01 19:55:11 +00:00
function typeface(name, text) {
return { style: 'font-family:' + name + ';', text:text }
2013-09-01 19:55:11 +00:00
}
function monospace(text) {
return { style: 'font-family:monospace;', text:text }
2013-09-01 19:55:11 +00:00
}
function italic(text) {
return { style: 'font-style:italic;', text:text }
2013-09-01 19:55:11 +00:00
}
function bold(text) {
return { style: 'font-weight:bold;', text:text }
}
function link(href, text) {
return { href: toText(href), text:text };
}
function underline(text) {
return { line: ' underline', text:text };
}
function overline(text) {
return { line: ' overline', text:text };
2013-09-01 19:55:11 +00:00
}
function strikeThrough(text) {
return { line: ' line-through', text:text };
2013-09-01 19:55:11 +00:00
}
function color(c, text) {
var color = (c._3 === 1)
? ('rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')')
: ('rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')');
return { style: 'color:' + color + ';', text:text };
2013-09-01 19:55:11 +00:00
}
2013-10-25 15:36:30 +00:00
function position(align) {
function create(text) {
2013-10-25 15:36:30 +00:00
var raw = {
ctor:'RawHtml',
html: Utils.makeText('text-align:' + align + ';', text),
2013-11-02 22:17:29 +00:00
guid: null,
2013-10-25 15:36:30 +00:00
args: [],
};
var pos = A2(Utils.htmlHeight, 0, raw);
return A3(Element.newElement, pos._0, pos._1, raw);
2013-09-01 19:58:42 +00:00
}
return create;
2013-09-01 19:55:11 +00:00
}
2013-10-25 15:36:30 +00:00
function markdown(text, guid) {
var raw = {
ctor:'RawHtml',
html: text,
guid: guid,
args: Array.prototype.slice.call(arguments, 2).map(function(arg) {
if (arg.props && arg.element) {
arg.isElement = true;
return arg;
} else if (!arg.isText) {
return Utils.txt('<code>' + show(arg) + '</code>');
}
return arg;
}),
};
var pos = A2(Utils.htmlHeight, 0, raw);
return A3(Element.newElement, pos._0, pos._1, raw);
}
var text = position('left');
2013-09-01 19:55:11 +00:00
function asText(v) {
return text(monospace(toText(show(v))));
2013-09-01 19:55:11 +00:00
}
2013-09-01 19:55:11 +00:00
function plainText(v) {
return text(toText(v));
}
2013-09-01 19:55:11 +00:00
return elm.Native.Text.values = {
2013-09-01 19:55:11 +00:00
toText: toText,
height : F2(height),
italic : italic,
bold : bold,
underline : underline,
overline : overline,
strikeThrough : strikeThrough,
monospace : monospace,
typeface : F2(typeface),
color : F2(color),
link : F2(link),
justified : position('justify'),
centered : position('center'),
righted : position('right'),
text : text,
2013-09-01 19:55:11 +00:00
plainText : plainText,
2013-10-25 15:36:30 +00:00
markdown : markdown,
2013-09-01 19:55:11 +00:00
2013-10-25 15:36:30 +00:00
asText : asText,
2013-09-01 19:55:11 +00:00
};
};