Skip to content

Commit

Permalink
[WIP] V4 (#427)
Browse files Browse the repository at this point in the history
* Reconsider the internal data types and the template types (#389)

* Change SchemaForUI's data and readOnlyValue to content

* Fix bug

* Remove sampledata and instead always use content. & Use content regardless of readOnly or not. & Eliminate defaultValue and replace it with always using content.

* Change website template

* Fix placeholder bug

* Change generator test template

* Change content to optional

* Move getInputFromTemplate to common

* Remove columns

* Fix test

* move dynamictable.excalidraw

* remove idea dir

* New basePdf type and Support adding new pages to template (#394)

* Minor fix

* IMPL Support adding new pages to template #111

* Fix test

* Minor fix

* IMPL padding behavior

* Update snapshot

* Minor fix

* Update snapshot

* add i18n

* remove option from BlankPdf.padding

* Minor fix

* Minor fix

* format

* Add changeSchemas unit test (#403)

* Minor fix

* Add a version number to pdfme template from V4 onwards (#404)

* Impl

* Minor fix

* Padding move width (#407)

* Refactor position and size handling in helper.ts

* Fix bug

* Add DynamicTable Schema #332 (#408)

* [tmp] add some comment

* Update snapshot

* Add deploy-table script to package.json

* add new template for playground

* bug fix for form

* fix cell editing bug

* Fix Adding rows doesn't change the overall height of the table

* fix padding problem

* Fix build error

* Fix bug

* Minor fix

* Fix New lines not reflecting correctly

* minor fix

* Minor fix

* Change tableStyles def

* add i18n

* small bugfix

* FIx some TODO

* Remove japanese comment

* Minor fix

* Fix infinity loom for form

* fix save inputs bug

* fix window resize bug

* add skip for failing test and update snapshot

* Minor fix

* add presets for playground

* Minor fix

* Reconsider the internal data types and the template types (#389)

* Change SchemaForUI's data and readOnlyValue to content

* Fix bug

* Remove sampledata and instead always use content. & Use content regardless of readOnly or not. & Eliminate defaultValue and replace it with always using content.

* Change website template

* Fix placeholder bug

* Change generator test template

* Change content to optional

* Move getInputFromTemplate to common

* Remove columns

* Fix test

* move dynamictable.excalidraw

* remove idea dir

* New basePdf type and Support adding new pages to template (#394)

* Minor fix

* IMPL Support adding new pages to template #111

* Fix test

* Minor fix

* IMPL padding behavior

* Update snapshot

* Minor fix

* Update snapshot

* add i18n

* remove option from BlankPdf.padding

* Minor fix

* Minor fix

* format

* Add changeSchemas unit test (#403)

* Minor fix

* Add a version number to pdfme template from V4 onwards (#404)

* Impl

* Minor fix

* Padding move width (#407)

* Refactor position and size handling in helper.ts

* Fix bug

* Add DynamicTable Schema #332 (#408)

* [tmp] add some comment

* Update snapshot

* Add deploy-table script to package.json

* add new template for playground

* bug fix for form

* fix cell editing bug

* Fix Adding rows doesn't change the overall height of the table

* fix padding problem

* Fix build error

* Fix bug

* Minor fix

* Fix New lines not reflecting correctly

* minor fix

* Minor fix

* Change tableStyles def

* add i18n

* small bugfix

* FIx some TODO

* Remove japanese comment

* Minor fix

* Fix infinity loom for form

* fix save inputs bug

* fix window resize bug

* add skip for failing test and update snapshot

* Minor fix

* add presets for playground

* Minor fix

* Minor fix

* Update imports and fix font rendering

* Add a Left Sidebar for Placing Schemas #400 (#452)

* Remove original 'Add new field' Button

* add icon

* FIx drag position bug

* Minor fix

* Fix sidebar position

* Update snapshot

* Minor fix

* Update packages/ui/src/components/Designer/index.tsx

Co-authored-by: Peter Ward <pete@pennyblack.io>

---------

Co-authored-by: Peter Ward <pete@pennyblack.io>

* Fix test

* Improve left sidebar icon drop placement accuracy (#454)

* Fix Spanish translations for v4 (#463)

* Fix #431

* V4 (#467)

* feat: add french language

* feat: relecture

* feat: add french language

---------

Co-authored-by: regis <regis>

* Add French language option to playground

* rename table export name to tableBeta

---------

Co-authored-by: Peter Ward <pete@pennyblack.io>
Co-authored-by: Iker Diez <32014358+ikerd@users.noreply.github.com>
Co-authored-by: Régis <regis.charnace@leandco.fr>
  • Loading branch information
4 people committed Apr 11, 2024
1 parent 995303d commit a3a2b0b
Show file tree
Hide file tree
Showing 141 changed files with 16,108 additions and 7,156 deletions.
269 changes: 263 additions & 6 deletions packages/common/__tests__/helper.test.ts
@@ -1,7 +1,16 @@
import { readFileSync } from 'fs';
import * as path from 'path';
import { mm2pt, pt2mm, pt2px, checkFont, checkPlugins, isHexValid } from '../src/helper';
import { PT_TO_PX_RATIO, BLANK_PDF, Template, Font, Plugins } from '../src';
import {
mm2pt,
pt2mm,
pt2px,
checkFont,
checkPlugins,
isHexValid,
calculateDiffMap,
normalizePositionsAndPageBreak,
} from '../src/helper';
import { PT_TO_PX_RATIO, BLANK_PDF, Template, Font, Plugins, Schema, CommonOptions } from '../src';

const sansData = readFileSync(path.join(__dirname, `/assets/fonts/SauceHanSansJP.ttf`));
const serifData = readFileSync(path.join(__dirname, `/assets/fonts/SauceHanSerifJP.ttf`));
Expand All @@ -16,13 +25,15 @@ const getTemplate = (): Template => ({
schemas: [
{
a: {
content: 'a',
type: 'text',
fontName: 'SauceHanSansJP',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
b: {
content: 'b',
type: 'text',
position: { x: 0, y: 0 },
width: 100,
Expand Down Expand Up @@ -85,12 +96,14 @@ describe('checkFont test', () => {
schemas: [
{
a: {
content: 'a',
type: 'text',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
b: {
content: 'b',
type: 'text',
position: { x: 0, y: 0 },
width: 100,
Expand Down Expand Up @@ -171,13 +184,15 @@ Check this document: https://pdfme.com/docs/custom-fonts#about-font-type`
{
a: {
type: 'text',
content: 'a',
fontName: 'SauceHanSansJP2',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
b: {
type: 'text',
content: 'b',
position: { x: 0, y: 0 },
width: 100,
height: 100,
Expand All @@ -204,13 +219,15 @@ Check this document: https://pdfme.com/docs/custom-fonts`
{
a: {
type: 'text',
content: 'a',
fontName: 'SauceHanSansJP2',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
b: {
type: 'text',
content: 'b',
fontName: 'SauceHanSerifJP2',
position: { x: 0, y: 0 },
width: 100,
Expand Down Expand Up @@ -239,17 +256,27 @@ describe('checkPlugins test', () => {
ui: async () => {},
propPanel: {
schema: {},
defaultValue: '',
defaultSchema: { type: 'myText', position: { x: 0, y: 0 }, width: 100, height: 100 },
defaultSchema: {
type: 'myText',
content: '',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
},
},
myImage: {
pdf: async () => {},
ui: async () => {},
propPanel: {
schema: {},
defaultValue: '',
defaultSchema: { type: 'myImage', position: { x: 0, y: 0 }, width: 100, height: 100 },
defaultSchema: {
type: 'myImage',
content: '',
position: { x: 0, y: 0 },
width: 100,
height: 100,
},
},
},
};
Expand Down Expand Up @@ -322,3 +349,233 @@ describe('checkPlugins test', () => {
}
});
});

describe.skip('getDynamicTemplate test', () => {
const options = { font: getSampleFont() };
const _cache = new Map();
const input = {};
const modifyTemplate = async (arg: {
template: Template;
input: Record<string, string>;
_cache: Map<any, any>;
options: CommonOptions;
}) => Promise.resolve(arg.template);
const getDynamicHeight = (_: string, args: { schema: Schema }) => {
const { schema } = args;
if (schema.type === 'test') return Promise.resolve(schema.height + 100);
return Promise.resolve(schema.height);
};
const generateTemplateConfig = (template: Template) => ({
template,
input,
_cache,
options,
modifyTemplate,
getDynamicHeight,
});

const getTemplateForDynamicTemplate = () => {
const template = getTemplate();
template.basePdf = { width: 210, height: 297, padding: [10, 10, 10, 10] };
const schema = template.schemas[0];
const schemaA = schema.a;
schemaA.position = { x: 0, y: 50 };
schemaA.height = 10;
const schemaB = schema.b;
schemaB.position = { x: 0, y: 75 };
schemaB.height = 10;
return template;
};

const getSingleDynamicTemplate = () => {
const template = getTemplateForDynamicTemplate();
const schema = template.schemas[0];
schema.test = { type: 'test', position: { x: 0, y: 10 }, width: 100, height: 10 };
return template;
};

const getMultiDynamicTemplate = () => {
const template = getTemplateForDynamicTemplate();
const schema = template.schemas[0];
schema.test = { type: 'test', position: { x: 0, y: 10 }, width: 100, height: 10 };
schema.test2 = { type: 'test', position: { x: 0, y: 20 }, width: 100, height: 10 };
return template;
};

describe('calculateDiffMap test', () => {
test('single dynamic schema', async () => {
const template = getSingleDynamicTemplate();
const tableConfig = generateTemplateConfig(template);

const diffMap = await calculateDiffMap(tableConfig);
expect(diffMap).toEqual(new Map([[20, 100]]));
});

test('multi dynamic schemas', async () => {
const template = getMultiDynamicTemplate();
const tableConfig = generateTemplateConfig(template);

const diffMap = await calculateDiffMap(tableConfig);
expect(diffMap).toEqual(
new Map([
[20, 100],
[130, 200],
])
);
});
});

// TODO Re-verify if the correct tests are written and revise the implementation of normalizePositionsAndPageBreak to pass the tests
describe('normalizePositionsAndPageBreak test', () => {
test('single dynamic schema', () => {
const template = getTemplateForDynamicTemplate();
const diffMap = new Map([[60, 100]]);
const newTemplate = normalizePositionsAndPageBreak(template, diffMap);
expect(newTemplate).toEqual({
basePdf: template.basePdf,
schemas: [
{
a: {
content: 'a',
type: 'text',
fontName: 'SauceHanSansJP',
// y: 50->50
position: { x: 0, y: 50 },
width: 100,
height: 10,
},
b: {
content: 'b',
type: 'text',
// y: 75->175
position: { x: 0, y: 175 },
width: 100,
height: 10,
},
},
],
});
});

test('single dynamic schema (page break)', () => {
const template = getTemplateForDynamicTemplate();
const diffMap = new Map([[60, 300]]);
const newTemplate = normalizePositionsAndPageBreak(template, diffMap);
expect(newTemplate).toEqual({
basePdf: template.basePdf,
schemas: [
{
a: {
content: 'a',
type: 'text',
fontName: 'SauceHanSansJP',
// y: 50->50
position: { x: 0, y: 50 },
width: 100,
height: 10,
},
},
{
b: {
content: 'b',
type: 'text',
// schema y: 75 + 300
// page: 297 - (10 + 10)
// (75 + 300) - (297 - (10 + 10)) = 98
// y: 75->98
position: { x: 0, y: 98 },
width: 100,
height: 10,
},
},
],
});
});

test('multi dynamic schemas', () => {
const template = getTemplateForDynamicTemplate();
const diffMap = new Map([
[45, 10],
[65, 30],
]);
const newTemplate = normalizePositionsAndPageBreak(template, diffMap);
expect(newTemplate).toEqual({
basePdf: template.basePdf,
schemas: [
{
a: {
content: 'a',
type: 'text',
fontName: 'SauceHanSansJP',
// y: 50->60
position: { x: 0, y: 60 },
width: 100,
height: 10,
},
b: {
content: 'b',
type: 'text',
// y: 75->105
position: { x: 0, y: 105 },
width: 100,
height: 10,
},
},
],
});
});

test('multi dynamic schemas (page break)', () => {
const template = getTemplateForDynamicTemplate();
const diffMap = new Map([
[45, 100],
[65, 300],
]);
const newTemplate = normalizePositionsAndPageBreak(template, diffMap);
expect(newTemplate).toEqual({
basePdf: template.basePdf,
schemas: [
{
a: {
content: 'a',
type: 'text',
fontName: 'SauceHanSansJP',
// y: 50->150
position: { x: 0, y: 150 },
width: 100,
height: 10,
},
},
{
b: {
content: 'b',
type: 'text',
// schema y: 75 + 300
// page: 297 - (10 + 10)
// (75 + 300) - (297 - (10 + 10)) = 98
// y: 75->98
position: { x: 0, y: 98 },
width: 100,
height: 10,
},
},
],
});
});

test('multi dynamic schemas (page break 2 pages)', () => {
const template = getTemplateForDynamicTemplate();
const diffMap = new Map([
[45, 300],
[65, 300],
]);
const newTemplate = normalizePositionsAndPageBreak(template, diffMap);
expect(newTemplate).toEqual({
basePdf: template.basePdf,
schemas: [
// TODO Write test
],
});
});
});
});
1 change: 1 addition & 0 deletions packages/common/src/constants.ts
@@ -1,3 +1,4 @@
export const PDFME_VERSION = '4-dev';
export const PT_TO_PX_RATIO = 1.333;
export const PT_TO_MM_RATIO = 0.3528;
export const MM_TO_PT_RATIO = 2.8346; // https://www.ddc.co.jp/words/archives/20090701114500.html
Expand Down

0 comments on commit a3a2b0b

Please sign in to comment.