draftjs_html 0.29.0 → 0.30.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/draftjs_html/html_depth.rb +93 -73
- data/lib/draftjs_html/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87efa57150dbbe1c730824cea917dbabaad3aec0512232102995863da4f0e30c
|
4
|
+
data.tar.gz: b634cbce19c5da38dbd36df706f9f4ba90a0391a1e487907bd355c2d4772eda0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
@
|
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
|
-
|
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
|
-
|
31
|
-
|
22
|
+
store_previous(block)
|
23
|
+
return
|
32
24
|
end
|
33
25
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
40
|
-
if
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
47
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
78
|
-
@body.parent.name != 'body'
|
103
|
+
store_previous(block)
|
79
104
|
end
|
80
105
|
|
81
|
-
|
82
|
-
BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
|
83
|
-
end
|
106
|
+
private
|
84
107
|
|
85
|
-
def
|
86
|
-
block.depth
|
108
|
+
def store_previous(block)
|
109
|
+
@current_depth = block.depth
|
110
|
+
@previous_block = block
|
87
111
|
end
|
88
112
|
|
89
|
-
def
|
90
|
-
|
113
|
+
def list_block?(block)
|
114
|
+
block&.type&.end_with?('list-item')
|
91
115
|
end
|
92
116
|
|
93
|
-
def
|
94
|
-
|
117
|
+
def pop_to_document_root
|
118
|
+
pop_parent until current_root_name == 'body'
|
95
119
|
end
|
96
120
|
|
97
|
-
def
|
98
|
-
@
|
121
|
+
def current_root_name
|
122
|
+
@body.parent.name
|
99
123
|
end
|
100
124
|
|
101
|
-
def
|
102
|
-
|
103
|
-
node = create_child(
|
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
|
-
|
109
|
-
@body.parent =
|
131
|
+
def pop_parent
|
132
|
+
@body.parent = @body.parent.parent
|
110
133
|
end
|
111
134
|
|
112
|
-
def
|
113
|
-
tagname
|
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
|
120
|
-
|
139
|
+
def set_li_as_root
|
140
|
+
@body.parent = @body.parent.last_element_child
|
121
141
|
end
|
122
142
|
end
|
123
143
|
end
|
data/lib/draftjs_html/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2023-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|