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