From 698f2aa71ffc1b9e56193c13e6366765134152c9 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Sat, 29 Nov 2014 19:02:35 -0500 Subject: formalizing import/export stuff --- index.html | 61 ++++++++++++++++++++++++++++++++++----------------------- js/clipboard.js | 31 +++++++++++++++++++++++++++-- js/lex.js | 13 +++++++++--- js/matrix.js | 33 ++++++++++++++++++++++++------- 4 files changed, 102 insertions(+), 36 deletions(-) diff --git a/index.html b/index.html index 9de3d1b..ec39e73 100644 --- a/index.html +++ b/index.html @@ -17,34 +17,47 @@ body { transition: 0.1s linear; } -
-
-
-
-
-
-
- square - circle - text - clear - grid - shader +
+
+
- brush size: 5 x 5 - canvas size: 80 x 24 +
+
+
+
+ square + circle + text + clear + grid + shader + import + export + + brush size: 5 x 5 + canvas size: 80 x 24 - _ animate -
- -
- + _ animate +
+ +
+ +
+ + + + + + +
+ + + diff --git a/js/clipboard.js b/js/clipboard.js index 744901c..501b189 100644 --- a/js/clipboard.js +++ b/js/clipboard.js @@ -1,18 +1,45 @@ var clipboard = (function () { + var format; var disabled = false; var contentType = 'text/plain;charset=utf-8' document.body.addEventListener('copy', function (e) { if (disabled) { return } if (e.clipboardData) { e.preventDefault(); - e.clipboardData.setData(contentType, canvas.ascii()); + e.clipboardData.setData(contentType, canvas.irssi()); } if (window.clipboardData) { e.returnValue = false; - window.clipboardData.setData(contentType, canvas.ascii()); + window.clipboardData.setData(contentType, canvas.irssi()); } }, false); + + function import_data () { + var data = import_textarea.value + lines = data.split("\n") + } + function export_data () { + var output + switch (format) { + case 'ascii': + output = canvas.ascii() + break + case 'mirc': + output = canvas.mirc() + break + case 'irssi': + output = canvas.irssi() + break + } + } + + import_ascii.addEventListener("click", function(){ format = "ascii" }) + import_irssi.addEventListener("click", function(){ format = "irssi" }) + import_mirc.addEventListener("click", function(){ format = "mirc" }) + import_button.addEventListener("click", import_data) + export_button.addEventListener("click", export_data) + // import_textarea return { enable: function(){ disabled = false }, diff --git a/js/lex.js b/js/lex.js index cf7c655..29f452b 100644 --- a/js/lex.js +++ b/js/lex.js @@ -25,16 +25,23 @@ Lex.prototype.read = function(){ this.char = this.span.innerHTML return this.char } +Lex.prototype.ascii = function(){ + return this.char || " " +} Lex.prototype.sanitize = function(){ return this.char == "%" ? '%%' : this.char || " " } -Lex.prototype.irc = function(){ - var char = this.sanitize() +var fgOnly = false +Lex.prototype.mirc = function(){ + var char = this.char || " " + if (fgOnly) { + return "\x03" + (this.fg&15) + char + } if (this.bg == 1 && this.fg == 0) { return char } else { - return "\\x03" + (this.fg&15) + "," + (this.bg&15) + char + return "\x03" + (this.fg&15) + "," + (this.bg&15) + char } } Lex.prototype.clone = function (lex){ diff --git a/js/matrix.js b/js/matrix.js index 73c2992..4366eaa 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -83,6 +83,17 @@ Matrix.prototype.region = function(w,h,x,y) { mat.f = this.f } Matrix.prototype.ascii = function () { + var lines = this.aa.map(function(row, y){ + var last, line = "" + row.forEach(function(lex, x) { + line += lex.ascii() + }) + return line.replace(/\s+$/,"") + }) + var txt = lines.join("\n") + return txt +} +Matrix.prototype.mirc = function () { var lines = this.aa.map(function(row, y){ var last, line = "" row.forEach(function(lex, x) { @@ -90,16 +101,24 @@ Matrix.prototype.ascii = function () { line += lex.sanitize() } else { - if (x > 0 && last && (last.bg != 1 || last.fg != 0)) line += "\\x03" - line += lex.irc() + if (x > 0 && last && (last.bg != 1 || last.fg != 0)) line += "\x03" + line += lex.mirc() last = lex } }) - if (last && ! last.isClear()) { line += "\\x03" } - return line.replace(/\s+$/,"").replace(/\"/g, '\\\"').replace(/\`/g, '\\\`') + if (last && ! last.isClear()) { line += "\x03" } + return line.replace(/\s+$/,"") }).filter(function(line){ return line.length > 0 }) - var txt = '/exec -out printf "' + lines.join("\\n") + '"\n' - return txt + return lines.join("\n") +} +Matrix.prototype.irssi = function(){ + var txt = this.mirc() + .replace(/\%/g, '%%') + .replace(/\"/g, '\\\"') + .replace(/\`/g, '\\\`') + .replace(/\\n/g, '\\n') + .replace(/\x03/g, '\\x03') + return '/exec -out printf "' + txt + '"\n' } Matrix.prototype.expand = function(i){ var w = this.w = clamp(this.w+i, 1, 9), h = this.h = clamp(this.h+i, 1, 9) @@ -111,5 +130,5 @@ Matrix.prototype.expand = function(i){ this.rebuild() } Matrix.prototype.contract = function(i){ - brush.expand(-i) + this .expand(-i) } -- cgit v1.2.3-70-g09d2