@@ -31,7 +31,7 @@ function getNodesGroupedbyPosition(nodes) {
3131}
3232
3333function getNameByPosition ( row , col , startName ) {
34-
34+
3535 var padLength = startName . length
3636 var parseStartName = parseInt ( startName )
3737 var rowName = parseStartName + row * Math . pow ( 10 , padLength - 1 )
@@ -62,7 +62,7 @@ function cmdRename(renameStrategy, startName) {
6262 row . columns . forEach ( ( col , colidx ) => {
6363 var name = getNameByPosition ( rowidx , colidx , startName )
6464 var match = allNodes . find ( node => node . id === col . id )
65-
65+
6666 if ( renameStrategy == 'merge' ) {
6767 match . name = `${ name } _${ match . name } `
6868 } else
@@ -117,10 +117,10 @@ function cmdTidy(xSpacing, ySpacing, wrapInstances) {
117117 xPos = col . x
118118 yPos = col . y
119119 }
120- var match = allNodes . find ( node => node . id === col . id )
120+ var match = allNodes . find ( node => node . id === col . id )
121121 var newXPos = ( colidx == 0 ) ? xPos : xPos + xSpacing ;
122122 var newYPos = yPos
123-
123+
124124 // Wrap instances with a frame around
125125 if ( wrapInstances && match . type == 'INSTANCE' ) {
126126 var instanceParent = figma . createFrame ( )
@@ -135,7 +135,7 @@ function cmdTidy(xSpacing, ySpacing, wrapInstances) {
135135 match . x = newXPos
136136 match . y = newYPos
137137 }
138-
138+
139139 xPos = newXPos + match . width
140140 } )
141141
@@ -144,6 +144,36 @@ function cmdTidy(xSpacing, ySpacing, wrapInstances) {
144144 } )
145145}
146146
147+ function cmdPager ( pager_variable ) {
148+ var selection = figma . currentPage . selection
149+ var parent = ( selection [ 0 ] . type == 'PAGE' ) ? figma . currentPage : selection [ 0 ] . parent
150+ var allNodes = parent . children
151+ var groupedNodes = getNodesGroupedbyPosition ( selection )
152+ var frameIndex = 0
153+
154+ function searchPagerNodes ( node , idx ) {
155+ if ( typeof node . children != 'undefined' ) {
156+ node . children . forEach ( child => {
157+ searchPagerNodes ( child , idx )
158+ } )
159+ } else if ( node . type == 'TEXT' && node . name == pager_variable ) {
160+ var font = node . fontName
161+ figma . loadFontAsync ( font ) . then ( ( ) => {
162+ node . characters = idx . toString ( )
163+ } )
164+ }
165+ }
166+
167+ groupedNodes . forEach ( row => {
168+ row . columns . forEach ( col => {
169+ var frame = allNodes . find ( node => node . id === col . id )
170+ searchPagerNodes ( frame , frameIndex )
171+ ++ frameIndex
172+ } )
173+ } )
174+
175+ }
176+
147177// Obtain UUID and preferences then trigger init event
148178Promise . all ( [
149179 figma . clientStorage . getAsync ( 'UUID' ) ,
@@ -157,40 +187,42 @@ Promise.all([
157187 let AD_LAST_SHOWN_DATE = values [ 2 ] || 572083200 // initial date, if no date was saved previously
158188 let AD_LAST_SHOWN_IMPRESSION = values [ 3 ] || 0 // initial impressions
159189 let spacing = values [ 4 ]
160-
190+
161191 let SPACING = { x : 100 , y : 200 }
162192 let START_NAME = '000'
193+ let PAGER_VARIABLE = '{current}'
163194 let WRAP_INSTANCES = true
164195 let RENAME_STRATEGY_REPLACE = 'replace'
165196 let RENAME_STRATEGY_MERGE = 'merge'
166197 let PREFERENCES = {
167198 spacing : SPACING ,
168199 start_name : START_NAME ,
200+ pager_variable : PAGER_VARIABLE ,
169201 wrap_instances : WRAP_INSTANCES ,
170202 rename_strategy : RENAME_STRATEGY_REPLACE
171203 }
172-
204+
173205 if ( ! UUID ) {
174206 UUID = Tracking . createUUID ( )
175207 figma . clientStorage . setAsync ( 'UUID' , UUID )
176208 }
177-
209+
178210 // legacy spacing preference
179211 if ( typeof spacing != 'undefined' ) {
180212 PREFERENCES . spacing = spacing
181213 }
182-
214+
183215 if ( typeof preferences == 'undefined' ) {
184216 preferences = PREFERENCES
185217 }
186-
218+
187219 figma . ui . postMessage ( {
188220 type : 'init-hidden' ,
189221 UUID : UUID ,
190222 cmd : cmd ,
191223 preferences : preferences
192224 } )
193-
225+
194226 // Command triggered by user
195227 if ( cmd == 'rename' ) {
196228 // RUNS WITHOUT UI
@@ -210,11 +242,18 @@ Promise.all([
210242 figma . notify ( 'Super Tidy: Tidy' )
211243 setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
212244 } else
245+ if ( cmd == 'pager' ) {
246+ // RUNS WITHOUT UI
247+ cmdPager ( preferences . pager_variable )
248+ figma . notify ( 'Super Tidy: Pager' )
249+ setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
250+ } else
213251 if ( cmd == 'all' ) {
214252 // RUNS WITHOUT UI
215253 cmdTidy ( preferences . spacing . x , preferences . spacing . y , preferences . wrap_instances )
216254 cmdReorder ( )
217255 cmdRename ( preferences . rename_strategy , preferences . start_name )
256+ cmdPager ( preferences . pager_variable )
218257 figma . notify ( 'Super Tidy' )
219258 setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
220259 } else
@@ -234,18 +273,20 @@ Promise.all([
234273 figma . on ( 'selectionchange' , ( ) => {
235274 figma . ui . postMessage ( { type : 'selection' , selection : figma . currentPage . selection } )
236275 } )
237-
276+
238277 figma . ui . onmessage = msg => {
239278 if ( msg . type === 'tidy' ) {
240279 var RENAMING_ENABLED = msg . options . renaming
241280 var REORDER_ENABLED = msg . options . reorder
242281 var TIDY_ENABLED = msg . options . tidy
282+ var PAGER_ENABLED = msg . options . pager
243283
244284 if ( TIDY_ENABLED ) cmdTidy ( preferences . spacing . x , preferences . spacing . y , preferences . wrap_instances )
245285 if ( RENAMING_ENABLED ) cmdRename ( preferences . rename_strategy , preferences . start_name )
246286 if ( REORDER_ENABLED ) cmdReorder ( )
287+ if ( PAGER_ENABLED ) cmdPager ( preferences . pager_variable )
247288 figma . notify ( 'Super Tidy' )
248- figma . closePlugin ( )
289+ setTimeout ( ( ) => figma . closePlugin ( ) , 100 )
249290 } else
250291 if ( msg . type === 'preferences' ) {
251292 preferences = msg . preferences
@@ -257,7 +298,7 @@ Promise.all([
257298 figma . clientStorage . setAsync ( 'AD_LAST_SHOWN_DATE' , Date . now ( ) )
258299 figma . clientStorage . setAsync ( 'AD_LAST_SHOWN_IMPRESSION' , parseInt ( AD_LAST_SHOWN_IMPRESSION ) + 1 )
259300 }
260-
301+
261302 if ( msg . type === 'resetImpression' ) {
262303 figma . clientStorage . setAsync ( 'AD_LAST_SHOWN_IMPRESSION' , 0 )
263304 }
0 commit comments