Class: DraftjsHtml::Draftjs::Block

Inherits:
Struct
  • Object
show all
Defined in:
lib/draftjs_html/draftjs/block.rb

Defined Under Namespace

Classes: CharRange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#depthObject

Returns the value of attribute depth

Returns:

  • (Object)

    the current value of depth



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def depth
  @depth
end

#inline_style_rangesObject

Returns the value of attribute inline_style_ranges

Returns:

  • (Object)

    the current value of inline_style_ranges



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def inline_style_ranges
  @inline_style_ranges
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def key
  @key
end

#raw_entity_rangesObject

Returns the value of attribute raw_entity_ranges

Returns:

  • (Object)

    the current value of raw_entity_ranges



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def raw_entity_ranges
  @raw_entity_ranges
end

#textObject Also known as: plaintext

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def text
  @text
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



5
6
7
# File 'lib/draftjs_html/draftjs/block.rb', line 5

def type
  @type
end

Class Method Details

.parse(raw) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/draftjs_html/draftjs/block.rb', line 6

def self.parse(raw)
  new(
    key: raw['key'],
    text: raw['text'],
    type: raw['type'],
    depth: raw.fetch('depth', 0).to_i,
    inline_style_ranges: Array(raw['inlineStyleRanges']),
    raw_entity_ranges: Array(raw['entityRanges']),
  )
end

Instance Method Details

#add_entity(key, range) ⇒ Object



77
78
79
# File 'lib/draftjs_html/draftjs/block.rb', line 77

def add_entity(key, range)
  entity_ranges << ApplicableRange.new(name: key, range: range)
end

#add_style(name, range) ⇒ Object



73
74
75
# File 'lib/draftjs_html/draftjs/block.rb', line 73

def add_style(name, range)
  inline_styles << ApplicableRange.new(name: name, range: range)
end

#blank?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/draftjs_html/draftjs/block.rb', line 21

def blank?
  text.empty? && entity_ranges.empty?
end

#each_charObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/draftjs_html/draftjs/block.rb', line 25

def each_char
  return to_enum(:each_char) unless block_given?

  text.chars.map.with_index do |char, index|
    yield CharacterMeta.new(
      char: char,
      style_names: inline_styles.select { _1.range.cover?(index) }.map(&:name),
      entity_key: entity_ranges.select { _1.range.cover?(index) }.map(&:name).first,
    )
  end
end

#each_range {|ranges.last| ... } ⇒ Object

Yields:

  • (ranges.last)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/draftjs_html/draftjs/block.rb', line 38

def each_range
  return to_enum(:each_range) unless block_given?

  current_styles = []
  current_entity = nil
  ranges = [CharRange.new(text: '', style_names: current_styles, entity_key: current_entity)]

  each_char.with_index do |char, index|
    if char.style_names != current_styles || char.entity_key != current_entity
      current_styles = char.style_names
      current_entity = char.entity_key
      yield(ranges.last) unless index == 0
      ranges << CharRange.new(text: '', style_names: current_styles, entity_key: current_entity)
    end

    ranges.last.text += char.char
  end

  yield ranges.last
end

#entity_rangesObject



67
68
69
70
71
# File 'lib/draftjs_html/draftjs/block.rb', line 67

def entity_ranges
  @entity_ranges ||= raw_entity_ranges.map do |raw|
    ApplicableRange.parse(raw['key'].to_s, raw)
  end
end

#inline_stylesObject



61
62
63
64
65
# File 'lib/draftjs_html/draftjs/block.rb', line 61

def inline_styles
  @inline_styles ||= inline_style_ranges.map do |raw|
    ApplicableRange.parse(raw['style'], raw)
  end
end

#lengthObject



17
18
19
# File 'lib/draftjs_html/draftjs/block.rb', line 17

def length
  text.length
end