Skip to content

Commit bbdf96a

Browse files
authored
Refactor cell.js (#2412)
Refactor Cell.js Remove bug in addPage Decrease lgtm alerts and warnings
1 parent a2f580c commit bbdf96a

28 files changed

+1172
-531
lines changed

build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ function renew(code) {
9797
code = code.replace(/\$\{versionID\}/g, version);
9898
code = code.replace('${commitID}', commit);
9999
code = code.replace(/1\.0\.0-trunk/, version + ' ' + date + ':' + whoami);
100-
return code
100+
return code;
101101
}

builder.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
html = '<div class="module">';
6767

6868
html += '<div class="checkbox">';
69-
html += '<input type="checkbox" name="' + name + '" value="' + name +'" />'
69+
html += '<input type="checkbox" name="' + name + '" value="' + name +'" />';
7070
html += '</div>';
7171

7272
html += '<div class="name">';
73-
html += configuration[name].name
73+
html += configuration[name].name;
7474
html += '</div>';
7575

7676
html += '<div class="description">';
@@ -85,7 +85,7 @@
8585
html = '<div class="module header">';
8686

8787
html += '<div class="checkbox">';
88-
html += '<input type="checkbox" name="all" value="all" />'
88+
html += '<input type="checkbox" name="all" value="all" />';
8989
html += '</div>';
9090

9191
html += '<div class="name">';
@@ -154,7 +154,6 @@
154154
}
155155

