summaryrefslogtreecommitdiff
path: root/js/clipboard.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/clipboard.js')
-rw-r--r--js/clipboard.js334
1 files changed, 167 insertions, 167 deletions
diff --git a/js/clipboard.js b/js/clipboard.js
index 66df875..d2d1592 100644
--- a/js/clipboard.js
+++ b/js/clipboard.js
@@ -1,175 +1,175 @@
var clipboard = (function () {
- 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_text)
- import_html.addEventListener("click", exports.import_html)
- export_button.addEventListener("click", exports.export_data)
- import_textarea.addEventListener("focus", exports.focus)
- import_textarea.addEventListener("blur", exports.blur)
- import_textarea.addEventListener('paste', exports.paste)
- 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 () {
- },
+ 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_text)
+ import_html.addEventListener("click", exports.import_html)
+ export_button.addEventListener("click", exports.export_data)
+ import_textarea.addEventListener("focus", exports.focus)
+ import_textarea.addEventListener("blur", exports.blur)
+ import_textarea.addEventListener('paste', exports.paste)
+ 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_buttons.style.display = "inline-block"
- export_button.style.display = format_group.style.display = "none"
- import_textarea.value = ""
- },
- export_mode: function () {
- focus()
- clipboard.importing = false
- import_buttons.style.display = "none"
- export_button.style.display = format_group.style.display = "inline-block"
- clipboard.export_data()
- },
+ import_mode: function () {
+ focus()
+ clipboard.importing = true
+ import_buttons.style.display = "inline-block"
+ export_button.style.display = format_group.style.display = "none"
+ import_textarea.value = ""
+ },
+ export_mode: function () {
+ focus()
+ clipboard.importing = false
+ import_buttons.style.display = "none"
+ export_button.style.display = format_group.style.display = "inline-block"
+ clipboard.export_data()
+ },
- paste: function (e) {
- e.preventDefault()
- // images will come through as files
- var types = toArray(e.clipboardData.types)
- import_textarea.value = ""
- types.forEach(function(type, i){
- console.log(type)
- // this can be text/plain or text/html..
- if (type.match('text/plain') && types.length > 1) {
- return
- }
- else if (type.match('text/')) {
- import_textarea.value = e.clipboardData.getData(type)
- }
- else {
- console.error("unknown type!", item.type)
- }
- })
-
- },
-
- import_html: function () {
- var alphabet = {}
- toArray("ABCDEFGHIJKLMNOP").forEach(function(c,i){ alphabet[c] = i })
-
- var rapper = document.createElement("div")
- rapper.innerHTML = import_textarea.value
- var y = 0;
- toArray(rapper.childNodes).forEach(function(span){
- if (span.nodeName !== "SPAN") { return }
- var x = 0;
- var row = canvas.aa[y++]
- if (! row) return
- toArray(span.childNodes).forEach(function(el){
- if (x >= row.length) return;
- var line;
- var colorcode = el.nodeName;
- if (colorcode === "SPAN") {
- colorcode = "F" + css_lookup[el.style.color]
- line = el.innerText || el.textContent
- }
- else if (colorcode === "#text") {
- colorcode = "FA"
- line = el.nodeValue
- }
- else {
- line = el.innerText || el.textContent
- }
- if (colorcode[0] !== "F") { return }
- if (colorcode.length == 2) { colorcode += "BB" }
-
- for (var i = 0; i < line.length; i++, x++) {
- var lex = row[x]
- if (! lex) return
- lex.char = line[i]
- lex.fg = alphabet[ colorcode[1] ]
- lex.bg = alphabet[ colorcode[3] ]
- lex.opacity = 1
- lex.build()
- }
- })
- })
- },
-
- import_text: function () {
- var data = import_textarea.value
- lines = data.split("\n")
- var width = lines.reduce(function(a,b){ console.log(a,b); return Math.max(a, b.length) }, 0)
- var height = lines.length
- if (width > 200) {
- return alert("input too wide")
- }
- if (height > 200) {
- return alert("input too tall")
- }
- canvas.clear()
- lines.forEach(function(line, y){
- var row = canvas.aa[y]
- if (! row) return
- for (var x = 0; x < line.length; x++) {
- var lex = row[x]
- if (! lex) return
- lex.char = line[x]
- lex.fg = brush.bg
- lex.opacity = 1
- lex.build()
- }
- })
- // TODO: some notion of a "selected" region which cuts/clones the underlying region
-
-// var pasted_region = new Matrix (width, height, function(x,y){
-// var lex = new Lex (x,y)
-// lex.char = lines[y][x] || " "
-// lex.build()
-// return lex
-// })
- },
- 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
- },
+ paste: function (e) {
+ e.preventDefault()
+ // images will come through as files
+ var types = toArray(e.clipboardData.types)
+ import_textarea.value = ""
+ types.forEach(function(type, i){
+ console.log(type)
+ // this can be text/plain or text/html..
+ if (type.match('text/plain') && types.length > 1) {
+ return
+ }
+ else if (type.match('text/')) {
+ import_textarea.value = e.clipboardData.getData(type)
+ }
+ else {
+ console.error("unknown type!", item.type)
+ }
+ })
+
+ },
+
+ import_html: function () {
+ var alphabet = {}
+ toArray("ABCDEFGHIJKLMNOP").forEach(function(c,i){ alphabet[c] = i })
+
+ var rapper = document.createElement("div")
+ rapper.innerHTML = import_textarea.value
+ var y = 0;
+ toArray(rapper.childNodes).forEach(function(span){
+ if (span.nodeName !== "SPAN") { return }
+ var x = 0;
+ var row = canvas.aa[y++]
+ if (! row) return
+ toArray(span.childNodes).forEach(function(el){
+ if (x >= row.length) return;
+ var line;
+ var colorcode = el.nodeName;
+ if (colorcode === "SPAN") {
+ colorcode = "F" + css_lookup[el.style.color]
+ line = el.innerText || el.textContent
+ }
+ else if (colorcode === "#text") {
+ colorcode = "FA"
+ line = el.nodeValue
+ }
+ else {
+ line = el.innerText || el.textContent
+ }
+ if (colorcode[0] !== "F") { return }
+ if (colorcode.length == 2) { colorcode += "BB" }
+
+ for (var i = 0; i < line.length; i++, x++) {
+ var lex = row[x]
+ if (! lex) return
+ lex.char = line[i]
+ lex.fg = alphabet[ colorcode[1] ]
+ lex.bg = alphabet[ colorcode[3] ]
+ lex.opacity = 1
+ lex.build()
+ }
+ })
+ })
+ },
+
+ import_text: function () {
+ var data = import_textarea.value
+ lines = data.split("\n")
+ var width = lines.reduce(function(a,b){ console.log(a,b); return Math.max(a, b.length) }, 0)
+ var height = lines.length
+ if (width > 200) {
+ return alert("input too wide")
+ }
+ if (height > 200) {
+ return alert("input too tall")
+ }
+ canvas.clear()
+ lines.forEach(function(line, y){
+ var row = canvas.aa[y]
+ if (! row) return
+ for (var x = 0; x < line.length; x++) {
+ var lex = row[x]
+ if (! lex) return
+ lex.char = line[x]
+ lex.fg = brush.bg
+ lex.opacity = 1
+ lex.build()
+ }
+ })
+ // TODO: some notion of a "selected" region which cuts/clones the underlying region
+
+// var pasted_region = new Matrix (width, height, function(x,y){
+// var lex = new Lex (x,y)
+// lex.char = lines[y][x] || " "
+// lex.build()
+// return lex
+// })
+ },
+ 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
+ },
- }
-
- exports.bind()
-
- return exports
-
+ }
+
+ exports.bind()
+
+ return exports
+
})()