From 69dc08290360711703c386adab3bb9075159c545 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Tue, 22 Oct 2024 19:36:56 +0300 Subject: [PATCH] feat: version 3.1 of plugin --- dist/index.html | 184 +++++++++++++----------------------------------- dist/plugin.js | 2 +- 2 files changed, 50 insertions(+), 136 deletions(-) diff --git a/dist/index.html b/dist/index.html index 304db61..ebd9aeb 100644 --- a/dist/index.html +++ b/dist/index.html @@ -4,7 +4,7 @@ Iconify - - diff --git a/dist/plugin.js b/dist/plugin.js index 4867e02..f1a5f8e 100644 --- a/dist/plugin.js +++ b/dist/plugin.js @@ -1 +1 @@ -const M={compactWidth:!1,v4Notice:!1},m=Object.assign({},M);function fe(e){var o;m.v4Notice=!0;const t=["compactWidth","windowAction","selectAfterImport","customizeDrop","dropToFrame"];for(const n of t){const i=(o=e.options)==null?void 0:o[n];i!==void 0&&(m[n]=i)}return m}async function ue(){try{const e=await figma.clientStorage.getAsync("config");switch(e==null?void 0:e.version){case 2:fe(e);break;case 3:Object.assign(m,e);break}}catch(e){}return m}function z(){const e={version:3};let t;for(t in M){const o=m[t],n=M[t];n!==void 0&&n!==o&&(e[t]=o)}figma.clientStorage.setAsync("config",e).catch()}function ge(){const e=figma;try{const t=e.editorType;switch(t){case"figma":case"figjam":return t}}catch(t){console.error(t)}try{if(typeof e.createSticky=="function")return"figjam"}catch(t){console.error(t)}return"figma"}const p={app:ge(),loaded:!1,minimized:!1,config:m};function F(){const e=p.app;return e==="figjam"?"FigJam":e.slice(0,1).toUpperCase()+e.slice(1)}const _=24,T=10,de=4,he=8,G=de*2*T+_+he*2,O={mini:{width:200,height:88+_},full:{width:48*T+G,min:580,max:720},compact:{width:32*T+G,min:520,max:640},innerDiff:90,outerDiff:150};function x(){const e=p.config.compactWidth,t=O[e?"compact":"full"];let o;if(p.windowInnerHeight){const n=p.windowOuterHeight?p.windowOuterHeight-O.outerDiff:p.windowInnerHeight-O.innerDiff;o=Math.max(Math.min(n,t.max),t.min)}else o=t.max;return{width:t.width,height:o}}function y(e){figma.ui.postMessage(e)}function pe(e){switch(e.type){case"PAGE":case"COMPONENT":case"FRAME":case"SECTION":case"GROUP":return e}}function me(e){switch(e.type){case"FRAME":case"COMPONENT":return e}}function U(e){return{type:"icon-set",prefix:e,parent:{type:"icon-sets"}}}function ye(e){function t(){if(e.getSharedPluginData("iconify","source")==="iconify"){const n=JSON.parse(e.getSharedPluginData("iconify","props")),c=n.name.split(":");if(c.length===2)switch(n.version){case 3:{const r=n.route||U(c[0]);return Object.assign(n,{route:r})}default:{const r=n.props;let s;return typeof n.color=="string"?s=n.color:typeof r=="object"&&(s=r.color),{version:3,name:n.name,props:{color:s},route:U(c[0])}}}}}try{return t()}catch(o){}}function Y(e){return{nodes:e.nodes||Object.create(null),ignoreIconNode:e.ignoreIconNode||!1,iconNodeID:e.iconNodeID||null,iconNodeData:e.iconNodeData||null}}function A(e,t,o){function n(i,c,r){const s=i.id,l=t.nodes[s];if(l)return r&&l.children.push(r),l;let a="invalid",h=!1,g,f;const u=me(i);u&&(g=ye(u),g&&(f=u.height,t.iconNodeID?t.ignoreIconNode=!0:(t.iconNodeID=s,t.iconNodeData=g)));const d=pe(i);d?d.type==="PAGE"?(a="valid",h=!0):(d.locked||(a="valid"),(!c||d.locked)&&(t.ignoreIconNode=!0),h=d.type!=="FRAME"):i.type==="COMPONENT_SET"?a="ignored":t.ignoreIconNode=!0;const w={type:i.type,id:s,name:i.name,target:a,children:r?[r]:[],primary:c,relative:h,icon:g,height:f};return t.nodes[s]=w,i.parent&&i.type!=="PAGE"&&n(i.parent,c,i.id),w}return n(e,o)}const Ne=16;function X(){const e=figma.currentPage,t=e.selection,o=Y({}),n=A(e,o,!1);t.forEach((d,w)=>{A(d,o,!w)});const{nodes:i,iconNodeData:c,iconNodeID:r,ignoreIconNode:s}=o,l=[];let a=n.id,h;function g(d,w=0){switch(d.target){case"ignored":{d.children.forEach(I=>{g(i[I],w)});break}case"valid":switch(d.type){case"PAGE":case"COMPONENT":case"FRAME":case"SECTION":case"GROUP":const I={id:d.id,name:d.name,depth:w,type:d.type};!s&&c&&r===d.id&&(I.icon=c,I.height=d.height,h=I),d.icon||(l.push(I),d.primary&&!d.icon&&(a=d.id),d.children.forEach(le=>{g(i[le],w+1)}))}}}g(n);let f=!1,u=Ne;for(;l.length>u;)l.pop()===h&&(f=!0,u--);return f&&h&&l.push(h),{nodes:l,defaultNode:a,iconNode:h}}let q=X();function Q(){return q}function we(e){q=e}const D="style:";function Z(e){function t(o){const n=Math.round(o*255).toString(16);return n.length<2?"0"+n:n}return"#"+t(e.r)+t(e.g)+t(e.b)}function H(e){const t=e.paints[0].color;return{id:e.id,name:e.name,color:Z(t),remote:e.remote}}function W(e){return e.type==="SOLID"&&e.visible!==!1&&e.blendMode==="NORMAL"&&e.opacity===1?e:void 0}function k(e){if(e.paints.length===1)return W(e.paints[0])}function ee(e){const t=figma.getStyleById(e);if((t==null?void 0:t.type)==="PAINT"&&k(t))return t.id}const v=new Set;async function Ie(){const e=[];return(await figma.getLocalPaintStylesAsync()).forEach(t=>{try{v.add(t.id),k(t)&&e.push(H(t))}catch(o){console.log("Error parsing color style:",o)}}),e}function te(){const e=figma.getSelectionColors(),t=[];return e==null||e.styles.forEach(o=>{const n=o.id;v.has(n)||(v.add(n),k(o)&&t.push(H(o)))}),t.length?t:void 0}function Pe(e){async function t(o){const n=[];for(const i of o)if(i.startsWith(D)){const c=i.slice(D.length);if(!v.has(c)){v.add(c);const r=await figma.getStyleByIdAsync(c);(r==null?void 0:r.type)==="PAINT"&&k(r)&&n.push(H(r))}}n.length&&y({type:"plugin:color-styles",styles:n})}e&&setTimeout(()=>{t(e).catch(console.error)})}let b=!1;function Se(){const e=X();if(JSON.stringify(e)!==JSON.stringify(Q())){we(e);const t=te();y({type:"plugin:nodes",nodes:e,styles:t})}}function E(){b||(b=!0,setTimeout(()=>{b=!1,Se()},250))}async function P(e,t=void 0,o){try{const n=await figma.clientStorage.getAsync(e);if(n)return o?o(n):n}catch(n){}return t}function S(e,t){figma.clientStorage.setAsync(e,t).catch()}const ne="recent";let N=[];const ve=64;async function ke(){const e=await P(ne);return e&&(N=e),e}let C=!1;function oe(){C||(C=!0,setTimeout(()=>{C=!1,S(ne,N),y({type:"plugin:recent-icons",icons:N})},1e3))}function ie(e){for(const t of e){const o=N.indexOf(t);o!==-1&&N.splice(o,1),N.unshift(t),N.length>ve&&N.pop(),oe()}}function De(){N=[],oe()}async function ce(e,t){function o(c){try{if(c.length===1){const r=c[0];if(W(c[0]))return!0}}catch(r){}}async function n(c){o(c.fills)&&(typeof t=="string"?await c.setFillStyleIdAsync(t):c.fills=[t]),o(c.strokes)&&(typeof t=="string"?await c.setStrokeStyleIdAsync(t):c.strokes=[t])}async function i(c){const r=c;if(r.locked||r.isMask)return;try{await n(r)}catch(l){}const s=c.children;if(s)for(const l of s)await i(l)}for(const c of e.children)await i(c)}const Ee={replace:"Replace icon",code:"Icon code"};function re(e,t,o,n){e.name=t,e.setSharedPluginData("iconify","source","iconify");const i={version:3,name:t,props:o,route:n};e.setSharedPluginData("iconify","props",JSON.stringify(i)),e.setRelaunchData(Ee)}function se(e,t){const o=figma.createNodeFromSvg(e);let n;if(t.replace?n=t.replace:t.component&&(n=figma.createComponent()),n){for(;n.children.length>0;)n.children[0].remove();n.resizeWithoutConstraints(o.width,o.height);for(const c of o.children)n.appendChild(c);if(o.remove(),t.replace)return n}const i=n||o;if(t.parent){const c=t.parent;let r=!0;const s=c.layoutMode;s&&s!=="NONE"&&(r=!1),r?c.appendChild(i):c.insertChild(0,i),t.x&&(i.x=t.x),t.y&&(i.y=t.y)}return i.constrainProportions=!0,i}function j(e,t,o,n){if(e.type==="PAGE")return t;let i=0,c=n?e.width:e.height;if(e.type==="GROUP"&&(i=n?e.x:e.y),tr?Math.max(i,r):t}function ae(e,t,o){const n=t.icons.reduce((s,l)=>s+Math.ceil(l.width),0);let i=j(e,Math.round(o.targetX-n/2),n,!0),c;t.props.style&&(c=ee(t.props.style));const r=[];for(const s of t.icons){const l=j(e,Math.round(o.targetY-s.height/2),s.height,!1),a=se(s.content,{parent:e,x:i,y:l,component:o.component});re(a,s.name,t.props,t.route),i+=Math.ceil(s.width),r.push(a),s.monotone&&c&&ce(a,c).catch(console.error)}return ie(t.icons.map(s=>s.name)),y({type:"plugin:notice",color:"success",text:`Imported ${t.icons.length>1?t.icons.length+" icons":t.icons[0].name} to ${F()}`}),figma.currentPage.selection=r,E(),r}function B(){y({type:"plugin:notice",color:"error",text:"Error dropping icon(s) to "+F()})}function Oe(e){try{if(e.dropMetadata.source!=="iconify")return!0}catch(f){return!0}let t=e.x,o=e.y;const n=e.node;n.type==="GROUP"&&(t+=n.x,o+=n.y);const i=Y({});A(n,i,!0);const c=i.nodes[figma.currentPage.id];if(!c)return console.error("Cannot find current page item in scanned nodes list"),B(),!0;let r=0,s=0,l=!0,a=c,h=c;for(;a.children.length;){const f=a.children[0];if(a=i.nodes[f],a.icon||a.target==="invalid"?l=!1:l&&a.target==="valid"&&(h=a),!l&&a.relative){const u=figma.getNodeById(a.id);u&&(r+=u.x||0,s+=u.y||0)}}const g=figma.getNodeById(h.id);return g?(ae(g,e.dropMetadata,{targetX:r+t,targetY:s+o}),!1):(console.error("Failed to get target node"),B(),!0)}function R(){y({type:"plugin:notice",color:"error",text:"Error importing icon(s) to "+F()})}function xe(e,t){const o=figma.getNodeById(e);if(!o){R();return}const n=o.name;let i,c;const r=figma.currentPage.selection;if(r.length===1&&r[0].id===o.id){const s=figma.getSelectionColors();if(s){const l=s.paints,a=s.styles;l.length+a.length===1?(a.length&&k(a[0])&&(i=a[0].id,c=D+i),l.length&&(i=W(l[0]),i&&(c=Z(i.color)))):t.props.style&&(i=ee(t.props.style))}}for(const s of t.icons){const l=s.name,a=se(s.content,{replace:o,x:o.x,y:o.y});re(a,l,t.props,t.route),ie([l]),y({type:"plugin:notice",color:"success",text:`Replaced ${n} with ${l}`}),figma.currentPage.selection=[a],E(),s.monotone&&i&&ce(a,i).catch(console.error),s.monotone&&c&&y({type:"plugin:recent-color",color:c});return}R()}function be(e,t,o){const n=figma.getNodeById(e);if(!n){R();return}let i,c;switch(n.type){case"PAGE":{const r=figma.viewport.center;i=r.x,c=r.y;break}case"GROUP":{i=n.x+n.width/2,c=n.y+n.height/2;break}default:{i=n.width/2,c=n.height/2;break}}ae(n,t,{component:o,targetX:i,targetY:c})}const K="icon-sets",V="route-v3",$="icon-sets-filters",J="recent-colors",L="customisations";(async()=>{var h;console.log("Starting plugin..."),await ue();const e=await P(K,void 0,g=>g.version===3?g.data:void 0);let t=await P(V);const o=await ke(),n=await P($),i=await P(J);let c=await P(L);const r=Q(),s=te();let l;if(figma.command==="replace"){const g=(h=r.iconNode)==null?void 0:h.icon;if(g){l=g.name,g.route&&(t=g.route);const f=g.props;f&&(c={size:f.size,color:f.style?D+f.style:f.color})}}figma.on("selectionchange",E),figma.on("currentpagechange",E),figma.on("drop",Oe),figma.ui.onmessage=g=>{try{if(typeof g.type!="string")return}catch(u){console.error(u)}const f=g;switch(f.type){case"ui:fatal-error":{figma.closePlugin(f.error);break}case"ui:loaded":{if(f.innerHeight||f.outerHeight){p.windowInnerHeight=f.innerHeight,p.windowOuterHeight=f.outerHeight;const u=x();u.height!==p.lastWindowHeight&&(p.lastWindowHeight=u.height,figma.ui.resize(u.width,u.height))}y({type:"plugin:starting",app:p.app,command:figma.command,config:m,nodes:r,styles:s,lists:e,recent:o,route:t,filters:n,selectIcon:l,recentColors:i,custom:c}),Ie().then(u=>{u.length&&y({type:"plugin:color-styles",styles:u})}).catch(console.error),Pe(i);break}case"ui:close":{figma.closePlugin();break}case"ui:compact":{m.compactWidth=!m.compactWidth;const u=x();u.height!==p.lastWindowHeight&&(p.lastWindowHeight=u.height,figma.ui.resize(u.width,u.height)),y({type:"plugin:resize",compactWidth:m.compactWidth,minimized:!1}),z();break}case"ui:dismiss-v4":{m.v4Notice=!1,z();break}case"ui:lists":{const u={version:3,data:f.lists};S(K,u);break}case"ui:route":{S(V,f.route);break}case"ui:filters":{S($,f.filters);break}case"ui:replace-icon":{xe(f.node,f.data);break}case"ui:import-icons":{be(f.node,f.data,f.component||!1);break}case"ui:recent-colors":{S(J,f.colors);break}case"ui:customisations":{S(L,f.custom);break}case"ui:reset-recent-icons":{De();break}}};const a=x();p.lastWindowHeight=a.height,figma.showUI(__html__,Object.assign({themeColors:!1},a))})(); +const M={compactWidth:!1,v4Notice:!1},p=Object.assign({},M);function se(e){var o;p.v4Notice=!0;const t=["compactWidth","windowAction","selectAfterImport","customizeDrop","dropToFrame"];for(const n of t){const i=(o=e.options)==null?void 0:o[n];i!==void 0&&(p[n]=i)}return p}async function ae(){try{const e=await figma.clientStorage.getAsync("config");switch(e==null?void 0:e.version){case 2:se(e);break;case 3:Object.assign(p,e);break}}catch(e){}return p}function W(){const e={version:3};let t;for(t in M){const o=p[t],n=M[t];n!==void 0&&n!==o&&(e[t]=o)}figma.clientStorage.setAsync("config",e).catch()}function le(){const e=figma;try{const t=e.editorType;switch(t){case"figma":case"figjam":return t}}catch(t){console.error(t)}try{if(typeof e.createSticky=="function")return"figjam"}catch(t){console.error(t)}return"figma"}const h={app:le(),loaded:!1,minimized:!1,config:p};function R(){const e=h.app;return e==="figjam"?"FigJam":e.slice(0,1).toUpperCase()+e.slice(1)}const z=32,O={mini:{width:200,height:88},full:{width:64*8+z+24,min:580,max:720},compact:{width:56*7+z+12,min:520,max:640},innerDiff:90,outerDiff:150};function x(){const e=h.config.compactWidth,t=O[e?"compact":"full"];let o;if(h.windowInnerHeight){const n=h.windowOuterHeight?h.windowOuterHeight-O.outerDiff:h.windowInnerHeight-O.innerDiff;o=Math.max(Math.min(n,t.max),t.min)}else o=t.max;return{width:t.width,height:o}}function m(e){figma.ui.postMessage(e)}function fe(e){switch(e.type){case"PAGE":case"COMPONENT":case"FRAME":case"SECTION":case"GROUP":return e}}function ue(e){switch(e.type){case"FRAME":case"COMPONENT":return e}}function G(e){return{type:"icon-set",prefix:e,state:{},parent:{type:"icon-sets",state:{}}}}function ge(e){function t(){if(e.getSharedPluginData("iconify","source")==="iconify"){const n=JSON.parse(e.getSharedPluginData("iconify","props")),c=n.name.split(":");if(c.length===2)switch(n.version){case 3:{const r=n.route||G(c[0]);return Object.assign(n,{route:r})}default:{const r=n.props;let s;return typeof n.color=="string"?s=n.color:typeof r=="object"&&(s=r.color),{version:3,name:n.name,props:{color:s},route:G(c[0])}}}}}try{return t()}catch(o){}}function J(e){return{nodes:e.nodes||Object.create(null),ignoreIconNode:e.ignoreIconNode||!1,iconNodeID:e.iconNodeID||null,iconNodeData:e.iconNodeData||null}}function T(e,t,o){function n(i,c,r){const s=i.id,f=t.nodes[s];if(f)return r&&f.children.push(r),f;let a="invalid",g=!1,l,u;const y=ue(i);y&&(l=ge(y),l&&(u=y.height,t.iconNodeID?t.ignoreIconNode=!0:(t.iconNodeID=s,t.iconNodeData=l)));const d=fe(i);d?d.type==="PAGE"?(a="valid",g=!0):(d.locked||(a="valid"),(!c||d.locked)&&(t.ignoreIconNode=!0),g=d.type!=="FRAME"):i.type==="COMPONENT_SET"?a="ignored":t.ignoreIconNode=!0;const w={type:i.type,id:s,name:i.name,target:a,children:r?[r]:[],primary:c,relative:g,icon:l,height:u};return t.nodes[s]=w,i.parent&&i.type!=="PAGE"&&n(i.parent,c,i.id),w}return n(e,o)}const de=16;function L(){const e=figma.currentPage,t=e.selection,o=J({}),n=T(e,o,!1);t.forEach((d,w)=>{T(d,o,!w)});const{nodes:i,iconNodeData:c,iconNodeID:r,ignoreIconNode:s}=o,f=[];let a=n.id,g;function l(d,w=0){switch(d.target){case"ignored":{d.children.forEach(I=>{l(i[I],w)});break}case"valid":switch(d.type){case"PAGE":case"COMPONENT":case"FRAME":case"SECTION":case"GROUP":const I={id:d.id,name:d.name,depth:w,type:d.type};!s&&c&&r===d.id&&(I.icon=c,I.height=d.height,g=I),d.icon||(f.push(I),d.primary&&!d.icon&&(a=d.id),d.children.forEach(re=>{l(i[re],w+1)}))}}}l(n);let u=!1,y=de;for(;f.length>y;)f.pop()===g&&(u=!0,y--);return u&&g&&f.push(g),{nodes:f,defaultNode:a,iconNode:g}}let _=L();function Y(){return _}function he(e){_=e}const D="style:";function X(e){function t(o){const n=Math.round(o*255).toString(16);return n.length<2?"0"+n:n}return"#"+t(e.r)+t(e.g)+t(e.b)}function F(e){const t=e.paints[0].color;return{id:e.id,name:e.name,color:X(t),remote:e.remote}}function H(e){return e.type==="SOLID"&&e.visible!==!1&&e.blendMode==="NORMAL"&&e.opacity===1?e:void 0}function k(e){if(e.paints.length===1)return H(e.paints[0])}function q(e){const t=figma.getStyleById(e);if((t==null?void 0:t.type)==="PAINT"&&k(t))return t.id}const v=new Set;async function pe(){const e=[];return(await figma.getLocalPaintStylesAsync()).forEach(t=>{try{v.add(t.id),k(t)&&e.push(F(t))}catch(o){console.log("Error parsing color style:",o)}}),e}function Q(){const e=figma.getSelectionColors(),t=[];return e==null||e.styles.forEach(o=>{const n=o.id;v.has(n)||(v.add(n),k(o)&&t.push(F(o)))}),t.length?t:void 0}function me(e){async function t(o){const n=[];for(const i of o)if(i.startsWith(D)){const c=i.slice(D.length);if(!v.has(c)){v.add(c);const r=await figma.getStyleByIdAsync(c);(r==null?void 0:r.type)==="PAINT"&&k(r)&&n.push(F(r))}}n.length&&m({type:"plugin:color-styles",styles:n})}e&&setTimeout(()=>{t(e).catch(console.error)})}let b=!1;function ye(){const e=L();if(JSON.stringify(e)!==JSON.stringify(Y())){he(e);const t=Q();m({type:"plugin:nodes",nodes:e,styles:t})}}function E(){b||(b=!0,setTimeout(()=>{b=!1,ye()},250))}async function S(e,t=void 0,o){try{const n=await figma.clientStorage.getAsync(e);if(n)return o?o(n):n}catch(n){}return t}function P(e,t){figma.clientStorage.setAsync(e,t).catch()}const Z="recent";let N=[];const Ne=128;async function we(){const e=await S(Z);return e&&(N=e),e}let C=!1;function ee(){C||(C=!0,setTimeout(()=>{C=!1,P(Z,N),m({type:"plugin:recent-icons",icons:N})},1e3))}function te(e){for(const t of e){const o=N.indexOf(t);o!==-1&&N.splice(o,1),N.unshift(t),N.length>Ne&&N.pop(),ee()}}function Ie(){N=[],ee()}async function ne(e,t){function o(c){try{if(c.length===1){const r=c[0];if(H(c[0]))return!0}}catch(r){}}async function n(c){o(c.fills)&&(typeof t=="string"?await c.setFillStyleIdAsync(t):c.fills=[t]),o(c.strokes)&&(typeof t=="string"?await c.setStrokeStyleIdAsync(t):c.strokes=[t])}async function i(c){const r=c;if(r.locked||r.isMask)return;try{await n(r)}catch(f){}const s=c.children;if(s)for(const f of s)await i(f)}for(const c of e.children)await i(c)}const Se={replace:"Replace icon",code:"Icon code"};function oe(e,t,o,n){e.name=t,e.setSharedPluginData("iconify","source","iconify");const i={version:3,name:t,props:o,route:n};e.setSharedPluginData("iconify","props",JSON.stringify(i)),e.setRelaunchData(Se)}function ie(e,t){const o=figma.createNodeFromSvg(e);let n;if(t.replace?n=t.replace:t.component&&(n=figma.createComponent()),n){for(;n.children.length>0;)n.children[0].remove();n.resizeWithoutConstraints(o.width,o.height);for(const c of o.children)n.appendChild(c);if(o.remove(),t.replace)return n}const i=n||o;if(t.parent){const c=t.parent;let r=!0;const s=c.layoutMode;s&&s!=="NONE"&&(r=!1),r?c.appendChild(i):c.insertChild(0,i),t.x&&(i.x=t.x),t.y&&(i.y=t.y)}return i.constrainProportions=!0,i}function U(e,t,o,n){if(e.type==="PAGE")return t;let i=0,c=n?e.width:e.height;if(e.type==="GROUP"&&(i=n?e.x:e.y),tr?Math.max(i,r):t}function ce(e,t,o){const n=t.icons.reduce((s,f)=>s+Math.ceil(f.width),0);let i=U(e,Math.round(o.targetX-n/2),n,!0),c;t.props.style&&(c=q(t.props.style));const r=[];for(const s of t.icons){const f=U(e,Math.round(o.targetY-s.height/2),s.height,!1),a=ie(s.content,{parent:e,x:i,y:f,component:o.component});oe(a,s.name,t.props,t.route),i+=Math.ceil(s.width),r.push(a),s.monotone&&c&&ne(a,c).catch(console.error)}return te(t.icons.map(s=>s.name)),m({type:"plugin:notice",color:"success",text:`Imported ${t.icons.length>1?t.icons.length+" icons":t.icons[0].name} to ${R()}`}),figma.currentPage.selection=r,E(),r}function j(){m({type:"plugin:notice",color:"error",text:"Error dropping icon(s) to "+R()})}function Pe(e){try{if(e.dropMetadata.source!=="iconify")return!0}catch(u){return!0}let t=e.x,o=e.y;const n=e.node;n.type==="GROUP"&&(t+=n.x,o+=n.y);const i=J({});T(n,i,!0);const c=i.nodes[figma.currentPage.id];if(!c)return console.error("Cannot find current page item in scanned nodes list"),j(),!0;let r=0,s=0,f=!0,a=c,g=c;for(;a.children.length;){const u=a.children[0];if(a=i.nodes[u],a.icon||a.target==="invalid"?f=!1:f&&a.target==="valid"&&(g=a),!f&&a.relative){const y=figma.getNodeById(a.id);y&&(r+=y.x||0,s+=y.y||0)}}const l=figma.getNodeById(g.id);return l?(ce(l,e.dropMetadata,{targetX:r+t,targetY:s+o}),!1):(console.error("Failed to get target node"),j(),!0)}function A(){m({type:"plugin:notice",color:"error",text:"Error importing icon(s) to "+R()})}function ve(e,t){const o=figma.getNodeById(e);if(!o){A();return}const n=o.name;let i,c;const r=figma.currentPage.selection;if(r.length===1&&r[0].id===o.id){const s=figma.getSelectionColors();if(s){const f=s.paints,a=s.styles;f.length+a.length===1?(a.length&&k(a[0])&&(i=a[0].id,c=D+i),f.length&&(i=H(f[0]),i&&(c=X(i.color)))):t.props.style&&(i=q(t.props.style))}}for(const s of t.icons){const f=s.name,a=ie(s.content,{replace:o,x:o.x,y:o.y});oe(a,f,t.props,t.route),te([f]),m({type:"plugin:notice",color:"success",text:`Replaced ${n} with ${f}`}),figma.currentPage.selection=[a],E(),s.monotone&&i&&ne(a,i).catch(console.error),s.monotone&&c&&m({type:"plugin:recent-color",color:c});return}A()}function ke(e,t,o){const n=figma.getNodeById(e);if(!n){A();return}let i,c;switch(n.type){case"PAGE":{const r=figma.viewport.center;i=r.x,c=r.y;break}case"GROUP":{i=n.x+n.width/2,c=n.y+n.height/2;break}default:{i=n.width/2,c=n.height/2;break}}ce(n,t,{component:o,targetX:i,targetY:c})}const V="icon-sets",B="route-v31",K="recent-colors",$="customisations";(async()=>{var a;console.log("Starting plugin..."),await ae();const e=await S(V,void 0,g=>g.version===3?g.data:void 0);let t=await S(B);const o=await we(),n=await S(K);let i=await S($);const c=Y(),r=Q();let s;if(figma.command==="replace"){const g=(a=c.iconNode)==null?void 0:a.icon;if(g){s=g.name,g.route&&(t=g.route);const l=g.props;l&&(i={size:l.size,color:l.style?D+l.style:l.color})}}figma.on("selectionchange",E),figma.on("currentpagechange",E),figma.on("drop",Pe),figma.ui.onmessage=g=>{try{if(typeof g.type!="string")return}catch(u){console.error(u)}const l=g;switch(l.type){case"ui:fatal-error":{figma.closePlugin(l.error);break}case"ui:loaded":{if(l.innerHeight||l.outerHeight){h.windowInnerHeight=l.innerHeight,h.windowOuterHeight=l.outerHeight;const u=x();u.height!==h.lastWindowHeight&&(h.lastWindowHeight=u.height,figma.ui.resize(u.width,u.height))}m({type:"plugin:starting",app:h.app,command:figma.command,config:p,nodes:c,styles:r,lists:e,recent:o,route:t,selectIcon:s,recentColors:n,custom:i}),pe().then(u=>{u.length&&m({type:"plugin:color-styles",styles:u})}).catch(console.error),me(n);break}case"ui:close":{figma.closePlugin();break}case"ui:compact":{p.compactWidth=!p.compactWidth;const u=x();u.height!==h.lastWindowHeight&&(h.lastWindowHeight=u.height,figma.ui.resize(u.width,u.height)),m({type:"plugin:resize",compactWidth:p.compactWidth,minimized:!1}),W();break}case"ui:dismiss-v4":{p.v4Notice=!1,W();break}case"ui:lists":{const u={version:3,data:l.lists};P(V,u);break}case"ui:route":{P(B,l.route);break}case"ui:replace-icon":{ve(l.node,l.data);break}case"ui:import-icons":{ke(l.node,l.data,l.component||!1);break}case"ui:recent-colors":{P(K,l.colors);break}case"ui:customisations":{P($,l.custom);break}case"ui:reset-recent-icons":{Ie();break}}};const f=x();h.lastWindowHeight=f.height,figma.showUI(__html__,Object.assign({themeColors:!1},f))})();