summaryrefslogtreecommitdiff
path: root/js/clipboard.js
blob: 577c6955c072ee097897b37c9c4cd138a0c7da48 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var clipboard = (function () {

  var exports = {
    format: "irssi",
    importing: false,
    visible: false,
    canvas: document.createElement("canvas"),
    canvas_r: document.createElement("canvas"),

    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_colorcode)
      import_html.addEventListener("click", exports.import_html)
      export_button.addEventListener("click", exports.export_data)
      save_button.addEventListener("click", exports.save_png)
      upload_button.addEventListener("click", exports.upload_png)
      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; changed = false },
    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
      gallery_rapper.style.display = 'none'
      format_el.style.display = 'none'
      cutoff_warning_el.style.display = 'none'
      import_buttons.style.display = "inline"
      import_textarea.value = ""
    },
    export_mode: function () {
      focus()
      clipboard.importing = false
      import_buttons.style.display = "none"
      format_el.style.display = 'inline'
      cutoff_warning_el.style.display = 'none'
      gallery_rapper.style.display = 'inline'
      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;
      undo.new()
      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]
            undo.save_lex(x, y, lex)
            if (! lex) return
            lex.char = line[i]
            lex.fg = alphabet[ colorcode[1] ]
            lex.bg = alphabet[ colorcode[3] ]
            lex.opacity = 1
            lex.build()
          }
        })
      })
    },
   
    import_colorcode: function () {
      var data = import_textarea.value
      
      var irssi_style_regex = /^\s*\/exec -out printf ("%b" )?"/;

      // turn irssi style into mirc style
      if (data.match(irssi_style_regex)){
        data = data.replace(/\\x03/gm, '\x03')
                   .replace(/(\\x..)+/gm, unicode.unescapeFromEscapedBytes)
                   .replace(/\\x5C/g, '\\')
                   .replace(/\\n/gm, '\n')
                   .replace(/\\`/gm, '`')
                   .replace(/\\"/gm, '"')
                   .replace(/\\\$/gm, '$')
                   .replace(irssi_style_regex, '')
                   .replace(/"\s*$/, '')
      }

      // not a colorcode
      if (!data.match(/\x03/))
        return exports.import_text();

      var json = colorcode.to_json(data, {fg:0, bg:1})

      undo.new()
      undo.save_rect(0,0, canvas.w, canvas.h)
      if (json.w !== canvas.w || json.h !== canvas.h){
        undo.save_size(canvas.w, canvas.h)
        canvas.resize(json.w, json.h, true)
      }
      canvas.clear()

      for (var y = 0, line; line = json.lines[y]; y++){
        var row = canvas.aa[y]
        for (var x = 0, char; char = line[x]; x++){
          var lex = row[x]
          lex.char = String.fromCharCode(char.value)
          lex.fg = char.fg
          lex.bg = char.bg
          lex.opacity = 1
          lex.build()
        }
      }
     
    },
    
    import_text: function () {
      var data = import_textarea.value
      var 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 > canvas.max) {
        return alert("input too wide")
      }
      if (height > canvas.max) {
        return alert("input too tall")
      }
      undo.new()
      undo.save_rect(0,0, canvas.w, canvas.h)
      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) {
      switch (controls.save_format.value) {
        case 'ascii':
          output = canvas.ascii()
          break
        case 'mirc':
          output = canvas.mirc({cutoff: 400})
          break
        case 'irssi':
          output = canvas.irssi({cutoff: 400})
          break
      }
      if (output.cutoff){
        cutoff_warning_el.style.display = 'block'
      }
      import_textarea.value = output
      clipboard.focus()
      return output
    },
    
    rotate_canvas: function(){
      var cr = clipboard.canvas_r, c = clipboard.canvas
      cr.width = c.height
      cr.height = c.width
      var ctx = cr.getContext('2d')
      ctx.resetTransform()
      ctx.translate(0, cr.height)
      ctx.rotate(-Math.PI / 2)
      ctx.drawImage(c, 0, 0)
      return cr
    },

    export_canvas: function (done_fn) {
      var opts = {
        palette: 'mirc',
        font: 'fixedsys_8x15',
        fg: 0,
        bg: 1,
        canvas: clipboard.canvas
      }
      opts.done = function(){
        var c = canvas.rotated ? clipboard.rotate_canvas() : clipboard.canvas
        if (done_fn) done_fn(c)
      }

      var start = Date.now();
      colorcode.to_canvas(canvas.mirc(), opts)
      var total = Date.now() - start;
      console.log("took " + total)
    },
    
    filename: function () {
      return [ +new Date, "ascii", user.username ].join("-")
    },

    save_png: function () {
      var save_fn = function(canvas_out){
        var filename = clipboard.filename() + ".png"
        var blob = PNG.canvas_to_blob_with_colorcode(canvas_out, canvas.mirc())
        saveAs(blob, filename);
      }
      clipboard.export_canvas(save_fn)
    },
    
    upload_png: function () {
      var upload_fn = function(canvas_out){
        var blob = PNG.canvas_to_blob_with_colorcode(canvas_out, canvas.mirc())
        var filename = clipboard.filename()
        var tag = 'ascii'
        upload(blob, filename, tag)
      }
      clipboard.export_canvas(upload_fn)
    },

  }
  
  exports.bind()
  
  return exports
  
})()