Fix multiple print service bugs (#594)

* Fix print service

* Fix printing

* Optimize CSS style and remove deprecated code
This commit is contained in:
Felix Häusler 2018-12-06 14:01:33 +01:00 committed by Ran Luo
parent eb11f110e9
commit 6ec0a215ac
10 changed files with 62 additions and 29 deletions

View File

@ -29,7 +29,8 @@ const handleResponseForExport = async (e, { type, content, pathname, markdown })
let data = content
try {
if (!content && type === 'pdf') {
data = await promisify(win.webContents.printToPDF.bind(win.webContents))({ printBackground: false })
data = await promisify(win.webContents.printToPDF.bind(win.webContents))({ printBackground: true })
removePrintServiceFromWindow(win)
}
if (data) {
await writeFile(filePath, data, extension)
@ -44,8 +45,20 @@ const handleResponseForExport = async (e, { type, content, pathname, markdown })
message: ERROR_MSG
})
}
} else {
// User canceled save dialog
if (type === 'pdf') {
removePrintServiceFromWindow(win)
}
}
}
const handleResponseForPrint = e => {
const win = BrowserWindow.fromWebContents(e.sender)
win.webContents.print({ printBackground: true }, () => {
removePrintServiceFromWindow(win)
})
}
const handleResponseForSave = (e, { id, markdown, pathname, options }) => {
const win = BrowserWindow.fromWebContents(e.sender)
@ -113,6 +126,11 @@ const pandocFile = async pathname => {
}
}
const removePrintServiceFromWindow = win => {
// remove print service content and restore GUI
win.webContents.send('AGANI::print-service-clearup')
}
ipcMain.on('AGANI::save-all', (e, unSavedFiles) => {
Promise.all(unSavedFiles.map(file => handleResponseForSave(e, file)))
.catch(log)
@ -178,6 +196,8 @@ ipcMain.on('AGANI::response-file-save', handleResponseForSave)
ipcMain.on('AGANI::response-export', handleResponseForExport)
ipcMain.on('AGANI::response-print', handleResponseForPrint)
ipcMain.on('AGANI::close-window', e => {
const win = BrowserWindow.fromWebContents(e.sender)
appWindow.forceClose(win)

View File

@ -81,9 +81,9 @@ class Muya {
return this.contentState.history.clearHistory()
}
exportStyledHTML (filename) {
exportStyledHTML (title = '', printOptimization = false) {
const { markdown } = this
return new ExportHtml(markdown).generate(filename)
return new ExportHtml(markdown).generate(title, printOptimization)
}
exportHtml () {

View File

@ -38,20 +38,27 @@ class ExportHtml {
}
})
}
// get html with style
generate (filename = '') {
/**
* Get HTML with style
*
* @param {*} title Page title
* @param {*} printOptimization Optimize HTML and CSS for printing
*/
generate (title = '', printOptimization = false) {
// WORKAROUND: Hide Prism.js style when exporting or printing. Otherwise the background color is white in the dark theme.
const highlightCssStyle = printOptimization ? `@media print { ${highlightCss} }` : highlightCss
const html = this.renderHtml()
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${filename}</title>
<title>${title}</title>
<style>
${githubMarkdownCss}
</style>
<style>
${highlightCss}
${highlightCssStyle}
</style>
<style>
${katexCss}

View File

@ -29,14 +29,11 @@
}
html, body {
font-size: 16px;
background: rgb(45, 45, 45);
}
body {
font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
color: rgb(217, 217, 217);
line-height: 1.6;
background: rgb(45, 45, 45);
}
::-webkit-scrollbar {

View File

@ -29,14 +29,11 @@
}
html, body {
font-size: 16px;
background: rgb(252, 252, 252);
}
body {
font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
color: #303133;
line-height: 1.6;
background: rgb(252, 252, 252);
}
::-webkit-scrollbar {

View File

@ -139,6 +139,7 @@
dispatch('LINTEN_FOR_SET_LINE_ENDING')
dispatch('LISTEN_FOR_NEW_TAB')
dispatch('LISTEN_FOR_CLOSE_TAB')
dispatch('LINTEN_FOR_PRINT_SERVICE_CLEARUP')
dispatch('LINTEN_FOR_EXPORT_SUCCESS')
dispatch('LISTEN_FOR_SET_TEXT_DIRECTION')
dispatch('LISTEN_FOR_TEXT_DIRECTION_MENU')

View File

@ -3,8 +3,8 @@ body article.print-container {
}
@media print {
/* General rules for printing. */
body {
background: transparent none;
html, body {
background: transparent !important;
}
body > div {

View File

@ -215,6 +215,7 @@
bus.$on('undo', this.handleUndo)
bus.$on('redo', this.handleRedo)
bus.$on('export', this.handleExport)
bus.$on('print-service-clearup', this.handlePrintServiceClearup)
bus.$on('paragraph', this.handleEditParagraph)
bus.$on('format', this.handleInlineFormat)
bus.$on('searchValue', this.handleSearch)
@ -367,9 +368,10 @@
},
handlePrint () {
const html = this.editor.exportHtml()
const printer = new Printer(html)
printer.print()
// generate styled HTML with empty title and optimized for printing
const html = this.editor.exportStyledHTML('', true)
this.printer.renderMarkdown(html)
this.$store.dispatch('PRINT_RESPONSE')
},
handleExport (type) {
@ -382,7 +384,8 @@
}
case 'pdf': {
const html = this.editor.exportStyledHTML()
// generate styled HTML with empty title and optimized for printing
const html = this.editor.exportStyledHTML('', true)
this.printer.renderMarkdown(html)
this.$store.dispatch('EXPORT', { type, markdown })
break
@ -390,6 +393,10 @@
}
},
handlePrintServiceClearup () {
this.printer.clearup()
},
handleEditParagraph (type) {
if (type === 'table') {
this.tableChecker = { rows: 4, columns: 3 }

View File

@ -9,12 +9,6 @@ class MarkdownPrint {
printContainer.innerHTML = html
}
print (html) {
this.renderMarkdown(html)
window.print()
this.clearup()
}
clearup () {
if (this.container) {
this.container.remove()

View File

@ -494,6 +494,16 @@ const actions = {
})
},
PRINT_RESPONSE ({ commit }) {
ipcRenderer.send('AGANI::response-print')
},
LINTEN_FOR_PRINT_SERVICE_CLEARUP ({ commit }) {
ipcRenderer.on('AGANI::print-service-clearup', e => {
bus.$emit('print-service-clearup')
})
},
LISTEN_FOR_INSERT_IMAGE ({ commit, state }) {
ipcRenderer.on('AGANI::INSERT_IMAGE', (e, { filename: imagePath, type }) => {
if (!hasKeys(state.currentFile)) return