Skip to content

Commit

Permalink
update generic-webtable to v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
krocheck authored and github-actions[bot] committed Oct 6, 2024
1 parent 92e74a0 commit 8de3017
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 30 deletions.
6 changes: 3 additions & 3 deletions generic-webtable/.build-info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MODULE_NAME=generic-webtable
GIT_REF=v1.0.1
RUN_URL=https://github.com/bitfocus/companion-bundled-modules/actions/runs/11035224769
UPDATE_DATE=2024-09-25
GIT_REF=v1.0.2
RUN_URL=https://github.com/bitfocus/companion-bundled-modules/actions/runs/11202447883
UPDATE_DATE=2024-10-06
2 changes: 1 addition & 1 deletion generic-webtable/companion/manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":"generic-webtable","name":"Generic Web-Table","shortname":"table","description":"Generic Web-Table","version":"1.0.1","license":"MIT","repository":"https://github.com/bitfocus/companion-module-generic-webtable","bugs":"https://github.com/bitfocus/companion-module-generic-webtable/issues","maintainers":[{"name":"Jan Görgen","email":"jg@goergen-medien.de"}],"legacyIds":[],"runtime":{"type":"node18","api":"nodejs-ipc","apiVersion":"1.10.0","entrypoint":"../main.js"},"manufacturer":"Generic","products":["Web-Table"],"keywords":["Browser","Web","Table"]}
{"id":"generic-webtable","name":"Generic Web-Table","shortname":"table","description":"Generic Web-Table","version":"1.0.2","license":"MIT","repository":"https://github.com/bitfocus/companion-module-generic-webtable","bugs":"https://github.com/bitfocus/companion-module-generic-webtable/issues","maintainers":[{"name":"Jan Görgen","email":"jg@goergen-medien.de"}],"legacyIds":[],"runtime":{"type":"node18","api":"nodejs-ipc","apiVersion":"1.10.0","entrypoint":"../main.js"},"manufacturer":"Generic","products":["Web-Table"],"keywords":["Browser","Web","Table"]}
2 changes: 1 addition & 1 deletion generic-webtable/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"Generic Web-Table","version":"1.0.1","license":"MIT","type":"commonjs","dependencies":{}}
{"name":"Generic Web-Table","version":"1.0.2","license":"MIT","type":"commonjs","dependencies":{}}
67 changes: 66 additions & 1 deletion generic-webtable/webserver-scripts-fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ function readFile(types='*.*', timeout=60) {
if (event.target.files.length === 0) res(undefined)
const type = event.target.files[0].name.split('.').slice(-1)[0]
if (!types.includes(type.toLowerCase()) && !types.includes(type)) res(undefined)
res(await fetch(window.URL.createObjectURL(event.target.files[0])))
const fileResponse = await fetch(window.URL.createObjectURL(event.target.files[0]))
fileResponse.fileType = type
res(fileResponse)
})
input.click()
setTimeout(() => {
Expand Down Expand Up @@ -80,6 +82,69 @@ function parseCSV(file, timeout=60) {



function parseXLSX(file, timeout=60) {

const parse = (xlsxString => {
if (typeof xlsxString !== 'string') return undefined


})

console.log(file)

// return new Promise(async (res) => {
// setTimeout(() => res(undefined), timeout*1000)
// file.text().then((text) => res(parse(text))).then(() => res(undefined))
// })
}
// Function to handle file input
// document.getElementById('input').addEventListener('change', function(e) {
// const file = e.target.files[0];
// const reader = new FileReader();

// // When file is read
// reader.onload = function(event) {
// const arrayBuffer = event.target.result;
// processExcelFile(arrayBuffer);
// };

// reader.readAsArrayBuffer(file);
// });

// Function to process the Excel file
// async function processExcelFile(arrayBuffer) {
// // Unzip the xlsx file
// const zip = await JSZip.loadAsync(arrayBuffer);

// // Get the 'sheet1.xml' from the unzipped file
// const sheetFile = zip.file("xl/worksheets/sheet1.xml");
// if (sheetFile) {
// const sheetXml = await sheetFile.async("string");

// // Parse the XML content
// const parser = new DOMParser();
// const xmlDoc = parser.parseFromString(sheetXml, "text/xml");

// // Extract cell values from the XML
// const rows = xmlDoc.getElementsByTagName("row");

// for (let i = 0; i < rows.length; i++) {
// const cells = rows[i].getElementsByTagName("c");
// let rowData = [];
// for (let j = 0; j < cells.length; j++) {
// const valueElement = cells[j].getElementsByTagName("v")[0];
// const value = valueElement ? valueElement.textContent : "";
// rowData.push(value);
// }
// console.log(rowData.join(", ")); // Log the row as comma-separated values
// }
// }
// }





// serialize JSON (nested arrays) to CSV
function serializeCSV(csvData, timeout=60) {

Expand Down
13 changes: 13 additions & 0 deletions generic-webtable/webserver-scripts-jsZip.js

Large diffs are not rendered by default.

111 changes: 87 additions & 24 deletions generic-webtable/webserver-templates-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
</div>
<div class="footer">
<div onclick="submitData(event)" ontouchstart="activate(event, '#ffffff', '#008a77')" ontouchend="deactivate(event, '#292929', '#e6e6e6')">Submit Data</div>
<u onclick="importCSV()">Import CSV</u>
<u onclick="importFile()">Import Table</u>
<u onclick="requestCurrentData()">Request Current Data</u>
<u onclick="exportCSV()">Export CSV</u>
</div>
Expand Down Expand Up @@ -359,11 +359,10 @@
const baseUrl = window.location.pathname.split('/').slice(0, 3).join('/')
document.title = `Companion Table - "${baseUrl.split('/').slice(-1)[0]}"`

await addScript(baseUrl + '/scripts/crypto.js')
await addScript(baseUrl + '/scripts/fileManager.js')
await addScript(baseUrl + '/scripts/jsZip.js')
await addScript(baseUrl + '/scripts/popupManager.js')
await addScript(baseUrl + '/scripts/crypto.js')

console.log(document.body.children)

const MENU = {
columnMenu: document.getElementById('contextMenuColumn'),
Expand Down Expand Up @@ -391,6 +390,7 @@

window.submitData = submitData
window.importCSV = importCSV
window.importFile = importFile
window.requestCurrentData = requestCurrentData
window.exportCSV = exportCSV

Expand Down Expand Up @@ -507,7 +507,7 @@
label = String.fromCharCode((index % 26) + 65) + label
index = Math.floor(index / 26) - 1;
}
headers[i].innerHTML = label
headers[i].textContent = label
}
}

Expand Down Expand Up @@ -564,7 +564,7 @@
MENU.columnClipboardSingleUse = singleUse
MENU.columnClipboard = []

for (const row of TABLE.htmlBody.children) MENU.columnClipboard.push(row.children[index].innerHTML)
for (const row of TABLE.htmlBody.children) MENU.columnClipboard.push(row.children[index].textContent)
}


Expand All @@ -581,7 +581,7 @@

for (let i=0; i<TABLE.htmlBody.children.length; i++) {
if (i === MENU.columnClipboard.length) break
TABLE.htmlBody.children[i].children[index].innerHTML = MENU.columnClipboard[i]
TABLE.htmlBody.children[i].children[index].textContent = MENU.columnClipboard[i]
}
if (MENU.columnClipboardSingleUse === true) MENU.columnClipboard = undefined

Expand All @@ -593,7 +593,7 @@
const index = [ ...TABLE.htmlHead.children[0].children ].indexOf(MENU.targetColumn)
if (index === -1) return

for (const row of TABLE.htmlBody.children) row.children[index].innerHTML = ''
for (const row of TABLE.htmlBody.children) row.children[index].textContent = ''

saveLocaleData('table', await getTableData())
}
Expand Down Expand Up @@ -655,7 +655,7 @@
MENU.rowMenu.children[3].style.display = (headers.length <= 1) ? 'none' : 'block'
MENU.rowMenu.children[6].style.display = (headers.length <= 1) ? 'none' : 'block'

for (let i=0; i<headers.length; i++) headers[i].children[0].innerHTML = i+1
for (let i=0; i<headers.length; i++) headers[i].children[0].textContent = i+1
}


Expand All @@ -669,7 +669,7 @@
else if (selection.type === 'Range' && selection.direction === 'forward') {
text = selection.focusNode.nodeValue.slice(selection.anchorOffset, selection.focusOffset)
}
else text = MENU.targetCell.innerHTML
else text = MENU.targetCell.textContent

MENU.clipboard = text
if (navigator.clipboard !== undefined) navigator.clipboard.writeText(text)
Expand All @@ -680,21 +680,21 @@
const paste = (text) => {
const selection = window.getSelection()
if (selection.rangeCount <= 0) {
MENU.targetCell.innerHTML = text
MENU.targetCell.textContent = text
return
}
const range = selection.getRangeAt(0)
const before = MENU.targetCell.innerHTML.slice(0, range.startOffset)
const after = MENU.targetCell.innerHTML.slice(range.endOffset)
if (text !== '') MENU.targetCell.innerHTML = before + text + after
const before = MENU.targetCell.textContent.slice(0, range.startOffset)
const after = MENU.targetCell.textContent.slice(range.endOffset)
if (text !== '') MENU.targetCell.textContent = before + text + after
}
if (navigator.clipboard !== undefined) navigator.clipboard.readText().then(paste)
else paste(MENU.clipboard)
}


function clearCell() {
MENU.targetCell.innerHTML = ''
MENU.targetCell.textContent = ''
MENU.targetCell.focus()
}

