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

Enh/Improve email printing #9669

Closed
wants to merge 8 commits into from
Closed
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
89 changes: 89 additions & 0 deletions src/components/MenuEnvelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@
</template>
{{ t('mail', 'View source') }}
</ActionButton>
<ActionButton :close-after-click="true"
@click="onPrint">
<template #icon>
<PrinterIcon :size="20" />
</template>
{{ t('mail', 'Print message') }}
</ActionButton>
<ActionLink :close-after-click="true"
:href="exportMessageLink">
<template #icon>
Expand Down Expand Up @@ -238,6 +245,7 @@
import { mailboxHasRights } from '../util/acl.js'
import { generateUrl } from '@nextcloud/router'
import InformationIcon from 'vue-material-design-icons/Information.vue'
import PrinterIcon from 'vue-material-design-icons/Printer.vue'
import ImportantIcon from './icons/ImportantIcon.vue'
import OpenInNewIcon from 'vue-material-design-icons/OpenInNew.vue'
import PlusIcon from 'vue-material-design-icons/Plus.vue'
Expand All @@ -254,7 +262,9 @@
import AlarmIcon from 'vue-material-design-icons/Alarm.vue'
import logger from '../logger.js'
import moment from '@nextcloud/moment'
import { shortRelativeDatetime } from '../util/shortRelativeDatetime.js'

Check failure on line 265 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'shortRelativeDatetime' is defined but never used
import { mapGetters } from 'vuex'
import { translate as t } from '@nextcloud/l10n'

export default {
name: 'MenuEnvelope',
Expand All @@ -281,6 +291,7 @@
ImportantIcon,
TaskIcon,
AlarmIcon,
PrinterIcon,
},
props: {
envelope: {
Expand Down Expand Up @@ -558,6 +569,84 @@
setCustomSnooze() {
this.onSnooze(this.customSnoozeDateTime.valueOf())
},
async onPrint() {
await this.$emit('print')

const printStyles = `
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Cantarell, Ubuntu, 'Helvetica Neue', Arial, 'Noto Color Emoji', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
};
`

let combinedContent = ''
const messageFrames = document.querySelectorAll('iframe.message-frame')

messageFrames.forEach(frame => {
const doc = frame.contentDocument || frame.contentWindow.document
combinedContent += doc.body.innerHTML
})

const frame = document.createElement('iframe')
frame.name = 'printFrame'
frame.style.position = 'absolute'
frame.style.top = '-1000000px'
document.body.appendChild(frame)
const frameDoc = frame.contentWindow ? frame.contentWindow : frame.contentDocument.document ? frame.contentDocument.document : frame.contentDocument
frameDoc.document.open()
frameDoc.document.write('<html><head><style>')
frameDoc.document.write(printStyles)
frameDoc.document.write('</style></head><body>')
frameDoc.document.write(document.querySelector('#mail-thread-header h2').outerHTML)

if (this.envelope.from.length) {
let fromString = '<strong>' + t('mail', 'From') + ':</strong> '
this.envelope.from.forEach((contact) => {
fromString += ` ${this.sanitize(contact.label)} <em>${this.sanitize(contact.email)}</em>`
})
frameDoc.document.write(fromString + '<br>')
}

if (this.envelope.to.length) {
let toString = '<strong>' + t('mail', 'Cc') + ':</strong> '
this.envelope.to.forEach((contact) => {
toString += ` ${this.sanitize(contact.label)} <em>${this.sanitize(contact.email)}</em>`
})
frameDoc.document.write(toString + '<br>')
}

if (this.envelope.bcc.length) {
let bccString = '<strong>' + t('mail', 'Bcc') + ':</strong> '
this.envelope.bcc.forEach((contact) => {
bccString += ` ${this.sanitize(contact.label)} <em>${this.sanitize(contact.email)}</em>`
})
frameDoc.document.write(bccString + '<br>')
}
const momentDate = moment(this.envelope.dateInt * 1000)

frameDoc.document.write(`<h4>${momentDate.format('H:mm MMM D, YYYY')}</h4>`)
frameDoc.document.write(combinedContent)
frameDoc.document.write('</body></html>')
frameDoc.document.close()

setTimeout(function() {
window.frames.printFrame.focus()
window.frames.printFrame.print()
document.body.removeChild(frame)
}, 500)
},

sanitize(string) {
const map = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
"/": '&#x2F;',

Check failure on line 645 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Strings must use singlequote
};

Check failure on line 646 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
const reg = /[&<>"'/]/ig;

Check failure on line 647 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
return string.replace(reg, (match)=>(map[match]));

Check failure on line 648 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing space before =>

Check failure on line 648 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing space after =>

Check failure on line 648 in src/components/MenuEnvelope.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
},
},
}
</script>
Expand Down
32 changes: 4 additions & 28 deletions src/components/Thread.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
:expanded="expandedThreads.includes(env.databaseId)"
:full-height="thread.length === 1"
@delete="$emit('delete', env.databaseId)"
@print="printSetup"
@move="onMove(env.databaseId)"
@toggle-expand="toggleExpand(env.databaseId)" />
</template>
Expand Down Expand Up @@ -309,6 +310,9 @@ export default {
}
}
},
printSetup(databaseId) {
this.expandedThreads = [databaseId]
},
},
}
</script>
Expand Down Expand Up @@ -429,34 +433,6 @@ export default {
}
}

@media print {
#mail-thread-header-fields {
position: relative;
}
.app-content-details,
.splitpanes__pane-details {
max-width: unset !important;
width: 100% !important;
}
#header,
.app-navigation,
#reply-composer,
#forward-button,
#mail-message-has-blocked-content,
.app-content-list,
.message-composer,
.splitpanes__pane-list,
.mail-message-attachments {
display: none !important;
}
.app-content {
margin-left: 0 !important;
}
.mail-message-body {
margin-bottom: 0 !important;
}
}

.message-source {
font-family: monospace;
white-space: pre-wrap;
Expand Down
3 changes: 2 additions & 1 deletion src/components/ThreadEnvelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@
:with-show-source="true"
:more-actions-open.sync="moreActionsOpen"
@reply="onReply"
@delete="$emit('delete',envelope.databaseId)"
@delete="$emit('delete', envelope.databaseId)"
@print="$emit('print', envelope.databaseId)"
@show-source-modal="onShowSourceModal"
@open-tag-modal="onOpenTagModal"
@open-move-modal="onOpenMoveModal"
Expand Down
Loading