Class: InspectorPanel::Component

Inherits:
Lookbook::BaseComponent
  • Object
show all
Defined in:
app/components/lookbook/inspector_panel/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, system: false, **html_attrs) ⇒ Component

Returns a new instance of Component.



7
8
9
10
11
12
13
14
# File 'app/components/lookbook/inspector_panel/component.rb', line 7

def initialize(id:, name:, system: false, **html_attrs)
  @id = id
  @name = name
  @system = system
  @panel_html = nil
  @panel_styles = nil
  super(**html_attrs)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'app/components/lookbook/inspector_panel/component.rb', line 5

def id
  @id
end

#panel_htmlObject (readonly)

Returns the value of attribute panel_html.



5
6
7
# File 'app/components/lookbook/inspector_panel/component.rb', line 5

def panel_html
  @panel_html
end

#panel_stylesObject (readonly)

Returns the value of attribute panel_styles.



5
6
7
# File 'app/components/lookbook/inspector_panel/component.rb', line 5

def panel_styles
  @panel_styles
end

Instance Method Details

#before_renderObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/components/lookbook/inspector_panel/component.rb', line 16

def before_render
  if @system == false
    panel_dom = ::Nokogiri::HTML.fragment(content)
    style_tags = panel_dom.css("style")
    if style_tags.any?
      css_parser = ::CssParser::Parser.new
      @panel_styles = ""
      style_tags.each do |style_tag|
        css_parser.load_string! style_tag.text
        css_parser.each_selector do |selector, declarations, specificity|
          @panel_styles += "##{id} #{selector} { #{declarations} }\n"
        end
      end
      @panel_html = content.gsub(/<style(?:\s[^>]*)?>.*<\/style>/, "").html_safe
    end
  end
  @panel_html ||= content
end