yh-hiprint 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ /**
2
+ * @Description: canvas 简单的水印工具
3
+ * @Author: CcSimple
4
+ * @Github: https://github.com/CcSimple
5
+ */
6
+
7
+ /**
8
+ * @Description: 时间格式化
9
+ * @param date
10
+ * @param format
11
+ * @returns {string|null}
12
+ */
13
+ function timeFormat(date, format = 'YYYY-MM-DD') {
14
+ if (!date) return null;
15
+ if (typeof date === 'number') {
16
+ date = new Date(date);
17
+ }
18
+ const year = date.getFullYear();
19
+ const month = date.getMonth();
20
+ const day = date.getDate();
21
+ const hours24 = date.getHours();
22
+ const hours = hours24 % 12 === 0 ? 12 : hours24 % 12;
23
+ const minutes = date.getMinutes();
24
+ const seconds = date.getSeconds();
25
+ const dd = t => `0${t}`.slice(-2);
26
+ const map = {
27
+ YYYY: year,
28
+ MM: dd(month + 1),
29
+ MMMM: `${month + 1}月`,
30
+ M: month + 1,
31
+ DD: dd(day),
32
+ D: day,
33
+ HH: dd(hours24),
34
+ H: hours24,
35
+ hh: dd(hours),
36
+ h: hours,
37
+ mm: dd(minutes),
38
+ m: minutes,
39
+ ss: dd(seconds),
40
+ s: seconds
41
+ };
42
+ return format.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Q/g, str => String(map[str]));
43
+ }
44
+
45
+ // 水印参数
46
+ const defaultOption = {
47
+ id: 'watermark', // 水印id
48
+ watch: false,
49
+ content: 'vue-plugin-hiprint', // 水印内容
50
+ container: '.hiprint-printPaper', // 水印容器
51
+ width: 200, // 水印宽度
52
+ height: 200, // 水印高度
53
+ textAlign: 'center', // 水印文字水平对齐方式
54
+ textBaseline: 'middle', // 水印文字垂直对齐方式
55
+ fontSize: '14px', // 水印文字大小
56
+ fontFamily: 'Microsoft Yahei', // 水印文字字体
57
+ fillStyle: 'rgba(184, 184, 184, 0.3)', // 水印文字颜色
58
+ rotate: 25,// 水印文字旋转角度
59
+ timestamp: false, // 是否显示时间戳
60
+ format: 'YYYY-MM-DD HH:mm', // 时间戳格式
61
+ zIndex: 0
62
+ };
63
+
64
+ // 监听器
65
+ let observerMap = {};
66
+
67
+ /**
68
+ * @Description: 创建水印
69
+ * @param param
70
+ * @private
71
+ */
72
+ function _createWatermark(param) {
73
+ const {
74
+ id,
75
+ watch,
76
+ content,
77
+ container,
78
+ width,
79
+ height,
80
+ textAlign,
81
+ textBaseline,
82
+ fontSize,
83
+ fontFamily,
84
+ fillStyle,
85
+ rotate,
86
+ timestamp,
87
+ format,
88
+ zIndex
89
+ } = param;
90
+
91
+ observerMap[id] = {
92
+ wmMo: null, // MutationObserver
93
+ wmTimer: null // timestamp
94
+ }
95
+
96
+ const canvas = document.createElement('canvas');
97
+ canvas.setAttribute('width', `${width}px`);
98
+ canvas.setAttribute('height', `${height}px`);
99
+
100
+ let containerDom = typeof container === 'string' ? document.querySelector(container) : container;
101
+
102
+ const ctx = canvas.getContext('2d');
103
+ ctx.textAlign = textAlign;
104
+ ctx.textBaseline = textBaseline;
105
+ ctx.font = `${fontSize} ${fontFamily}`;
106
+ ctx.fillStyle = fillStyle;
107
+ ctx.translate(width / 2, height / 2);
108
+ ctx.rotate(-(Math.PI / 180) * rotate);
109
+ ctx.fillText(`${content}`, 0, 0);
110
+ timestamp && ctx.fillText(`${timeFormat(new Date(), format)}`, 0, parseInt(fontSize) + 5);
111
+
112
+ let __vm = containerDom.querySelector('.__vm__' + id);
113
+ const watermarkDiv = __vm || document.createElement('div');
114
+ const withHeightStr = containerDom.getAttribute('style');
115
+ const styleStr = `position:absolute;user-select:none;top:0;left:0;${withHeightStr};z-index:${zIndex};pointer-events:none !important;background-repeat:repeat;background-image:url('${canvas.toDataURL()}')`;
116
+
117
+ watermarkDiv.setAttribute('style', styleStr);
118
+ watermarkDiv.classList.add('__vm__' + id);
119
+
120
+ if (!__vm) {
121
+ containerDom.insertBefore(watermarkDiv, containerDom.firstChild);
122
+ }
123
+
124
+ if (watch) {
125
+ const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
126
+ if (MutationObserver) {
127
+ observerMap[id]["wmMo"] = new MutationObserver((e) => {
128
+ let change = e.some(item => item.target.className == containerDom.className && item.type == 'attributes');
129
+ __vm = containerDom.querySelector('.__vm__' + id);
130
+ if ((__vm && __vm.getAttribute('style') !== styleStr) || !__vm || change) {
131
+ // 避免一直触发
132
+ observerMap[id]["wmMo"].disconnect();
133
+ observerMap[id]["wmMo"] = null;
134
+ delete observerMap[id]["wmMo"];
135
+ _createWatermark(param);
136
+ }
137
+ });
138
+ observerMap[id]["wmMo"].observe(containerDom, {
139
+ attributes: true, subtree: true, childList: true
140
+ });
141
+ }
142
+ }
143
+
144
+ if (format) {
145
+ let timeout = 1000 * 60 * 60 * 24;
146
+ if (format.includes('s')) {
147
+ timeout = 1000;
148
+ } else if (format.includes('m')) {
149
+ timeout = 1000 * 60;
150
+ } else if (format.includes('h') || format.includes('H')) {
151
+ timeout = 1000 * 60 * 60;
152
+ }
153
+
154
+ observerMap[id]["wmTimer"] = window.setTimeout(() => {
155
+ // 触发 MutationObserver
156
+ watermarkDiv.style.bottom = '0';
157
+ }, timeout);
158
+ }
159
+ }
160
+
161
+ /**
162
+ * @Description: 销毁水印
163
+ */
164
+ const destroyWatermark = function (options) {
165
+ const {
166
+ id,
167
+ watch,
168
+ container,
169
+ } = options;
170
+ if (watch) {
171
+ let containerDom = typeof container === 'string' ? document.querySelector(container) : container;
172
+ // 监听器关闭
173
+ if (observerMap[id]) {
174
+ observerMap[id]["wmMo"] && observerMap[id]["wmMo"].disconnect();
175
+ observerMap[id]["wmMo"] = null;
176
+ observerMap[id]["wmTimer"] && window.clearTimeout(observerMap[id]["wmTimer"]);
177
+ observerMap[id]["wmTimer"] = null;
178
+ delete observerMap[id];
179
+ }
180
+ // 删除水印元素
181
+ const __vm = containerDom.querySelector('.__vm__' + id);
182
+ __vm && __vm.parentNode.removeChild(__vm);
183
+ }
184
+ }
185
+
186
+ /**
187
+ * @Description: 创建水印
188
+ * @param option
189
+ */
190
+ const createWatermark = function (option) {
191
+ let options = Object.assign({}, defaultOption, option);
192
+ destroyWatermark(options);
193
+ _createWatermark(options);
194
+ }
195
+
196
+ // 暴露接口
197
+ export default {
198
+ createWatermark, destroyWatermark
199
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "yh-hiprint",
3
+ "version": "1.0.9",
4
+ "description": "Hiprint for Vue3 by NoahLiu in ForceCon in Hunan Changesha",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "pub:aliyun": "npm publish --registry https://packages.aliyun.com/60765e0161a945067837bb5f/npm/npm-registry/ --no-git-checks",
9
+ "pub:npm": "npm publish --registry https://registry.npmjs.org/ --no-git-checks"
10
+ },
11
+ "dependencies": {
12
+ "jquery": "3.7.0",
13
+ "@claviska/jquery-minicolors": "2.3.6",
14
+ "jsbarcode": "3.11.5",
15
+ "jspdf": "2.5.1",
16
+ "html2canvas": "1.4.1",
17
+ "nzh": "1.0.9",
18
+ "canvg": "4.0.1"
19
+ },
20
+ "peerDependencies": {
21
+ "vue": "3.2.47"
22
+ },
23
+ "author": "Liubin"
24
+ }
package/panel.js ADDED
@@ -0,0 +1,58 @@
1
+ export default {
2
+ panels: [
3
+ {
4
+ index: 0,
5
+ height: 296.6,
6
+ width: 210,
7
+ paperHeader: 49.5,
8
+ paperFooter: 780,
9
+ watermarkOptions: {
10
+ content: "力控远海",
11
+ rotate: 25,
12
+ },
13
+ printElements: [
14
+ {
15
+ options: { left: 60, top: 27, height: 13, width: 52, title: "页眉线", textAlign: "center" },
16
+ printElementType: { type: "text" },
17
+ },
18
+ {
19
+ options: {
20
+ left: 10,
21
+ top: 100,
22
+ height: 100,
23
+ width: 570,
24
+ field: "table",
25
+ columns: [
26
+ [
27
+ {
28
+ width: 100,
29
+ title: "姓名",
30
+ align: "left",
31
+ },
32
+ {
33
+ width: 100,
34
+ title: "性别",
35
+ },
36
+ {
37
+ width: 100,
38
+ title: "销售数量",
39
+ },
40
+ {
41
+ width: 100,
42
+ title: "销售金额",
43
+ },
44
+ ],
45
+ ],
46
+ },
47
+ printElementType: { type: "table" },
48
+ },
49
+ {
50
+ options: { left: 525, top: 784.5, height: 13, width: 63, title: "页尾线", textAlign: "center" },
51
+ printElementType: { title: "自定义文本", type: "text" },
52
+ },
53
+ ],
54
+ paperNumberLeft: 565.5,
55
+ paperNumberTop: 819,
56
+ },
57
+ ],
58
+ };
package/scale.js ADDED
@@ -0,0 +1,42 @@
1
+ import $ from "jquery";
2
+ export default (function () {
3
+ function t() {
4
+ // json模板 options 对应键值 key
5
+ this.name = "scale";
6
+ }
7
+ // 涉及修改元素样式, 添加一个 css 方法
8
+ // t: 元素对象, e 参数值
9
+ return (
10
+ (t.prototype.css = function (t, e) {
11
+ if (t && t.length) {
12
+ if (e) return t.css("transform", "scale(" + e + ")");
13
+ }
14
+ return null;
15
+ }),
16
+ // 创建 DOM
17
+ (t.prototype.createTarget = function (t, i, e) {
18
+ // t: 元素对象,i: 元素options, e: 元素printElementType
19
+ return (
20
+ (this.target = $(
21
+ '<div class="hiprint-option-item">\n <div class="hiprint-option-item-label">\n 缩放\n </div>\n <div class="hiprint-option-item-field">\n <input type="number" value="1" step="0.1" min="0.1" max="3" class="auto-submit"/>\n </div>\n </div>'
22
+ )),
23
+ this.target
24
+ );
25
+ }),
26
+ // 获取值
27
+ (t.prototype.getValue = function () {
28
+ var t = this.target.find("input").val();
29
+ if (t) return parseFloat(t.toString());
30
+ }),
31
+ // 设置值
32
+ (t.prototype.setValue = function (t) {
33
+ // t: options 对应键的值
34
+ this.target.find("input").val(t);
35
+ }),
36
+ // 销毁 DOM
37
+ (t.prototype.destroy = function () {
38
+ this.target.remove();
39
+ }),
40
+ t
41
+ );
42
+ })();
package/z-index.js ADDED
@@ -0,0 +1,35 @@
1
+ import $ from "jquery";
2
+
3
+ export default (function () {
4
+ function t() {
5
+ this.name = "zIndex";
6
+ }
7
+
8
+ return (
9
+ (t.prototype.css = function (t, e) {
10
+ if (t && t.length) {
11
+ if (e) return t.css("z-index", e);
12
+ }
13
+ return null;
14
+ }),
15
+ (t.prototype.createTarget = function () {
16
+ return (
17
+ (this.target = $(
18
+ '<div class="hiprint-option-item">\n <div class="hiprint-option-item-label">\n 元素层级2\n </div>\n <div class="hiprint-option-item-field">\n <input type="number" class="auto-submit"/>\n </div>\n </div>'
19
+ )),
20
+ this.target
21
+ );
22
+ }),
23
+ (t.prototype.getValue = function () {
24
+ var t = this.target.find("input").val();
25
+ if (t) return parseInt(t.toString());
26
+ }),
27
+ (t.prototype.setValue = function (t) {
28
+ this.target.find("input").val(t);
29
+ }),
30
+ (t.prototype.destroy = function () {
31
+ this.target.remove();
32
+ }),
33
+ t
34
+ );
35
+ })();