kopyl

Fake translate for dev console

Oct 20th, 2021 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fsel = () => figma.currentPage.selection[0]
  2.  
  3. const isSpeacialCharAndNotNumber = (inputString) => {
  4.     return inputString
  5.            .match(/^[^a-zA-Z]+$/)
  6.            ? true : false
  7. }
  8.  
  9.  
  10. const enumerate = (array) => {
  11.     return array.map((item, index) => [index, item])
  12. }
  13.  
  14.  
  15. const insertAfterFirsItemOfArray = (array, value) => {
  16.     const newArray = [
  17.         array[0], value, ...array.slice(1)
  18.     ]
  19.     return newArray
  20. }
  21.  
  22.  
  23. const fakeTranslateReplacementForWord = (charObject) => {
  24.     if (charObject.isSpecialOrNumber) {
  25.         return charObject.char
  26.     } else {
  27.         return charObject.fakeTranslateReplacement
  28.     }
  29. }
  30.  
  31. const defineCharToAdd = (wordObject) => {
  32.     if (wordObject.isAllUpper) return "P"
  33.     return "v"
  34. }
  35.  
  36.  
  37. const fakeTranslateReplacementForStr = (wordObject) => {
  38.     let word
  39.  
  40.     if (wordObject.wholeWordIsSpecialCharOrNumber) {
  41.         word = wordObject.fakeTranslateReplacement
  42.         word = word.join("")
  43.         return word
  44.     }
  45.  
  46.     const fakeTranslateReplacement = wordObject.fakeTranslateReplacement
  47.  
  48.     let amountCharsToAdd = 12 - wordObject.length.nonSpecial
  49.     if (amountCharsToAdd < 0) amountCharsToAdd = 0
  50.    
  51.     const charToAdd = defineCharToAdd(wordObject)
  52.     const charsToAdd = charToAdd.repeat(amountCharsToAdd)
  53.  
  54.     word = insertAfterFirsItemOfArray(fakeTranslateReplacement, charsToAdd)
  55.     word = word.join("")
  56.     return word
  57. }
  58.  
  59.  
  60. const convertStrToStrObject = (inputString) => {
  61.  
  62.  
  63.     const strObject = {}
  64.     strObject.string = inputString
  65.     strObject.fakeTranslateReplacement = ""
  66.  
  67.     const words = inputString.split(" ")
  68.     const wordObjects = []
  69.  
  70.  
  71.     for (let [wordOrder, word] of enumerate(words)) {
  72.         const wordObject = {}
  73.  
  74.         wordObject.word = word
  75.         wordObject.fakeTranslateReplacement = []
  76.         wordObject.specialCharInWord = false
  77.         wordObject.isLast = wordOrder == words.length-1
  78.         wordObject.isAllUpper = false
  79.         wordObject.wholeWordIsSpecialCharOrNumber = false
  80.  
  81.         wordObject.length = {}
  82.         wordObject.length.total = word.length
  83.         wordObject.length.nonSpecial = 0
  84.         wordObject.length.special = 0
  85.  
  86.         wordObject.chars = []
  87.        
  88.         word = Array.from(word)
  89.         for (const [charOrder, char] of enumerate(word)) {
  90.             const speacialChar = isSpeacialCharAndNotNumber(char)
  91.             wordObject.length.nonSpecial += ( () => !speacialChar ? 1 : 0 )()
  92.             wordObject.length.special += ( () => speacialChar ? 1 : 0 )()
  93.             wordObject.specialCharInWord = speacialChar
  94.             strObject.specialCharInStr = speacialChar
  95.  
  96.             const charObject = {}
  97.             charObject.char = char
  98.             charObject.fakeTranslateReplacement = ""
  99.             charObject.isSpecialOrNumber = speacialChar
  100.             charObject.isLast = charOrder == word.length-1
  101.             charObject.isUpper = char == char.toUpperCase()
  102.             charObject.fakeTranslateReplacement = charObject.isUpper ? "P" : "v"
  103.             charObject.charOrder = charOrder
  104.  
  105.             wordObject.chars.push(charObject)
  106.         }
  107.  
  108.         wordObject.fakeTranslateReplacement = wordObject.chars.map(
  109.             charObject => (fakeTranslateReplacementForWord(charObject))
  110.         )
  111.  
  112.         wordObject.isAllUpper = (
  113.             wordObject.chars
  114.             .every(char => char.isUpper)
  115.         )
  116.  
  117.         wordObject.wholeWordIsSpecialCharOrNumber = (
  118.             wordObject.chars
  119.             .every(char => char.isSpecialOrNumber)
  120.         )
  121.  
  122.         wordObjects.push(wordObject)
  123.     }
  124.  
  125.     strObject.wordObjects = wordObjects
  126.  
  127.     let strFakeTranslateReplacement = strObject.wordObjects.map(
  128.         word => ( fakeTranslateReplacementForStr(word) )
  129.     )
  130.     strFakeTranslateReplacement = strFakeTranslateReplacement.join(" ")
  131.     strObject.fakeTranslateReplacement = strFakeTranslateReplacement
  132.  
  133.     return strObject
  134. }
  135.  
  136.  
  137. const fakeTranslate = async() => {
  138.  
  139.  
  140.  
  141.     await figma.loadFontAsync( fsel().fontName )
  142.     const layersText = fsel().characters
  143.    
  144.     const StrObject = convertStrToStrObject(layersText)
  145.    
  146.     fsel().name = layersText
  147.     fsel().characters = StrObject.fakeTranslateReplacement
  148. }
  149.  
  150.  
  151.  
  152. const revertTranslate = async() => {
  153.    
  154.     await figma.loadFontAsync( fsel().fontName )
  155.     fsel().characters = fsel().name
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. document.addEventListener('keydown', function(event) {
  167.     /* option + shift + L */
  168.   if (event.shiftKey && event.altKey && event.which === 76) {
  169.     fakeTranslate()
  170.   }
  171. });
  172.  
  173.  
  174. document.addEventListener('keydown', function(event) {
  175.     /* option + shift + K */
  176.   if (event.shiftKey && event.altKey && event.which === 75) {
  177.     revertTranslate()
  178.   }
  179. });
Add Comment
Please, Sign In to add comment