Skip to content

Commit cd997d1

Browse files
committed
Added Pager action
1 parent 38a1e1b commit cd997d1

5 files changed

Lines changed: 114 additions & 38 deletions

File tree

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{"name": "Rename", "command": "rename"},
99
{"name": "Reorder", "command": "reorder"},
1010
{"name": "Tidy", "command": "tidy"},
11+
{"name": "Pager", "command": "pager"},
1112
{"name": "Run all", "command": "all"},
1213
{"separator": true},
1314
{"name": "Run custom...", "command": "options"}

src/App.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ class ui extends Element {
2222
Tracking.setup(WP_AMPLITUDE_KEY, msg.UUID)
2323
Tracking.track('openPlugin', { cmd: msg.cmd })
2424
}
25-
25+
2626
if (msg.type == 'init') {
2727
this.insertDisplay(msg.AD_LAST_SHOWN_DATE, msg.AD_LAST_SHOWN_IMPRESSION)
2828
}
2929
})
30-
30+
3131
Router.setup({
3232
index: '#index',
3333
preferences: '#preferences'
3434
})
3535
}
36-
36+
3737
bind() {
3838
Router.on('change:url', url => this.showActiveView(url))
3939
}
40-
40+
4141
insertDisplay(lastShownDate, lastShownImpression) {
4242
let elem = document.createElement('c-display')
4343
elem.setAttribute('lastshowndate', lastShownDate)
4444
elem.setAttribute('lastshownimpression', lastShownImpression)
45-
elem.setAttribute('hidden', '')
45+
elem.setAttribute('hidden', '')
4646
document.body.insertBefore(elem, document.body.querySelector('root-ui'))
4747
}
48-
48+
4949
showActiveView(url) {
5050
let view = url.replace('#', '')
5151
this.findAll('[data-view]').forEach(view => view.setAttribute('hidden', ''))
@@ -61,6 +61,7 @@ class ui extends Element {
6161
xspacing="${this.data.preferences.spacing.x}"
6262
yspacing="${this.data.preferences.spacing.y}"
6363
startname="${this.data.preferences.start_name}"
64+
pager_variable="${this.data.preferences.pager_variable}"
6465
wrapinstances="${this.data.preferences.wrap_instances}"
6566
renamestrategy="${this.data.preferences.rename_strategy}"
6667
>

src/Core.js

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function getNodesGroupedbyPosition(nodes) {
3131
}
3232

3333
function 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
148178
Promise.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
}

src/ui/views/form/FormView.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ class FormView extends Element {
3030
var renaming_check = document.getElementById('renaming_check')
3131
var reorder_check = document.getElementById('reorder_check')
3232
var tidy_check = document.getElementById('tidy_check')
33+
var pager_check = document.getElementById('pager_check')
3334
var tidy = document.getElementById('tidy')
3435

3536
function applySuperTidy() {
3637
var renamingEnabled = renaming_check.checked;
3738
var reorderEnabled = reorder_check.checked;
3839
var tidyEnabled = tidy_check.checked;
40+
var pagerEnabled = pager_check.checked;
3941
var options = {
4042
renaming: renamingEnabled,
4143
reorder: reorderEnabled,
4244
tidy: tidyEnabled,
45+
pager: pagerEnabled
4346
}
4447

4548
Tracking.track('clickApply', options)
@@ -76,7 +79,7 @@ class FormView extends Element {
7679
</div>
7780
</label>
7881
</fieldset>
79-
82+
8083
<fieldset>
8184
<label class="switch">
8285
<div class="switch__container">
@@ -86,12 +89,12 @@ class FormView extends Element {
8689
<div class="switch__label">
8790
<strong>Reorder</strong>
8891
<p class="type type--11-pos-normal">
89-
Reorder your layers on the sidebar based on their position on the canvas.
92+
Reorder your layers on the sidebar based on their position on the canvas.
9093
</p>
9194
</div>
9295
</label>
9396
</fieldset>
94-
97+
9598
<fieldset>
9699
<label class="switch">
97100
<div class="switch__container">
@@ -106,7 +109,22 @@ class FormView extends Element {
106109
</div>
107110
</label>
108111
</fieldset>
109-
112+
113+
<fieldset>
114+
<label class="switch">
115+
<div class="switch__container">
116+
<input id="pager_check" type="checkbox" class="switch__checkbox" checked>
117+
<span class="switch__slider"></span>
118+
</div>
119+
<div class="switch__label">
120+
<strong>Pager</strong>
121+
<p class="type type--11-pos-normal">
122+
Creates a pagination for the given selection using the position in the canvas of each frame. The page number is written inside a layer with the name {current} by default.
123+
</p>
124+
</div>
125+
</label>
126+
</fieldset>
127+
110128
<button type="submit" id="tidy" class="button button--primary">Run</button>
111129
</form>
112130
`

0 commit comments

Comments
 (0)