Skip to content

Commit a828f38

Browse files
authored
feat(Print): support cavas html element (#6203)
* refactor: 移除 console.log 语句 * refactor: 兼容 canvas 元素 * chore: bump version 9.7.1
1 parent 5cdf89a commit a828f38

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.7.1-beta07</Version>
4+
<Version>9.7.1</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Print/PrintButton.razor.js

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getUID } from "../../modules/utility.js"
1+
import { insertBefore, getUID } from "../../modules/utility.js"
22
import { showTooltip, removeTooltip } from "../Button/Button.razor.js"
33
import Data from "../../modules/data.js"
44
import EventHandler from "../../modules/event-handler.js"
@@ -23,48 +23,24 @@ export function init(id) {
2323
const print = el => {
2424
const modal = el.closest('.modal-content')
2525
if (modal) {
26-
const modalBody = modal.querySelectorAll('.modal-body')
26+
const modalBody = [...modal.querySelectorAll('.modal-body')];
2727
if (modalBody.length > 0) {
28-
modalBody[0].querySelectorAll("input").forEach(ele => {
29-
const id = ele.getAttribute('id')
30-
if (!id) {
31-
ele.setAttribute('id', getUID())
32-
}
33-
})
34-
const printContent = modalBody[0].innerHTML
28+
modal.classList.add('d-none');
29+
30+
const dialog = modalBody.pop();
3531
const body = document.querySelector('body')
3632
body.classList.add('bb-printview-open')
37-
const dialog = document.createElement('div')
38-
dialog.classList.add('bb-printview')
39-
dialog.innerHTML = printContent
40-
body.appendChild(dialog)
41-
42-
// assign value
43-
const elements = ["input", "textarea"];
44-
elements.forEach(tag => {
45-
console.log(tag);
46-
dialog.querySelectorAll(tag).forEach(ele => {
47-
const id = ele.getAttribute('id')
48-
const vEl = document.getElementById(id)
49-
if (vEl) {
50-
if (ele.getAttribute('type') === 'checkbox') {
51-
const v1 = vEl.checked
52-
if (v1 === true) {
53-
ele.setAttribute('checked', 'checked')
54-
}
55-
}
56-
else {
57-
ele.value = vEl.value
58-
}
59-
}
60-
});
61-
});
33+
34+
const printContentEl = createPrintContent(dialog);
35+
body.appendChild(printContentEl);
6236

6337
const handler = setTimeout(() => {
6438
clearTimeout(handler)
65-
window.print()
39+
window.print();
40+
restoreCanvas(printContentEl);
6641
body.classList.remove('bb-printview-open')
67-
dialog.remove()
42+
printContentEl.remove()
43+
modal.classList.remove('d-none')
6844
}, 50)
6945
}
7046
}
@@ -76,6 +52,69 @@ const print = el => {
7652
}
7753
}
7854

55+
const createPrintContent = content => {
56+
content.querySelectorAll("input").forEach(ele => {
57+
const id = ele.getAttribute('id')
58+
if (!id) {
59+
ele.setAttribute('id', getUID())
60+
}
61+
})
62+
const dialog = document.createElement('div')
63+
dialog.classList.add('bb-printview')
64+
dialog.innerHTML = content.innerHTML
65+
66+
const elements = ["input", "textarea"];
67+
elements.forEach(tag => {
68+
dialog.querySelectorAll(tag).forEach(ele => {
69+
const id = ele.getAttribute('id')
70+
const vEl = document.getElementById(id)
71+
if (vEl) {
72+
if (ele.getAttribute('type') === 'checkbox') {
73+
const v1 = vEl.checked
74+
if (v1 === true) {
75+
ele.setAttribute('checked', 'checked')
76+
}
77+
}
78+
else {
79+
ele.value = vEl.value
80+
}
81+
}
82+
});
83+
});
84+
85+
const canvas = [...content.querySelectorAll('canvas')];
86+
const targetCanvas = [...dialog.querySelectorAll('canvas')];
87+
for (var i = 0; i < canvas.length; i++) {
88+
const canvasEl = canvas[i];
89+
createCanvasPlaceholder(canvasEl);
90+
moveCanvas(canvasEl, targetCanvas[i]);
91+
}
92+
93+
return dialog;
94+
}
95+
96+
const createCanvasPlaceholder = canvas => {
97+
const sectionEl = document.createElement('section');
98+
sectionEl.classList.add('bb-print-canvas-placeholder');
99+
insertBefore(canvas, sectionEl);
100+
}
101+
102+
const moveCanvas = (canvas, target) => {
103+
insertBefore(target, canvas);
104+
target.remove();
105+
}
106+
107+
const restoreCanvas = printContentEl => {
108+
const canvas = [...printContentEl.querySelectorAll('canvas')];
109+
const targetCanvas = [...document.querySelectorAll('.bb-print-canvas-placeholder')];
110+
for (var i = 0; i < canvas.length; i++) {
111+
const canvasEl = canvas[i];
112+
const target = targetCanvas[i];
113+
moveCanvas(canvasEl, target);
114+
target.remove();
115+
}
116+
}
117+
79118
export function dispose(id) {
80119
const p = Data.get(id)
81120
Data.remove(id)

0 commit comments

Comments
 (0)