diff options
Diffstat (limited to 'js')
| -rw-r--r-- | js/app.js | 2 | ||||
| -rw-r--r-- | js/clipboard.js | 89 | ||||
| -rw-r--r-- | js/lex.js | 13 | ||||
| -rw-r--r-- | js/matrix.js | 33 | ||||
| -rw-r--r-- | js/ui/controls.js | 34 |
5 files changed, 140 insertions, 31 deletions
@@ -34,7 +34,7 @@ function bind () { window.addEventListener('mouseup', function(){ dragging = erasing = false - if (current_tool.name != 'shader') { cursor_input.focus() } + if (current_tool.name != 'shader' && current_tool.name != 'load' && current_tool.name != 'save') { cursor_input.focus() } }); window.addEventListener('mousedown', function(e){ diff --git a/js/clipboard.js b/js/clipboard.js index 744901c..97261bc 100644 --- a/js/clipboard.js +++ b/js/clipboard.js @@ -1,22 +1,79 @@ var clipboard = (function () { - 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()); - } - if (window.clipboardData) { - e.returnValue = false; - window.clipboardData.setData(contentType, canvas.ascii()); - } - }, false); + var exports = { + format: "irssi", + importing: false, + visible: false, + + bind: function () { + import_ascii.addEventListener("change", exports.setFormat("ascii")) + import_irssi.addEventListener("change", exports.setFormat("irssi")) + import_mirc.addEventListener("change", exports.setFormat("mirc")) + import_button.addEventListener("click", exports.import_data) + export_button.addEventListener("click", exports.export_data) + import_textarea.addEventListener("focus", exports.focus) + import_textarea.addEventListener("blur", exports.blur) + import_irssi.setAttribute("checked", true) + }, + setFormat: function (name) { + return function () { + clipboard.format = name + if (! clipboard.importing) { clipboard.export_data() } + } + }, + show: function () { import_rapper.style.display = "block"; clipboard.visible = true }, + hide: function () { import_rapper.style.display = "none"; clipboard.visible = false }, + focus: function () { + if (! clipboard.importing) { + import_textarea.focus() + import_textarea.select() + } + }, + blur: function () { + }, + + import_mode: function () { + focus() + clipboard.importing = true + import_button.style.display = "inline-block" + export_button.style.display = format_group.display = "none" + import_textarea.value = "" + }, + export_mode: function () { + focus() + clipboard.importing = false + import_button.style.display = "none" + export_button.style.display = format_group.display = "inline-block" + clipboard.export_data() + }, + import_data: function () { + var data = import_textarea.value + lines = data.split("\n") + var width = lines.reduce(function(a,b){ return Math.max(a, b.length) }) + var height = lines.length + }, + export_data: function () { + var output + switch (clipboard.format) { + case 'ascii': + output = canvas.ascii() + break + case 'mirc': + output = canvas.mirc() + break + case 'irssi': + output = canvas.irssi() + break + } + import_textarea.value = output + clipboard.focus() + return output + }, - return { - enable: function(){ disabled = false }, - disable: function(){ disabled = true } } + exports.bind() + + return exports + })()
\ No newline at end of file @@ -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..81ced3e 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -86,20 +86,39 @@ 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) { if (lex.eq(last)) { 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 }).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) } diff --git a/js/ui/controls.js b/js/ui/controls.js index c780c50..d575725 100644 --- a/js/ui/controls.js +++ b/js/ui/controls.js @@ -64,12 +64,10 @@ var controls = (function(){ shader_textarea.style.display = "block" // setTimeout(function(){ shader_textarea.focus() }) shader_textarea.focus() - clipboard.disable() } controls.shader.blur = function(){ Tool.prototype.blur.call(this) shader_textarea.style.display = "none" - clipboard.enable() } shader_textarea.value = demo_shader.innerHTML shader_textarea.addEventListener("input", function(){ @@ -77,7 +75,25 @@ var controls = (function(){ fn && shader.run(canvas) }) - + controls.save = new Tool (save_el) + controls.save.use = function(){ + clipboard.show() + clipboard.export_mode() + } + controls.save.blur = function(){ + Tool.prototype.blur.call(this) + clipboard.hide() + } + controls.load = new Tool (load_el) + controls.load.use = function(){ + clipboard.show() + clipboard.import_mode() + } + controls.load.blur = function(){ + Tool.prototype.blur.call(this) + clipboard.hide() + } + controls.animate = new Tool (animate_checkbox) controls.animate.use = function(){ var state = shader.toggle() @@ -100,7 +116,17 @@ var controls = (function(){ }) }); - [controls.square, controls.circle, controls.text, controls.clear, controls.grid, controls.shader, controls.animate].forEach(function(tool){ + [ + controls.square, + controls.circle, + controls.text, + controls.clear, + controls.grid, + controls.shader, + controls.animate, + controls.save, + controls.load + ].forEach(function(tool){ tool.span.addEventListener('mousedown', function(e){ tool.focus() }) |
