-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
eb5d1f9
commit c21e23e
Showing
4 changed files
with
5,263 additions
and
1,617 deletions.
There are no files selected for viewing
196 changes: 104 additions & 92 deletions
196
packages/cw/print_block_sdk/components/cw-print-view/index.vue
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 |
---|---|---|
@@ -1,108 +1,120 @@ | ||
<template> | ||
<div class="print-view" v-show="$env.VUE_APP_DESIGNER||isShowPrint"> | ||
<div class="print-room" vusion-slot-name="default" ref="print-room"> | ||
<slot></slot> | ||
<div v-if="!$slots.default" style="color:#ccccccd0;padding: 3px;"> | ||
请将打印的内容拖入这块区域 | ||
</div> | ||
<div class="print-view" v-show="$env.VUE_APP_DESIGNER || isShowPrint"> | ||
123123239991010 | ||
<div class="print-room" vusion-slot-name="default" ref="print-room"> | ||
<slot></slot> | ||
<div v-if="!$slots.default" style="color: #ccccccd0; padding: 3px"> | ||
请将打印的内容拖入这块区域 | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name:"cw-print-view", | ||
props:{ | ||
value:{ | ||
type:String, | ||
default:"请在这里编写代码" | ||
}, | ||
isShowPrint: { | ||
type: Boolean, | ||
default:false | ||
} | ||
name: "cw-print-view", | ||
props: { | ||
value: { | ||
type: String, | ||
default: "请在这里编写代码", | ||
}, | ||
methods:{ | ||
async print() { | ||
const historyEl = document.querySelector(".print-block-room") | ||
if(historyEl){ | ||
historyEl.remove() | ||
} | ||
const el = this.$refs["print-room"].cloneNode(true); | ||
el.classList.add("print-block-room"); | ||
document.body.appendChild(el) | ||
let promises = [] | ||
this.getCanvas( el,"",promises) | ||
console.log(promises); | ||
await Promise.all(promises) | ||
isShowPrint: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
methods: { | ||
async print() { | ||
// const historyEl = document.querySelector(".print-block-room"); | ||
// if (historyEl) { | ||
// historyEl.remove(); | ||
// } | ||
// 确认打印前所有元素已经完全加载和显示 | ||
const el = this.$refs["print-room"].cloneNode(true); | ||
el.classList.add("print-block-room"); | ||
document.body.appendChild(el); | ||
console.log("el", el); | ||
let promises = []; | ||
this.getCanvas(el, "", promises); | ||
console.log("Promises:", promises); | ||
try { | ||
await Promise.all(promises); | ||
console.log("All canvas data copied successfully"); | ||
window.print(); | ||
}, | ||
getCanvas(el,path="",promises) { | ||
const children = el.childNodes | ||
children.forEach((item,index) => { | ||
if(item.childNodes.length>0){ | ||
this.getCanvas(item,path+=item.tagName,promises) | ||
} else { | ||
if (item.tagName === "CANVAS") { | ||
const source = this.$refs["print-room"].querySelector(`${path.toLowerCase()} ${item.tagName.toLowerCase()}:nth-child(${index + 1})`) | ||
promises.push(this.copyCanvasData(item,source)) | ||
} | ||
} | ||
}) | ||
}, | ||
copyCanvasData(target, source) { | ||
return new Promise((resolve, reject) => { | ||
const data = source.toDataURL("image/png") | ||
const img = new Image() | ||
img.src = data | ||
img.onload = () => { | ||
const ctx = target.getContext("2d") | ||
ctx.drawImage(img, 0, 0,source.width,source.height) | ||
resolve(true) | ||
} | ||
img.onerror = () => { | ||
reject(false) | ||
} | ||
}) | ||
} catch (error) { | ||
console.error("Error during canvas data copying:", error); | ||
} | ||
} | ||
} | ||
}, | ||
getCanvas(el, path = "", promises) { | ||
const children = el.childNodes; | ||
children.forEach((item, index) => { | ||
if (item.childNodes.length > 0) { | ||
this.getCanvas(item, (path += item.tagName), promises); | ||
} else { | ||
if (item.tagName === "CANVAS") { | ||
const source = this.$refs["print-room"].querySelector( | ||
`${path.toLowerCase()} ${item.tagName.toLowerCase()}:nth-child(${ | ||
index + 1 | ||
})` | ||
); | ||
promises.push(this.copyCanvasData(item, source)); | ||
} | ||
} | ||
}); | ||
}, | ||
copyCanvasData(target, source) { | ||
return new Promise((resolve, reject) => { | ||
const data = source.toDataURL("image/png"); | ||
const img = new Image(); | ||
img.src = data; | ||
img.onload = () => { | ||
const ctx = target.getContext("2d"); | ||
ctx.drawImage(img, 0, 0, source.width, source.height); | ||
resolve(true); | ||
}; | ||
img.onerror = () => { | ||
reject(false); | ||
}; | ||
}); | ||
}, | ||
}, | ||
}; | ||
</script> | ||
<style> | ||
.print-block-room{ | ||
.print-block-room { | ||
display: none; | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
width: 100vw; | ||
background: white; | ||
z-index: 9000; | ||
} | ||
.print-view { | ||
min-height: 200px; | ||
border: 1px #ccc dashed; | ||
position: relative; | ||
background: transparent; | ||
} | ||
@media print { | ||
[column="auto"] { | ||
page-break-inside: auto; | ||
} | ||
[class^="l-root__"], | ||
body > div:first-of-type, | ||
[class^="u-drawer_drawer__"], | ||
[class^="u-modal_dialog__"] { | ||
display: none; | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
width: 100vw; | ||
background: white; | ||
z-index: 9000; | ||
} | ||
.print-view{ | ||
min-height: 200px; | ||
border: 1px #ccc dashed; | ||
position: relative; | ||
background: transparent; | ||
[class^="u-info-list_item__"]{ | ||
display: flex !important; | ||
} | ||
@media print { | ||
[class^='l-root__']{ | ||
display: none; | ||
} | ||
body>div:first-of-type{ | ||
display: none; | ||
} | ||
[class^='u-drawer_drawer__']{ | ||
display: none; | ||
} | ||
[class^='u-modal_dialog__']{ | ||
display: none; | ||
} | ||
.print-block-room{ | ||
display: block; | ||
} | ||
} | ||
.print-block-room { | ||
display: block; | ||
} | ||
} | ||
</style> | ||
|
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
Plugin-Version: 1.0.0 | ||
Library-Type: Frontend | ||
Metadata-File: usage.json | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/chunk-0a84b673.2a488f9b.js: dist-theme/chunk-0a84b673.2a488f9b.js | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/chunk-0a84b673.2a488f9b.js.map: dist-theme/chunk-0a84b673.2a488f9b.js.map | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/chunk-2d216214.016bb56b.js: dist-theme/chunk-2d216214.016bb56b.js | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/chunk-2d216214.016bb56b.js.map: dist-theme/chunk-2d216214.016bb56b.js.map | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/demo.html: dist-theme/demo.html | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/index.js: dist-theme/index.js | ||
packages/extension/print_block_sdk@1.1.0/dist-theme/index.js.map: dist-theme/index.js.map | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/chunk-0a84b673.60bf2b6b.js: dist-theme/chunk-0a84b673.60bf2b6b.js | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/chunk-0a84b673.60bf2b6b.js.map: dist-theme/chunk-0a84b673.60bf2b6b.js.map | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/chunk-2d216214.016bb56b.js: dist-theme/chunk-2d216214.016bb56b.js | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/chunk-2d216214.016bb56b.js.map: dist-theme/chunk-2d216214.016bb56b.js.map | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/demo.html: dist-theme/demo.html | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/index.js: dist-theme/index.js | ||
packages/extension/print_block_sdk@1.3.1/dist-theme/index.js.map: dist-theme/index.js.map |
Oops, something went wrong.