156156
var responseText = req.responseText;
157-
var responseTextLen = req.responseText.length;
158157
var StringFromCharCode = String.fromCharCode;
159158
if (unicodeCleanUp === true) {
160159
var i = 0;

definitely_typed/index.d.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ declare module 'jspdf' {
338338
}
339339

340340
enum ImageCompression { 'NONE', 'FAST', 'MEDIUM', 'SLOW' }
341-
enum ColorSpace {'DeviceRGB' , 'DeviceGray' , 'DeviceCMYK' , 'CalGray' , 'CalRGB' , 'Lab' , 'ICCBased' , 'Indexed' , 'Pattern' , 'Separation' , 'DeviceN'}
341+
enum ColorSpace { 'DeviceRGB', 'DeviceGray', 'DeviceCMYK', 'CalGray', 'CalRGB', 'Lab', 'ICCBased', 'Indexed', 'Pattern', 'Separation', 'DeviceN' }
342342

343343
interface ImageOptions {
344344
imageData: string | HTMLImageElement | HTMLCanvasElement | Uint8Array;
@@ -391,6 +391,14 @@ declare module 'jspdf' {
391391
y: number;
392392
}
393393

394+
interface CellConfig {
395+
name: string;
396+
prompt: string;
397+
align: 'left' | 'center' | 'right';
398+
padding: number;
399+
width: number;
400+
}
401+
394402
interface jsPDFOptions {
395403
orientation?: 'p' | 'portrait' | 'l' | 'landscape';
396404
unit?: 'pt' | 'px' | 'in' | 'mm' | 'cm' | 'ex' | 'em' | 'pc';
@@ -635,15 +643,13 @@ declare module 'jspdf' {
635643
};
636644

637645
// jsPDF plugin: Cell
638-
setHeaderFunction(func: Function): void;
639-
getTextDimensions(txt: string, options?: any): any;
640-
cellAddPage(): void;
641-
cellInitialize(): void;
646+
setHeaderFunction(func: (jsPDFInstance: jsPDF, pages: number) => number[]): jsPDF;
647+
getTextDimensions(txt: string, options?: any): { x: number; y: number };
648+
cellAddPage(): jsPDF;
642649
cell(x: number, y: number, w: number, h: number, txt: string, ln: number, align: string): jsPDF;
643-
arrayMax(array: any[], comparisonFn?: () => number): number;
644650
table(x: number, y: number, data: any, headers: string[], config: any): jsPDF;
645651
calculateLineHeight(headerNames: string[], columnWidths: number[], model: any[]): number;
646-
setTableHeaderRow(config: any[]): void;
652+
setTableHeaderRow(config: CellConfig[]): void;
647653
printHeaderRow(lineNumber: number, new_page?: boolean): void;
648654

649655
context2d: Context2d;
@@ -652,7 +658,7 @@ declare module 'jspdf' {
652658
outline: Outline;
653659
// jsPDF plugin: fileloading
654660
loadFile(url: string, sync?: true): string;
655-
loadFile(url: string, sync: false, callback: (data: string) => string): void;
661+
loadFile(url: string, sync: false, callback: (data: string) => string): void;
656662

657663
// jsPDF plugin: html
658664
html(src: string | HTMLElement, options?: HTMLOptions): Promise<HTMLWorker>;

examples/html2pdf/acid2.html

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
<script>
103103
function onLoad() {
104104
window.setTimeout (function () {
105-
var pdf = new jsPDF('p', 'pt', 'letter');
106105
document.body.style.width = 400 + 'px';
107106

108107
var pdf = new jsPDF('p', 'pt', 'letter');

examples/images.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@
7676
}
7777
}).appendTo('body');
7878

79-
var option,
80-
img;
79+
var img;
8180

8281
for (img in images) {
8382

84-
option = $('<option>', {
83+
$('<option>', {
8584
text: img.replace(/_/g, " "),
8685
value: img,
8786
}).appendTo(select);

examples/js/basic.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/js/cell.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
var generateData = function (amount) {
3+
var result = [];
4+
var data =
5+
{
6+
coin: "100",
7+
game_group: "GameGroup",
8+
game_name: "XPTO2",
9+
game_version: "25",
10+
machine: "20485861",
11+
vlt: "0"
12+
};
13+
for (var i = 0; i < amount; i += 1) {
14+
data.id = (i + 1).toString();
15+
result.push(Object.assign({}, data));
16+
}
17+
return result;
18+
};
19+
20+
function createHeaders(keys) {
21+
var result = [];
22+
for (var i = 0; i < keys.length; i += 1) {
23+
result.push({
24+
'id' : keys[i],
25+
'name': keys[i],
26+
'prompt': keys[i],
27+
'width': 65,
28+
'align': 'center',
29+
'padding': 0
30+
});
31+
}
32+
return result;
33+
}
34+
35+
36+
37+
var headers = createHeaders(["id", "coin", "game_group", "game_name", "game_version", "machine", "vlt"]);
38+
39+
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' });
40+
doc.table(1, 1, generateData(100), headers, { autoSize: true });

examples/js/editor.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/js/two-page.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var doc = new jsPDF();
22
doc.text('Hello world!', 20, 20);
33
doc.text('This is client-side Javascript, pumping out a PDF.', 20, 30);
4-
doc.addPage('l', 'a6');
4+
doc.addPage('a6', 'l');
55
doc.text('Do you like that?', 20, 20);

examples/node/japanese.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//https://www.freejapanesefont.com/mouhitsu-bold-font-download/
22

33
const fs = require('fs');
4-
const jsPDF = require('../../dist/jspdf.node.min')
4+
const jsPDF = require('../../dist/jspdf.node.min');
55

66
var MouhitsuBold = fs.readFileSync('../../spec/reference/MouhitsuBold.ttf', {encoding: 'latin1'});
77

fontconverter/fontconverter.html

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
</div>
3434
</form>
3535
<script>
36-
window.loadedFile;
37-
window.loadedFileContents;
3836
var opts = {
3937
on: {
4038
load: function (e, file) {

spec/cell.spec.js

+56-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/* global describe, it, expect, jsPDF */
1+
/* global describe, it, expect, comparePdf jsPDF */
22

33
describe('Module: Cell', () => {
44
it('getTextDimensions', () => {
5-
var doc = new jsPDF('p','pt', 'a4');
5+
var doc = new jsPDF('p', 'pt', 'a4');
66
expect(doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 50)).w).toEqual(43.35999999999999)
77
expect(doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 50)).h).toEqual(71.19999999999999)
88
expect(doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 150)).w).toEqual(144.48000000000002)
@@ -13,11 +13,60 @@ describe('Module: Cell', () => {
1313
expect(doc.getTextDimensions("").h).toEqual(0)
1414
expect(doc.getTextDimensions([""]).w).toEqual(0)
1515
expect(doc.getTextDimensions([""]).h).toEqual(0)
16-
expect(function () {doc.getTextDimensions()}).toThrow(new Error('getTextDimensions expects text-parameter to be of type String or an Array of Strings.'));
16+
expect(function () { doc.getTextDimensions() }).toThrow(new Error('getTextDimensions expects text-parameter to be of type String or an Array of Strings.'));
1717
})
18-
it('arrayMax', () => {
19-
var doc = new jsPDF('p','pt', 'a4');
20-
expect(doc.arrayMax([1,2,3,4,5])).toEqual(5);
21-
expect(doc.arrayMax([1,2,3,4,5,4,3,2,1])).toEqual(5);
18+
19+
var generateData = function (amount) {
20+
var result = [];
21+
var data =
22+
{
23+
coin: "100",
24+
game_group: "GameGroup",
25+
game_name: "XPTO2",
26+
game_version: "25",
27+
machine: "20485861",
28+
vlt: "0"
29+
};
30+
for (var i = 0; i < amount; i += 1) {
31+
result.push(data);
32+
}
33+
return result;
34+
};
35+
36+
function createHeaders(keys) {
37+
return keys.map(key => ({
38+
'name': key,
39+
'prompt': key,
40+
'width': 65,
41+
'align': 'center',
42+
'padding': 0
43+
}));
44+
}
45+
46+
var header = createHeaders(["coin", "game_group", "game_name", "game_version", "machine", "vlt"]);
47+
48+
49+
it('cellInitialize', () => {
50+
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' });
51+
doc.cellInitialize();
52+
})
53+
it('table', () => {
54+
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' });
55+
doc.table(1, 1, generateData(100), header);
56+
comparePdf(doc.output(), 'table.pdf')
57+
})
58+
59+
it('table-autoSize', () => {
60+
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' });
61+
doc.table(1, 1, generateData(100), header, {autoSize: true});
62+
comparePdf(doc.output(), 'table-autoSize.pdf')
63+
})
64+
65+
it('table error handling', () => {
66+
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' });
67+
expect(function () {doc.table(1, 1, undefined, header, {autoSize: true})}).toThrow(new Error('No data for PDF table.'));
68+
expect(function () {doc.printHeaderRow(1, false)}).toThrow(new Error('Property tableHeaderRow does not exist.'));
69+
2270
})
2371
});
72+

spec/pages.spec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* global describe, it, jsPDF, comparePdf */
2+
/* global describe, it, jsPDF, comparePdf, expect */
33
/**
44
* Standard spec tests
55
*
@@ -79,4 +79,11 @@ describe('Core: Paging', () => {
7979

8080
comparePdf(doc.output(), '2pages.pdf', 'pages')
8181
})
82+
83+
it('portrait mode and landscape mode should not switch', () => {
84+
const doc = new jsPDF({orientation: 'landscape'})
85+
doc.addPage()
86+
expect(doc.getPageWidth(0)).toEqual(doc.getPageWidth(1));
87+
expect(doc.getPageHeight(0)).toEqual(doc.getPageHeight(1));
88+
})
8289
})
6.97 KB
Loading

spec/reference/table-autoSize.pdf

56.9 KB
Binary file not shown.

spec/reference/table.pdf

57.8 KB
Binary file not shown.

spec/utils/compare.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
/* global XMLHttpRequest, expect, fail */
23
var globalVar = (typeof self !== "undefined" && self || typeof global !== "undefined" && global || typeof window !== "undefined" && window || (Function("return this"))());
34

@@ -75,5 +76,5 @@ globalVar.comparePdf = function (actual, expectedFile, suite, unicodeCleanUp) {
7576
var expected = cleanUpUnicode(resetFile(pdf.replace(/^\s+|\s+$/g, '')));
7677
actual = cleanUpUnicode(resetFile(actual.replace(/^\s+|\s+$/g, '')));
7778

78-
expect(actual.replace(/[\r]/g, '')).toEqual(expected.replace(/[\r]/g, ''))
79+
expect(actual.replace(/[\r]/g, '').split('\n')).toEqual(expected.replace(/[\r]/g, '').split('\n'));
7980
}

0 commit comments

Comments
 (0)