-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
104 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,6 @@ export const DefaultToolbar = [ | |
'separator', | ||
'undo', | ||
'redo', | ||
'separator', | ||
'print', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
c1d7cbd
There was a problem hiding this comment.
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