Class: DraftjsExporter::StyleState

Inherits:
Object
  • Object
show all
Defined in:
lib/draftjs_exporter/style_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(style_map) ⇒ StyleState

Returns a new instance of StyleState.



6
7
8
9
# File 'lib/draftjs_exporter/style_state.rb', line 6

def initialize(style_map)
  @styles = []
  @style_map = style_map
end

Instance Attribute Details

#style_mapObject (readonly)

Returns the value of attribute style_map.



4
5
6
# File 'lib/draftjs_exporter/style_state.rb', line 4

def style_map
  @style_map
end

#stylesObject (readonly)

Returns the value of attribute styles.



4
5
6
# File 'lib/draftjs_exporter/style_state.rb', line 4

def styles
  @styles
end

Instance Method Details

#apply(command) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/draftjs_exporter/style_state.rb', line 11

def apply(command)
  case command.name
  when :start_inline_style
    styles.push(command.data)
  when :stop_inline_style
    styles.delete(command.data)
  end
end

#element_attributesObject



24
25
26
27
# File 'lib/draftjs_exporter/style_state.rb', line 24

def element_attributes
  return {} unless styles.any?
  { style: styles_css }
end

#styles_cssObject



29
30
31
32
33
34
35
# File 'lib/draftjs_exporter/style_state.rb', line 29

def styles_css
  styles.map { |style|
    style_map.fetch(style)
  }.inject({}, :merge).map { |key, value|
    "#{key}: #{value};"
  }.join
end

#text?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/draftjs_exporter/style_state.rb', line 20

def text?
  styles.empty?
end