draftjs_html 0.11.0 → 0.13.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 +1 -1
- data/lib/draftjs_html/html_depth.rb +61 -31
- data/lib/draftjs_html/to_html.rb +1 -2
- 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: 050fd09294a00e0826e07a04238cf197b5d8ff0bbe39afc643400ddda3222bcc
|
4
|
+
data.tar.gz: a3fa4a939eb34470bfc9f3f0e00cccfbb28257a101bc32e8cdcbcd94a2246fac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d9625eb7f4752c9f1f62a0cbf26f90d99835ec7dfd4ac133b4fdd7a67348b2aeb0c9e62b1187bc3b9a48ee3b0887fb24261e5f8daa139b3e570c7c1797ad71f
|
7
|
+
data.tar.gz: 4be2ef23835b8d614ea53359d01650ce881404dd3cf69db9bc65b99063157dbee515aa6670d668b0d0f3a0817141a943457cc3bd11bfac3b24d2edac00fe423c
|
@@ -8,58 +8,88 @@ module DraftjsHtml
|
|
8
8
|
'unordered-list-item' => 'ul',
|
9
9
|
}.freeze
|
10
10
|
|
11
|
-
attr_reader :body
|
12
|
-
|
13
11
|
def initialize(body)
|
14
12
|
@current_depth = 0
|
15
13
|
@body = body
|
16
14
|
@previous_parents = [body.parent]
|
15
|
+
@nesting_roots = [body.parent.name]
|
17
16
|
end
|
18
17
|
|
19
18
|
def apply(block)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
19
|
+
return unless nesting_root_changed?(block) || depth_changed?(block)
|
20
|
+
|
21
|
+
if deepening?(block)
|
22
|
+
deepen(block)
|
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)
|
29
|
+
push_parent(block)
|
30
|
+
elsif nested?
|
31
|
+
pop_parent
|
33
32
|
end
|
33
|
+
|
34
|
+
@current_depth = block.depth
|
34
35
|
end
|
35
36
|
|
36
37
|
private
|
37
38
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
def deepen(block)
|
40
|
+
tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
|
41
|
+
@previous_parents << @body.parent
|
42
|
+
@nesting_roots << tagname
|
43
|
+
@body.parent = @body.parent.last_element_child
|
44
|
+
push_parent(block)
|
42
45
|
end
|
43
46
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
def push_parent(block)
|
48
|
+
tagname = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
|
49
|
+
node = create_child(tagname)
|
50
|
+
@previous_parents << @body.parent
|
51
|
+
@body.parent = node
|
48
52
|
end
|
49
53
|
|
50
|
-
def
|
54
|
+
def rise(times:)
|
51
55
|
times.times do
|
52
|
-
|
53
|
-
|
56
|
+
begin
|
57
|
+
pop_parent
|
58
|
+
end while @body.parent.name != @nesting_roots.last
|
59
|
+
@nesting_roots.pop
|
54
60
|
end
|
55
61
|
end
|
56
62
|
|
57
|
-
def
|
58
|
-
|
63
|
+
def pop_parent
|
64
|
+
@body.parent = @previous_parents.pop
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_child(tagname)
|
68
|
+
@body.parent.add_child(@body.doc.create_element(tagname))
|
69
|
+
end
|
70
|
+
|
71
|
+
def nested?
|
72
|
+
@body.parent.name != 'body'
|
73
|
+
end
|
74
|
+
|
75
|
+
def still_nested?(block)
|
76
|
+
BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
|
77
|
+
end
|
78
|
+
|
79
|
+
def depth_changed?(block)
|
80
|
+
block.depth != @current_depth
|
81
|
+
end
|
82
|
+
|
83
|
+
def nesting_root_changed?(block)
|
84
|
+
@body.parent.name != BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
|
85
|
+
end
|
86
|
+
|
87
|
+
def rising?(block)
|
88
|
+
@current_depth > block.depth
|
59
89
|
end
|
60
90
|
|
61
|
-
def
|
62
|
-
|
91
|
+
def deepening?(block)
|
92
|
+
@current_depth < block.depth
|
63
93
|
end
|
64
94
|
end
|
65
|
-
end
|
95
|
+
end
|
data/lib/draftjs_html/to_html.rb
CHANGED
@@ -9,7 +9,6 @@ module DraftjsHtml
|
|
9
9
|
def initialize(options)
|
10
10
|
@options = ensure_options!(options)
|
11
11
|
@document = Nokogiri::HTML::Builder.new(encoding: @options.fetch(:encoding, 'UTF-8'))
|
12
|
-
@html_depth = HtmlDepth.new(@document)
|
13
12
|
end
|
14
13
|
|
15
14
|
def convert(raw_draftjs)
|
@@ -17,7 +16,7 @@ module DraftjsHtml
|
|
17
16
|
|
18
17
|
@document.html do |html|
|
19
18
|
html.body do |body|
|
20
|
-
@
|
19
|
+
@html_depth = HtmlDepth.new(body)
|
21
20
|
|
22
21
|
draftjs.blocks.each do |block|
|
23
22
|
@html_depth.apply(block)
|
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.13.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: 2022-10-
|
11
|
+
date: 2022-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -79,7 +79,7 @@ metadata:
|
|
79
79
|
homepage_uri: https://github.com/dugancathal/draftjs_html
|
80
80
|
source_code_uri: https://github.com/dugancathal/draftjs_html
|
81
81
|
changelog_uri: https://github.com/dugancathal/draftjs_html/tree/main/CHANGELOG.md
|
82
|
-
post_install_message:
|
82
|
+
post_install_message:
|
83
83
|
rdoc_options: []
|
84
84
|
require_paths:
|
85
85
|
- lib
|
@@ -94,8 +94,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
rubygems_version: 3.1.
|
98
|
-
signing_key:
|
97
|
+
rubygems_version: 3.1.6
|
98
|
+
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: A tool for converting DraftJS JSON to HTML (and back again)
|
101
101
|
test_files: []
|