draftjs_html 0.28.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee7df9bd0f6a54994b17925c487f2c824ec538e773abe2c56b5591e5cd846573
4
- data.tar.gz: c874e8b188f77d2ffd7ade661ff5e61cea5c136b0d89e3a9bf022a5e87de142f
3
+ metadata.gz: 87efa57150dbbe1c730824cea917dbabaad3aec0512232102995863da4f0e30c
4
+ data.tar.gz: b634cbce19c5da38dbd36df706f9f4ba90a0391a1e487907bd355c2d4772eda0
5
5
  SHA512:
6
- metadata.gz: 4f4dc0f13111d6a0424d24620810bb5ac928bade2a1ba29fee46baa182e91a67430909920b3c9be69f4836299fbc44953f4db8c20a4948431e33bfc496def6a0
7
- data.tar.gz: 852986e0ce26c74d377bd8c202de3a898510b5ef5b20f594b65e035c3b25258a11375a4aed562aa62cbfcdd42388125316d9bef82cbf8ff670ba442a766ff926
6
+ metadata.gz: 80fc8946627d72d1d3add5c3e0e6e4a91656aa715cec1d20d4471273ad129abccbbe47c81077b2874c896733528537c8bdcce7a99d825f0d908b3e524f2efd48
7
+ data.tar.gz: dd460fa38fea6e915a442ba231993e231c009784618b0ed36f9d7f7e583cedef6246f20447ae16937c0ba98b09f9977390738771161a4a08571ff6ee8ab107e6
@@ -11,113 +11,133 @@ module DraftjsHtml
11
11
  def initialize(body)
12
12
  @current_depth = 0
13
13
  @body = body
14
- @previous_parents = [body.parent]
15
- @nesting_roots = [body.parent.name]
14
+ @previous_block = nil
16
15
  end
17
16
 
17
+ LIST_ROOTS = %w[ol ul]
18
+
18
19
  def apply(block)
19
- return unless nesting_root_changed?(block) || depth_changed?(block)
20
-
21
- if deepening?(block)
22
- deepen(block, desired_depth_change: block.depth - @current_depth)
23
- elsif rising?(block) && still_nested?(block)
24
- rise(times: @current_depth - block.depth)
25
- elsif rising?(block)
26
- rise(times: @current_depth - block.depth)
27
- pop_parent
28
- elsif still_nested?(block)
20
+ if block.type == 'code-block'
29
21
  push_parent(block)
30
- elsif nested?
31
- pop_parent
22
+ store_previous(block)
23
+ return
32
24
  end
33
25
 
34
- @current_depth = block.depth
35
- end
36
-
37
- private
26
+ # Nesting lists - extra content same level
27
+ if @current_depth > 0 && block.depth == @current_depth && block.type != @previous_block&.type
28
+ set_li_as_root
29
+ store_previous(block)
30
+ return
31
+ end
38
32
 
39
- def deepen(block, desired_depth_change: 0)
40
- if inside_valid_nesting_root?
41
- set_previous_li_as_parent(block)
42
- else
43
- create_valid_nesting_root(block)
33
+ # Nesting lists - deepening inside a proper list
34
+ if block.depth == @current_depth + 1 && block.type == @previous_block&.type
35
+ set_li_as_root
36
+ push_parent(block)
37
+ store_previous(block)
38
+ return
44
39
  end
45
40
 
46
- (desired_depth_change - 1).times do
47
- create_valid_nesting_root(block)
41
+ # Nesting lists - deepening skipping level 0
42
+ if block.depth > @current_depth && !list_block?(@previous_block)
43
+ while @current_depth < block.depth
44
+ push_parent(block)
45
+ @body.parent = create_child('li')
46
+ @current_depth += 1
47
+ end
48
+ push_parent(block)
49
+ store_previous(block)
50
+ return
48
51
  end
49
52
 
50
- push_parent(block)
51
- end
53
+ # Nesting lists - deepening from inside skipping several levels
54
+ if block.depth > @current_depth && list_block?(@previous_block)
55
+ set_li_as_root
56
+ while block.depth > @current_depth + 1
57
+ push_parent(block)
58
+ @body.parent = create_child('li')
59
+ @current_depth += 1
60
+ end
61
+ push_parent(block)
62
+ store_previous(block)
63
+ return
64
+ end
52
65
 
