Skip to content

Commit

Permalink
Add CSV/TXT export according to #289
Browse files Browse the repository at this point in the history
  • Loading branch information
mhorsche authored and qu1ck committed May 17, 2022
1 parent f26a828 commit eeb727c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
9 changes: 8 additions & 1 deletion InteractiveHtmlBom/web/ibom.html
Expand Up @@ -235,6 +235,13 @@
<button class="savebtn" onclick="loadSettings()">Import</button>
</div>
</div>
<div class="menu-label">
<span style="margin-left: 5px;">Save bom table as</span>
<div class="flexbox">
<button class="savebtn" onclick="saveBomTable('csv')">csv</button>
<button class="savebtn" onclick="saveBomTable('txt')">txt</button>
</div>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -270,7 +277,7 @@
oninput="updateFilter(this.value)">
<div class="button-container hideonprint" style="float: left; margin: 0;">
<button id="copy" title="Copy bom table to clipboard"
onclick="copyToClipboard()"></button>
onclick="saveBomTable()"></button>
</div>
</div>
<div id="dbg"></div>
Expand Down
65 changes: 46 additions & 19 deletions InteractiveHtmlBom/web/util.js
Expand Up @@ -66,53 +66,80 @@ function focusInputField(input) {
input.select();
}

function copyToClipboard() {
function saveBomTable(e) {
var text = '';
for (var node of bomhead.childNodes[0].childNodes) {
if (node.firstChild) {
text = text + node.firstChild.nodeValue;
text += (e == 'csv' ? `"${node.firstChild.nodeValue}"` : node.firstChild.nodeValue);
}
if (node != bomhead.childNodes[0].lastChild) {
text += '\t';
text += (e == 'csv' ? ',' : '\t');
}
}
text += '\n';
for (var row of bombody.childNodes) {
for (var cell of row.childNodes) {
let val = '';
for (var node of cell.childNodes) {
if (node.nodeName == "INPUT") {
if (node.checked) {
text = text + '✓';
val += '✓';
}
} else if (node.nodeName == "MARK") {
text = text + node.firstChild.nodeValue;
val += node.firstChild.nodeValue;
} else {
text = text + node.nodeValue;
val += node.nodeValue;
}
}
if (e == 'csv') {
val = val.replace(/\"/g, '\"\"'); // pair of double-quote characters
if (isNumeric(val)) {
val = +val; // use number
} else {
val = `"${val}"`; // enclosed within double-quote
}
}
text += val;
if (cell != row.lastChild) {
text += '\t';
text += (e == 'csv' ? ',' : '\t');
}
}
text += '\n';
}
var textArea = document.createElement("textarea");
textArea.classList.add('clipboard-temp');
textArea.value = text;

document.body.appendChild(textArea);
textArea.focus();
textArea.select();
if (e) {
// To file: csv or txt
var blob = new Blob([text], {
type: `text/${e}`
});
saveFile(`${pcbdata.metadata.title}.${e}`, blob);
} else {
// To clipboard
var textArea = document.createElement("textarea");
textArea.classList.add('clipboard-temp');
textArea.value = text;

try {
if (document.execCommand('copy')) {
console.log('Bom copied to clipboard.');
document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
if (document.execCommand('copy')) {
console.log('Bom copied to clipboard.');
}
} catch (err) {
console.log('Can not copy to clipboard.');
}
} catch (err) {
console.log('Can not copy to clipboard.');

document.body.removeChild(textArea);
}
}

document.body.removeChild(textArea);
function isNumeric(str) {
/* https://stackoverflow.com/a/175787 */
if (typeof str != "string") return false // we only process strings!
return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
!isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
}

function removeGutterNode(node) {
Expand Down

0 comments on commit eeb727c

Please sign in to comment.