draftjs_html 0.12.0 → 0.13.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: 64964e629da00518122848b73f2d233df3ccf36ad70b01df292a9f3d6596b35d
4
- data.tar.gz: 4e17e6825b69e260d3e6e08e7391037be69940ae39a28a06ea689ead498da010
3
+ metadata.gz: 050fd09294a00e0826e07a04238cf197b5d8ff0bbe39afc643400ddda3222bcc
4
+ data.tar.gz: a3fa4a939eb34470bfc9f3f0e00cccfbb28257a101bc32e8cdcbcd94a2246fac
5
5
  SHA512:
6
- metadata.gz: cc2162d612c85e58a26a91ec25412fcca1d780609563d9032bc4108b8e015198da53cbf39f0cd7cf91b25b03f2bf9e9f566e083f4fc981703f7adef81a6c3368
7
- data.tar.gz: 1ae9b058c647284138228d54a503766c4a16a94c15b0060598577ab45cdf07af25ddce9b2fa6cfc118c0d1d9e0e7bb04fc2e77609eaf2b3478d1f78e31134409
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
- new_wrapper_tag = BLOCK_TYPE_TO_HTML_WRAPPER[block.type]
21
- if body.parent.name != new_wrapper_tag || block.depth != @current_depth
22
- if @current_depth < block.depth
23
- push_depth(body, new_wrapper_tag)
24
- elsif @current_depth > block.depth
25
- pop_depth(body, times: @current_depth - block.depth)
26
- pop_nesting(body) unless new_wrapper_tag
27
- elsif new_wrapper_tag
28
- push_nesting(body, new_wrapper_tag)
29
- elsif @previous_parents.size > 1
30
- pop_nesting(body)
31
- end
32
- @current_depth = block.depth
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 push_depth(builder, tagname)
39
- @previous_parents << builder.parent
40
- builder.parent = builder.parent.last_element_child
41
- push_nesting(builder, tagname)
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 push_nesting(builder, tagname)
45
- node = create_child(builder, tagname)
46
- @previous_parents << builder.parent
47
- builder.parent = node
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 pop_depth(builder, times:)
54
+ def rise(times:)
51
55
  times.times do
52
- pop_nesting(builder)
53
- pop_nesting(builder)
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 pop_nesting(builder)
58
- builder.parent = @previous_parents.pop
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 create_child(builder, tagname)
62
- builder.parent.add_child(builder.doc.create_element(tagname))
91
+ def deepening?(block)
92
+ @current_depth < block.depth
63
93
  end
64
94
  end
65
- end
95
+ end
@@ -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
- @previous_parents = [body.parent]
19
+ @html_depth = HtmlDepth.new(body)
21
20
 
22
21
  draftjs.blocks.each do |block|
23
22
  @html_depth.apply(block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DraftjsHtml
4
- VERSION = "0.12.0"
4
+ VERSION = "0.13.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.12.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-12 00:00:00.000000000 Z
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.2
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: []