Expand Down Expand Up @@ -751,7 +751,7 @@
MENU.rowClipboard = []
for (const cell of MENU.targetRow.parentNode.children) {
if (cell.tagName.toLowerCase() === 'th') continue
MENU.rowClipboard.push(cell.innerHTML)
MENU.rowClipboard.push(cell.textContent)
}
}

Expand All @@ -767,7 +767,7 @@
for (let i=1; i<MENU.targetRow.parentNode.children.length; i++) {
if (i > MENU.rowClipboard.length) break
else if (MENU.targetRow.parentNode.children[i].tagName.toLowerCase() === 'th') continue
MENU.targetRow.parentNode.children[i].innerHTML = MENU.rowClipboard[i-1]
MENU.targetRow.parentNode.children[i].textContent = MENU.rowClipboard[i-1]
}
if (MENU.rowClipboardSingleUse === true) MENU.rowClipboard = undefined
saveLocaleData('table', await getTableData())
Expand All @@ -777,7 +777,7 @@
async function clearRow() {
for (const cell of MENU.targetRow.parentNode.children) {
if (cell.tagName.toLowerCase() === 'th') continue
cell.innerHTML = ''
cell.textContent = ''
}
saveLocaleData('table', await getTableData())
}
Expand Down Expand Up @@ -938,8 +938,71 @@
}


async function importCSV() {
setData(await parseCSV(await readFile('.csv')))
function getCellPosition(cellId) {
const match = cellId.match(/^([A-Z]+)(\d+)$/)
if (!match) {
throw new Error("Invalid cell identifier format")
}

const letters = match[1]
const row = parseInt(match[2], 10) - 1

let col = 0
for (let i = 0; i < letters.length; i++) {
col *= 26
col += letters.charCodeAt(i) - 'A'.charCodeAt(0) + 1
}

return [ row, col-1 ]
}


async function importCSV(file) {
setData(await parseCSV(file))
}


async function importXLSX(file) {
const arrayBuffer = await file.arrayBuffer()
const zip = await JSZip.loadAsync(arrayBuffer)

const sharedStrings = zip.file('xl/sharedStrings.xml')
if (!sharedStrings) return

const sharedStrings_xmlString = await sharedStrings.async('string')
const sharedStrings_parser = new DOMParser()
const sharedStrings_xmlDoc = sharedStrings_parser.parseFromString(sharedStrings_xmlString, 'text/xml')
const sharedStrings_elements = []

for (const element of sharedStrings_xmlDoc.getElementsByTagName('si')) {
const elementText = element.getElementsByTagName('t')[0]
sharedStrings_elements.push(elementText ? elementText.textContent : '')
}

const sheet1 = zip.file('xl/worksheets/sheet1.xml')
if (!sheet1) return

const sheet1_xmlString = await sheet1.async('string')
const sheet1_parser = new DOMParser()
const sheet1_xmlDoc = sheet1_parser.parseFromString(sheet1_xmlString, 'text/xml')
const data = []

for (const cell of sheet1_xmlDoc.getElementsByTagName('c')) {
const [ row, column ] = getCellPosition(cell.getAttribute('r'))
const cellValueElement = cell.getElementsByTagName('v')[0]
const cellTextContent = cellValueElement ? cellValueElement.textContent : ''
const cellValueText = (cell.getAttribute('t') === 's') ? sharedStrings_elements[parseInt(cellTextContent, 10)] : cellTextContent
if (data.length === row) data.push([])
data[row][column] = cellValueText
}

setData(data)
}

async function importFile() {
const file = await readFile('.csv,.xlsx')
if (file.fileType.toLowerCase() === 'csv') await importCSV(file)
else if (file.fileType.toLowerCase() === 'xlsx') await importXLSX(file)
}


Expand Down Expand Up @@ -989,7 +1052,6 @@
const script = document.createElement('script')
script.setAttribute('src', src)
document.body.appendChild(script)
console.log(script)
script.onload = () => res(true)
script.onerror = () => res(false)
})
Expand Down Expand Up @@ -1038,7 +1100,7 @@
const rangeStart = range.startOffset
const rangeEnd = range.endOffset

cell.innerHTML = cell.innerText.slice(0, rangeStart) + text + cell.innerText.slice(rangeEnd)
cell.textContent = cell.innerText.slice(0, rangeStart) + text + cell.innerText.slice(rangeEnd)

const newRange = document.createRange()
const textNode = cell.childNodes[0]
Expand All @@ -1059,8 +1121,9 @@
const range = selection.getRangeAt(0)

const cursorPosition = range.startOffset
const text = cell.innerText
cell.innerHTML = text
const text = cell.textContent
// console.log(text, cell.textContent)
// cell.textContent = text

const newRange = document.createRange()
const textNode = cell.childNodes[0]
Expand Down

0 comments on commit 8de3017

Please sign in to comment.