draftjs_html 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64964e629da00518122848b73f2d233df3ccf36ad70b01df292a9f3d6596b35d
4
- data.tar.gz: 4e17e6825b69e260d3e6e08e7391037be69940ae39a28a06ea689ead498da010
3
+ metadata.gz: b708c9dfd4a3d4b6cdf22de377edd9a271e6eb527c70648d11337c1d090613e5
4
+ data.tar.gz: e472de0d6b79d5b7cf7380d6ed89f8c85aedf79d30d329a42550b1cf94eb7dd7
5
5
  SHA512:
6
- metadata.gz: cc2162d612c85e58a26a91ec25412fcca1d780609563d9032bc4108b8e015198da53cbf39f0cd7cf91b25b03f2bf9e9f566e083f4fc981703f7adef81a6c3368
7
- data.tar.gz: 1ae9b058c647284138228d54a503766c4a16a94c15b0060598577ab45cdf07af25ddce9b2fa6cfc118c0d1d9e0e7bb04fc2e77609eaf2b3478d1f78e31134409
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
@@ -7,6 +7,7 @@ require_relative 'draftjs/block'
7
7
  require_relative 'draftjs/entity_map'
8
8
  require_relative 'draftjs/entity'
9
9
  require_relative 'draftjs/to_raw'
10
+ require_relative 'draftjs/raw_builder'
10
11
 
11
12
  module DraftjsHtml
12
13
  module Draftjs
@@ -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.14.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.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-12 00:00:00.000000000 Z
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.2
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: []