draftjs_html 0.29.0 → 0.31.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/draftjs/block.rb +4 -0
- data/lib/draftjs_html/draftjs/content.rb +15 -0
- data/lib/draftjs_html/html_depth.rb +93 -73
- data/lib/draftjs_html/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 664e4ae07bc1d64294767d98e5a41b2adf8f998dcf6b7109d30b924d2947f931
|
4
|
+
data.tar.gz: 6e3a4339a7a2c0d812d64b19260a8c57c1ca9b52ee8cb61969645bf1d75a0558
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa6e7d24bf3389ab5cec981ec5e22f0a1ef10e68b51479a33ae54eaa0701b110b5b21fae3c1b7a14936dff54fb29bf52bc3c92a6202511ed463ffe1c770b1e32
|
7
|
+
data.tar.gz: 3897c00b471a7dc3dec926f90c8047d71d38ff9299acc7a9a8435a52b2d4b4ec204892cbadadf3269131a12e0d87d119b01f8eae81cecfd95727ce08644c0ac4
|
@@ -18,9 +18,24 @@ module DraftjsHtml
|
|
18
18
|
entity_map[key]
|
19
19
|
end
|
20
20
|
|
21
|
+
def attach_entity(entity, block, range)
|
22
|
+
new_key = new_entity_key
|
23
|
+
entity_map[new_key] = entity
|
24
|
+
block.add_entity(new_key, range)
|
25
|
+
end
|
26
|
+
|
21
27
|
def to_raw
|
22
28
|
ToRaw.new.convert(self)
|
23
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def new_entity_key
|
34
|
+
loop do
|
35
|
+
key = SecureRandom.uuid
|
36
|
+
break key unless entity_map.key?(key)
|
37
|
+
end
|
38
|
+
end
|
24
39
|
end
|
25
40
|
end
|
26
41
|
end
|
@@ -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.31.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Taylor
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -88,7 +88,7 @@ metadata:
|
|
88
88
|
homepage_uri: https://github.com/dugancathal/draftjs_html
|
89
89
|
source_code_uri: https://github.com/dugancathal/draftjs_html
|
90
90
|
changelog_uri: https://github.com/dugancathal/draftjs_html/tree/main/CHANGELOG.md
|
91
|
-
post_install_message:
|
91
|
+
post_install_message:
|
92
92
|
rdoc_options: []
|
93
93
|
require_paths:
|
94
94
|
- lib
|
@@ -103,8 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
requirements: []
|
106
|
-
rubygems_version: 3.
|
107
|
-
signing_key:
|
106
|
+
rubygems_version: 3.4.10
|
107
|
+
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: A tool for converting DraftJS JSON to HTML (and back again)
|
110
110
|
test_files: []
|