tav-ui
Version:
79 lines (76 loc) • 2.49 kB
JavaScript
import { defineComponent, ref, unref, onMounted, watch, openBlock, createElementBlock, createBlock, resolveDynamicComponent } from 'vue';
import { toDataURL } from 'qrcode';
import { downloadByUrl } from '../../../utils/file/download2.mjs';
import './qrcodePlus2.mjs';
import { qrcodeProps } from './types2.mjs';
import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
import { toCanvas } from './toCanvas2.mjs';
const _sfc_main = defineComponent({
name: "TaQrCode",
props: qrcodeProps,
emits: { done: (data) => !!data, error: (error) => !!error },
setup(props, { emit }) {
const wrapRef = ref(null);
async function createQrcode() {
try {
const { tag, value, options = {}, width, logo } = props;
const renderValue = String(value);
const wrapEl = unref(wrapRef);
if (!wrapEl)
return;
if (tag === "canvas") {
const url = await toCanvas({
canvas: wrapEl,
width,
logo,
content: renderValue,
options: options || {}
});
emit("done", { url, ctx: wrapEl.getContext("2d") });
return;
}
if (tag === "img") {
const url = await toDataURL(renderValue, {
errorCorrectionLevel: "H",
width,
...options
});
unref(wrapRef).src = url;
emit("done", { url });
}
} catch (error) {
emit("error", error);
}
}
function download(fileName) {
let url = "";
const wrapEl = unref(wrapRef);
if (wrapEl instanceof HTMLCanvasElement) {
url = wrapEl.toDataURL();
} else if (wrapEl instanceof HTMLImageElement) {
url = wrapEl.src;
}
if (!url)
return;
downloadByUrl({
url,
fileName
});
}
onMounted(createQrcode);
watch(props, () => {
createQrcode();
}, {
deep: true
});
return { wrapRef, download };
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("div", null, [
(openBlock(), createBlock(resolveDynamicComponent(_ctx.tag), { ref: "wrapRef" }, null, 512))
]);
}
var Qrcode = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/home/runner/work/tav-ui/tav-ui/packages/components/qr-code/src/qr-code.vue"]]);
export { Qrcode as default };
//# sourceMappingURL=qr-code2.mjs.map