Class: FigmaToDomainConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/admiral-tools-figma/helper/figma/figma_client/models/domain/converters/figma_to_domain_converter.rb

Instance Method Summary collapse

Instance Method Details

#convert_figma_style(figma_style:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/admiral-tools-figma/helper/figma/figma_client/models/domain/converters/figma_to_domain_converter.rb', line 6

def convert_figma_style(figma_style:)
  return nil if figma_style.nil?

  Style.new(
    name: figma_style.name,
    node_id: figma_style.node_id,
    style_type: figma_style.style_type,
    description: figma_style.description
  )
end

#fulfill_components(figma_components:, image_links_hash:, image_format:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/admiral-tools-figma/helper/figma/figma_client/models/domain/converters/figma_to_domain_converter.rb', line 63

def fulfill_components(figma_components:, image_links_hash:, image_format:)
  return [] if figma_components.nil? || image_links_hash.nil?

  components = []
  figma_components.each do |c|
    component = Component.new(
      key: c.key,
      file_key: c.file_key,
      node_id: c.node_id,
      name: c.name,
      description: c.description
    )

    component.containing_frame = FrameInfo.new(
      node_id: c.containing_frame.node_id,
      name: c.containing_frame.name,
      background_color: c.containing_frame.background_color,
      page_id: c.containing_frame.page_id,
      page_name: c.containing_frame.page_name
    )

    image_links = []
    image_links_hash.each do |key, link_hash|
      link = link_hash[c.node_id]
      if !link.nil? && !key.nil? && !image_format.nil?
        image_link = ImageLink.new(link: link, scale: key, image_format: image_format)
        image_links.push(image_link)
      end
    end
    component.image_links = image_links
    components.push(component)
  end

  components
end

#fulfill_styles(figma_nodes_result:, figma_styles_result:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/admiral-tools-figma/helper/figma/figma_client/models/domain/converters/figma_to_domain_converter.rb', line 17

def fulfill_styles(figma_nodes_result:, figma_styles_result:)
  return [] if figma_nodes_result.nil? || figma_styles_result.nil?

  styles = []
  figma_styles = figma_styles_result&.meta&.styles || []
  nodes = figma_nodes_result&.nodes || {}

  figma_styles.each do |s|
    style = convert_figma_style(figma_style: s)
    node = nodes.dig(s.node_id, 'document')

    case s.style_type
    when FIGMA_STYLE_TYPE_FILL
      fills = node&.fills
      next if fills.nil?

      figma_paint = fills[0]
      next if figma_paint.nil?

      style&.color = Color.new(
        r: figma_paint.color.r,
        g: figma_paint.color.g,
        b: figma_paint.color.b,
        a: figma_paint.opacity
      )
      styles.push(style)
    when FIGMA_STYLE_TYPE_TEXT
      type_style = node&.style
      next if type_style.nil?

      style&.font = Font.new(
        font_family: type_style.font_family,
        font_postscript_name: type_style.font_postscript_name,
        font_size: type_style.font_size,
        font_weight: type_style.font_weight,
        letter_spacing: type_style.letter_spacing
      )
      styles.push(style)
    else
      next
    end
  end

  styles
end