|
| 1 | +/*! |
| 2 | + * summernote-ext-print |
| 3 | + * Copyright (c) lqez (https://github.com/lqez/summernote-ext-print) |
| 4 | + * Licensed under the MIT License |
| 5 | + */ |
| 6 | +(function (factory) { |
| 7 | + /* global define */ |
| 8 | + if (typeof define === 'function' && define.amd) { |
| 9 | + // AMD. Register as an anonymous module. |
| 10 | + define(['jquery'], factory); |
| 11 | + } else if (typeof module === 'object' && module.exports) { |
| 12 | + // Node/CommonJS |
| 13 | + module.exports = factory(require('jquery')); |
| 14 | + } else { |
| 15 | + // Browser globals |
| 16 | + factory(window.jQuery); |
| 17 | + } |
| 18 | +}(function ($) { |
| 19 | + // Extends lang for print plugin. |
| 20 | + $.extend(true, $.summernote.lang, { |
| 21 | + 'en-US': { |
| 22 | + print: { |
| 23 | + print: 'Print' |
| 24 | + } |
| 25 | + }, |
| 26 | + 'ko-KR': { |
| 27 | + print: { |
| 28 | + print: '인쇄' |
| 29 | + } |
| 30 | + }, |
| 31 | + 'pt-BR': { |
| 32 | + print: { |
| 33 | + print: 'Imprimir' |
| 34 | + } |
| 35 | + }, |
| 36 | + 'fr-FR': { |
| 37 | + print: { |
| 38 | + print: 'Imprimer' |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + // Extends plugins for print plugin. |
| 44 | + $.extend($.summernote.plugins, { |
| 45 | + /** |
| 46 | + * @param {Object} context - context object has status of editor. |
| 47 | + */ |
| 48 | + 'print': function (context) { |
| 49 | + var self = this; |
| 50 | + |
| 51 | + // ui has renders to build ui elements. |
| 52 | + // - you can create a button with `ui.button` |
| 53 | + var ui = $.summernote.ui; |
| 54 | + var $editor = context.layoutInfo.editor; |
| 55 | + var options = context.options; |
| 56 | + var lang = options.langInfo; |
| 57 | + |
| 58 | + var isFF = function () { |
| 59 | + const userAgent = navigator.userAgent; |
| 60 | + const isEdge = /Edge\/\d+/.test(userAgent); |
| 61 | + return !isEdge && /firefox/i.test(userAgent) |
| 62 | + } |
| 63 | + |
| 64 | + var fillContentAndPrint = function($frame, content) { |
| 65 | + $frame.contents().find('body').html(content); |
| 66 | + |
| 67 | + setTimeout(function () { |
| 68 | + $frame[0].contentWindow.focus(); |
| 69 | + $frame[0].contentWindow.print(); |
| 70 | + $frame.remove(); |
| 71 | + $frame = null; |
| 72 | + }, 250); |
| 73 | + } |
| 74 | + |
| 75 | + var getPrintframe = function ($container) { |
| 76 | + var $frame = $( |
| 77 | + '<iframe name="summernotePrintFrame"' + |
| 78 | + 'width="0" height="0" frameborder="0" src="about:blank" style="visibility:hidden">' + |
| 79 | + '</iframe>'); |
| 80 | + $frame.appendTo($editor.parent()); |
| 81 | + |
| 82 | + var $head = $frame.contents().find('head'); |
| 83 | + if (options.print && options.print.stylesheetUrl) { |
| 84 | + // Use dedicated styles |
| 85 | + var css = document.createElement('link'); |
| 86 | + css.href = options.print.stylesheetUrl; |
| 87 | + css.rel = 'stylesheet'; |
| 88 | + css.type = 'text/css'; |
| 89 | + $head.append(css); |
| 90 | + } else { |
| 91 | + // Inherit styles from document |
| 92 | + $('style, link[rel=stylesheet]', document).each(function () { |
| 93 | + $head.append($(this).clone()); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + return $frame; |
| 98 | + }; |
| 99 | + |
| 100 | + // add print button |
| 101 | + context.memo('button.print', function () { |
| 102 | + // create button |
| 103 | + var button = ui.button({ |
| 104 | + contents: '<i class="fa fa-print"/> ' + lang.print.print, |
| 105 | + tooltip: lang.print.print, |
| 106 | + container: options.container, |
| 107 | + click: function () { |
| 108 | + var $frame = getPrintframe(); |
| 109 | + var content = context.invoke('code'); |
| 110 | + |
| 111 | + if (isFF()) { |
| 112 | + $frame[0].onload = function () { |
| 113 | + fillContentAndPrint($frame, content); |
| 114 | + }; |
| 115 | + } else { |
| 116 | + fillContentAndPrint($frame, content); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + return button.render(); |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | +})); |
0 commit comments