summaryrefslogtreecommitdiff
path: root/js/clipboard.js
blob: 53547715a16bad73148b7811ae866f297bf0021d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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_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.style.display = "none"
			import_textarea.value = ""
		},
		export_mode: function () {
			focus()
			clipboard.importing = false
			import_button.style.display = "none"
			export_button.style.display = format_group.style.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
			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.build()
				}
			})
// 			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
	
})()