Class: DraftjsHtml::HtmlDepth

Inherits:
Object
  • Object
show all
Defined in:
lib/draftjs_html/html_depth.rb

Overview

This class manages the depth and nesting of the myriad HTML tags generated by DraftjsHtml::ToHtml. It is intended to be a private implementation detail.

Constant Summary collapse

BLOCK_TYPE_TO_HTML_WRAPPER =

:nodoc:

{
  'code-block' => 'pre',
  'ordered-list-item' => 'ol',
  'unordered-list-item' => 'ul',
}.freeze
LIST_ROOTS =
%w[ol ul]

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ HtmlDepth

Returns a new instance of HtmlDepth.



11
12
13
14
15
# File 'lib/draftjs_html/html_depth.rb', line 11

def initialize(body)
  @current_depth = 0
  @body = body
  @previous_block = nil
end

Instance Method Details

#apply(block) ⇒ Object



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
62
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
98
99
100
101
102
103
104
# File 'lib/draftjs_html/html_depth.rb', line 19

def apply(block)
  if block.type == 'code-block'
    push_parent(block)
    store_previous(block)
    return
  end

  # Nesting lists - extra content same level
  if @current_depth > 0 && block.depth == @current_depth && block.type != @previous_block&.type
    set_li_as_root
    store_previous(block)
    return
  end

  # Nesting lists - deepening inside a proper list
  if block.depth == @current_depth + 1 && block.type == @previous_block&.type
    set_li_as_root
    push_parent(block)
    store_previous(block)
    return
  end

  # Nesting lists - deepening skipping level 0
  if block.depth > @current_depth && !list_block?(@previous_block)
    while @current_depth < block.depth
      push_parent(block)
      @body.parent = create_child('li')
      @current_depth += 1
    end
    push_parent(block)
    store_previous(block)
    return
  end

  # Nesting lists - deepening from inside skipping several levels
  if block.depth > @current_depth && list_block?(@previous_block)
    set_li_as_root
    while block.depth > @current_depth + 1
      push_parent(block)
      @body.parent = create_child('li')
      @current_depth += 1
    end
    push_parent(block)
    store_previous(block)
    return
  end

  # Nesting lists - rising
  if block.depth < @current_depth
    # pop_to_nearest_list_root, n times
    (@current_depth - block.depth).times do
      if @body.parent.name == 'li'
        pop_parent
        pop_parent
        pop_parent
      elsif LIST_ROOTS.include?(@body.parent.name)
        pop_parent
        pop_parent
      else
        pop_parent
      end
    end
  end

  # Sibling list items
  if block.depth == 0 && list_block?(block) && @previous_block&.type == block.type
    store_previous(block)
    return
  end

  # Any-old root list item
  if block.depth == 0 && list_block?(block) && @previous_block&.depth.to_i == 0
    push_parent(block)
    store_previous(block)
    return
  end

  # Leaving the list
  if block.depth == 0 && !list_block?(block) && (list_block?(@previous_block) || @previous_block&.depth.to_i > 0)
    pop_to_document_root
    store_previous(block)
    return
  end

  store_previous(block)
end