@@ -87,7 +87,7 @@ function cmdReorder() {
8787 } )
8888}
8989
90- function cmdTidy ( xSpacing , ySpacing ) {
90+ function cmdTidy ( xSpacing , ySpacing , wrapInstances ) {
9191 var selection = figma . currentPage . selection
9292 var parent = ( selection [ 0 ] . type == 'PAGE' ) ? figma . currentPage : selection [ 0 ] . parent
9393 var allNodes = parent . children
@@ -117,20 +117,34 @@ function cmdTidy(xSpacing, ySpacing) {
117117 xPos = col . x
118118 yPos = col . y
119119 }
120- var match = allNodes . find ( node => node . id === col . id )
121- match . x = ( colidx == 0 ) ? xPos : xPos + xSpacing ;
122- match . y = yPos
123- xPos = match . x + match . width
120+ var match = allNodes . find ( node => node . id === col . id )
121+ var newXPos = ( colidx == 0 ) ? xPos : xPos + xSpacing ;
122+ var newYPos = yPos
123+
124+ // Wrap instances with a frame around
125+ if ( wrapInstances && match . type == 'INSTANCE' ) {
126+ var instanceParent = figma . createFrame ( )
127+ instanceParent . x = newXPos
128+ instanceParent . y = newYPos
129+ instanceParent . resize ( match . width , match . height )
130+ instanceParent . appendChild ( match )
131+ match . x = 0
132+ match . y = 0
133+ figma . currentPage . selection = figma . currentPage . selection . concat ( instanceParent )
134+ } else {
135+ match . x = newXPos
136+ match . y = newYPos
137+ }
138+
139+ xPos = newXPos + match . width
124140 } )
125141
126142 xPos = x0
127143 yPos = yPos + ( tallestInRow [ rowidx ] + ySpacing )
128144 } )
129145}
130146
131- // Obtain UUID then trigger init event
132-
133- // Obtain UUID and saved file url then trigger init event
147+ // Obtain UUID and preferences then trigger init event
134148Promise . all ( [
135149 figma . clientStorage . getAsync ( 'UUID' ) ,
136150 figma . clientStorage . getAsync ( 'preferences' ) ,
@@ -144,11 +158,13 @@ Promise.all([
144158
145159 let SPACING = { x : 100 , y : 200 }
146160 let START_NAME = '000'
161+ let WRAP_INSTANCES = true
147162 let RENAME_STRATEGY_REPLACE = 'replace'
148163 let RENAME_STRATEGY_MERGE = 'merge'
149164 let PREFERENCES = {
150165 spacing : SPACING ,
151166 start_name : START_NAME ,
167+ wrap_instances : WRAP_INSTANCES ,
152168 rename_strategy : RENAME_STRATEGY_REPLACE
153169 }
154170
@@ -188,21 +204,21 @@ Promise.all([
188204 } else
189205 if ( cmd == 'tidy' ) {
190206 // RUNS WITHOUT UI
191- cmdTidy ( preferences . spacing . x , preferences . spacing . y )
207+ cmdTidy ( preferences . spacing . x , preferences . spacing . y , preferences . wrap_instances )
192208 figma . notify ( 'Super Tidy: Tidy' )
193209 setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
194210 } else
195211 if ( cmd == 'all' ) {
196212 // RUNS WITHOUT UI
197- cmdTidy ( preferences . spacing . x , preferences . spacing . y )
213+ cmdTidy ( preferences . spacing . x , preferences . spacing . y , preferences . wrap_instances )
198214 cmdReorder ( )
199215 cmdRename ( preferences . rename_strategy , preferences . start_name )
200216 figma . notify ( 'Super Tidy' )
201217 setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
202218 } else
203219 if ( cmd == 'options' ) {
204220 // OPEN UI
205- figma . showUI ( __html__ , { width : 320 , height : 460 } )
221+ figma . showUI ( __html__ , { width : 320 , height : 540 } )
206222 figma . ui . postMessage ( {
207223 type : 'init' ,
208224 UUID : UUID ,
@@ -215,14 +231,14 @@ Promise.all([
215231 figma . on ( 'selectionchange' , ( ) => {
216232 figma . ui . postMessage ( { type : 'selection' , selection : figma . currentPage . selection } )
217233 } )
218-
234+
219235 figma . ui . onmessage = msg => {
220236 if ( msg . type === 'tidy' ) {
221237 var RENAMING_ENABLED = msg . options . renaming
222238 var REORDER_ENABLED = msg . options . reorder
223239 var TIDY_ENABLED = msg . options . tidy
224240
225- if ( TIDY_ENABLED ) cmdTidy ( preferences . spacing . x , preferences . spacing . y )
241+ if ( TIDY_ENABLED ) cmdTidy ( preferences . spacing . x , preferences . spacing . y , preferences . wrap_instances )
226242 if ( RENAMING_ENABLED ) cmdRename ( preferences . rename_strategy , preferences . start_name )
227243 if ( REORDER_ENABLED ) cmdReorder ( )
228244 figma . notify ( 'Super Tidy' )
@@ -234,7 +250,7 @@ Promise.all([
234250 figma . notify ( 'Preferences saved' )
235251 } else
236252 if ( msg . type === 'displayImpression' ) {
237- figma . ui . resize ( 320 , 460 + 124 )
253+ figma . ui . resize ( 320 , 540 + 124 )
238254 figma . clientStorage . setAsync ( 'AD_LAST_SHOWN_DATE' , Date . now ( ) )
239255 }
240256 }
0 commit comments