Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filename input #20

Merged
merged 6 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
"react-sizeme": "^3.0.1",
"regenerator-runtime": "^0.13.5",
"source-map-support": "^0.5.19",
"unidecode-plus": "^1.0.4",
"xlsx": "^0.17.0"
},
"devEngines": {
Expand Down
58 changes: 54 additions & 4 deletions src/components/merge/MailMerge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
const [outputPdf, setOutputPdf] = useState('');
const [smtpValid, setSmtpValid] = useState(false);
const [tab, setTab] = useState('local');
const [filename, setFilename] = useState('');

const tagSettings = {
pattern: /@/,
Expand Down Expand Up @@ -65,6 +66,10 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
setBody(e.detail.value);
};

const handleChangeFilename = (e: any) => {
setFilename(e.detail.value);
};

const handleChooseFile = () => {
const name = 'PDF file';
const extensions = ['pdf'];
Expand All @@ -76,20 +81,21 @@ const MailMerge = ({ configPath }: MailMergeProps) => {

const handleSendEmail = async () => {
setSending(true);
const conf = { ...pdfConfig, filename };
await ipcRenderer.invoke(
'send-email',
fromEmail,
emailIndex,
subject,
body,
pdfConfig
conf
);
setSending(false);
};

const handleSavePdf = async () => {
setSaving(true);
const conf = { ...pdfConfig, combinePdf, outputPdf };
const conf = { ...pdfConfig, combinePdf, outputPdf, filename };
await ipcRenderer.invoke('save-pdf', conf);
setSaving(false);
};
Expand Down Expand Up @@ -117,7 +123,7 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
<button
type="button"
onClick={() => setTab('local')}
className={`inline-flex outline-none appearance-none items-center px-4 py-4 text-sm font-medium text-center border-b-2 border-transparent rounded-t-lg group ${
className={`inline-flex outline-none appearance-none items-center px-4 pt-4 pb-2 text-sm font-medium text-center border-b-2 border-transparent rounded-t-lg group ${
tab === 'email'
? 'text-gray-500 hover:text-gray-600 hover:border-gray-300'
: 'text-blue-600 border-blue-600'
Expand All @@ -131,7 +137,7 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
<button
type="button"
onClick={() => setTab('email')}
className={`inline-flex outline-none appearance-none items-center px-4 py-4 text-sm font-medium text-center border-b-2 rounded-t-lg active group ${
className={`inline-flex outline-none appearance-none items-center px-4 pt-4 pb-2 text-sm font-medium text-center border-b-2 rounded-t-lg active group ${
tab === 'local'
? 'text-gray-500 hover:text-gray-600 hover:border-gray-300'
: 'text-blue-600 border-blue-600'
Expand Down Expand Up @@ -189,6 +195,29 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
</label>
) : null}
</section>

{!combinePdf && (
<div className="flex flex-col space-y-2">
<section className="flex items-center justify-between space-y-2">
<p className="font-medium">Filename:</p>
<small className="text-xs text-right opacity-70">
Type @ to insert column values into the filename
</small>
</section>
<Tags
placeholder="Filename"
InputMode="textarea"
onChange={handleChangeFilename}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
settings={tagSettings}
whitelist={headers.map(({ index, label }) => ({
id: index,
value: label,
}))}
/>
</div>
)}
</section>

<section className="flex flex-col items-start justify-center space-y-8">
Expand Down Expand Up @@ -308,6 +337,27 @@ const MailMerge = ({ configPath }: MailMergeProps) => {
}))}
/>
</div>

<div className="flex flex-col space-y-2">
<section className="flex items-center justify-between space-y-2">
<p className="font-medium">Filename:</p>
<small className="text-xs text-right opacity-70">
Type @ to insert column values into the filename
</small>
</section>
<Tags
placeholder="Filename"
InputMode="textarea"
onChange={handleChangeFilename}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
settings={tagSettings}
whitelist={headers.map(({ index, label }) => ({
id: index,
value: label,
}))}
/>
</div>
</section>

<section className="flex flex-col items-start justify-center mb-10 space-y-8">
Expand Down
1 change: 1 addition & 0 deletions src/components/pdf/PdfEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface RenderPdfState {
canvasData?: Record<number, CanvasObjects>;
formData?: Record<string, number>;
configPath?: string;
filename?: string;
}
interface FieldType {
type: string;
Expand Down
92 changes: 45 additions & 47 deletions src/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,20 @@ const savePdf = async (params: RenderPdfState) => {
return;
}

const {
pdfFile,
excelFile,
combinePdf,
canvasData,
formData,
outputPdf,
} = params;

try {
const created = await renderPdf(
outputPdf,
pdfFile,
excelFile,
getRowsLimit(),
combinePdf,
writeFile,
(page, total) =>
const created = await renderPdf({
output: params.outputPdf,
pdfFile: params.pdfFile,
excelFile: params.excelFile,
rowsLimit: getRowsLimit(),
combinePdf: params.combinePdf,
saveFileFunc: writeFile,
updateProgressFunc: (page, total) =>
mailMergeWindow?.webContents.send('save-progress', { page, total }),
canvasData,
formData
);
canvasData: params.canvasData,
formData: params.formData,
filenameTemplate: params.filename,
});

if (created > 0) {
if (Notification.isSupported()) {
Expand Down Expand Up @@ -294,24 +286,22 @@ const previewPdf = async (params: RenderPdfState) => {
return;
}

const { pdfFile, excelFile, canvasData, formData } = params;

try {
const output = path.join(
app.getPath('temp'),
`preview-${path.basename(pdfFile)}`
`preview-${path.basename(params.pdfFile)}`
);
await renderPdf(
await renderPdf({
output,
pdfFile,
excelFile,
1,
true,
writeFile,
() => {},
canvasData,
formData || {}
);
pdfFile: params.pdfFile,
excelFile: params.excelFile,
rowsLimit: 1,
combinePdf: true,
saveFileFunc: writeFile,
updateProgressFunc: () => {},
canvasData: params.canvasData,
formData: params.formData || {},
});
openPdf(output);
} catch (e) {
dialog.showErrorBox('Preview failed', e.message);
Expand Down Expand Up @@ -369,7 +359,7 @@ const sendMailFunc = (
emailIndex: number,
subjectTemplate: string,
bodyTemplate: string
) => async (fileName: string, buffer: Uint8Array, rowData?: RowMap) => {
) => async (filename: string, buffer: Uint8Array, rowData?: RowMap) => {
const config = store.get('smtp-config');

const transporter = nodemailer.createTransport({
Expand Down Expand Up @@ -397,7 +387,7 @@ const sendMailFunc = (
html: text,
attachments: [
{
filename: path.basename(fileName),
filename: path.basename(filename),
content: buffer,
},
],
Expand Down Expand Up @@ -425,21 +415,29 @@ const sendPdfMail = async (
bodyTemplate: string,
params: RenderPdfState
) => {
const { pdfFile, excelFile, canvasData, formData } = params;
try {
const output = path.join(app.getPath('temp'), path.basename(pdfFile));
const output = path.join(
app.getPath('temp'),
path.basename(params.pdfFile)
);

const created = await renderPdf(
const created = await renderPdf({
output,
pdfFile,
excelFile,
getRowsLimit(),
false,
sendMailFunc(fromEmail, emailIndex, subjectTemplate, bodyTemplate),
emailProgressFunc(emailIndex),
canvasData,
formData
);
pdfFile: params.pdfFile,
excelFile: params.excelFile,
rowsLimit: getRowsLimit(),
combinePdf: false,
saveFileFunc: sendMailFunc(
fromEmail,
emailIndex,
subjectTemplate,
bodyTemplate
),
updateProgressFunc: emailProgressFunc(emailIndex),
canvasData: params.canvasData,
formData: params.formData,
filenameTemplate: params.filename,
});

if (created > 0) {
if (Notification.isSupported()) {
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "plainmerge",
"productName": "PlainMerge",
"version": "0.2.1",
"version": "0.3.0",
"description": "Offline PDF mail merger",
"main": "./main.prod.js",
"author": {
Expand Down
Loading