53
- def push_parent(block)
54
- tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
55
- node = create_child(tagname)
56
- @previous_parents << @body.parent
57
- @body.parent = node
58
- end
66
+ # Nesting lists - rising
67
+ if block.depth < @current_depth
68
+ # pop_to_nearest_list_root, n times
69
+ (@current_depth - block.depth).times do
70
+ if @body.parent.name == 'li'
71
+ pop_parent
72
+ pop_parent
73
+ pop_parent
74
+ elsif LIST_ROOTS.include?(@body.parent.name)
75
+ pop_parent
76
+ pop_parent
77
+ else
78
+ pop_parent
79
+ end
80
+ end
81
+ end
59
82
 
60
- def rise(times:)
61
- times.times do
62
- begin
63
- pop_parent
64
- end while @body.parent.name != @nesting_roots.last
65
- @nesting_roots.pop
83
+ # Sibling list items
84
+ if block.depth == 0 && list_block?(block) && @previous_block&.type == block.type
85
+ store_previous(block)
86
+ return
66
87
  end
67
- end
68
88
 
69
- def pop_parent
70
- @body.parent = @previous_parents.pop
71
- end
89
+ # Any-old root list item
90
+ if block.depth == 0 && list_block?(block) && @previous_block&.depth.to_i == 0
91
+ push_parent(block)
92
+ store_previous(block)
93
+ return
94
+ end
72
95
 
73
- def create_child(tagname)
74
- @body.parent.add_child(@body.doc.create_element(tagname))
75
- end
96
+ # Leaving the list
97
+ if block.depth == 0 && !list_block?(block) && (list_block?(@previous_block) || @previous_block&.depth.to_i > 0)
98
+ pop_to_document_root
99
+ store_previous(block)
100
+ return
101
+ end
76
102
 
77
- def nested?
78
- @body.parent.name != 'body'
103
+ store_previous(block)
79
104
  end
80
105
 
81
- def still_nested?(block)
82
- BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
83
- end
106
+ private
84
107
 
85
- def depth_changed?(block)
86
- block.depth != @current_depth
108
+ def store_previous(block)
109
+ @current_depth = block.depth
110
+ @previous_block = block
87
111
  end
88
112
 
89
- def nesting_root_changed?(block)
90
- @body.parent.name != BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
113
+ def list_block?(block)
114
+ block&.type&.end_with?('list-item')
91
115
  end
92
116
 
93
- def rising?(block)
94
- @current_depth > block.depth
117
+ def pop_to_document_root
118
+ pop_parent until current_root_name == 'body'
95
119
  end
96
120
 
97
- def deepening?(block)
98
- @current_depth < block.depth
121
+ def current_root_name
122
+ @body.parent.name
99
123
  end
100
124
 
101
- def create_valid_nesting_root(block)
102
- parent_tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
103
- node = create_child(parent_tagname)
104
- @previous_parents << node
105
- @nesting_roots << parent_tagname
125
+ def push_parent(block)
126
+ tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
127
+ node = create_child(tagname)
106
128
  @body.parent = node
129
+ end
107
130
 
108
- list_item = create_child('li')
109
- @body.parent = list_item
131
+ def pop_parent
132
+ @body.parent = @body.parent.parent
110
133
  end
111
134
 
112
- def set_previous_li_as_parent(block)
113
- tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
114
- @previous_parents << @body.parent
115
- @nesting_roots << tagname
116
- @body.parent = @body.parent.last_element_child
135
+ def create_child(tagname)
136
+ @body.parent.add_child(@body.doc.create_element(tagname))
117
137
  end
118
138
 
119
- def inside_valid_nesting_root?
120
- BLOCK_TYPE_TO_HTML_WRAPPER.values.include?(@body.parent.name)
139
+ def set_li_as_root
140
+ @body.parent = @body.parent.last_element_child
121
141
  end
122
142
  end
123
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.28.0"
4
+ VERSION = "0.30.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draftjs_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.0
4
+ version: 0.30.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-21 00:00:00.000000000 Z
11
+ date: 2023-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri