Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fsel = () => figma.currentPage.selection[0]
- const isSpeacialCharAndNotNumber = (inputString) => {
- return inputString
- .match(/^[^a-zA-Z]+$/)
- ? true : false
- }
- const enumerate = (array) => {
- return array.map((item, index) => [index, item])
- }
- const insertAfterFirsItemOfArray = (array, value) => {
- const newArray = [
- array[0], value, ...array.slice(1)
- ]
- return newArray
- }
- const fakeTranslateReplacementForWord = (charObject) => {
- if (charObject.isSpecialOrNumber) {
- return charObject.char
- } else {
- return charObject.fakeTranslateReplacement
- }
- }
- const defineCharToAdd = (wordObject) => {
- if (wordObject.isAllUpper) return "P"
- return "v"
- }
- const fakeTranslateReplacementForStr = (wordObject) => {
- let word
- if (wordObject.wholeWordIsSpecialCharOrNumber) {
- word = wordObject.fakeTranslateReplacement
- word = word.join("")
- return word
- }
- const fakeTranslateReplacement = wordObject.fakeTranslateReplacement
- let amountCharsToAdd = 12 - wordObject.length.nonSpecial
- if (amountCharsToAdd < 0) amountCharsToAdd = 0
- const charToAdd = defineCharToAdd(wordObject)
- const charsToAdd = charToAdd.repeat(amountCharsToAdd)
- word = insertAfterFirsItemOfArray(fakeTranslateReplacement, charsToAdd)
- word = word.join("")
- return word
- }
- const convertStrToStrObject = (inputString) => {
- const strObject = {}
- strObject.string = inputString
- strObject.fakeTranslateReplacement = ""
- const words = inputString.split(" ")
- const wordObjects = []
- for (let [wordOrder, word] of enumerate(words)) {
- const wordObject = {}
- wordObject.word = word
- wordObject.fakeTranslateReplacement = []
- wordObject.specialCharInWord = false
- wordObject.isLast = wordOrder == words.length-1
- wordObject.isAllUpper = false
- wordObject.wholeWordIsSpecialCharOrNumber = false
- wordObject.length = {}
- wordObject.length.total = word.length
- wordObject.length.nonSpecial = 0
- wordObject.length.special = 0
- wordObject.chars = []
- word = Array.from(word)
- for (const [charOrder, char] of enumerate(word)) {
- const speacialChar = isSpeacialCharAndNotNumber(char)
- wordObject.length.nonSpecial += ( () => !speacialChar ? 1 : 0 )()
- wordObject.length.special += ( () => speacialChar ? 1 : 0 )()
- wordObject.specialCharInWord = speacialChar
- strObject.specialCharInStr = speacialChar
- const charObject = {}
- charObject.char = char
- charObject.fakeTranslateReplacement = ""
- charObject.isSpecialOrNumber = speacialChar
- charObject.isLast = charOrder == word.length-1
- charObject.isUpper = char == char.toUpperCase()
- charObject.fakeTranslateReplacement = charObject.isUpper ? "P" : "v"
- charObject.charOrder = charOrder
- wordObject.chars.push(charObject)
- }
- wordObject.fakeTranslateReplacement = wordObject.chars.map(
- charObject => (fakeTranslateReplacementForWord(charObject))
- )
- wordObject.isAllUpper = (
- wordObject.chars
- .every(char => char.isUpper)
- )
- wordObject.wholeWordIsSpecialCharOrNumber = (
- wordObject.chars
- .every(char => char.isSpecialOrNumber)
- )
- wordObjects.push(wordObject)
- }
- strObject.wordObjects = wordObjects
- let strFakeTranslateReplacement = strObject.wordObjects.map(
- word => ( fakeTranslateReplacementForStr(word) )
- )
- strFakeTranslateReplacement = strFakeTranslateReplacement.join(" ")
- strObject.fakeTranslateReplacement = strFakeTranslateReplacement
- return strObject
- }
- const fakeTranslate = async() => {
- await figma.loadFontAsync( fsel().fontName )
- const layersText = fsel().characters
- const StrObject = convertStrToStrObject(layersText)
- fsel().name = layersText
- fsel().characters = StrObject.fakeTranslateReplacement
- }
- const revertTranslate = async() => {
- await figma.loadFontAsync( fsel().fontName )
- fsel().characters = fsel().name
- }
- document.addEventListener('keydown', function(event) {
- /* option + shift + L */
- if (event.shiftKey && event.altKey && event.which === 76) {
- fakeTranslate()
- }
- });
- document.addEventListener('keydown', function(event) {
- /* option + shift + K */
- if (event.shiftKey && event.altKey && event.which === 75) {
- revertTranslate()
- }
- });
Add Comment
Please, Sign In to add comment