Skip to content

Commit

Permalink
const instead of let
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbengtsson committed Apr 26, 2020
1 parent d95de29 commit 60afc49
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 128 deletions.
8 changes: 4 additions & 4 deletions src/applyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function (jsPDF: jsPDFConstructor) {

jsPDF.API.autoTableHtmlToJson = function (
tableElem: HTMLTableElement,
includeHiddenElements: boolean = false
includeHiddenElements = false
) {
if (typeof window === 'undefined') {
console.error('Cannot run autoTableHtmlToJson in non browser environment')
Expand All @@ -44,14 +44,14 @@ export default function (jsPDF: jsPDFConstructor) {
return null
}

let { head, body, foot } = parseHtml(
const { head, body, foot } = parseHtml(
this,
tableElem,
window,
includeHiddenElements,
false
)
let firstRow = head[0] || body[0] || foot[0]
const firstRow = head[0] || body[0] || foot[0]

return { columns: firstRow, rows: body, data: body }
}
Expand All @@ -63,7 +63,7 @@ export default function (jsPDF: jsPDFConstructor) {
console.error(
'Use of deprecated function: autoTableEndPosY. Use doc.previousAutoTable.finalY instead.'
)
let prev = this.previousAutoTable
const prev = this.previousAutoTable
if (prev.cursor && typeof prev.cursor.y === 'number') {
return prev.cursor.y
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/autoTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function autoTable(this: jsPDFDocument) {
}

// 1. Parse and unify user input
let table = parseInput(arguments, doc, win)
const table = parseInput(arguments, doc, win)

// 2. Calculate preliminary table, column, row and cell dimensions
calculateWidths(table, doc)
Expand Down
8 changes: 4 additions & 4 deletions src/autoTableText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default function (
doc: jsPDFDocument
) {
styles = styles || {}
let FONT_ROW_RATIO = 1.15
const FONT_ROW_RATIO = 1.15

let k = doc.internal.scaleFactor
let fontSize = doc.internal.getFontSize() / k
const k = doc.internal.scaleFactor
const fontSize = doc.internal.getFontSize() / k

let splitRegex = /\r\n|\r|\n/g
const splitRegex = /\r\n|\r|\n/g
let splitText: string | string[] = ''
let lineCount = 1
if (
Expand Down
10 changes: 5 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function getStringWidth(text: Text, styles: Styles, doc: DocHandler) {
}

export function addTableBorder(table: Table, doc: DocHandler) {
let lineWidth = table.settings.tableLineWidth
let lineColor = table.settings.tableLineColor
const lineWidth = table.settings.tableLineWidth
const lineColor = table.settings.tableLineColor
doc.applyStyles({ lineWidth, lineColor })
let fillStyle = getFillStyle(lineWidth, false)
const fillStyle = getFillStyle(lineWidth, false)
if (fillStyle) {
doc.rect(
table.pageStartX,
Expand All @@ -31,8 +31,8 @@ export function addTableBorder(table: Table, doc: DocHandler) {
}

export function getFillStyle(lineWidth: number, fillColor: Color) {
let drawLine = lineWidth > 0
let drawBackground = fillColor || fillColor === 0
const drawLine = lineWidth > 0
const drawBackground = fillColor || fillColor === 0
if (drawLine && drawBackground) {
return 'DF' // Fill then stroke
} else if (drawLine) {
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CellHook, PageHook } from './models'
/**
* Ratio between font size and font height. The number comes from jspdf's source code
*/
export let FONT_ROW_RATIO = 1.15
export const FONT_ROW_RATIO = 1.15

export interface Styles {
font: 'helvetica' | 'times' | 'courier' | string
Expand Down Expand Up @@ -128,7 +128,7 @@ export function defaultStyles(scaleFactor: number): Styles {
*/
export type ThemeName = 'striped' | 'grid' | 'plain'
export function getTheme(name: ThemeName): { [key: string]: Partial<Styles> } {
let themes: { [key in ThemeName]: { [key: string]: Partial<Styles> } } = {
const themes: { [key in ThemeName]: { [key: string]: Partial<Styles> } } = {
striped: {
table: { fillColor: 255, textColor: 80, fontStyle: 'normal' },
head: { textColor: 255, fillColor: [41, 128, 185], fontStyle: 'bold' },
Expand Down
14 changes: 7 additions & 7 deletions src/htmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function parseHtml(
tableElement = input
}

let supportedFonts = Object.keys(doc.getFontList())
let scaleFactor = doc.scaleFactor()
const supportedFonts = Object.keys(doc.getFontList())
const scaleFactor = doc.scaleFactor()

const head: RowInput[] = [],
body: RowInput[] = [],
Expand All @@ -31,7 +31,7 @@ export function parseHtml(
for (let i = 0; i < tableElement.rows.length; i++) {
const element = tableElement.rows[i]
const tagName = element?.parentElement?.tagName?.toLowerCase()
let row = parseRowContent(
const row = parseRowContent(
supportedFonts,
scaleFactor,
window,
Expand Down Expand Up @@ -61,10 +61,10 @@ function parseRowContent(
includeHidden: boolean,
useCss: boolean
) {
let resultRow = new HtmlRowInput(row)
const resultRow = new HtmlRowInput(row)
for (let i = 0; i < row.cells.length; i++) {
let cell = row.cells[i]
let style = window.getComputedStyle(cell)
const cell = row.cells[i]
const style = window.getComputedStyle(cell)
if (includeHidden || style.display !== 'none') {
let cellStyles
if (useCss) {
Expand All @@ -79,7 +79,7 @@ function parseRowContent(
})
}
}
let style = window.getComputedStyle(row)
const style = window.getComputedStyle(row)
if (resultRow.length > 0 && (includeHidden || style.display !== 'none')) {
return resultRow
}
Expand Down
82 changes: 44 additions & 38 deletions src/inputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export function parseInput(
doc: DocHandler,
window?: Window
) {
let current = parseUserInput(userInput)
let document = doc.getDocumentOptions()
let global = doc.getGlobalOptions()
const current = parseUserInput(userInput)
const document = doc.getDocumentOptions()
const global = doc.getGlobalOptions()

validateOptions(global, document, current, doc)
let options = assign({}, global, document, current)
const options = assign({}, global, document, current)

let previous = doc.getPreviousAutoTable()
const previous = doc.getPreviousAutoTable()
const sf = doc.scaleFactor()

const margin = marginOrPadding(options.margin, 40 / sf)
Expand All @@ -47,7 +47,7 @@ export function parseInput(
const styles = parseStyles(global, document, current)
const content = parseContent(doc, options, styles, settings.theme, sf, window)

let table = new Table(
const table = new Table(
current.tableId,
settings,
styles,
Expand Down Expand Up @@ -76,12 +76,12 @@ export function parseInput(

function calculate(table: Table, sf: number, doc: DocHandler) {
table.allRows().forEach((row) => {
for (let column of table.columns) {
for (const column of table.columns) {
const cell = row.cells[column.index]
if (!cell) continue
table.callCellHooks(doc, table.hooks.didParseCell, cell, row, column)

let padding = cell.padding('horizontal')
const padding = cell.padding('horizontal')
cell.contentWidth = getStringWidth(cell.text, cell.styles, doc) + padding

const longestWordWidth = getStringWidth(
Expand Down Expand Up @@ -110,8 +110,8 @@ function calculate(table: Table, sf: number, doc: DocHandler) {
})

table.allRows().forEach((row) => {
for (let column of table.columns) {
let cell = row.cells[column.index]
for (const column of table.columns) {
const cell = row.cells[column.index]

// For now we ignore the minWidth and wrappedWidth of colspan cells when calculating colspan widths.
// Could probably be improved upon however.
Expand All @@ -130,11 +130,11 @@ function calculate(table: Table, sf: number, doc: DocHandler) {

// Note that this is not perfect for now since for example row and table styles are
// not accounted for
let columnStyles =
const columnStyles =
table.styles.columnStyles[column.dataKey] ||
table.styles.columnStyles[column.index] ||
{}
let cellWidth = columnStyles.cellWidth
const cellWidth = columnStyles.cellWidth
if (cellWidth && typeof cellWidth === 'number') {
column.minWidth = cellWidth
column.wrappedWidth = cellWidth
Expand All @@ -159,23 +159,23 @@ function parseStyles(
dInput: UserOptions,
cInput: UserOptions
) {
let styleOptions: StylesProps = {
const styleOptions: StylesProps = {
styles: {},
headStyles: {},
bodyStyles: {},
footStyles: {},
alternateRowStyles: {},
columnStyles: {},
}
for (let prop of Object.keys(styleOptions) as StyleProp[]) {
for (const prop of Object.keys(styleOptions) as StyleProp[]) {
if (prop === 'columnStyles') {
let global = dInput.columnStyles
let document = dInput.columnStyles
let current = dInput.columnStyles
const global = dInput.columnStyles
const document = dInput.columnStyles
const current = dInput.columnStyles
styleOptions.columnStyles = assign({}, global, document, current)
} else {
let allOptions = [gInput, dInput, cInput]
let styles = allOptions.map((opts) => opts[prop] || {})
const allOptions = [gInput, dInput, cInput]
const styles = allOptions.map((opts) => opts[prop] || {})
styleOptions[prop] = assign({}, styles[0], styles[1], styles[2])
}
}
Expand All @@ -187,7 +187,7 @@ function parseHooks(
document: UserOptions,
current: UserOptions
) {
let allOptions = [global, document, current]
const allOptions = [global, document, current]
const result = {
didParseCell: [] as CellHook[],
willDrawCell: [] as CellHook[],
Expand Down Expand Up @@ -255,7 +255,7 @@ function getStartY(
) {
let isSamePageAsPreviousTable = false
if (previous) {
let endingPage = previous.startPageNumber + previous.pageNumber - 1
const endingPage = previous.startPageNumber + previous.pageNumber - 1
isSamePageAsPreviousTable = endingPage === currentPage
}

Expand All @@ -276,7 +276,7 @@ function parseUserInput(args: IArguments): UserOptions {
return args[0]
} else {
// Deprecated initialization on format doc.autoTable(columns, body, [options])
let opts = args[2] || {}
const opts = args[2] || {}

opts.body = args[1]
opts.columns = args[0]
Expand Down Expand Up @@ -334,23 +334,23 @@ function parseSection(
theme: ThemeName,
scaleFactor: number
): Row[] {
let rowSpansLeftForColumn: {
const rowSpansLeftForColumn: {
[key: string]: { left: number; times: number }
} = {}
if (sectionRows.length === 0 && settings.columns && sectionName !== 'body') {
// If no head or foot is set, try generating one with content in columns
let sectionRow = generateSectionRowFromColumnData(columns, sectionName)
const sectionRow = generateSectionRowFromColumnData(columns, sectionName)
if (sectionRow) {
sectionRows.push(sectionRow)
}
}
return sectionRows.map((rawRow, rowIndex) => {
let skippedRowForRowSpans = 0
let row = new Row(rawRow, rowIndex, sectionName)
const row = new Row(rawRow, rowIndex, sectionName)

let colSpansAdded = 0
let columnSpansLeft = 0
for (let column of columns) {
for (const column of columns) {
if (
rowSpansLeftForColumn[column.index] == null ||
rowSpansLeftForColumn[column.index].left === 0
Expand All @@ -368,7 +368,7 @@ function parseSection(
if (typeof rawCell === 'object' && !Array.isArray(rawCell)) {
cellInputStyles = rawCell?.styles || {}
}
let styles = cellStyles(
const styles = cellStyles(
sectionName,
column,
rowIndex,
Expand All @@ -377,7 +377,7 @@ function parseSection(
scaleFactor,
cellInputStyles
)
let cell = new Cell(rawCell, styles, sectionName)
const cell = new Cell(rawCell, styles, sectionName)
// dataKey is not used internally no more but keep for
// backwards compat in hooks
row.cells[column.dataKey] = cell
Expand Down Expand Up @@ -406,9 +406,9 @@ function generateSectionRowFromColumnData(
columns: Column[],
sectionName: Section
): RowInput | null {
let sectionRow: { [key: string]: CellInput } = {}
const sectionRow: { [key: string]: CellInput } = {}
columns.forEach((col) => {
let columnData = col.raw
const columnData = col.raw
if (sectionName === 'head' && columnData?.header) {
sectionRow[col.dataKey] = columnData.header
} else if (sectionName === 'foot' && columnData?.footer) {
Expand All @@ -431,8 +431,8 @@ function createColumns(
return new Column(key, input, index)
})
} else {
let firstRow: RowInput = head[0] || body[0] || foot[0] || []
let columns: Column[] = []
const firstRow: RowInput = head[0] || body[0] || foot[0] || []
const columns: Column[] = []
Object.keys(firstRow)
.filter((key) => key !== '_element')
.forEach((key) => {
Expand Down Expand Up @@ -469,7 +469,7 @@ function cellStyles(
scaleFactor: number,
cellInputStyles: Partial<Styles>
) {
let theme = getTheme(themeName)
const theme = getTheme(themeName)
let sectionStyles
if (sectionName === 'head') {
sectionStyles = styles.headStyles
Expand All @@ -478,23 +478,29 @@ function cellStyles(
} else if (sectionName === 'foot') {
sectionStyles = styles.footStyles
}
let otherStyles = assign(
const otherStyles = assign(
{},
theme.table,
theme[sectionName],
styles.styles,
sectionStyles
)
let columnStyles =
const columnStyles =
styles.columnStyles[column.dataKey] ||
styles.columnStyles[column.index] ||
{}
let colStyles = sectionName === 'body' ? columnStyles : {}
let rowStyles =
const colStyles = sectionName === 'body' ? columnStyles : {}
const rowStyles =
sectionName === 'body' && rowIndex % 2 === 0
? assign({}, theme.alternateRow, styles.alternateRowStyles)
: {}
const defaultStyle = defaultStyles(scaleFactor)
let themeStyles = assign({}, defaultStyle, otherStyles, rowStyles, colStyles)
const themeStyles = assign(
{},
defaultStyle,
otherStyles,
rowStyles,
colStyles
)
return assign(themeStyles, cellInputStyles)
}
Loading

0 comments on commit 60afc49

Please sign in to comment.