Skip to content

Commit

Permalink
Desktop: Fixes #1039: Always print or export to PDF using light theme
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Dec 16, 2018
1 parent d6eacb2 commit 982c982
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
97 changes: 56 additions & 41 deletions ElectronClient/app/gui/NoteText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -911,32 +911,35 @@ class NoteTextComponent extends React.Component {
async doCommand(command) {
if (!command || !this.state.note) return;

let commandProcessed = true;
let fn = null;

if (command.name === 'exportPdf' && this.webview_) {
this.commandSavePdf();
fn = this.commandSavePdf;
} else if (command.name === 'print' && this.webview_) {
this.webview_.print();
fn = this.commandPrint;
} else if (command.name === 'textBold') {
this.commandTextBold();
fn = this.commandTextBold;
} else if (command.name === 'textItalic') {
this.commandTextItalic();
fn = this.commandTextItalic;
} else if (command.name === 'insertDateTime' ) {
this.commandDateTime();
fn = this.commandDateTime;
} else if (command.name === 'commandStartExternalEditing') {
this.commandStartExternalEditing();
fn = this.commandStartExternalEditing;
} else if (command.name === 'showLocalSearch') {
this.commandShowLocalSearch();
} else {
commandProcessed = false;
fn = this.commandShowLocalSearch;
}

if (commandProcessed) {
this.props.dispatch({
type: 'WINDOW_COMMAND',
name: null,
});
}
if (!fn) return;

this.props.dispatch({
type: 'WINDOW_COMMAND',
name: null,
});

requestAnimationFrame(() => {
fn = fn.bind(this);
fn();
});
}

commandShowLocalSearch() {
Expand Down Expand Up @@ -994,42 +997,54 @@ class NoteTextComponent extends React.Component {
});
}

commandSavePdf() {
const path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf']}],
defaultPath: safeFilename(this.state.note.title),
});
printTo_(target, options) {
const previousBody = this.state.note.body;
const tempBody = "# " + this.state.note.title + "\n\n" + previousBody;

if (path) {
// Temporarily add a <h2> title in the webview
const newHtml = this.insertHtmlHeading_(this.lastSetHtml_, this.state.note.title);
this.webview_.send('setHtml', newHtml);
const previousTheme = Setting.value('theme');
Setting.setValue('theme', Setting.THEME_LIGHT);
this.lastSetHtml_ = '';
this.updateHtml(tempBody);
this.forceUpdate();

const restoreSettings = () => {
Setting.setValue('theme', previousTheme);
this.lastSetHtml_ = '';
this.updateHtml(previousBody);
this.forceUpdate();
}

setTimeout(() => {
setTimeout(() => {
if (target === 'pdf') {
this.webview_.printToPDF({}, (error, data) => {
restoreSettings();

if (error) {
bridge().showErrorMessageBox(error.message);
} else {
shim.fsDriver().writeFile(path, data, 'buffer');
shim.fsDriver().writeFile(options.path, data, 'buffer');
}

// Refresh the webview, restoring the previous content
this.lastSetHtml_ = '';
this.forceUpdate();
});
}, 100);
} else if (target === 'printer') {
this.webview_.print();
restoreSettings();
}
}, 100);
}

commandSavePdf() {
const path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf']}],
defaultPath: safeFilename(this.state.note.title),
});

if (path) {
this.printTo_('pdf', { path: path });
}
}

insertHtmlHeading_(s, heading) {
const tag = 'h2';
const marker = '<!-- START_OF_DOCUMENT -->'
let splitStyle = s.split(marker);
const index = splitStyle.length > 1 ? 1 : 0;
let toInsert = escapeHtml(heading);
toInsert = '<' + tag + '>' + toInsert + '</' + tag + '>';
splitStyle[index] = toInsert + splitStyle[index];
return splitStyle.join(marker);
commandPrint() {
this.printTo_('printer');
}

async commandStartExternalEditing() {
Expand Down
1 change: 0 additions & 1 deletion ElectronClient/app/gui/note-viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<body id="body">
<div id="hlScriptContainer"></div>
<div id="markScriptContainer"></div>
<!-- START_OF_DOCUMENT -->
<div id="content" ondragstart="return false;" ondrop="return false;"></div>

<script>
Expand Down

0 comments on commit 982c982

Please sign in to comment.