Skip to content

Commit

Permalink
textile format, copy module cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gebrkn committed Oct 1, 2017
1 parent dff64ff commit a5a693d
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 93 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ Chrome extension to select and copy table cells.
* Copy as CSV or tab-delimited text (for Excel).
* Copy as HTML (for your website).

###New in version 0.5
### New in version 0.5

* Configurable hotkeys for cell, column, row and table selection.
* Capture mode: select with simple click and drag.
* Full support of framed documents.
* Popup information about selected cells.
* Swap (transpose) copy.

###New in version 0.4
### New in version 0.4

* New popup menu.
* Table search function.
* Configurable hotkey (Ctrl/Alt).

###New in version 0.3
### New in version 0.3

* Styled HTML export.

###New in version 0.2
### New in version 0.2

* CSV export.
* Better HTML export.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "copytables",
"version": "0.5.5",
"version": "0.5.6",
"description": "Chrome extension to select and copy table cells.",
"scripts": {
"chrome": "'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' --enable-logging=stderr --v=1 2>&1 | grep CONSOLE",
Expand Down
60 changes: 60 additions & 0 deletions src/background/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var M = module.exports = {};

var
dom = require('../lib/dom'),
util = require('../lib/util')
;


M.copyRich = function (text) {
console.log(util.timeStart('copyRich'));

var t = document.createElement('div');
document.body.appendChild(t);

t.contentEditable = true;
t.innerHTML = text;

dom.select(t);
document.execCommand('copy');
document.body.removeChild(t);

console.log(util.timeEnd('copyRich'));
};

M.copyText = function (text) {
console.log(util.timeStart('copyText'));

var t = document.createElement('textarea');
document.body.appendChild(t);

t.value = text;
t.focus();
t.select();

document.execCommand('copy');
document.body.removeChild(t);

console.log(util.timeEnd('copyText'));
};

M.content = function () {
var cc = '',
pasted = false;

var pasteHandler = function (evt) {
if (pasted)
return;
pasted = true;
console.log('paste handler toggled');
cc = evt.clipboardData.getData('text/html');
evt.stopPropagation();
evt.preventDefault();
};

document.addEventListener('paste', pasteHandler, true);
console.log('exec paste');
document.execCommand('paste');
document.removeEventListener('paste', pasteHandler);
return cc;
};
139 changes: 79 additions & 60 deletions src/background/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,12 @@ var M = module.exports = {};
var paste = require('./paste'),
dom = require('../lib/dom'),
matrix = require('../lib/matrix'),
util = require('../lib/util')
util = require('../lib/util'),
clipboard = require('./clipboard')
;

M.richCopy = function (text) {
console.log(util.timeStart('richCopy'));

var t = document.createElement('div');
document.body.appendChild(t);

t.contentEditable = true;
t.innerHTML = text;

dom.select(t);
document.execCommand('copy');
document.body.removeChild(t);

console.log(util.timeEnd('richCopy'));
};

M.textCopy = function (text) {
console.log(util.timeStart('textCopy'));

var t = document.createElement('textarea');
document.body.appendChild(t);

t.value = text;
t.focus();
t.select();

document.execCommand('copy');
document.body.removeChild(t);

console.log(util.timeEnd('textCopy'));
};

function trimTextMatrix(mat) {
mat = matrix.map(mat, function(row, cell) {
mat = matrix.map(mat, function (row, cell) {
return util.strip(util.nobr(cell));
});
return matrix.trim(mat, Boolean);
Expand All @@ -61,70 +30,120 @@ function asCSV(mat) {
}).join('\n');
}

function asTextile(mat) {
return trimTextMatrix(mat).map(function (row) {
return '|' + row.map(function (cell) {
return ' ' + cell + ' ';
}).join('|')
}).join('|\n') + '|';
function asTextile(mat, withHTML) {
var rows = mat.map(function (row) {

var cells = row
.filter(function (node) {
return node.td
})
.map(function (node) {

var t = '', s = '';

if (withHTML)
t = dom.htmlContent(node.td);
else
t = dom.textContent(node.td);

if (node.colSpan)
s += '\\' + (node.colSpan + 1);
if (node.rowSpan)
s += '/' + (node.rowSpan + 1);
if (s)
s += '.';

return '|' + s + ' ' + t.replace('|', '|');

});

return cells.join(' ') + ' |';
});

return rows.join('\n');
}


M.formats = {};

M.formats.richHTMLCSS = {
opts: { method: 'clipboard', withSelection: true, keepStyles: true, keepHidden: false },
exec: function(t) { M.richCopy(t.html()) }
opts: {method: 'clipboard', withSelection: true, keepStyles: true, keepHidden: false},
exec: function (t) {
clipboard.copyRich(t.html())
}
};

M.formats.richHTML = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.richCopy(t.html()) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyRich(t.html())
}
};

M.formats.textTabs = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.textCopy(asTabs(t.textMatrix())) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asTabs(t.textMatrix()))
}
};

M.formats.textTabsSwap = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.textCopy(asTabs(matrix.transpose(t.textMatrix()))) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asTabs(matrix.transpose(t.textMatrix())))
}
};

M.formats.textCSV = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.textCopy(asCSV(t.textMatrix())) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asCSV(t.textMatrix()))
}
};

M.formats.textCSVSwap = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.textCopy(asCSV(matrix.transpose(t.textMatrix()))) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asCSV(matrix.transpose(t.textMatrix())))
}
};

M.formats.textHTML = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: true },
exec: function(t) { M.textCopy(util.reduceWhitespace(t.html())) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: true},
exec: function (t) {
clipboard.copyText(util.reduceWhitespace(t.html()))
}
};

M.formats.textHTMLCSS = {
opts: { method: 'clipboard', withSelection: true, keepStyles: true, keepHidden: true },
exec: function(t) { M.textCopy(util.reduceWhitespace(t.html())) }
opts: {method: 'clipboard', withSelection: true, keepStyles: true, keepHidden: true},
exec: function (t) {
clipboard.copyText(util.reduceWhitespace(t.html()))
}
};

M.formats.textTextileHTML = {
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asTextile(t.nodeMatrix(), true))
}
};

M.formats.textTextile = {
opts: { method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false },
exec: function(t) { M.textCopy(asTextile(t.textMatrix())) }
opts: {method: 'clipboard', withSelection: true, keepStyles: false, keepHidden: false},
exec: function (t) {
clipboard.copyText(asTextile(t.nodeMatrix(), false))
}
};


//

M.options = function(format) {
M.options = function (format) {
return M.formats[format].opts;
};

M.exec = function(format, data) {
M.exec = function (format, data) {
var ok = false,
t = new paste.table(),
fmt = M.formats[format];
Expand Down
Loading

0 comments on commit a5a693d

Please sign in to comment.