| |
| |
| |
|
|
| import { DEF_FONT_SIZE, DEF_SLIDE_MARGIN_IN, EMU, LINEH_MODIFIER, ONEPT, SLIDE_OBJECT_TYPES } from './core-enums' |
| import { PresLayout, SlideLayout, TableCell, TableToSlidesProps, TableRow, TableRowSlide, TableCellProps } from './core-interfaces' |
| import { getSmartParseNumber, inch2Emu, rgbToHex, valToPts } from './gen-utils' |
| import PptxGenJS from './pptxgen' |
|
|
| |
| |
| |
| |
| |
| |
| function parseTextToLines(cell: TableCell, colWidth: number, verbose?: boolean): TableCell[][] { |
| |
| |
| |
| |
| const FOCO = 2.3 + (cell.options?.autoPageCharWeight ? cell.options.autoPageCharWeight : 0) |
| const CPL = Math.floor((colWidth / ONEPT) * EMU) / ((cell.options?.fontSize ? cell.options.fontSize : DEF_FONT_SIZE) / FOCO) |
|
|
| const parsedLines: TableCell[][] = [] |
| let inputCells: TableCell[] = [] |
| const inputLines1: TableCell[][] = [] |
| const inputLines2: TableCell[][] = [] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| if (cell.text && cell.text.toString().trim().length === 0) { |
| |
| inputCells.push({ _type: SLIDE_OBJECT_TYPES.tablecell, text: ' ' }) |
| } else if (typeof cell.text === 'number' || typeof cell.text === 'string') { |
| inputCells.push({ _type: SLIDE_OBJECT_TYPES.tablecell, text: (cell.text || '').toString().trim() }) |
| } else if (Array.isArray(cell.text)) { |
| inputCells = cell.text |
| } |
| if (verbose) { |
| console.log('[1/4] inputCells') |
| inputCells.forEach((cell, idx) => console.log(`[1/4] [${idx + 1}] cell: ${JSON.stringify(cell)}`)) |
| |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| let newLine: TableCell[] = [] |
| inputCells.forEach(cell => { |
| |
| if (typeof cell.text === 'string') { |
| if (cell.text.split('\n').length > 1) { |
| cell.text.split('\n').forEach(textLine => { |
| newLine.push({ |
| _type: SLIDE_OBJECT_TYPES.tablecell, |
| text: textLine, |
| options: { ...cell.options, ...{ breakLine: true } }, |
| }) |
| }) |
| } else { |
| newLine.push({ |
| _type: SLIDE_OBJECT_TYPES.tablecell, |
| text: cell.text.trim(), |
| options: cell.options, |
| }) |
| } |
|
|
| if (cell.options?.breakLine) { |
| if (verbose) console.log(`inputCells: new line > ${JSON.stringify(newLine)}`) |
| inputLines1.push(newLine) |
| newLine = [] |
| } |
| } |
|
|
| |
| if (newLine.length > 0) { |
| inputLines1.push(newLine) |
| newLine = [] |
| } |
| }) |
| if (verbose) { |
| console.log(`[2/4] inputLines1 (${inputLines1.length})`) |
| inputLines1.forEach((line, idx) => console.log(`[2/4] [${idx + 1}] line: ${JSON.stringify(line)}`)) |
| |
| } |
|
|
| |
| inputLines1.forEach(line => { |
| line.forEach(cell => { |
| const lineCells: TableCell[] = [] |
| const cellTextStr = String(cell.text) |
| const lineWords = cellTextStr.split(' ') |
|
|
| lineWords.forEach((word, idx) => { |
| const cellProps = { ...cell.options } |
| |
| if (cellProps?.breakLine) cellProps.breakLine = idx + 1 === lineWords.length |
| lineCells.push({ _type: SLIDE_OBJECT_TYPES.tablecell, text: word + (idx + 1 < lineWords.length ? ' ' : ''), options: cellProps }) |
| }) |
|
|
| inputLines2.push(lineCells) |
| }) |
| }) |
| if (verbose) { |
| console.log(`[3/4] inputLines2 (${inputLines2.length})`) |
| inputLines2.forEach(line => console.log(`[3/4] line: ${JSON.stringify(line)}`)) |
| |
| } |
|
|
| |
| inputLines2.forEach(line => { |
| let lineCells: TableCell[] = [] |
| let strCurrLine = '' |
|
|
| line.forEach(word => { |
| |
| if (strCurrLine.length + word.text.length > CPL) { |
| |
| parsedLines.push(lineCells) |
| lineCells = [] |
| strCurrLine = '' |
| } |
|
|
| |
| lineCells.push(word) |
|
|
| |
| strCurrLine += word.text.toString() |
| }) |
|
|
| |
| if (lineCells.length > 0) parsedLines.push(lineCells) |
| }) |
| if (verbose) { |
| console.log(`[4/4] parsedLines (${parsedLines.length})`) |
| parsedLines.forEach((line, idx) => console.log(`[4/4] [Line ${idx + 1}]:\n${JSON.stringify(line)}`)) |
| console.log('...............................................\n\n') |
| } |
|
|
| |
| return parsedLines |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function getSlidesForTableRows(tableRows: TableCell[][] = [], tableProps: TableToSlidesProps = {}, presLayout: PresLayout, masterSlide?: SlideLayout): TableRowSlide[] { |
| let arrInchMargins = DEF_SLIDE_MARGIN_IN |
| let emuSlideTabW = EMU * 1 |
| let emuSlideTabH = EMU * 1 |
| let emuTabCurrH = 0 |
| let numCols = 0 |
| const tableRowSlides: TableRowSlide[] = [] |
| const tablePropX = getSmartParseNumber(tableProps.x, 'X', presLayout) |
| const tablePropY = getSmartParseNumber(tableProps.y, 'Y', presLayout) |
| const tablePropW = getSmartParseNumber(tableProps.w, 'X', presLayout) |
| const tablePropH = getSmartParseNumber(tableProps.h, 'Y', presLayout) |
| let tableCalcW = tablePropW |
|
|
| function calcSlideTabH(): void { |
| let emuStartY = 0 |
| if (tableRowSlides.length === 0) emuStartY = tablePropY || inch2Emu(arrInchMargins[0]) |
| if (tableRowSlides.length > 0) emuStartY = inch2Emu(tableProps.autoPageSlideStartY || tableProps.newSlideStartY || arrInchMargins[0]) |
| emuSlideTabH = (tablePropH || presLayout.height) - emuStartY - inch2Emu(arrInchMargins[2]) |
| |
| |
| if (tableRowSlides.length > 1) { |
| |
| if (typeof tableProps.autoPageSlideStartY === 'number') { |
| emuSlideTabH = (tablePropH || presLayout.height) - inch2Emu(tableProps.autoPageSlideStartY + arrInchMargins[2]) |
| } else if (typeof tableProps.newSlideStartY === 'number') { |
| |
| emuSlideTabH = (tablePropH || presLayout.height) - inch2Emu(tableProps.newSlideStartY + arrInchMargins[2]) |
| } else if (tablePropY) { |
| emuSlideTabH = (tablePropH || presLayout.height) - inch2Emu((tablePropY / EMU < arrInchMargins[0] ? tablePropY / EMU : arrInchMargins[0]) + arrInchMargins[2]) |
| |
| if (emuSlideTabH < tablePropH) emuSlideTabH = tablePropH |
| } |
| } |
| } |
|
|
| if (tableProps.verbose) { |
| console.log('[[VERBOSE MODE]]') |
| console.log('|-- TABLE PROPS --------------------------------------------------------|') |
| console.log(`| presLayout.width ................................ = ${(presLayout.width / EMU).toFixed(1)}`) |
| console.log(`| presLayout.height ............................... = ${(presLayout.height / EMU).toFixed(1)}`) |
| console.log(`| tableProps.x .................................... = ${typeof tableProps.x === 'number' ? (tableProps.x / EMU).toFixed(1) : tableProps.x}`) |
| console.log(`| tableProps.y .................................... = ${typeof tableProps.y === 'number' ? (tableProps.y / EMU).toFixed(1) : tableProps.y}`) |
| console.log(`| tableProps.w .................................... = ${typeof tableProps.w === 'number' ? (tableProps.w / EMU).toFixed(1) : tableProps.w}`) |
| console.log(`| tableProps.h .................................... = ${typeof tableProps.h === 'number' ? (tableProps.h / EMU).toFixed(1) : tableProps.h}`) |
| console.log(`| tableProps.slideMargin .......................... = ${tableProps.slideMargin ? String(tableProps.slideMargin) : ''}`) |
| console.log(`| tableProps.margin ............................... = ${String(tableProps.margin)}`) |
| console.log(`| tableProps.colW ................................. = ${String(tableProps.colW)}`) |
| console.log(`| tableProps.autoPageSlideStartY .................. = ${tableProps.autoPageSlideStartY}`) |
| console.log(`| tableProps.autoPageCharWeight ................... = ${tableProps.autoPageCharWeight}`) |
| console.log('|-- CALCULATIONS -------------------------------------------------------|') |
| console.log(`| tablePropX ...................................... = ${tablePropX / EMU}`) |
| console.log(`| tablePropY ...................................... = ${tablePropY / EMU}`) |
| console.log(`| tablePropW ...................................... = ${tablePropW / EMU}`) |
| console.log(`| tablePropH ...................................... = ${tablePropH / EMU}`) |
| console.log(`| tableCalcW ...................................... = ${tableCalcW / EMU}`) |
| } |
|
|
| |
| { |
| |
| if (!tableProps.slideMargin && tableProps.slideMargin !== 0) tableProps.slideMargin = DEF_SLIDE_MARGIN_IN[0] |
|
|
| if (masterSlide && typeof masterSlide._margin !== 'undefined') { |
| if (Array.isArray(masterSlide._margin)) arrInchMargins = masterSlide._margin |
| else if (!isNaN(Number(masterSlide._margin))) { arrInchMargins = [Number(masterSlide._margin), Number(masterSlide._margin), Number(masterSlide._margin), Number(masterSlide._margin)] } |
| } else if (tableProps.slideMargin || tableProps.slideMargin === 0) { |
| if (Array.isArray(tableProps.slideMargin)) arrInchMargins = tableProps.slideMargin |
| else if (!isNaN(tableProps.slideMargin)) arrInchMargins = [tableProps.slideMargin, tableProps.slideMargin, tableProps.slideMargin, tableProps.slideMargin] |
| } |
|
|
| if (tableProps.verbose) console.log(`| arrInchMargins .................................. = [${arrInchMargins.join(', ')}]`) |
| } |
|
|
| |
| { |
| |
| |
| const firstRow = tableRows[0] || [] |
| firstRow.forEach(cell => { |
| if (!cell) cell = { _type: SLIDE_OBJECT_TYPES.tablecell } |
| const cellOpts = cell.options || null |
| numCols += Number(cellOpts?.colspan ? cellOpts.colspan : 1) |
| }) |
| if (tableProps.verbose) console.log(`| numCols ......................................... = ${numCols}`) |
| } |
|
|
| |
| if (!tablePropW && tableProps.colW) { |
| tableCalcW = Array.isArray(tableProps.colW) ? tableProps.colW.reduce((p, n) => p + n) * EMU : tableProps.colW * numCols || 0 |
| if (tableProps.verbose) console.log(`| tableCalcW ...................................... = ${tableCalcW / EMU}`) |
| } |
|
|
| |
| { |
| emuSlideTabW = tableCalcW || inch2Emu((tablePropX ? tablePropX / EMU : arrInchMargins[1]) + arrInchMargins[3]) |
| if (tableProps.verbose) console.log(`| emuSlideTabW .................................... = ${(emuSlideTabW / EMU).toFixed(1)}`) |
| } |
|
|
| |
| if (!tableProps.colW || !Array.isArray(tableProps.colW)) { |
| if (tableProps.colW && !isNaN(Number(tableProps.colW))) { |
| const arrColW = [] |
| const firstRow = tableRows[0] || [] |
| firstRow.forEach(() => arrColW.push(tableProps.colW)) |
| tableProps.colW = [] |
| arrColW.forEach(val => { |
| if (Array.isArray(tableProps.colW)) tableProps.colW.push(val) |
| }) |
| } else { |
| |
| tableProps.colW = [] |
| for (let iCol = 0; iCol < numCols; iCol++) { |
| tableProps.colW.push(emuSlideTabW / EMU / numCols) |
| } |
| } |
| } |
|
|
| |
| let newTableRowSlide: TableRowSlide = { rows: [] as TableRow[] } |
| tableRows.forEach((row, iRow) => { |
| |
| const rowCellLines: TableCell[] = [] |
| let maxCellMarTopEmu = 0 |
| let maxCellMarBtmEmu = 0 |
|
|
| |
| let currTableRow: TableRow = [] |
| row.forEach(cell => { |
| currTableRow.push({ |
| _type: SLIDE_OBJECT_TYPES.tablecell, |
| text: [], |
| options: cell.options, |
| }) |
|
|
| |
| |
| |
| |
| if (cell.options.margin && cell.options.margin[0] >= 1) { |
| if (cell.options?.margin && cell.options.margin[0] && valToPts(cell.options.margin[0]) > maxCellMarTopEmu) maxCellMarTopEmu = valToPts(cell.options.margin[0]) |
| else if (tableProps?.margin && tableProps.margin[0] && valToPts(tableProps.margin[0]) > maxCellMarTopEmu) maxCellMarTopEmu = valToPts(tableProps.margin[0]) |
| if (cell.options?.margin && cell.options.margin[2] && valToPts(cell.options.margin[2]) > maxCellMarBtmEmu) maxCellMarBtmEmu = valToPts(cell.options.margin[2]) |
| else if (tableProps?.margin && tableProps.margin[2] && valToPts(tableProps.margin[2]) > maxCellMarBtmEmu) maxCellMarBtmEmu = valToPts(tableProps.margin[2]) |
| } else { |
| if (cell.options?.margin && cell.options.margin[0] && inch2Emu(cell.options.margin[0]) > maxCellMarTopEmu) maxCellMarTopEmu = inch2Emu(cell.options.margin[0]) |
| else if (tableProps?.margin && tableProps.margin[0] && inch2Emu(tableProps.margin[0]) > maxCellMarTopEmu) maxCellMarTopEmu = inch2Emu(tableProps.margin[0]) |
| if (cell.options?.margin && cell.options.margin[2] && inch2Emu(cell.options.margin[2]) > maxCellMarBtmEmu) maxCellMarBtmEmu = inch2Emu(cell.options.margin[2]) |
| else if (tableProps?.margin && tableProps.margin[2] && inch2Emu(tableProps.margin[2]) > maxCellMarBtmEmu) maxCellMarBtmEmu = inch2Emu(tableProps.margin[2]) |
| } |
| }) |
|
|
| |
| calcSlideTabH() |
| emuTabCurrH += maxCellMarTopEmu + maxCellMarBtmEmu |
| if (tableProps.verbose && iRow === 0) console.log(`| SLIDE [${tableRowSlides.length}]: emuSlideTabH ...... = ${(emuSlideTabH / EMU).toFixed(1)} `) |
|
|
| |
| row.forEach((cell, iCell) => { |
| const newCell: TableCell = { |
| _type: SLIDE_OBJECT_TYPES.tablecell, |
| _lines: null, |
| _lineHeight: inch2Emu( |
| ((cell.options?.fontSize ? cell.options.fontSize : tableProps.fontSize ? tableProps.fontSize : DEF_FONT_SIZE) * |
| (LINEH_MODIFIER + (tableProps.autoPageLineWeight ? tableProps.autoPageLineWeight : 0))) / |
| 100 |
| ), |
| text: [], |
| options: cell.options, |
| } |
|
|
| |
| if (newCell.options.rowspan) newCell._lineHeight = 0 |
|
|
| |
| newCell.options.autoPageCharWeight = tableProps.autoPageCharWeight ? tableProps.autoPageCharWeight : null |
|
|
| |
| let totalColW = tableProps.colW[iCell] |
| if (cell.options.colspan && Array.isArray(tableProps.colW)) { |
| totalColW = tableProps.colW.filter((_cell, idx) => idx >= iCell && idx < idx + cell.options.colspan).reduce((prev, curr) => prev + curr) |
| } |
|
|
| |
| newCell._lines = parseTextToLines(cell, totalColW, false) |
|
|
| |
| rowCellLines.push(newCell) |
| }) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (tableProps.verbose) console.log(`\n| SLIDE [${tableRowSlides.length}]: ROW [${iRow}]: START...`) |
| let currCellIdx = 0 |
| let emuLineMaxH = 0 |
| let isDone = false |
| while (!isDone) { |
| const srcCell: TableCell = rowCellLines[currCellIdx] |
| let tgtCell: TableCell = currTableRow[currCellIdx] |
|
|
| |
| rowCellLines.forEach(cell => { |
| if (cell._lineHeight >= emuLineMaxH) emuLineMaxH = cell._lineHeight |
| }) |
|
|
| |
| if (emuTabCurrH + emuLineMaxH > emuSlideTabH) { |
| if (tableProps.verbose) { |
| console.log('\n|-----------------------------------------------------------------------|') |
| |
| console.log(`|-- NEW SLIDE CREATED (currTabH+currLineH > maxH) => ${(emuTabCurrH / EMU).toFixed(2)} + ${(srcCell._lineHeight / EMU).toFixed(2)} > ${emuSlideTabH / EMU}`) |
| console.log('|-----------------------------------------------------------------------|\n\n') |
| } |
|
|
| |
| if (currTableRow.length > 0 && currTableRow.map(cell => cell.text.length).reduce((p, n) => p + n) > 0) newTableRowSlide.rows.push(currTableRow) |
|
|
| |
| tableRowSlides.push(newTableRowSlide) |
|
|
| |
| const newRows: TableRow[] = [] |
| newTableRowSlide = { rows: newRows } |
|
|
| |
| currTableRow = [] |
| row.forEach(cell => currTableRow.push({ _type: SLIDE_OBJECT_TYPES.tablecell, text: [], options: cell.options })) |
|
|
| |
| calcSlideTabH() |
| emuTabCurrH += maxCellMarTopEmu + maxCellMarBtmEmu |
| if (tableProps.verbose) console.log(`| SLIDE [${tableRowSlides.length}]: emuSlideTabH ...... = ${(emuSlideTabH / EMU).toFixed(1)} `) |
|
|
| |
| emuTabCurrH = 0 |
|
|
| |
| if ((tableProps.addHeaderToEach || tableProps.autoPageRepeatHeader) && tableProps._arrObjTabHeadRows) { |
| tableProps._arrObjTabHeadRows.forEach(row => { |
| const newHeadRow: TableRow = [] |
| let maxLineHeight = 0 |
| row.forEach(cell => { |
| newHeadRow.push(cell) |
| if (cell._lineHeight > maxLineHeight) maxLineHeight = cell._lineHeight |
| }) |
| newTableRowSlide.rows.push(newHeadRow) |
| emuTabCurrH += maxLineHeight |
| }) |
| } |
|
|
| |
| tgtCell = currTableRow[currCellIdx] |
| } |
|
|
| |
| const currLine: TableCell[] = srcCell._lines.shift() |
|
|
| |
| if (Array.isArray(tgtCell.text)) { |
| if (currLine) tgtCell.text = tgtCell.text.concat(currLine) |
| else if (tgtCell.text.length === 0) tgtCell.text = tgtCell.text.concat({ _type: SLIDE_OBJECT_TYPES.tablecell, text: '' }) |
| |
| } |
|
|
| |
| if (currCellIdx === rowCellLines.length - 1) emuTabCurrH += emuLineMaxH |
|
|
| |
| currCellIdx = currCellIdx < rowCellLines.length - 1 ? currCellIdx + 1 : 0 |
|
|
| |
| const brent = rowCellLines.map(cell => cell._lines.length).reduce((prev, next) => prev + next) |
| if (brent === 0) isDone = true |
| } |
|
|
| |
| if (currTableRow.length > 0) newTableRowSlide.rows.push(currTableRow) |
|
|
| if (tableProps.verbose) { |
| console.log( |
| `- SLIDE [${tableRowSlides.length}]: ROW [${iRow}]: ...COMPLETE ...... emuTabCurrH = ${(emuTabCurrH / EMU).toFixed(2)} ( emuSlideTabH = ${( |
| emuSlideTabH / EMU |
| ).toFixed(2)} )` |
| ) |
| } |
| }) |
|
|
| |
| tableRowSlides.push(newTableRowSlide) |
|
|
| if (tableProps.verbose) { |
| console.log('\n|================================================|') |
| console.log(`| FINAL: tableRowSlides.length = ${tableRowSlides.length}`) |
| tableRowSlides.forEach(slide => console.log(slide)) |
| console.log('|================================================|\n\n') |
| } |
|
|
| |
| return tableRowSlides |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function genTableToSlides(pptx: PptxGenJS, tabEleId: string, options: TableToSlidesProps = {}, masterSlide?: SlideLayout): void { |
| const opts = options || {} |
| opts.slideMargin = opts.slideMargin || opts.slideMargin === 0 ? opts.slideMargin : 0.5 |
| let emuSlideTabW = opts.w || pptx.presLayout.width |
| const arrObjTabHeadRows: [TableCell[]?] = [] |
| const arrObjTabBodyRows: [TableCell[]?] = [] |
| const arrObjTabFootRows: [TableCell[]?] = [] |
| const arrColW: number[] = [] |
| const arrTabColW: number[] = [] |
| let arrInchMargins: [number, number, number, number] = [0.5, 0.5, 0.5, 0.5] |
| let intTabW = 0 |
|
|
| |
| if (!document.getElementById(tabEleId)) throw new Error('tableToSlides: Table ID "' + tabEleId + '" does not exist!') |
|
|
| |
| if (masterSlide?._margin) { |
| if (Array.isArray(masterSlide._margin)) arrInchMargins = masterSlide._margin |
| else if (!isNaN(masterSlide._margin)) arrInchMargins = [masterSlide._margin, masterSlide._margin, masterSlide._margin, masterSlide._margin] |
| opts.slideMargin = arrInchMargins |
| } else if (opts?.slideMargin) { |
| if (Array.isArray(opts.slideMargin)) arrInchMargins = opts.slideMargin |
| else if (!isNaN(opts.slideMargin)) arrInchMargins = [opts.slideMargin, opts.slideMargin, opts.slideMargin, opts.slideMargin] |
| } |
| emuSlideTabW = (opts.w ? inch2Emu(opts.w) : pptx.presLayout.width) - inch2Emu(arrInchMargins[1] + arrInchMargins[3]) |
|
|
| if (opts.verbose) { |
| console.log('[[VERBOSE MODE]]') |
| console.log('|-- `tableToSlides` ----------------------------------------------------|') |
| console.log(`| tableProps.h .................................... = ${opts.h}`) |
| console.log(`| tableProps.w .................................... = ${opts.w}`) |
| console.log(`| pptx.presLayout.width ........................... = ${(pptx.presLayout.width / EMU).toFixed(1)}`) |
| console.log(`| pptx.presLayout.height .......................... = ${(pptx.presLayout.height / EMU).toFixed(1)}`) |
| console.log(`| emuSlideTabW .................................... = ${(emuSlideTabW / EMU).toFixed(1)}`) |
| } |
|
|
| |
| let firstRowCells = document.querySelectorAll(`#${tabEleId} tr:first-child th`) |
| if (firstRowCells.length === 0) firstRowCells = document.querySelectorAll(`#${tabEleId} tr:first-child td`) |
| firstRowCells.forEach((cellEle: Element) => { |
| const cell = cellEle as HTMLTableCellElement |
| if (cell.getAttribute('colspan')) { |
| |
| |
| for (let idxc = 0; idxc < Number(cell.getAttribute('colspan')); idxc++) { |
| arrTabColW.push(Math.round(cell.offsetWidth / Number(cell.getAttribute('colspan')))) |
| } |
| } else { |
| arrTabColW.push(cell.offsetWidth) |
| } |
| }) |
| arrTabColW.forEach(colW => { |
| intTabW += colW |
| }) |
|
|
| |
| arrTabColW.forEach((colW, idxW) => { |
| const intCalcWidth = Number(((Number(emuSlideTabW) * ((colW / intTabW) * 100)) / 100 / EMU).toFixed(2)) |
| let intMinWidth = 0 |
| const colSelectorMin = document.querySelector(`#${tabEleId} thead tr:first-child th:nth-child(${idxW + 1})`) |
| if (colSelectorMin) intMinWidth = Number(colSelectorMin.getAttribute('data-pptx-min-width')) |
| const intSetWidth = 0 |
| const colSelectorSet = document.querySelector(`#${tabEleId} thead tr:first-child th:nth-child(${idxW + 1})`) |
| if (colSelectorSet) intMinWidth = Number(colSelectorSet.getAttribute('data-pptx-width')) |
| arrColW.push(intSetWidth || (intMinWidth > intCalcWidth ? intMinWidth : intCalcWidth)) |
| }) |
| if (opts.verbose) { |
| console.log(`| arrColW ......................................... = [${arrColW.join(', ')}]`) |
| } |
|
|
| |
| |
| const tableParts = ['thead', 'tbody', 'tfoot'] |
| tableParts.forEach(part => { |
| document.querySelectorAll(`#${tabEleId} ${part} tr`).forEach((row: Element) => { |
| const htmlRow = row as HTMLTableRowElement |
| const arrObjTabCells: TableCell[] = [] |
| Array.from(htmlRow.cells).forEach(cell => { |
| |
| const arrRGB1 = window.getComputedStyle(cell).getPropertyValue('color').replace(/\s+/gi, '').replace('rgba(', '').replace('rgb(', '').replace(')', '').split(',') |
| let arrRGB2 = window |
| .getComputedStyle(cell) |
| .getPropertyValue('background-color') |
| .replace(/\s+/gi, '') |
| .replace('rgba(', '') |
| .replace('rgb(', '') |
| .replace(')', '') |
| .split(',') |
| if ( |
| |
| window.getComputedStyle(cell).getPropertyValue('background-color') === 'rgba(0, 0, 0, 0)' || |
| window.getComputedStyle(cell).getPropertyValue('transparent') |
| ) { |
| arrRGB2 = ['255', '255', '255'] |
| } |
|
|
| |
| const cellOpts: TableCellProps = { |
| align: null, |
| bold: |
| !!(window.getComputedStyle(cell).getPropertyValue('font-weight') === 'bold' || |
| Number(window.getComputedStyle(cell).getPropertyValue('font-weight')) >= 500), |
| border: null, |
| color: rgbToHex(Number(arrRGB1[0]), Number(arrRGB1[1]), Number(arrRGB1[2])), |
| fill: { color: rgbToHex(Number(arrRGB2[0]), Number(arrRGB2[1]), Number(arrRGB2[2])) }, |
| fontFace: |
| (window.getComputedStyle(cell).getPropertyValue('font-family') || '').split(',')[0].replace(/"/g, '').replace('inherit', '').replace('initial', '') || |
| null, |
| fontSize: Number(window.getComputedStyle(cell).getPropertyValue('font-size').replace(/[a-z]/gi, '')), |
| margin: null, |
| colspan: Number(cell.getAttribute('colspan')) || null, |
| rowspan: Number(cell.getAttribute('rowspan')) || null, |
| valign: null, |
| } |
|
|
| if (['left', 'center', 'right', 'start', 'end'].includes(window.getComputedStyle(cell).getPropertyValue('text-align'))) { |
| const align = window.getComputedStyle(cell).getPropertyValue('text-align').replace('start', 'left').replace('end', 'right') |
| cellOpts.align = align === 'center' ? 'center' : align === 'left' ? 'left' : align === 'right' ? 'right' : null |
| } |
| if (['top', 'middle', 'bottom'].includes(window.getComputedStyle(cell).getPropertyValue('vertical-align'))) { |
| const valign = window.getComputedStyle(cell).getPropertyValue('vertical-align') |
| cellOpts.valign = valign === 'top' ? 'top' : valign === 'middle' ? 'middle' : valign === 'bottom' ? 'bottom' : null |
| } |
|
|
| |
| |
| if (window.getComputedStyle(cell).getPropertyValue('padding-left')) { |
| cellOpts.margin = [0, 0, 0, 0] |
| const sidesPad = ['padding-top', 'padding-right', 'padding-bottom', 'padding-left'] |
| sidesPad.forEach((val, idxs) => { |
| cellOpts.margin[idxs] = Math.round(Number(window.getComputedStyle(cell).getPropertyValue(val).replace(/\D/gi, ''))) |
| }) |
| } |
|
|
| |
| if ( |
| window.getComputedStyle(cell).getPropertyValue('border-top-width') || |
| window.getComputedStyle(cell).getPropertyValue('border-right-width') || |
| window.getComputedStyle(cell).getPropertyValue('border-bottom-width') || |
| window.getComputedStyle(cell).getPropertyValue('border-left-width') |
| ) { |
| cellOpts.border = [null, null, null, null] |
| const sidesBor = ['top', 'right', 'bottom', 'left'] |
| sidesBor.forEach((val, idxb) => { |
| const intBorderW = Math.round( |
| Number( |
| window |
| .getComputedStyle(cell) |
| .getPropertyValue('border-' + val + '-width') |
| .replace('px', '') |
| ) |
| ) |
| let arrRGB = [] |
| arrRGB = window |
| .getComputedStyle(cell) |
| .getPropertyValue('border-' + val + '-color') |
| .replace(/\s+/gi, '') |
| .replace('rgba(', '') |
| .replace('rgb(', '') |
| .replace(')', '') |
| .split(',') |
| const strBorderC = rgbToHex(Number(arrRGB[0]), Number(arrRGB[1]), Number(arrRGB[2])) |
| cellOpts.border[idxb] = { pt: intBorderW, color: strBorderC } |
| }) |
| } |
|
|
| |
| arrObjTabCells.push({ |
| _type: SLIDE_OBJECT_TYPES.tablecell, |
| text: cell.innerText, |
| options: cellOpts, |
| }) |
| }) |
| switch (part) { |
| case 'thead': |
| arrObjTabHeadRows.push(arrObjTabCells) |
| break |
| case 'tbody': |
| arrObjTabBodyRows.push(arrObjTabCells) |
| break |
| case 'tfoot': |
| arrObjTabFootRows.push(arrObjTabCells) |
| break |
| default: |
| console.log(`table parsing: unexpected table part: ${part}`) |
| break |
| } |
| }) |
| }) |
|
|
| |
| |
| opts._arrObjTabHeadRows = arrObjTabHeadRows || null |
| opts.colW = arrColW |
| getSlidesForTableRows([...arrObjTabHeadRows, ...arrObjTabBodyRows, ...arrObjTabFootRows], opts, pptx.presLayout, masterSlide).forEach((slide, idxTr) => { |
| |
| const newSlide = pptx.addSlide({ masterName: opts.masterSlideName || null }) |
|
|
| |
| if (idxTr === 0) opts.y = opts.y || arrInchMargins[0] |
| if (idxTr > 0) opts.y = opts.autoPageSlideStartY || opts.newSlideStartY || arrInchMargins[0] |
| if (opts.verbose) console.log(`| opts.autoPageSlideStartY: ${opts.autoPageSlideStartY} / arrInchMargins[0]: ${arrInchMargins[0]} => opts.y = ${opts.y}`) |
|
|
| |
| newSlide.addTable(slide.rows, { x: opts.x || arrInchMargins[3], y: opts.y, w: Number(emuSlideTabW) / EMU, colW: arrColW, autoPage: false }) |
|
|
| |
| if (opts.addImage) { |
| opts.addImage.options = opts.addImage.options || {} |
| if (!opts.addImage.image || (!opts.addImage.image.path && !opts.addImage.image.data)) { |
| console.warn('Warning: tableToSlides.addImage requires either `path` or `data`') |
| } else { |
| newSlide.addImage({ |
| path: opts.addImage.image.path, |
| data: opts.addImage.image.data, |
| x: opts.addImage.options.x, |
| y: opts.addImage.options.y, |
| w: opts.addImage.options.w, |
| h: opts.addImage.options.h, |
| }) |
| } |
| } |
| if (opts.addShape) newSlide.addShape(opts.addShape.shapeName, opts.addShape.options || {}) |
| if (opts.addTable) newSlide.addTable(opts.addTable.rows, opts.addTable.options || {}) |
| if (opts.addText) newSlide.addText(opts.addText.text, opts.addText.options || {}) |
| }) |
| } |
|
|