diff --git a/README.md b/README.md new file mode 100644 index 0000000..410d7cd --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# Sketch-Measure-Compare + +A tool for easy comparison of UI and web pages + +## Guide + +### Step1: Installation +#### Method 1: Using CDN in HTML (Recommend) +```js +const script = document.createElement('script'); +script.onload = () => { + // sketchMeasureCompare.init(); + // or init with options + sketchMeasureCompare.init({ + zIndex: 10000, + width: 0, + enableDomRulers: false, + enableTextReplace: false, + offsetX: 0, + offsetY: 0 + }); +}; +script.src = "https://unpkg.com/sketch-measure-compare@latest/index.min.js"; +document.head.appendChild(script); +``` + +Available CDN: + +- https://unpkg.com/sketch-measure-compare@latest/index.min.js +- https://cdn.jsdelivr.net/gh/Neco86/Sketch-Measure-Compare@latest/index.min.js + +Backup: + +- https://neco86.github.io/Sketch-Measure-Compare/index.min.js + +#### Method 2: Using Chrome Extension +Download and unzip: https://github.com/Neco86/Sketch-Measure-Compare/archive/refs/heads/extension.zip + +[Load the unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked) + + +### Step2: Click the UI button to switch page + +### Step3: Choose files + +Click `upload` button to upload local sketch measure folder; + +### Step4: Compare UI and your website page + +![demo](https://raw.githubusercontent.com/Neco86/Sketch-Measure-Compare/main/demo.png) diff --git a/img/icon.png b/img/icon.png new file mode 100644 index 0000000..3fe3c42 Binary files /dev/null and b/img/icon.png differ diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..c4fb0b0 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,36 @@ +interface Config { + /** + * 页面层级 + */ + zIndex?: number; + /** + * 页面测量尺 + */ + enableDomRulers?: Boolean; + /** + * 右键文本替换 + */ + enableTextReplace?: Boolean; + /** + * 水平方向折叠 + */ + offsetX?: number; + /** + * 垂直方向折叠 + */ + offsetY?: number; + /** + * 窗口大小 + */ + width?: number; + enableBlockClose?: boolean; +} + +interface SketchMeasureCompare { + init: (config?: Config) => void; +} + +declare module 'sketch-measure-compare' { + const sketchMeasureCompare: SketchMeasureCompare; + export = sketchMeasureCompare; +} \ No newline at end of file diff --git a/index.min.js b/index.min.js new file mode 100644 index 0000000..fc5501a --- /dev/null +++ b/index.min.js @@ -0,0 +1 @@ +(()=>{"use strict";var e={539:e=>{e.exports=function(e){var t=[];return t.toString=function(){return this.map((function(t){var n="",o=void 0!==t[5];return t[4]&&(n+="@supports (".concat(t[4],") {")),t[2]&&(n+="@media ".concat(t[2]," {")),o&&(n+="@layer".concat(t[5].length>0?" ".concat(t[5]):""," {")),n+=e(t),o&&(n+="}"),t[2]&&(n+="}"),t[4]&&(n+="}"),n})).join("")},t.i=function(e,n,o,a,l){"string"==typeof e&&(e=[[null,e,void 0]]);var i={};if(o)for(var r=0;r0?" ".concat(s[5]):""," {").concat(s[1],"}")),s[5]=l),n&&(s[2]?(s[1]="@media ".concat(s[2]," {").concat(s[1],"}"),s[2]=n):s[2]=n),a&&(s[4]?(s[1]="@supports (".concat(s[4],") {").concat(s[1],"}"),s[4]=a):s[4]="".concat(a)),t.push(s))}},t}},725:e=>{e.exports=function(e){return e[1]}},693:(e,t,n)=>{n.d(t,{Z:()=>r});var o=n(725),a=n.n(o),l=n(539),i=n.n(l)()(a());i.push([e.id,"html,body{margin:0;padding:0;width:100vw;height:100vh;overflow:hidden;background-color:#fff;display:flex;align-items:center;justify-content:center}#iframe{width:100vw;height:100vh;border:0;display:none}",""]);const r=URL.createObjectURL(new Blob([i],{type:"text/css"}))}},t={};function n(o){var a=t[o];if(void 0!==a)return a.exports;var l=t[o]={id:o,exports:{}};return e[o](l,l.exports,n),l.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var o={};(()=>{n.d(o,{default:()=>y});const e=URL.createObjectURL(new Blob(["("+function(){!function e(t){if(t.isInitSketchMeasureRulersDoc)return;t.isInitSketchMeasureRulersDoc=!0;var n=function(){!function(e){Array.from(e.querySelectorAll("*")).filter((function(e){return!["SCRIPT","STYLE","TITLE","HTML","HEAD","META","BODY","NOSCRIPT"].includes(e.nodeName)})).forEach((function(e){e.isInitSketchMeasureRulersNode||(e.isInitSketchMeasureRulersNode=!0,e.addEventListener("mouseenter",(function(){window.top.sketchMeasureCompare.hover=e,window.top.sketchMeasureCompare.config.enableDomRulers&&window.parent.postMessage({msg:"updateRulers",payload:e.getBoundingClientRect()},"*")})),e.addEventListener("mouseleave",(function(){window.top.sketchMeasureCompare.hover=null,window.top.sketchMeasureCompare.config.enableDomRulers&&window.parent.postMessage({msg:"updateRulers",payload:{width:0,height:0,top:0,left:0}},"*")})))}))}(t),document.querySelectorAll("iframe").forEach((function(t){t.contentDocument&&e(t.contentDocument)}))};n(),new MutationObserver(n).observe(t.documentElement,{attributes:!0,childList:!0,subtree:!0})}(document)}.toString()+")()"],{type:"text/javascript"})),t=URL.createObjectURL(new Blob(["("+function(){var e,t,n,o,a,l,i,r,c,d,s,p,u,h,m,f,v,b,y,g=function(e,t){Object.keys(t).forEach((function(n){e.style[n]=t[n]}))},w=function(){var e=window.top.sketchMeasureCompare.config.offsetY;g(i,{top:"".concat(e,"px"),height:"calc(100% - ".concat(e,"px)")})},x=function(){var e=window.top.sketchMeasureCompare.config.offsetX;g(i,{left:"".concat(e,"px"),width:"calc(100% - ".concat(e,"px)")})},k=function(){b=window.top.sketchMeasureCompare.config.enableBlockClose,window.top.sketchMeasureCompare.config.enableBlockClose=!1},C=function(){window.top.sketchMeasureCompare.config.enableBlockClose=b},M=function(e){var t=o.cloneNode(!0);return t.appendChild(t.childNodes[0].cloneNode(!0)),t.appendChild(t.childNodes[1].cloneNode(!0)),g(t.childNodes[0],{border:"none",borderLeft:"1px dashed ".concat(e),width:"0px"}),g(t.childNodes[1],{border:"none",borderTop:"1px dashed ".concat(e),height:"0px"}),g(t.childNodes[2],{border:"none",borderRight:"1px dashed ".concat(e),width:"0px"}),g(t.childNodes[3],{border:"none",borderBottom:"1px dashed ".concat(e),height:"0px"}),t},N=function(){var e,t,l=n.style,r=l.width,c=l.height,b=l.marginTop;r!==v.style.width&&(k(),i.src="",i.src=f.value,C()),g(v,{width:r,height:c,marginTop:b,marginLeft:a.checked?0:"".concat(parseFloat(r)/2+10,"px"),opacity:a.checked?.5:1}),v.className=n.className,g(f,{width:r,marginTop:"-".concat(parseFloat(c)/2+20+10,"px"),marginLeft:a.checked?0:"".concat(parseFloat(r)/2+10,"px")}),f.className=n.className,g(d,o.style),g(d.childNodes[0],{left:o.childNodes[0].style.left}),g(d.childNodes[1],{top:o.childNodes[1].style.top}),g(d.childNodes[2],{left:"".concat(parseFloat(o.childNodes[0].style.left)+parseFloat(o.childNodes[0].style.width),"px")}),g(d.childNodes[3],{top:"".concat(parseFloat(o.childNodes[1].style.top)+parseFloat(o.childNodes[1].style.height),"px")});var y=null===(e=document.getElementsByClassName("selected"))||void 0===e||null===(t=e[0])||void 0===t?void 0:t.style;y&&(g(s,{display:""}),g(s.childNodes[0],{left:y.left}),g(s.childNodes[1],{top:y.top}),g(s.childNodes[2],{left:"".concat(parseFloat(y.left)+parseFloat(y.width),"px")}),g(s.childNodes[3],{top:"".concat(parseFloat(y.top)+parseFloat(y.height),"px")})),a.checked&&(g(o,{display:"none"}),g(d,{display:"none"}),g(s,{display:"none"}),g(p,{display:"none"}),g(m,{display:"none"}),g(u,{display:"none"}),g(h,{display:"none"}),inspector.className=inspector.className.replace("active",""))},R=function(){var b,R,E,L,T,S;e||(t=null===(T=document.getElementsByClassName("screen-viewer-inner"))||void 0===T?void 0:T[0],n=document.getElementById("screen"),o=document.getElementById("rulers"),a=document.getElementById("flow-mode"),l=null===(S=document.getElementsByClassName("header-center"))||void 0===S?void 0:S[0],t&&n&&o&&a&&l&&(e=!0,window.top.onbeforeunload=function(){return!1},(L=document.createElement("style")).innerHTML="\n .layer {\n z-index: 1;\n }\n .color-bg {\n position: relative;\n background-color: #fff;\n background-image: linear-gradient(45deg, #dddadc 25%, transparent 25%, transparent 75%, #dddadc 75%, #dddadc), linear-gradient(45deg, #dddadc 25%, transparent 25%, transparent 75%, #dddadc 75%, #dddadc);\n background-size: 12px 12px;\n background-position: 0 0, 6px 6px;\n border-radius: 2px;\n overflow: hidden;\n width: 24px;\n height: 24px;\n }\n .color-bg span {\n display: block;\n width: 24px;\n height: 24px;\n }\n ",document.head.appendChild(L),R=document.createElement("div"),(E=document.createElement("div")).innerHTML="CLEAR UI",g(R,{display:"flex",alignItems:"center",justifyContent:"center",padding:"0 25px 0 10px",whiteSpace:"nowrap"}),g(E,{padding:"5px",border:"1px solid",cursor:"pointer"}),E.onclick=function(){window.parent.postMessage({msg:"CLEAR UI"},"*")},R.appendChild(E),l.appendChild(R),i=document.createElement("iframe"),x(),w(),g(i,{border:"none",position:"absolute"}),i.width="100%",i.height="100%",i.src=window.top.location.href,i.onload=function(){if(x(),w(),i.contentDocument){if(window.top.sketchMeasureCompare.config.enableBlockClose&&(i.contentWindow.onbeforeunload=function(){return!1}),r=window.top.sketchMeasureCompare.config.enableDomRulers){var e=document.createElement("script");e.src=window.top.sketchMeasureCompare.rulers,i.contentDocument.head.appendChild(e)}if(c=window.top.sketchMeasureCompare.config.enableTextReplace){var t=document.createElement("script");t.src=window.top.sketchMeasureCompare.textReplace,i.contentDocument.head.appendChild(t)}}},d=M("#419bf9"),s=M("#ee6723"),p=document.getElementById("ld").cloneNode(!0),m=document.getElementById("td").cloneNode(!0),u=document.getElementById("rd").cloneNode(!0),h=document.getElementById("bd").cloneNode(!0),f=document.createElement("input"),g(f,{border:"none",outline:"none",height:"20px"}),f.placeholder="url",f.value=i.src,f.oninput=function(e){k(),i.src=e.target.value,C()},f.onkeyup=function(e){"Enter"===e.key&&(k(),i.src="",i.src=e.target.value,C())},v=n.cloneNode(!1),g(v,{backgroundImage:"",overflowY:"hidden"}),v.appendChild(i),v.appendChild(d),v.appendChild(s),v.appendChild(p),v.appendChild(m),v.appendChild(u),v.appendChild(h),t.appendChild(f),t.appendChild(v),fetch(window.top.sketchMeasureCompare.inspectorCenterTpl).then((function(e){return e.text()})).then((function(e){y=e})),t.addEventListener("click",(function(){inspector.className=inspector.className.replace("active","")})),b={get:function(e,t){return e[t]},set:function(e,t,n){if(e[t]=n,"offsetX"===t&&x(),"offsetY"===t&&w(),"enableBlockClose"===t&&i.contentDocument&&(i.contentWindow.onbeforeunload=function(){return!n&&void 0}),"enableDomRulers"===t&&!r){k();var o=i.src;i.src="",i.src=o,C()}if("enableTextReplace"===t&&!c){k();var a=i.src;i.src="",i.src=a,C()}}},window.top.sketchMeasureCompare.config=new Proxy(window.top.sketchMeasureCompare.config,b),new MutationObserver(N).observe(n,{attributes:!0,childList:!0,subtree:!0}),N(),window.addEventListener("message",(function(e){var t=e.data;if(t&&t.msg){var n=t.msg,l=t.payload,i=window.top.sketchMeasureCompare.config.offsetY;if("updateRulers"===n&&(g(o,{display:a.checked?"none":""}),g(o.childNodes[0],{left:"".concat(l.left,"px"),width:"".concat(l.width,"px")}),g(o.childNodes[1],{top:"".concat(l.top+i,"px"),height:"".concat(l.height,"px")}),g(d,{display:a.checked?"none":""}),g(d.childNodes[0],{left:"".concat(l.left,"px")}),g(d.childNodes[1],{top:"".concat(l.top+i,"px")}),g(d.childNodes[2],{left:"".concat(l.left+l.width,"px")}),g(d.childNodes[3],{top:"".concat(l.top+l.height+i,"px")}),"none"!==s.style.display)){var r=parseFloat(s.childNodes[0].style.left),c=parseFloat(s.childNodes[1].style.top),f=parseFloat(s.childNodes[2].style.left),v=parseFloat(s.childNodes[3].style.top),b=l.left,y=l.top+i,w=l.left+l.width,x=l.top+l.height+i,k=b||y||w||x;g(p,w<=r?{display:k?"":"none",left:"".concat(w,"px"),top:"".concat(c+(v-c)/2,"px"),width:"".concat(r-w,"px")}:{display:b=f?{display:k?"":"none",left:"".concat(f,"px"),top:"".concat(c+(v-c)/2,"px"),width:"".concat(b-f,"px")}:{display:w>r&&k?"":"none",left:"".concat(w>f?f:w,"px"),top:"".concat(c+(v-c)/2,"px"),width:"".concat(Math.abs(w-f),"px")}),u.childNodes[0].dataset.width=u.style.width,g(m,x<=c?{display:k?"":"none",left:"".concat(r+(f-r)/2,"px"),top:"".concat(x,"px"),height:"".concat(c-x,"px")}:{display:y=v?{display:k?"":"none",left:"".concat(r+(f-r)/2,"px"),top:"".concat(v,"px"),height:"".concat(y-v,"px")}:{display:x>c&&k?"":"none",left:"".concat(r+(f-r)/2,"px"),top:"".concat(x>v?v:x,"px"),height:"".concat(Math.abs(x-v),"px")}),h.childNodes[0].dataset.height=h.style.height}}})),window.addEventListener("keyup",(function(e){if("c"===e.key&&!a.checked){var n=window.top.sketchMeasureCompare.hover;t.click(),g(s,{display:""}),g(s.childNodes[0],{left:d.childNodes[0].style.left}),g(s.childNodes[1],{top:d.childNodes[1].style.top}),g(s.childNodes[2],{left:d.childNodes[2].style.left}),g(s.childNodes[3],{top:d.childNodes[3].style.top});var o=function(e){var t=+parseFloat(e).toFixed(2);return"NaN"==="".concat(t)?e:"".concat(t,"px")};if(n&&y&&inspector){inspector.className=inspector.className+" active";var l=getComputedStyle(n),i={width:o(l.width),height:o(l.height),marginTop:o(l.marginTop),marginBottom:o(l.marginBottom),marginRight:o(l.marginRight),marginLeft:o(l.marginLeft),paddingTop:o(l.paddingTop),paddingBottom:o(l.paddingTop),paddingRight:o(l.paddingRight),paddingLeft:o(l.paddingLeft),fontSize:o(l.fontSize),lineHeight:o(l.lineHeight),borderRadius:o(l.borderRadius),borderWidth:o(l.borderWidth),opacity:"".concat(100*+l.opacity,"%"),fontFamily:l.fontFamily.replace(/"/g,""),letterSpacing:l.letterSpacing,textAlign:l.textAlign,fontWeight:l.fontWeight,borderStyle:l.borderStyle,borderColor:l.borderColor,color:l.color,backgroundColor:l.backgroundColor,innerText:n.innerText.trim()};inspectorCenter.innerHTML=y.split(/([a-z]+="\${.*?}")/).map((function(e){if(/([a-z]+="\${.*?}")/.test(e)){var t=e.match(/([a-z]+)="\${(.*?)}"/),n=t[1],o=t[2];if("color"===n)return'style="background-color: '.concat(i[o],';"');if("value"===n)return'value="'.concat(i[o],'"')}return e})).join("").replace("${innerText}",i.innerText)}}})),window.addEventListener("keyup",(function(e){if("r"===e.key&&!a.checked){k();var t=i.src;i.src="",i.src=t,C()}}))))};setTimeout(R,0),setTimeout(R,220),setTimeout(R,500),setTimeout(R,1e3),setTimeout(R,3e3),setTimeout(R,5e3)}.toString()+")()"],{type:"text/javascript"})),a=URL.createObjectURL(new Blob(["("+function(){var e=document.getElementById("uploader"),t=document.getElementById("iframe"),n=document.getElementById("placeholder"),o=[];e.onchange=function(a){var l,i,r,c,d=a.target.files;if(d.length){var s=!!(o=Array.from(d)).find((function(e){return"index.html"===e.name})),p=o.filter((function(e){return/image/.test(e.type)}));if(!s&&!p.length)return e.value="",void alert("WRONG FOLDER!");!s&&p.length&&(window.top.sketchMeasureCompare.tplImgList=p.map((function(e){return{name:e.name.replace(/\.[^\.]+$/g,""),url:"".concat(URL.createObjectURL(e),"#")}})));var u=null!==(l=null===(i=o.find((function(e){return"index.html"===e.name})))||void 0===i||null===(r=i.webkitRelativePath)||void 0===r||null===(c=r.replace)||void 0===c?void 0:c.call(r,"index.html",""))&&void 0!==l?l:"",h=new RegExp("^".concat(u)),m=(o=o.map((function(e){return{name:e.name,path:e.webkitRelativePath.replace(h,""),url:URL.createObjectURL(e)}}))).find((function(e){return"index.html"===e.name}))||{url:window.top.sketchMeasureCompare.tpl};m&&fetch(m.url).then((function(e){return e.text()})).then((function(e){return o.forEach((function(t){var n=t.path.split("/").map(encodeURIComponent).join("/");e=e.replace(n,"".concat(t.url,"#"))})),e=e.replace(/\.\/blob/g,"blob")})).then((function(e){return e=e.replace("",' + +