draftjs_html 0.12.0 → 0.14.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/raw_builder.rb +65 -0
- data/lib/draftjs_html/draftjs.rb +1 -0
- 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 +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b708c9dfd4a3d4b6cdf22de377edd9a271e6eb527c70648d11337c1d090613e5
|
4
|
+
data.tar.gz: e472de0d6b79d5b7cf7380d6ed89f8c85aedf79d30d329a42550b1cf94eb7dd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6511068eab5eb7c99f7815c85bb6b40bd998f80cee7ed8b41a418d9295bf467f4ed4a1de47049342aa27ec02e0e29207031f1f3cfa9e853da13acc79d98e21b1
|
7
|
+
data.tar.gz: bd939bb048b11692d520cfef3202b7e9d7106f36cac901cd4171f82991ccf24981b8cc610f0e61626231c463c84202d5dbb80b26221cecf1f68b9f48b435b32a
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module DraftjsHtml
|
4
|
+
module Draftjs
|
5
|
+
class RawBuilder
|
6
|
+
def self.build(&block)
|
7
|
+
instance = new
|
8
|
+
instance.instance_eval(&block)
|
9
|
+
instance.to_h
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@blocks = []
|
14
|
+
@entity_map = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def text_block(text)
|
18
|
+
typed_block('unstyled', text)
|
19
|
+
end
|
20
|
+
|
21
|
+
def typed_block(type, text, depth: 0)
|
22
|
+
@blocks << { 'key' => SecureRandom.urlsafe_base64(10), 'text' => text, 'type' => type, 'depth' => depth }
|
23
|
+
end
|
24
|
+
|
25
|
+
def inline_style(style_name, range)
|
26
|
+
(@blocks.last['inlineStyleRanges'] ||= []) << { 'style' => style_name, 'offset' => range.begin, 'length' => range.size }
|
27
|
+
end
|
28
|
+
|
29
|
+
def entity_range(key, range)
|
30
|
+
(@blocks.last['entityRanges'] ||= []) << { 'key' => key, 'offset' => range.begin, 'length' => range.size }
|
31
|
+
end
|
32
|
+
|
33
|
+
def apply_entity(type, range, data: {}, mutability: 'IMMUTABLE', key: SecureRandom.uuid)
|
34
|
+
@entity_map[key] = {
|
35
|
+
'type' => type,
|
36
|
+
'mutability' => mutability,
|
37
|
+
'data' => deep_stringify_keys(data),
|
38
|
+
}
|
39
|
+
|
40
|
+
entity_range(key, range)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_h
|
44
|
+
{
|
45
|
+
'blocks' => @blocks,
|
46
|
+
'entityMap' => @entity_map,
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def deep_stringify_keys(object)
|
53
|
+
case object
|
54
|
+
when Hash
|
55
|
+
object.each_with_object({}) do |(key, value), result|
|
56
|
+
result[key.to_s] = deep_stringify_keys(value)
|
57
|
+
end
|
58
|
+
when Array then object.map { |e| deep_stringify_keys(e) }
|
59
|
+
else
|
60
|
+
object
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/draftjs_html/draftjs.rb
CHANGED
@@ -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.14.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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/draftjs_html/draftjs/content.rb
|
65
65
|
- lib/draftjs_html/draftjs/entity.rb
|
66
66
|
- lib/draftjs_html/draftjs/entity_map.rb
|
67
|
+
- lib/draftjs_html/draftjs/raw_builder.rb
|
67
68
|
- lib/draftjs_html/draftjs/to_raw.rb
|
68
69
|
- lib/draftjs_html/html_defaults.rb
|
69
70
|
- lib/draftjs_html/html_depth.rb
|
@@ -79,7 +80,7 @@ metadata:
|
|
79
80
|
homepage_uri: https://github.com/dugancathal/draftjs_html
|
80
81
|
source_code_uri: https://github.com/dugancathal/draftjs_html
|
81
82
|
changelog_uri: https://github.com/dugancathal/draftjs_html/tree/main/CHANGELOG.md
|
82
|
-
post_install_message:
|
83
|
+
post_install_message:
|
83
84
|
rdoc_options: []
|
84
85
|
require_paths:
|
85
86
|
- lib
|
@@ -94,8 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
95
|
- !ruby/object:Gem::Version
|
95
96
|
version: '0'
|
96
97
|
requirements: []
|
97
|
-
rubygems_version: 3.1.
|
98
|
-
signing_key:
|
98
|
+
rubygems_version: 3.1.6
|
99
|
+
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: A tool for converting DraftJS JSON to HTML (and back again)
|
101
102
|
test_files: []
|