-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
659 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,30 @@ | ||
figma.showUI(__html__, { | ||
width: 420, | ||
height: 220 | ||
}); | ||
/// <reference types="@figma/plugin-typings" /> | ||
import handleDateTimeTab from './date-time' | ||
import handleNumericTab from './numeric' | ||
|
||
function setBaseNumber (base = '0', orderType = 'asc', length) { | ||
const baseNumber = +base | ||
let stack = 0 | ||
let res | ||
return () => { | ||
if (orderType === 'asc') { | ||
res = baseNumber + stack | ||
} else if (orderType === 'desc') { | ||
res = baseNumber - stack | ||
} else { | ||
res = Math.floor(Math.random() * Math.pow(10, length)) | ||
} | ||
stack += 1 | ||
return res.toString().padStart(length, '0') | ||
} | ||
enum TabHeight { | ||
numeric = 220, | ||
dateTime = 550 | ||
} | ||
|
||
figma.ui.onmessage = async payload => { | ||
const { prefix, baseNumber, orderType, action, isReverse } = payload | ||
const nodes = figma.currentPage.selection | ||
const nodesLength = nodes.length | ||
if (action === 'generate') { | ||
const isInvalid = nodes.some(node => node.type !== 'TEXT'); | ||
if (isInvalid) { | ||
return "Select a single text node." | ||
} | ||
|
||
const genNextNumber = setBaseNumber(baseNumber, orderType, baseNumber.length) | ||
figma.showUI(__html__, { | ||
width: 350, | ||
height: TabHeight.numeric | ||
}) | ||
|
||
for (let i = 0; i < nodesLength; i += 1) { | ||
//! Noted: order of page selections are not reliable. | ||
await figma.loadFontAsync(nodes[i].fontName) | ||
const result = isReverse | ||
? `${genNextNumber()}${prefix}` | ||
: `${prefix}${genNextNumber()}` | ||
nodes[i].characters = result | ||
nodes[i].name = result | ||
figma.ui.onmessage = payload => { | ||
// TODO: should separate tabChange and others | ||
if (payload.action === 'tabChange') { | ||
const height = TabHeight[payload.tab] as unknown as number | ||
figma.ui.resize(350, height) | ||
} else { | ||
const tabHandlers = { | ||
numeric: handleNumericTab, | ||
dateTime: handleDateTimeTab | ||
} | ||
const tabHandler = tabHandlers[payload.tab] | ||
if (tabHandler) { | ||
tabHandler(payload) | ||
} | ||
} | ||
|
||
// on cancel | ||
if (action === 'cancel') { | ||
figma.closePlugin(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
function addDays (date: Date, days: number): Date { | ||
const result = new Date(date) | ||
result.setDate(result.getDate() + days) | ||
return result | ||
} | ||
|
||
function formatDate(date: Date, format: string): string { | ||
const month = date.getMonth() + 1 | ||
const day = date.getDate() | ||
const year = date.getFullYear() | ||
const weekday = date.getDay() | ||
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | ||
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
|
||
const patterns = { | ||
'YYYY-MM-DD': `${year}-${(month).toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`, | ||
'MM-DD-YYYY': `${(month).toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}-${year}`, | ||
'MM/DD/YYYY': `${month.toString().padStart(2, '0')}/${day.toString().padStart(2, '0')}/${year}`, | ||
'MMMM DD, YYYY': `${months[date.getMonth()]} ${day}, ${year}`, | ||
'MMMM DD, YYYY h:mm A': `${months[date.getMonth()]} ${day}, ${year} ${date.toLocaleTimeString('en-US', {hour: 'numeric', minute: 'numeric', hour12: true})}`, | ||
'dddd, MMMM DD, YYYY h:mm A': `${days[weekday]}, ${months[date.getMonth()]} ${day}, ${year} ${date.toLocaleTimeString('en-US', {hour: 'numeric', minute: 'numeric', hour12: true})}`, | ||
'M/D/YYYY': `${month}/${day}/${year}`, | ||
'MMM D, YYYY': `${months[date.getMonth()].slice(0, 3)} ${day}, ${year}`, | ||
'MMM D, YYYY h:mm A': `${months[date.getMonth()].slice(0, 3)} ${day}, ${year} ${date.toLocaleTimeString('en-US', {hour: 'numeric', minute: 'numeric', hour12: true})}`, | ||
'ddd, MMM D, YYYY h:mm A': `${days[weekday].slice(0, 3)}, ${months[date.getMonth()].slice(0, 3)} ${date.getDate()}, ${date.getFullYear()} ${date.toLocaleTimeString('en-US', {hour: 'numeric', minute: 'numeric', hour12: true})}` | ||
} | ||
|
||
if (patterns[format]) { | ||
return patterns[format] | ||
} else { | ||
throw new Error(`Unsupported format: ${format}`) | ||
} | ||
} | ||
|
||
function setBaseDate (base = '', orderType = 'asc', format) { | ||
const baseDate = new Date(base) | ||
let stack = 0 | ||
let res | ||
return () => { | ||
if (orderType === 'asc') { | ||
res = addDays(baseDate, stack) | ||
} else if (orderType === 'desc') { | ||
res = addDays(baseDate, -stack) | ||
} | ||
stack += 1 | ||
return formatDate(res, format) | ||
} | ||
} | ||
|
||
export default async function handleDateTimeTab (payload) { | ||
const { | ||
action, | ||
orderType, | ||
selectedDate, | ||
format | ||
} = payload | ||
const nodes = figma.currentPage.selection | ||
const nodesLength = nodes.length | ||
if (action === 'generate') { | ||
const isInvalid = nodes.some(node => node.type !== 'TEXT'); | ||
if (isInvalid) { | ||
return "Select a single text node." | ||
} | ||
|
||
const genNextDate = setBaseDate(selectedDate, orderType, format) | ||
|
||
for (let i = 0; i < nodesLength; i += 1) { | ||
//! Noted: order of page selections are not reliable. | ||
await figma.loadFontAsync(nodes[i].fontName) | ||
nodes[i].characters = genNextDate() | ||
} | ||
} | ||
|
||
// on cancel | ||
if (action === 'cancel') { | ||
figma.closePlugin(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function setBaseNumber (base = '0', orderType = 'asc', length) { | ||
const baseNumber = +base | ||
let stack = 0 | ||
let res | ||
return () => { | ||
if (orderType === 'asc') { | ||
res = baseNumber + stack | ||
} else if (orderType === 'desc') { | ||
res = baseNumber - stack | ||
} else { | ||
res = Math.floor(Math.random() * Math.pow(10, length)) | ||
} | ||
stack += 1 | ||
return res.toString().padStart(length, '0') | ||
} | ||
} | ||
|
||
export default async function handleNumericTab (payload) { | ||
const { prefix, baseNumber, orderType, action, isReverse } = payload | ||
const nodes = figma.currentPage.selection | ||
const nodesLength = nodes.length | ||
if (action === 'generate') { | ||
const isInvalid = nodes.some(node => node.type !== 'TEXT'); | ||
if (isInvalid) { | ||
return 'Select a single text node.' | ||
} | ||
|
||
const genNextNumber = setBaseNumber(baseNumber, orderType, baseNumber.length) | ||
|
||
for (let i = 0; i < nodesLength; i += 1) { | ||
//! Noted: order of page selections are not reliable. | ||
await figma.loadFontAsync(nodes[i].fontName) | ||
const result = isReverse | ||
? `${genNextNumber()}${prefix}` | ||
: `${prefix}${genNextNumber()}` | ||
nodes[i].characters = result | ||
nodes[i].name = result | ||
} | ||
} | ||
|
||
// on cancel | ||
if (action === 'cancel') { | ||
figma.closePlugin(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.