Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
IceEnd committed Aug 9, 2019
2 parents aec8c08 + 8d7f7f0 commit 540cf11
Show file tree
Hide file tree
Showing 62 changed files with 361 additions and 3,674 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
]
},
"test": {
"presets": ["@babel/preset-env"]
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-syntax-dynamic-import",
["@babel/plugin-proposal-class-properties", { "loose": true }],
]
}
},
"plugins": ["@babel/plugin-transform-runtime"]
Expand Down
47 changes: 21 additions & 26 deletions app/main/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import {
setDocumentsPath,
splitFlag,
} from './paths';
import { generateHtml, exportPDF } from './utils/utils';

import uploder from './services/uploader/index';
import { markedToHtml } from '../views/utils/utils';
import schedule from './schedule';
import PDF from './pdf';

import { editorMode } from './config/shortcuts.json';

Expand Down Expand Up @@ -438,9 +437,8 @@ export function eventListener(menus) {
schedule.cancelReleases();
});

ipcMain.on('export-note', (event, args) => {
ipcMain.on('export-note', async (event, args) => {
const { projectName, fileName, type, data } = args;
const folderPath = `${getProjectsPath()}/${projectName}`;
const filePath = `${getProjectsPath()}/${projectName}/${fileName}.md`;
try {
let content;
Expand All @@ -456,9 +454,6 @@ export function eventListener(menus) {
title = 'Export as Markdown';
} else if (type === 'html') {
title = 'Export as Html';
if (!data) {
content = markedToHtml(content, false);
}
} else if (type === 'pdf') {
title = 'Export as PDF';
}
Expand All @@ -474,10 +469,12 @@ export function eventListener(menus) {
if (!reg.test(fname)) {
file += `.${type}`;
}
if (type === 'html' || type === 'pdf') {
content = await generateHtml(content);
}
if (type === 'pdf') {
event.sender.send('async-export-file');
const pdf = new PDF([`${fileName}.md`], folderPath, file, false);
await pdf.start();
await exportPDF(file, content);
event.sender.send('async-export-file-complete');
} else {
fs.writeFileSync(file, content);
Expand All @@ -486,6 +483,7 @@ export function eventListener(menus) {
});
} catch (error) {
console.warn(error);
event.sender.send('async-export-file-complete');
}
});

Expand All @@ -496,39 +494,36 @@ export function eventListener(menus) {
ipcMain.on('export-notebook', async (event, args) => {
const { notebook, type } = args;
try {
event.sender.send('async-export-file');
const folderPath = `${getProjectsPath()}/${notebook}`;
const exportPath = `${DESKTOP_PATH}/${notebook}`;
if (!fs.existsSync(exportPath)) {
fs.mkdirSync(exportPath);
}
if (type === 'md') {
fse.copySync(folderPath, exportPath);
} else if (type === 'pdf') {
const notes = fs.readdirSync(folderPath);
const pdf = new PDF(notes, folderPath, exportPath);
await pdf.start();
} else {
const promiseArr = [];
const notes = fs.readdirSync(folderPath);
if (type === 'pdf') {
event.sender.send('async-export-file');
}
/* eslint-disable no-await-in-loop */
for (const note of notes) {
let content = fs.readFileSync(`${folderPath}/${note}`, {
encoding: 'utf8',
});
content = markedToHtml(content, false);
content = await generateHtml(content);
const name = note.replace(/\.md/ig, '');
promiseArr.push(new Promise((resolve) => {
fs.writeFile(`${exportPath}/${name}.${type}`, content, (err) => {
if (err) {
console.warn(err);
}
resolve('done');
});
}));
const fileName = `${exportPath}/${name}.${type}`;
if (type === 'html') {
fs.writeFileSync(fileName, content);
} else if (type === 'pdf') {
await exportPDF(fileName, content);
}
}
await Promise.all(promiseArr);
}
event.sender.send('async-export-file-complete');
if (type === 'pdf') {
event.sender.send('async-export-file-complete');
}
shell.openExternal(`file://${exportPath}`);
} catch (ex) {
console.warn(ex);
Expand Down
122 changes: 0 additions & 122 deletions app/main/pdf.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/main/services/uploader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import github from './GitHub';
import weibo from './Weibo';
import smms from './SMMS';
import { formatDate } from '../../../views/utils/utils';
import { formatDate } from '../../utils/utils';

function base64Encode(filePath) {
const bitmap = fs.readFileSync(filePath);
Expand Down
109 changes: 109 additions & 0 deletions app/main/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import fs from 'fs';
import { splitFlag } from '../paths';

const TIME_OUT = 5000; // 5s 过期
let generatorSeed = 0;
let pdfSeed = 0;

const tempPath = app.getPath('temp');

function printPDF(win, file, tempFile, resolve) {
win.webContents.printToPDF({
pageSize: 'A4',
printBackground: true,
}, (err, pdfData) => {
win.removeAllListeners('did-finish-load');
win.removeAllListeners('did-fail-load');
win.close(); // 销毁window
if (err) {
throw err;
}
fs.writeFileSync(file, pdfData);
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
}
pdfSeed++;
resolve('done');
});
}

// 导出为PDF
export function exportPDF(filePath, content) {
const tempFile = `${tempPath}${splitFlag}yosoro_pdf_${pdfSeed}.html`;
fs.writeFileSync(tempFile, content); // 写入临时html文件
let windowToPDF = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
},
});
windowToPDF.loadURL(`file://${tempFile}`);
return new Promise((resolve) => {
let timer = setTimeout(() => {
console.warn('waiting time over');
printPDF(windowToPDF, filePath, tempFile, resolve);
}, 10000);
windowToPDF.webContents.once('did-finish-load', () => {
clearTimeout(timer);
timer = null;
printPDF(windowToPDF, filePath, tempFile, resolve);
});
windowToPDF.webContents.once('did-fail-load', () => {
clearTimeout(timer);
timer = null;
windowToPDF.removeAllListeners('did-finish-load');
windowToPDF.removeAllListeners('did-fail-load');
windowToPDF.destroy();
windowToPDF = null;
resolve('fail');
});
});
}

export function generateHtml(content) {
let timer = null;
const webContents = BrowserWindow.getAllWindows()[0].webContents;
return new Promise((resolve, reject) => {
webContents.send('generate-html', {
seed: generatorSeed,
content,
});
ipcMain.once(`return-generate-html-${generatorSeed}`, (event, payload) => {
clearTimeout(timer);
timer = null;
if (payload.success) {
resolve(payload.html);
} else {
reject(payload.msg);
}
});
// 设置过期回调
timer = setTimeout(() => {
reject('time out.');
}, TIME_OUT);
generatorSeed++;
});
}

function formatNumber(number) {
if (number < 10) {
return `0${number}`;
}
return `${number}`;
}


export function formatDate(date, type = 'normal') {
const newDate = new Date(date);
const year = newDate.getFullYear();
const month = formatNumber(newDate.getMonth() + 1);
const day = formatNumber(newDate.getDate());
const hour = formatNumber(newDate.getHours());
const minutes = formatNumber(newDate.getMinutes());
const seconds = formatNumber(newDate.getSeconds());
if (type === 'normal') {
return `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
}
return `${year}${month}${day}${hour}${minutes}${seconds}`;
}
36 changes: 0 additions & 36 deletions app/main/webview/webview.html

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/component/editor/Markdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default class Markdown extends Component {
latestDate: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
content: PropTypes.string.isRequired,
html: PropTypes.string.isRequired,
status: PropTypes.number.isRequired,
start: PropTypes.number.isRequired,
}).isRequired,
Expand Down Expand Up @@ -65,15 +64,6 @@ export default class Markdown extends Component {
start={start}
editorMode={editorMode}
/>
{/* <Preview
html={html}
drag={drag}
editorMode={editorMode}
editorWidth={editorWidth}
editorWidthValue={editorWidthValue}
fontSize={previewFontSize}
ref={node => (this.preview = node)}
/> */}
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion app/views/component/note/Files.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class Files extends Component {
this.handleUpload();
});
// 收集将要导出的笔记的信息
ipcRenderer.on('export-get-note-info', (event, type) => {
ipcRenderer.on('export-get-note-info', async (event, type) => {
const { projectName } = this.props;
const { name } = this.state.contextNote;
ipcRenderer.send('export-note', {
Expand Down
Loading

0 comments on commit 540cf11

Please sign in to comment.