mirror of
https://gitee.com/wot-design-uni/wot-design-uni.git
synced 2025-12-06 17:18:40 +08:00
2 lines
8.6 KiB
JavaScript
2 lines
8.6 KiB
JavaScript
var t=Object.defineProperty,e=Object.defineProperties,a=Object.getOwnPropertyDescriptors,s=Object.getOwnPropertySymbols,l=Object.prototype.hasOwnProperty,n=Object.prototype.propertyIsEnumerable,i=(e,a,s)=>a in e?t(e,a,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[a]=s,r=(t,e)=>{for(var a in e||(e={}))l.call(e,a)&&i(t,a,e[a]);if(s)for(var a of s(e))n.call(e,a)&&i(t,a,e[a]);return t},o=(t,s)=>e(t,a(s));import{d as u,J as c,r as d,Y as h,an as v,C as p,c as f,B as g,H as m,G as x,y,a7 as b,e as W,f as w,b as T,g as k,w as C,h as S,i as P,_ as X,E as Y,D as _,m as M,z as F,F as H,j,t as L,l as q,L as O,a1 as $,$ as D}from"./index-ChV97F5w.js";import{_ as N}from"./wd-button.DXEkLyH0.js";import{b as B,n as R,_ as z}from"./base64.D3gEw_Hf.js";import{u as E}from"./useTranslate.BPiTMMfr.js";const I=o(r({},B),{penColor:{type:String,default:"#000"},lineWidth:{type:Number,default:3},clearText:String,revokeText:String,restoreText:String,confirmText:String,fileType:{type:String,default:"png"},quality:{type:Number,default:1},exportScale:{type:Number,default:1},disabled:{type:Boolean,default:!1},height:R,width:R,backgroundColor:String,disableScroll:{type:Boolean,default:!0},enableHistory:{type:Boolean,default:!1},step:{type:Number,default:1},undoText:String,redoText:String,pressure:{type:Boolean,default:!1},minWidth:{type:Number,default:2},maxWidth:{type:Number,default:6},minSpeed:{type:Number,default:1.5}}),J=z(u(o(r({},{name:"wd-signature",options:{addGlobalClass:!0,virtualHost:!0,styleIsolation:"shared"}}),{props:I,emits:["start","end","signing","confirm","clear"],setup(t,{expose:e,emit:a}){const s=t,l=a,{translate:n}=E("signature"),{proxy:i}=c(),u=d(`signature${h()}`);let B=null;const R=d(!1),z=d(1),I=v({canvasWidth:0,canvasHeight:0,ctx:null});function J(t){if(!t)return!0;if(["transparent","#0000","#00000000"].includes(t.toLowerCase()))return!0;const e=t.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*(\d*\.?\d+))?\)$/i);if(e){return 0===(e[4]?parseFloat(e[4]):1)}const a=t.match(/^hsla?\(\s*(\d+)(?:deg)?\s*,\s*(\d+)%\s*,\s*(\d+)%(?:\s*,\s*(\d*\.?\d+))?\)$/i);if(a){return 0===(a[4]?parseFloat(a[4]):1)}const s=t.match(/^#([0-9a-f]{8}|[0-9a-f]{4})$/i);if(s){const t=s[1],e=8===t.length?t.slice(6,8):t.slice(3,4).repeat(2);return 0===parseInt(e,16)}return!1}p((()=>s.penColor),(()=>{ct()})),p((()=>s.lineWidth),(()=>{ct()}));const G=f((()=>{const t={};return g(s.width)&&(t.width=m(s.width)),g(s.height)&&(t.height=m(s.height)),`${x(t)}`})),U=f((()=>s.disableScroll)),A=f((()=>s.enableHistory)),K=d([]),Q=d([]),V=d(),Z=d(0);const tt=()=>s.pressure?(s.maxWidth+s.minWidth)/2:s.lineWidth,et=t=>{t.preventDefault(),R.value=!0,ct(),l("start",t);const{x:e,y:a}=t.touches[0];V.value={points:[{x:e,y:a,t:Date.now()}],color:s.penColor,width:tt(),backgroundColor:s.backgroundColor,isPressure:s.pressure},Q.value=[],it(t)},at=t=>{t.preventDefault(),R.value=!1,V.value&&(K.value.push(o(r({},V.value),{points:V.value.points.map((t=>o(r({},t),{t:t.t,speed:t.speed,distance:t.distance,lineWidth:t.lineWidth,lastX1:t.lastX1,lastY1:t.lastY1,lastX2:t.lastX2,lastY2:t.lastY2,isFirstPoint:t.isFirstPoint})))})),Z.value=K.value.length),V.value=void 0;const{ctx:e}=I;e&&e.beginPath(),l("end",t)},st=(t=!1)=>{!t&&I.canvasHeight&&I.canvasWidth||new Promise((t=>{const{ctx:e}=I;if(e)return t(e);O(`#${u.value}`,!1,i).then((e=>{var a,s;a=e.width,s=e.height,I.canvasHeight=s*z.value,I.canvasWidth=a*z.value,I.ctx=$(u.value,i),I.ctx&&I.ctx.scale(z.value,z.value),t(I.ctx)}))})).then((()=>{const{ctx:t}=I;t&&g(s.backgroundColor)&&(t.setFillStyle(s.backgroundColor),t.fillRect(0,0,I.canvasWidth,I.canvasHeight),t.draw())}))},lt=()=>{K.value=[],Q.value=[],Z.value=0,function(){const{canvasWidth:t,canvasHeight:e,ctx:a}=I;a&&(a.clearRect(0,0,t,e),J(s.backgroundColor)||(a.setFillStyle(s.backgroundColor),a.fillRect(0,0,t,e)),a.draw())}(),l("clear")},nt=()=>{!function(){const{fileType:t,quality:e,exportScale:a}=s,{canvasWidth:n,canvasHeight:r}=I;D({width:n,height:r,destWidth:n*a,destHeight:r*a,fileType:t,quality:e,canvasId:u.value,canvas:B,success:t=>{const e={tempFilePath:t.tempFilePath,width:n*a/z.value,height:r*a/z.value,success:!0};l("confirm",e)},fail:()=>{const t={tempFilePath:"",width:n*a/z.value,height:r*a/z.value,success:!1};l("confirm",t)}},i)}()},it=t=>{t.preventDefault();const{ctx:e}=I;if(!R.value||s.disabled||!e)return;const{x:a,y:n}=t.touches[0],i={x:a,y:n,t:Date.now()};if(V.value){const t=V.value.points,l=t[t.length-1];if(l.t===i.t||l.x===a&&l.y===n)return;if(i.distance=Math.sqrt(Math.pow(i.x-l.x,2)+Math.pow(i.y-l.y,2)),i.speed=i.distance/(i.t-l.t||.1),s.pressure&&(i.lineWidth=function(t){if(!s.pressure)return s.lineWidth;const e=s.minSpeed||1.5,a=Math.min(10*e,Math.max(e,t)),l=(s.maxWidth-s.minWidth)*(a-e)/e,n=Math.max(s.maxWidth-l,s.minWidth);return Math.min(n,s.maxWidth)}(i.speed),t.length>=2)){if(t[t.length-2].lineWidth&&l.lineWidth){const t=(i.lineWidth-l.lineWidth)/l.lineWidth,e=.2;if(Math.abs(t)>e){const a=t>0?e:-.2;i.lineWidth=l.lineWidth*(1+a)}}}t.push(i),s.pressure?t.length>=2&&function(t,e){const{ctx:a}=I;if(!a)return;const l=e.x-t.x,n=e.y-t.y;if(Math.sqrt(l*l+n*n)<=2)e.lastX1=e.lastX2=t.x+.5*l,e.lastY1=e.lastY2=t.y+.5*n;else{const a=e.speed||0,i=s.minSpeed||1.5,r=Math.max(.1,Math.min(.9,a/(10*i)));e.lastX1=t.x+l*(.2+.3*r),e.lastY1=t.y+n*(.2+.3*r),e.lastX2=t.x+l*(.8-.3*r),e.lastY2=t.y+n*(.8-.3*r)}const i=e.lineWidth||s.lineWidth;"number"==typeof t.lastX1?(a.setLineWidth(i),a.beginPath(),a.moveTo(t.lastX2,t.lastY2),a.quadraticCurveTo(t.x,t.y,e.lastX1,e.lastY1),a.stroke(),t.isFirstPoint||(a.beginPath(),a.moveTo(t.lastX1,t.lastY1),a.quadraticCurveTo(t.x,t.y,t.lastX2,t.lastY2),a.stroke()),a.draw(!0)):e.isFirstPoint=!0}(l,i):(e.beginPath(),e.moveTo(l.x,l.y),e.lineTo(i.x,i.y),e.stroke(),e.draw(!0))}l("signing",t)},rt=()=>{const{ctx:t}=I;t&&(J(s.backgroundColor)?t.clearRect(0,0,I.canvasWidth,I.canvasHeight):(t.setFillStyle(s.backgroundColor),t.fillRect(0,0,I.canvasWidth,I.canvasHeight)),0!==K.value.length?(K.value.forEach((e=>{e.points.length&&(t.setStrokeStyle(e.color),t.setLineJoin("round"),t.setLineCap("round"),e.isPressure&&s.pressure?e.points.forEach(((a,l)=>{if(0===l)return;const n=e.points[l-1],i=a.x-n.x,r=a.y-n.y;if(Math.sqrt(i*i+r*r)<=2)a.lastX1=a.lastX2=n.x+.5*i,a.lastY1=a.lastY2=n.y+.5*r;else{const t=a.speed||0,e=s.minSpeed||1.5,l=Math.max(.1,Math.min(.9,t/(10*e)));a.lastX1=n.x+i*(.2+.3*l),a.lastY1=n.y+r*(.2+.3*l),a.lastX2=n.x+i*(.8-.3*l),a.lastY2=n.y+r*(.8-.3*l)}const o=a.lineWidth||e.width;"number"==typeof n.lastX1?(t.setLineWidth(o),t.beginPath(),t.moveTo(n.lastX2,n.lastY2),t.quadraticCurveTo(n.x,n.y,a.lastX1,a.lastY1),t.stroke(),n.isFirstPoint||(t.beginPath(),t.moveTo(n.lastX1,n.lastY1),t.quadraticCurveTo(n.x,n.y,n.lastX2,n.lastY2),t.stroke())):a.isFirstPoint=!0})):(t.setLineWidth(e.width),e.points.forEach(((a,s)=>{if(0===s)return;const l=e.points[s-1];t.beginPath(),t.moveTo(l.x,l.y),t.lineTo(a.x,a.y),t.stroke()}))))})),t.draw()):t.draw())},ot=()=>{if(!K.value.length)return;const t=Math.min(s.step,K.value.length),e=K.value.splice(K.value.length-t);Q.value.push(...e),Z.value=Math.max(0,Z.value-t),rt()},ut=()=>{if(!Q.value.length)return;const t=Math.min(s.step,Q.value.length),e=Q.value.splice(Q.value.length-t);K.value.push(...e),Z.value=Math.min(K.value.length,Z.value+t),rt()};function ct(){const{ctx:t}=I;t&&(t.setLineWidth(tt()),t.setStrokeStyle(s.penColor),t.setLineJoin("round"),t.setLineCap("round"))}return y((()=>{st()})),b((()=>{})),e({init:st,clear:lt,confirm:nt,restore:ut,revoke:ot}),(t,e)=>{const a=X,s=P,l=W(w("wd-button"),N);return k(),T(s,{class:"wd-signature"},{default:C((()=>[S(s,{class:"wd-signature__content"},{default:C((()=>[S(a,{class:"wd-signature__content-canvas","canvas-id":u.value,style:Y(G.value),width:I.canvasWidth,height:I.canvasHeight,id:u.value,"disable-scroll":U.value,onTouchstart:et,onTouchend:at,onTouchmove:it},null,8,["canvas-id","style","width","height","id","disable-scroll"])])),_:1}),S(s,{class:"wd-signature__footer"},{default:C((()=>[_(t.$slots,"footer",{clear:lt,confirm:nt,currentStep:Z.value,revoke:ot,restore:ut,canUndo:K.value.length>0,canRedo:Q.value.length>0,historyList:K.value},(()=>[A.value?(k(),M(H,{key:0},[S(l,{size:"small",plain:"",onClick:ot,disabled:K.value.length<=0},{default:C((()=>[j(L(t.revokeText||q(n)("revokeText")),1)])),_:1},8,["disabled"]),S(l,{size:"small",plain:"",onClick:ut,disabled:Q.value.length<=0},{default:C((()=>[j(L(t.restoreText||q(n)("restoreText")),1)])),_:1},8,["disabled"])],64)):F("",!0),S(l,{size:"small",plain:"",onClick:lt},{default:C((()=>[j(L(t.clearText||q(n)("clearText")),1)])),_:1}),S(l,{size:"small",onClick:nt},{default:C((()=>[j(L(t.confirmText||q(n)("confirmText")),1)])),_:1})]),!0)])),_:3})])),_:3})}}})),[["__scopeId","data-v-37aa24ef"]]);export{J as _};
|