-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
1 lines (1 loc) · 7.53 KB
/
plugin.js
1
function S(e){penpot.ui.sendMessage({pluginMessage:e})}function v(e){if("type"in e)return e}function q(e){switch(e.type){case"board":case"group":return e}}function A(e){switch(e.type){case"board":case"group":return e}}function Q(e){return e.type==="board"&&(!e.parent||e.parent.id===e.id)}function Z(){return{type:"icon-sets",state:{}}}function _(e){return{type:"icon-set",prefix:e,state:{},parent:Z()}}const H=new Set(["animate","animateTransform","animateMotion"]),ee=new Set(["set","discard"]);[...H,...ee];function te(e){function n(){if(e.getSharedPluginData("iconify","source")==="iconify"){const i=JSON.parse(e.getSharedPluginData("iconify","props")),c=i.name.split(":");if(c.length===2)switch(i.version){case 1:{const o=i.route||_(c[0]);return Object.assign(i,{route:o})}}}}try{return n()}catch{}}function ne(e){return{nodes:e.nodes||Object.create(null),page:e.page||null,ignoreIconNode:e.ignoreIconNode||!1,iconNodeID:e.iconNodeID||null,iconNodeData:e.iconNodeData||null}}function oe(e,n,t,i){function r(o){const a="page",s=t.nodes[a]??t.page;if(s)return o&&s.children.push(o),t.page=t.nodes[a]=s,s;const p={type:"page",id:a,name:e.name,target:"valid",children:o?[o]:[],primary:i,relative:!0};return t.nodes[p.id]=p,t.page=p,p}function c(o,a,s){if(Q(o))return r(s);const p=o.id,u=t.nodes[p];if(u)return s&&u.children.push(s),u;let f="invalid",I=!1,h,l;const d=A(o);d&&(h=te(d),h&&(l=d.height,t.iconNodeID?t.ignoreIconNode=!0:(t.iconNodeID=p,t.iconNodeData=h)));const g=q(o);if(g){const k=v(g);k?(k.blocked||(f="valid"),(!a||k.blocked)&&(t.ignoreIconNode=!0),I=k.type!=="board"):f="valid"}else t.ignoreIconNode=!0;const b={type:o.type,id:p,name:o.name,target:f,children:s?[s]:[],primary:a,relative:I,icon:h,height:l};t.nodes[p]=b;const T=o.parent;return T?c(T,a,o.id):r(o.id),b}return c(n,i)}const ie=16;function M(){const e=penpot.currentPage;return{defaultNode:"page",nodes:[{type:"page",id:"page",name:(e==null?void 0:e.name)??"Page"}]}}function $(){const e=penpot.currentPage,n=penpot.selection;if(!e||!n.length)return M();const t=ne({});n.forEach((l,d)=>{oe(e,l,t,!d)});const{nodes:i,page:r,iconNodeData:c,iconNodeID:o,ignoreIconNode:a}=t;if(!r)return M();const s=[];let p=r.type,u;function f(l,d=0){switch(l.target){case"ignored":{l.children.forEach(g=>{f(i[g],d)});break}case"valid":switch(l.type){case"page":{const g={type:"page",id:"page",name:l.name};s.push(g),l.children.forEach(b=>{f(i[b],d+1)});break}case"board":case"group":{const g={id:l.id,name:l.name,depth:d,type:l.type};!a&&c&&o===l.id&&(g.icon=c,g.height=l.height,u=g),l.icon||(s.push(g),l.primary&&!l.icon&&(p=l.id),l.children.forEach(b=>{f(i[b],d+1)}))}}}}f(r);let I=!1,h=ie;for(;s.length>h;)s.pop()===u&&(I=!0,h--);return I&&u&&s.push(u),{nodes:s,defaultNode:p,iconNode:u}}let F=$();function L(){return F}function re(e){F=e}let x=!1;function ce(){const e=$();JSON.stringify(e)!==JSON.stringify(L())&&(re(e),S({type:"plugin:nodes",nodes:e}))}function P(){x||(x=!0,setTimeout(()=>{x=!1,ce()},250))}function N(e,n,t=void 0){var i;try{const r=(i=penpot.currentFile)==null?void 0:i.getPluginData(e);if(typeof r=="string")return n?r:JSON.parse(r)}catch{}return t}function m(e,n){var t;(t=penpot.currentFile)==null||t.setPluginData(e,typeof n=="string"?n:JSON.stringify(n))}const j="recent";let y=[];const se=128;function ae(){const e=N(j);return e&&(y=e),e}let D=!1;function z(){D||(D=!0,setTimeout(()=>{D=!1,m(j,y),S({type:"plugin:recent-icons",icons:y})},1e3))}function V(e){for(const n of e){const t=y.indexOf(n);t!==-1&&y.splice(t,1),y.unshift(n),y.length>se&&y.pop(),z()}}function ue(){y=[],z()}function B(e,n,t,i){e.name=n,e.setSharedPluginData("iconify","source","iconify");const r={version:1,name:n,props:t,route:i};e.setSharedPluginData("iconify","props",JSON.stringify(r))}function E(e,n){const t=n/e.height,i=e.width/e.height;e.proportionLock=!1,e.resize(n*i,n),e.proportionLock=!0;function r(c){switch(c.type){case"group":case"board":c.children.forEach(r);break;default:try{const o=[...c.strokes];let a=!1;o.forEach((s,p)=>{const u=s.strokeWidth;u&&(o[p]={...s,strokeWidth:u*t},a=!0)}),a&&(c.strokes=o)}catch(o){console.log(o)}}}r(e)}function U(e,n){const t=penpot.createShapeFromSvg(e);if(!t)return null;let i=n.replace;if(i){const c=i.parent;if(!c)return t.remove(),null;const o=c.children.findIndex(p=>p.id===i.id),a=i.parentX,s=i.parentY;return t.height!==i.height&&E(t,i.height),c.insertChild(o<0?0:o,t),t.parentX=a,t.parentY=s,t.proportionLock=!0,i.remove(),t}const r=i||t;return n.parent&&n.parent.appendChild(r),r.proportionLock=!0,n.height&&r.height!==n.height&&E(r,n.height),n.x&&(r.parentX=n.x),n.y&&(r.parentY=n.y),r}function O(e,n,t,i){const r=v(e);if(!r)return n;let c=0,o=i?r.width:r.height;if(n<c)return c;const a=c+o-t;return n>a?Math.max(c,a):n}function G(e,n,t){const i=n.icons.reduce((o,a)=>o+Math.ceil(a.width),0);let r=O(e,Math.round(t.targetX-i/2),i,!0);const c=[];for(const o of n.icons){const a=O(e,Math.round(t.targetY-o.height/2),o.height,!1),s=U(o.content,{parent:v(e),x:r,y:a,height:o.height});s&&(B(s,o.name,n.props,n.route),r+=Math.ceil(o.width),c.push(s))}return V(n.icons.map(o=>o.name)),S({type:"plugin:notice",color:"success",text:`Imported ${n.icons.length>1?n.icons.length+" icons":n.icons[0].name}`}),penpot.selection=c,P(),c}function w(){S({type:"plugin:notice",color:"error",text:"Error importing icon(s) to Penpot"})}function pe(e,n){const t=penpot.currentPage,i=t==null?void 0:t.getShapeById(e);if(!t||!i){w();return}const r=i.name;for(const c of n.icons){const o=c.name,a=U(c.content,{replace:i,x:i.parentX,y:i.parentY});if(!a){w();return}B(a,o,n.props,n.route),V([o]),S({type:"plugin:notice",color:"success",text:`Replaced ${r} with ${o}`}),penpot.selection=[a],P();return}w()}function le(e,n){const t=penpot.currentPage;if(!t){w();return}const i=e==="page"?t:t.getShapeById(e);if(!i){w();return}const r=v(i);let c,o;if(r)c=r.width/2,o=r.height/2;else{const a=penpot.viewport.center;c=a.x,o=a.y}G(i,n,{targetX:c,targetY:o})}function fe(e){const n=penpot.currentPage;if(!n){w();return}const t=e.data;t.icons[0];let i=e.drop.x,r=e.drop.y;const c=penpot.viewport,o=c.center.x,a=c.center.y,s=c.zoom,p=c.bounds.width*s,u=c.bounds.height*s,I=e.window.width-p-276*2;i-=I/2;const h=e.window.height-u;r-=h/2;const l=Math.round(o+i/s),d=Math.round(a+r/s);G(n,t,{targetX:l,targetY:d})}const X="icon-sets",Y="route-v31",K="recent-colors",R="customisations",W="pinned-filters",C="sort-icon-sets",J="group-icon-sets";function de(){console.log("Starting plugin..."),penpot.on("themechange",p=>{S({type:"plugin:theme",theme:p})});const e=N(X,!1,void 0),n=(e==null?void 0:e.version)===1?e.data:void 0;let t=N(Y,!1);const i=ae(),r=N(K,!1),c=N(W,!1),o=N(C,!0,"auto"),a=N(J,!0,"auto");let s=N(R,!1);penpot.on("selectionchange",P),penpot.on("pagechange",P),penpot.ui.onMessage(p=>{try{if(typeof(p==null?void 0:p.pluginMessage)!="object")return}catch(f){console.error(f)}const u=p.pluginMessage;switch(u.type){case"ui:fatal-error":{console.error("Fatal error:",u.error),penpot.closePlugin();break}case"ui:loaded":{S({type:"plugin:starting",nodes:L(),lists:n,pinned:c,recent:i,route:t,recentColors:r,custom:s,sortIconSets:o,groupIconSets:a});break}case"ui:lists":{const f={version:1,data:u.lists};m(X,f);break}case"ui:pinned-filters":{m(W,u.pinned);break}case"ui:route":{m(Y,u.route);break}case"ui:replace-icon":{pe(u.node,u.data);break}case"ui:import-icons":{le(u.node,u.data);break}case"ui:drop-icon":{fe(u);break}case"ui:recent-colors":{m(K,u.colors);break}case"ui:customisations":{m(R,u.custom);break}case"ui:reset-recent-icons":{ue();break}case"ui:sort":{m(C,u.sortIconSets),m(J,u.groupIconSets);break}}}),penpot.ui.open("Iconify",`./index.html?theme=${penpot.theme}`,{width:570,height:640})}de();