Working version

This commit is contained in:
Eden Kirin
2023-08-01 14:20:00 +02:00
parent 75242cda1d
commit 87b17e3745
2 changed files with 51 additions and 1 deletions

View File

@ -25,7 +25,10 @@
<div class="row">
<div class="col">
<button class="btn btn-success render">
Render
Render ZPL
</button>
<button class="btn btn-success print">
Print
</button>
</div>
<div class="col">
@ -40,6 +43,8 @@
<script>
let selectedDevice = null;
function createHooks() {
document.querySelector("button.render").addEventListener("click", (e) => {
const options = {
@ -54,10 +59,29 @@
document.querySelector(`textarea[name="zpl"]`).value = zpl;
});
});
document.querySelector("button.print").addEventListener("click", (e) => {
if (!selectedDevice) {
console.error("Device not selected");
}
const zpl = document.querySelector(`textarea[name="zpl"]`).value;
selectedDevice.send(zpl, undefined, (error) => {
console.error(error);
})
});
}
(function () {
createHooks();
getAvailablePrinters().then(devices => {
if (devices) {
selectedDevice = devices[0];
}
})
})();
</script>

View File

@ -169,3 +169,29 @@ async function createInternalStickerZPL(options) {
...options,
});
}
function getAvailablePrinters() {
return new Promise((resolve, reject) => {
const devices = [];
BrowserPrint.getDefaultDevice("printer", device => {
devices.push(device);
const defaultDevice = device;
BrowserPrint.getLocalDevices(deviceList => {
//console.log(deviceList)
deviceList.printer.forEach(device => {
if (!defaultDevice || device.uid != defaultDevice.uid) {
devices.push(device);
}
}, error => {
reject("Error getting local devices");
}, "printer");
resolve(devices);
});
}, error => {
reject(error);
})
})
}