let stickies = [ ] ; let stamps = figma . currentPage . findAll ( node => node . type === "STAMP" ) ; if ( figma . currentPage . selection . length > 0 ) { stickies = figma . currentPage . selection . filter ( node => node . type === "STICKY" ) ; } else { stickies = figma . currentPage . findChildren ( node => node . type === "STICKY" ) ; } function isWithinProximity ( node1 , node2 , tolerance = 40 ) { let node1Center = { x : node1 . x + node1 . width / 2 , y : node1 . y + node1 . height / 2 } ; let node2Center = { x : node2 . x + node2 . width / 2 , y : node2 . y + node2 . height / 2 } ; let proximity = node1 . width / 2 + tolerance ; return Math . abs ( node1Center . x - node2Center . x ) <= proximity && Math . abs ( node1Center . y - node2Center . y ) <= proximity ; } function getStampsNearNode ( sticky ) { let stampGroups = { } ; stamps . forEach ( stamp => { if ( isWithinProximity ( sticky , stamp , 60 ) ) { if ( ! stampGroups [ stamp . name ] ) { stampGroups [ stamp . name ] = [ ] ; } stampGroups [ stamp . name ] . push ( stamp ) ; } } ) ; return stampGroups ; } async function tallyStickyVotes ( sticky , removeStamps = false ) { let stampVotes = getStampsNearNode ( sticky ) ; await figma . loadFontAsync ( { family : "Inter" , style : "Medium" } ) ; let tallyFrame = figma . createFrame ( ) ; tallyFrame . cornerRadius = 8 ; tallyFrame . resize ( 128 , 128 ) ; tallyFrame . layoutMode = "VERTICAL" ; tallyFrame . paddingLeft = tallyFrame . paddingRight = tallyFrame . paddingTop = tallyFrame . paddingBottom = 16 ; tallyFrame . itemSpacing = 8 ; tallyFrame . primaryAxisSizingMode = tallyFrame . counterAxisSizingMode = "AUTO" ; tallyFrame . fills = [ { type : "SOLID" , color : { r : 1 , g : 1 , b : 1 } } ] ; tallyFrame . x = sticky . x + sticky . width + 20 ; tallyFrame . y = sticky . y ; tallyFrame . strokes = [ { type : "SOLID" , color : { r : 0 , g : 0 , b : 0 } } ] ; tallyFrame . strokeWeight = 2 ; tallyFrame . primaryAxisAlignItems = tallyFrame . counterAxisAlignItems = "CENTER" ; let tallyTitle = figma . createText ( ) ; tallyFrame . appendChild ( tallyTitle ) ; tallyTitle . characters = "Votes" ; tallyTitle . textAutoResize = "HEIGHT" ; tallyTitle . layoutAlign = "INHERIT" ; tallyTitle . textAlignHorizontal = "CENTER" ; let sortedVotes = Object . keys ( stampVotes ) . sort ( ( a , b ) => { return stampVotes [ b ] . length - stampVotes [ a ] . length ; } ) ; sortedVotes . forEach ( stampName => { let tallyLine = figma . createFrame ( ) ; tallyFrame . appendChild ( tallyLine ) ; tallyLine . layoutMode = "HORIZONTAL" ; tallyLine . itemSpacing = 8 ; tallyLine . layoutAlign = "STRETCH" ; tallyLine . primaryAxisAlignItems = "MIN" ; tallyLine . counterAxisAlignItems = "CENTER" ; tallyLine . counterAxisSizingMode = "AUTO" ; tallyLine . primaryAxisSizingMode = "AUTO" ; tallyLine . clipsContent = false ; let stampsToShow = stampName === "Profile" ? stampVotes [ stampName ] : [ stampVotes [ stampName ] [ 0 ] ] ; let stampFrame = figma . createFrame ( ) ; tallyLine . appendChild ( stampFrame ) ; stampFrame . layoutMode = "HORIZONTAL" ; stampFrame . itemSpacing = 0 ; stampFrame . layoutAlign = "MIN" ; stampFrame . primaryAxisAlignItems = "SPACE_BETWEEN" ; stampFrame . counterAxisAlignItems = "CENTER" ; stampFrame . primaryAxisSizingMode = "FIXED" ; stampFrame . clipsContent = false ; stampFrame . resize ( stampsToShow . length * 32 * 0.66 , 32 ) ; stampsToShow . forEach ( currentStamp => { let stamp = currentStamp . clone ( ) ; stamp . rotation = 0 ; stamp . resize ( 32 , 32 ) ; stampFrame . appendChild ( stamp ) ; } ) ; let tally = figma . createText ( ) ; tallyLine . appendChild ( tally ) ; tally . characters = `${ stampVotes [ stampName ] . length }` ; tally . textAutoResize = "HEIGHT" ; tally . layoutAlign = "INHERIT" ; if ( removeStamps ) { stampVotes [ stampName ] . forEach ( stamp => stamp . remove ( ) ) ; } } ) ; } if ( stickies . length > 0 ) { stickies . forEach ( sticky => { tallyStickyVotes ( sticky , true ) ; } ) ; figma . notify ( `Tallied ${ stickies . length } ${ stickies . length > 1 ? 'stickies' : 'sticky' }.` ) ; } else { figma . notify ( `No stickies found. Add some!` ) ; } figma . closePlugin ( ) ;