Skip to content

Commit

Permalink
feat(editor): print extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mekery committed Apr 19, 2020
1 parent c745fb7 commit c1d7cbd
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/components/QuasarTiptap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
OKatexBlock,
OKatexInline,
OFormatClear,
OPrint,
RecommendedExtensions
} from 'src/extentions'
Expand Down
1 change: 1 addition & 0 deletions src/components/buttons/OSimpleCommandBtn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default {
indent: { icon: 'format_indent_increase', tooltip: this.$o.lang.editor.indent, isActive: false, command: this.commands.indent },
outdent: { icon: 'format_indent_decrease', tooltip: this.$o.lang.editor.outdent, isActive: false, command: this.commands.outdent },
format_clear: { icon: 'format_clear', tooltip: this.$o.lang.editor.removeFormat, isActive: false, command: this.commands.formatClear },
print: { icon: 'print', tooltip: this.$o.lang.editor.print, isActive: false, command: this.commands.print },
}
},
config () {
Expand Down
4 changes: 2 additions & 2 deletions src/data/article.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/data/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ export const DefaultToolbar = [
'separator',
'undo',
'redo',
'separator',
'print',
]
32 changes: 32 additions & 0 deletions src/extentions/Print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Extension } from 'tiptap'
import { printHtml } from 'src/utils/print'

export default class Print extends Extension {
get name () {
return 'print'
}

commands () {
return () => (_state, _dispatch, view) => {
const editorContent = view.dom.closest('.editor__content')
if (editorContent) {
printHtml(editorContent)
return true
}
return false
}
}

keys ({ view }) {
return {
'Mod-p': function (_state, _dispatch, view) {
const editorContent = view.dom.closest('.editor__content')
if (editorContent) {
printHtml(editorContent)
return true
}
return false
}
}
}
}
6 changes: 4 additions & 2 deletions src/extentions/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import {
ODiagram,
OKatexBlock,
OKatexInline,
OFormatClear
OFormatClear,
OPrint,
} from 'src/extentions'

const classes = {
Expand Down Expand Up @@ -92,7 +93,8 @@ const classes = {
ODiagram,
OKatexBlock,
OKatexInline,
OFormatClear
OFormatClear,
OPrint,
}

class DynamicClass {
Expand Down
2 changes: 2 additions & 0 deletions src/extentions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { default as OAlignment } from 'src/extentions/Alignment'
export { default as OIndent } from 'src/extentions/Indent'
export { default as OLineHeight } from 'src/extentions/LineHeight'
export { default as OFormatClear } from 'src/extentions/FormatClear'
export { default as OPrint } from 'src/extentions/Print'

// --------------------------------------------------------------------------------
// Exposed Extension List
Expand Down Expand Up @@ -71,6 +72,7 @@ export const QuasarTipTapExtensions = [
'OKatexBlock',
'OKatexInline',
'OFormatClear',
'OPrint',
]

export const RecommendedExtensions = [
Expand Down
31 changes: 0 additions & 31 deletions src/pages/examples/all.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,6 @@ export default {
}),
],
toolbar: [
'add-more',
'separator',
'bold',
'italic',
'underline',
'strike',
'code',
'separator',
'heading',
'font-family',
'fore-color',
'back-color',
'format_clear',
'separator',
'align-dropdown',
'indent',
'outdent',
'line-height',
'separator',
'horizontal',
'bullet_list',
'ordered_list',
'todo_list',
'separator',
'blockquote',
'code_block',
'photo',
'table',
'separator',
'undo',
'redo',
]
},
isSlideShow: false,
Expand Down
60 changes: 60 additions & 0 deletions src/utils/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Utils: print
* @see https://github.com/Leecason/element-tiptap/blob/master/src/utils/print.ts
*/
const IS_TEST = false

export function printHtml (dom) {
let style = Array.from(document.querySelectorAll('style, link'))
.reduce((str, style) => str + style.outerHTML, '')
style = style.replace(new RegExp('statics/', 'g'), '/statics/')
style = style.replace(new RegExp('fonts/', 'g'), '/fonts/')

const content = style + `<section class="tiptap tiptap-editor quasar-tiptap">${dom.outerHTML}</section>`

if (IS_TEST) {
// open a new window, for test
let newWindow = window.open('print window', '_blank')
newWindow.document.write(content)
newWindow.document.close()
} else {
// iframe, for print
const iframe = document.createElement('iframe')
iframe.id = 'quasar-tiptap-iframe'
iframe.setAttribute('style', 'position: absolute; width: 0; height: 0; top: -10px; left: -10px;')
document.body.appendChild(iframe)

const frameWindow = iframe.contentWindow
const doc = iframe.contentDocument || (iframe.contentWindow && iframe.contentWindow.document)

if (doc) {
doc.open()
doc.write(content)
doc.close()
}

if (frameWindow) {
iframe.onload = function () {
try {
setTimeout(() => {
frameWindow.focus()
try {
if (!frameWindow.document.execCommand('print', false)) {
frameWindow.print()
}
} catch (e) {
frameWindow.print()
}
frameWindow.close()
}, 10)
} catch (err) {
console.error(err)
}

setTimeout(function () {
document.body.removeChild(iframe)
}, 100)
}
}
}
}

1 comment on commit c1d7cbd

@Kishiro80
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason, the print doesnt show properly for me. Maybe it is becauase of CSS. So, I think this method of hiding everything will make it more appealing while printing.
https://stackoverflow.com/questions/468881/print-div-id-printarea-div-only

Please sign in to comment.