UNPKG

ll-package

Version:

2 lines (1 loc) 4 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("../../../@babel/runtime/helpers/esm/objectSpread2.js"),M=require("../../../@babel/runtime/helpers/esm/extends.js"),i=require("vue"),D=require("./interface.js"),E=require("./qrcodegen.js"),F={L:E.default.QrCode.Ecc.LOW,M:E.default.QrCode.Ecc.MEDIUM,Q:E.default.QrCode.Ecc.QUARTILE,H:E.default.QrCode.Ecc.HIGH},x=128,P="L",z="#FFFFFF",N="#000000",Q=!1,j=4,W=0,Z=.1;function T(l){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;const r=[];return l.forEach(function(n,o){let t=null;n.forEach(function(c,u){if(!c&&t!==null){r.push(`M${t+e} ${o+e}h${u-t}v1H${t+e}z`),t=null;return}if(u===n.length-1){if(!c)return;t===null?r.push(`M${u+e},${o+e} h1v1H${u+e}z`):r.push(`M${t+e},${o+e} h${u+1-t}v1H${t+e}z`);return}c&&t===null&&(t=u)})}),r.join("")}function U(l,e){return l.slice().map((r,n)=>n<e.y||n>=e.y+e.h?r:r.map((o,t)=>t<e.x||t>=e.x+e.w?o:!1))}function V(l,e,r,n){if(n==null)return null;const o=l.length+r*2,t=Math.floor(e*Z),c=o/e,u=(n.width||t)*c,a=(n.height||t)*c,s=n.x==null?l.length/2-u/2:n.x*c,f=n.y==null?l.length/2-a/2:n.y*c;let d=null;if(n.excavate){const h=Math.floor(s),R=Math.floor(f),L=Math.ceil(u+s-h),w=Math.ceil(a+f-R);d={x:h,y:R,w:L,h:w}}return{x:s,y:f,h:a,w:u,excavation:d}}function b(l,e){return e!=null?Math.floor(e):l?j:W}const k=function(){try{new Path2D().addPath(new Path2D)}catch{return!1}return!0}(),B=i.defineComponent({name:"QRCodeCanvas",inheritAttrs:!1,props:M.default(M.default({},D.qrProps()),{level:String,bgColor:String,fgColor:String,marginSize:Number}),setup(l,e){let{attrs:r,expose:n}=e;const o=i.computed(()=>{var a;return(a=l.imageSettings)===null||a===void 0?void 0:a.src}),t=i.shallowRef(null),c=i.shallowRef(null),u=i.shallowRef(!1);return n({toDataURL:(a,s)=>{var f;return(f=t.value)===null||f===void 0?void 0:f.toDataURL(a,s)}}),i.watchEffect(()=>{const{value:a,size:s=x,level:f=P,bgColor:d=z,fgColor:h=N,includeMargin:R=Q,marginSize:L,imageSettings:w}=l;if(t.value!=null){const _=t.value,g=_.getContext("2d");if(!g)return;let m=E.default.QrCode.encodeText(a,F[f]).getModules();const v=b(R,L),p=m.length+v*2,C=V(m,s,v,w),S=c.value,y=u.value&&C!=null&&S!==null&&S.complete&&S.naturalHeight!==0&&S.naturalWidth!==0;y&&C.excavation!=null&&(m=U(m,C.excavation));const A=window.devicePixelRatio||1;_.height=_.width=s*A;const I=s/p*A;g.scale(I,I),g.fillStyle=d,g.fillRect(0,0,p,p),g.fillStyle=h,k?g.fill(new Path2D(T(m,v))):m.forEach(function(G,H){G.forEach(function(q,O){q&&g.fillRect(O+v,H+v,1,1)})}),y&&g.drawImage(S,C.x+v,C.y+v,C.w,C.h)}},{flush:"post"}),i.watch(o,()=>{u.value=!1}),()=>{var a;const s=(a=l.size)!==null&&a!==void 0?a:x,f={height:`${s}px`,width:`${s}px`};let d=null;return o.value!=null&&(d=i.createVNode("img",{src:o.value,key:o.value,style:{display:"none"},onLoad:()=>{u.value=!0},ref:c},null)),i.createVNode(i.Fragment,null,[i.createVNode("canvas",$.default($.default({},r),{},{style:[f,r.style],ref:t}),null),d])}}}),X=i.defineComponent({name:"QRCodeSVG",inheritAttrs:!1,props:M.default(M.default({},D.qrProps()),{color:String,level:String,bgColor:String,fgColor:String,marginSize:Number,title:String}),setup(l){let e=null,r=null,n=null,o=null,t=null,c=null;return i.watchEffect(()=>{const{value:u,size:a=x,level:s=P,includeMargin:f=Q,marginSize:d,imageSettings:h}=l;e=E.default.QrCode.encodeText(u,F[s]).getModules(),r=b(f,d),n=e.length+r*2,o=V(e,a,r,h),h!=null&&o!=null&&(o.excavation!=null&&(e=U(e,o.excavation)),c=i.createVNode("image",{"xlink:href":h.src,height:o.h,width:o.w,x:o.x+r,y:o.y+r,preserveAspectRatio:"none"},null)),t=T(e,r)}),()=>{const u=l.bgColor&&z,a=l.fgColor&&N;return i.createVNode("svg",{height:l.size,width:l.size,viewBox:`0 0 ${n} ${n}`},[!!l.title&&i.createVNode("title",null,[l.title]),i.createVNode("path",{fill:u,d:`M0,0 h${n}v${n}H0z`,"shape-rendering":"crispEdges"},null),i.createVNode("path",{fill:a,d:t,"shape-rendering":"crispEdges"},null),c])}}});exports.QRCodeCanvas=B;exports.QRCodeSVG=X;