Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. // Convert "translate(10px) rotate(0)" into ["translate", "rotate"]
  2. // This helps interpolate strings of different arity.
  3. const keyRegex = /(?:^| )([\w-]+)\(([^ ,]+(?:(?:,[ ]*|[ ]+)[^ ,)]+)*)\)/g
  4.  
  5. /**
  6. * Create a specialized interpolator for strings like:
  7. *
  8. * "translate(...) rotate(...)"
  9. */
  10. export const createKeyedInterpolator = (config: InterpolatorConfig<string>) => {
  11. type KeyFrame = { [key: string]: string }
  12.  
  13. // Collect the keyframes and from values.
  14. const fromValues: KeyFrame = {}
  15. const keyframes = config.output.map((output, i) => {
  16. // Replace colors with rgba(...)
  17. output = output
  18. .replace(colorRegex, colorToRgba)
  19. .replace(colorNamesRegex, colorToRgba)
  20.  
  21. const keyframe: KeyFrame = {}
  22. keyRegex.lastIndex = 0
  23. for (let match; (match = keyRegex.exec(output)); ) {
  24. const key = match[1]
  25. const value = match[2]
  26. if (fromValues[key] == null) {
  27. // Infer 0 as the from value when none exists.
  28. fromValues[key] = i == 0 ? value : value.replace(numberRegex, '0')
  29. }
  30. // Keep the parsed argument list as a string.
  31. keyframe[key] = value
  32. }
  33. return keyframe
  34. })
  35.  
  36. console.log('interpolate:', { config, fromValues, keyframes })
  37.  
  38. // Each key has its own interpolator.
  39. const keys = Object.keys(fromValues)
  40. const interpolators = keys.map(key => {
  41. let prev = fromValues[key]
  42. return createInterpolator({
  43. ...config,
  44. output: keyframes.map(keyframe => {
  45. const value = keyframe[key]
  46. return value !== void 0 ? (prev = value) : prev
  47. }),
  48. })
  49. })
  50.  
  51. return (input: number) =>
  52. keys.map((key, i) => `${key}(${interpolators[i](input)})`).join(' ')
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement