2018-10-24 15:14:46 +00:00
|
|
|
var clipboard = (function () {
|
|
|
|
|
|
|
|
var exports = {
|
2018-11-24 12:16:11 +00:00
|
|
|
format: "mirc",
|
2019-08-25 16:39:49 +00:00
|
|
|
exporting: false,
|
2018-10-24 15:14:46 +00:00
|
|
|
visible: false,
|
|
|
|
canvas: document.createElement("canvas"),
|
|
|
|
canvas_r: document.createElement("canvas"),
|
|
|
|
|
|
|
|
bind: function () {
|
2019-08-25 16:39:49 +00:00
|
|
|
export_textarea.addEventListener("focus", exports.export_focus)
|
|
|
|
export_textarea.addEventListener("blur", exports.blur)
|
|
|
|
import_button.addEventListener("click", exports.import_click)
|
|
|
|
import_textarea.addEventListener("focus", exports.import_focus)
|
|
|
|
import_textarea.addEventListener("paste", exports.paste)
|
2018-10-24 15:14:46 +00:00
|
|
|
},
|
|
|
|
setFormat: function (name) {
|
|
|
|
return function () {
|
|
|
|
clipboard.format = name
|
2019-08-25 16:39:49 +00:00
|
|
|
if (! clipboard.exporting) { clipboard.export_data() }
|
2018-10-24 15:14:46 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-25 16:39:49 +00:00
|
|
|
import_hide: function () { import_wrapper.style.display = "none"; clipboard.visible = false },
|
|
|
|
import_show: function () { import_wrapper.style.display = "block"; clipboard.visible = true; },
|
|
|
|
export_hide: function () { export_wrapper.style.display = "none"; clipboard.visible = false },
|
|
|
|
export_show: function () { export_wrapper.style.display = "block"; clipboard.visible = true; changed = false },
|
|
|
|
export_focus: function () {
|
|
|
|
if (! clipboard.exporting) {
|
|
|
|
export_textarea.focus()
|
|
|
|
export_textarea.select()
|
2018-10-24 15:14:46 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-25 16:39:49 +00:00
|
|
|
import_focus: function () {
|
|
|
|
import_textarea.focus()
|
|
|
|
import_textarea.select()
|
2018-10-24 15:14:46 +00:00
|
|
|
},
|
|
|
|
|
2019-08-25 16:39:49 +00:00
|
|
|
blur: function () {
|
2018-10-24 15:14:46 +00:00
|
|
|
},
|
|
|
|
|
2018-10-25 00:18:04 +00:00
|
|
|
export_mode: function () {
|
2019-08-25 16:39:49 +00:00
|
|
|
exports.export_focus()
|
|
|
|
clipboard.exporting = true
|
|
|
|
export_cutoff_warning_el.style.display = "none"
|
|
|
|
export_format_el.style.display = "inline"
|
2018-10-25 00:18:04 +00:00
|
|
|
clipboard.export_data()
|
|
|
|
},
|
|
|
|
|
2019-08-25 16:39:49 +00:00
|
|
|
import_mode: function () {
|
|
|
|
import_button.style.display = "inline"
|
|
|
|
import_format_el.style.display = "inline"
|
|
|
|
import_textarea.value = ""
|
|
|
|
exports.import_focus()
|
|
|
|
},
|
|
|
|
|
2018-10-24 15:14:46 +00:00
|
|
|
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')) {
|
|
|
|
import_textarea.value = e.clipboardData.getData(type)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error("unknown type!", item.type)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-11-24 07:49:02 +00:00
|
|
|
|
2019-08-25 16:39:49 +00:00
|
|
|
import_click: function (data, no_undo) {
|
|
|
|
switch (controls.load_format.value) {
|
|
|
|
case 'ansi':
|
|
|
|
exports.import_ansi(data, no_undo)
|
|
|
|
break
|
|
|
|
case 'mirc':
|
|
|
|
exports.import_mirc(data, no_undo)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
import_ansi: function (data, no_undo) {
|
|
|
|
if (data && data.preventDefault) {
|
|
|
|
data = import_textarea.value
|
|
|
|
} else {
|
|
|
|
data = data || import_textarea.value
|
|
|
|
}
|
|
|
|
|
|
|
|
var to_json = function(string, opts){
|
|
|
|
var lines_in = string.split(/\r?\n/)
|
|
|
|
var lines_out = []
|
2019-08-28 09:31:42 +00:00
|
|
|
var bg = 1, bg_ansi = 30, bold = false, fg = 15, fg_ansi = 37
|
2019-08-25 16:39:49 +00:00
|
|
|
var w = 0, h = 0
|
|
|
|
for (var y = 0; y < lines_in.length; y++) {
|
|
|
|
var cells = [], line = lines_in[y]
|
|
|
|
if (line.length === 0) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
for (var x = 0; x < line.length; x++) {
|
2019-08-27 06:45:39 +00:00
|
|
|
var m = line.substring(x).match(/^\x1b\[((?:\d{1,3};?)+)m/)
|
2019-08-25 16:39:49 +00:00
|
|
|
if (m !== null) {
|
2019-08-27 06:45:39 +00:00
|
|
|
m[1].split(";").forEach(function(c){
|
|
|
|
c = parseInt(c);
|
|
|
|
if (c == 0) {
|
2019-08-28 09:31:42 +00:00
|
|
|
bg = 1; bg_ansi = 30; bold = false; fg = 15; fg_ansi = 37;
|
2019-08-27 06:45:39 +00:00
|
|
|
} else if (c == 1) {
|
2019-08-28 09:31:42 +00:00
|
|
|
bold = true; fg = ansi_fg_bold_import[fg_ansi];
|
2019-08-27 06:45:39 +00:00
|
|
|
} else if (c == 2) {
|
2019-08-28 09:31:42 +00:00
|
|
|
bold = false; fg = ansi_fg_import[fg_ansi];
|
|
|
|
} else if (ansi_bg_import[c] !== undefined) {
|
|
|
|
bg = ansi_bg_import[c]; bg_ansi = c;
|
2019-08-27 06:45:39 +00:00
|
|
|
} else if (bold && (ansi_fg_bold_import[c] !== undefined)) {
|
2019-08-28 09:31:42 +00:00
|
|
|
fg = ansi_fg_bold_import[c]; fg_ansi = c;
|
2019-08-27 06:45:39 +00:00
|
|
|
} else if (!bold && (ansi_fg_import[c] !== undefined)) {
|
2019-08-28 09:31:42 +00:00
|
|
|
fg = ansi_fg_import[c]; fg_ansi = c;
|
2019-08-25 16:39:49 +00:00
|
|
|
}
|
2019-08-27 06:45:39 +00:00
|
|
|
});
|
|
|
|
x += (m[0].length - 1);
|
|
|
|
} else {
|
2019-08-28 09:31:42 +00:00
|
|
|
m = line.substring(x).match(/^\x1b\[(\d+)C/)
|
|
|
|
if (m !== null) {
|
|
|
|
for (var n = 0, nmax = parseInt(m[1]); n < nmax; n++) {
|
|
|
|
cells.push({bg: bg, fg: fg, value: " "})
|
|
|
|
}
|
|
|
|
x += (m[0].length - 1);
|
|
|
|
} else {
|
|
|
|
cells.push({bg: bg, fg: fg, value: line[x]})
|
|
|
|
}
|
2019-08-25 16:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cells.length > 0) {
|
|
|
|
if (w < cells.length) {
|
|
|
|
w = cells.length
|
2019-08-28 09:31:42 +00:00
|
|
|
} else if (w > cells.length) {
|
|
|
|
for (var n = cells.length, nmax = w; n < nmax; n++) {
|
|
|
|
cells.push({bg: bg, fg: fg, value: " "})
|
|
|
|
}
|
2019-08-25 16:39:49 +00:00
|
|
|
}
|
|
|
|
lines_out.push(cells); h++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {h: h, lines: lines_out, w: w}
|
|
|
|
}
|
|
|
|
var json = to_json(data, {fg:0, bg:1})
|
|
|
|
|
|
|
|
if (!no_undo) undo.new()
|
|
|
|
if (!no_undo) undo.save_rect(0,0, canvas.w, canvas.h)
|
|
|
|
if (json.w !== canvas.w || json.h !== canvas.h){
|
|
|
|
if (!no_undo) 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 = char.value
|
|
|
|
lex.fg = char.fg
|
|
|
|
lex.bg = char.bg
|
|
|
|
lex.opacity = 1
|
|
|
|
lex.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current_filetool && current_filetool.blur()
|
|
|
|
},
|
|
|
|
|
|
|
|
import_mirc: function (data, no_undo) {
|
2018-11-24 07:52:28 +00:00
|
|
|
if (data && data.preventDefault) {
|
|
|
|
data = import_textarea.value
|
|
|
|
} else {
|
|
|
|
data = data || import_textarea.value
|
|
|
|
}
|
2018-11-24 07:49:02 +00:00
|
|
|
|
2018-10-24 22:21:33 +00:00
|
|
|
var to_json = function(string, opts){
|
|
|
|
var lines_in = string.split(/\r?\n/)
|
|
|
|
var lines_out = []
|
|
|
|
var w = 0, h = 0
|
|
|
|
for (var y = 0; y < lines_in.length; y++) {
|
|
|
|
var bg = 1, fg = 15
|
|
|
|
var cells = [], line = lines_in[y]
|
|
|
|
if (line.length === 0) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
for (var x = 0; x < line.length; x++) {
|
|
|
|
switch (line[x]) {
|
|
|
|
case "\x02": // ^B (unimplemented)
|
|
|
|
break
|
|
|
|
case "\x03": // ^C
|
|
|
|
var parseColour = function(line, x) {
|
|
|
|
if (/1[0-5]/.test(line.substr(x, 2))) {
|
|
|
|
colour = parseInt(line.substr(x, 2))
|
|
|
|
return [colour, x + 2]
|
|
|
|
} else if (/0[0-9]/.test(line.substr(x, 2))) {
|
|
|
|
colour = parseInt(line.substr(x, 2))
|
|
|
|
return [colour, x + 2]
|
|
|
|
} else if (/[0-9]/.test(line.substr(x, 1))) {
|
|
|
|
colour = parseInt(line.substr(x, 1))
|
|
|
|
return [colour, x + 1]
|
|
|
|
} else {
|
|
|
|
return [undefined, x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var bg_ = undefined, fg_ = undefined, x_ = x + 1;
|
|
|
|
[fg_, x_] = parseColour(line, x_)
|
|
|
|
if (line[x_] === ",") {
|
|
|
|
[bg_, x_] = parseColour(line, x_ + 1)
|
|
|
|
}
|
|
|
|
if ((bg_ == undefined) && (fg_ == undefined)) {
|
|
|
|
[bg, fg] = [1, 15]
|
|
|
|
} else {
|
|
|
|
bg = (bg_ != undefined) ? bg_ : bg;
|
|
|
|
fg = (fg_ != undefined) ? fg_ : fg;
|
|
|
|
};
|
|
|
|
if (x_ != x) {x = x_ - 1}; break;
|
|
|
|
case "\x06": // ^F (unimplemented)
|
|
|
|
break
|
|
|
|
case "\x0f": // ^O
|
|
|
|
[bg, fg] = [1, 15]; break;
|
|
|
|
case "\x16": // ^V
|
|
|
|
[bg, fg] = [fg, bg]; break;
|
|
|
|
case "\x1f": // ^_ (unimplemented)
|
|
|
|
break
|
|
|
|
default:
|
2018-10-24 22:33:42 +00:00
|
|
|
cells.push({bg: bg, fg: fg, value: line[x]})
|
2018-10-24 22:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cells.length > 0) {
|
|
|
|
if (w < cells.length) {
|
|
|
|
w = cells.length
|
|
|
|
}
|
|
|
|
lines_out.push(cells); h++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {h: h, lines: lines_out, w: w}
|
|
|
|
}
|
|
|
|
var json = to_json(data, {fg:0, bg:1})
|
2018-10-24 15:14:46 +00:00
|
|
|
|
|
|
|
if (!no_undo) undo.new()
|
|
|
|
if (!no_undo) undo.save_rect(0,0, canvas.w, canvas.h)
|
|
|
|
if (json.w !== canvas.w || json.h !== canvas.h){
|
|
|
|
if (!no_undo) 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]
|
2018-10-24 22:21:33 +00:00
|
|
|
lex.char = char.value
|
2018-10-24 15:14:46 +00:00
|
|
|
lex.fg = char.fg
|
|
|
|
lex.bg = char.bg
|
|
|
|
lex.opacity = 1
|
|
|
|
lex.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 07:49:02 +00:00
|
|
|
current_filetool && current_filetool.blur()
|
2018-10-24 15:14:46 +00:00
|
|
|
},
|
2018-11-24 07:49:02 +00:00
|
|
|
|
2018-10-24 15:14:46 +00:00
|
|
|
export_data: function () {
|
|
|
|
var output
|
|
|
|
// switch (clipboard.format) {
|
|
|
|
switch (controls.save_format.value) {
|
|
|
|
case 'ascii':
|
|
|
|
output = canvas.ascii()
|
|
|
|
break
|
2019-08-25 16:39:49 +00:00
|
|
|
case 'ansi':
|
|
|
|
output = canvas.ansi()
|
|
|
|
break
|
2018-10-24 15:14:46 +00:00
|
|
|
case 'mirc':
|
2018-11-21 14:50:07 +00:00
|
|
|
output = canvas.mirc({cutoff: 425})
|
2018-10-24 15:14:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if (output.cutoff){
|
2019-08-25 16:39:49 +00:00
|
|
|
export_cutoff_warning_el.style.display = 'block'
|
2018-10-24 15:14:46 +00:00
|
|
|
} else {
|
2019-08-25 16:39:49 +00:00
|
|
|
export_cutoff_warning_el.style.display = 'none'
|
2018-10-24 15:14:46 +00:00
|
|
|
}
|
2019-08-25 16:39:49 +00:00
|
|
|
export_textarea.value = output
|
|
|
|
clipboard.export_focus()
|
2018-10-24 15:14:46 +00:00
|
|
|
return output
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
2018-11-24 07:49:02 +00:00
|
|
|
|
2018-10-24 15:14:46 +00:00
|
|
|
return exports
|
2018-11-24 07:49:02 +00:00
|
|
|
|
2018-10-24 15:14:46 +00:00
|
|
|
})()
|
|
|
|
|
|
|
|
|