richer_text 0.6.0 → 0.8.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: 823a38cf937e83676b16a06828bf3a3fa461f8b3d38ae628beecd33dfc3927c2
4
- data.tar.gz: 4f02bdb13f66fb9b896dee5d52a01806dfab1cf842c4aa33250d1420b49c8fad
3
+ metadata.gz: a91f47881a33b61d264e59fe2500da26b0ba346f3fe1eda0b2f2a3e8759e6700
4
+ data.tar.gz: a5a605c07462c979d3b30cce5c9756cb65a7d4a271ca463edb2e75e81192a859
5
5
  SHA512:
6
- metadata.gz: 1cf13ee1015adee390ccdb1da3a3f2f317f01c70ea2f10364762c526a1619f56aa79085ddae04ab1690ed602bca0ccdedc3f83c0691fca21c9d4f46cc4314ade
7
- data.tar.gz: 9f1fffb59f348833c3a7ebdf1b86075bddfb2a4b4b4459bb7bdb211418e6fbf34c2ffe42c3b9c70f9c2610f6bd2874386003a11a0aaa75adbc64cc55149f6aa6
6
+ metadata.gz: 6d4faf62c78eb8bc372f11c2f0c9209eb14d0c0c622b79cab2c19de14a01da6c9fd4705a7384548bdb810fea8ad7cce4ca87c28fb9e7181974962054af217746
7
+ data.tar.gz: e462e01e68d7edbb0cea0882e28469fdf8b9aaed92df8f7df7ed405abda49b78e9bb1f7c9dff62df081bbdfff30d0d8dbade9b7c9cc88a71aac77c8cdc6a15f8
@@ -17,16 +17,40 @@ module ActionView::Helpers
17
17
  end
18
18
  end
19
19
 
20
+ class Tags::RhinoEditor < Tags::Base
21
+ include Tags::Placeholderable
22
+
23
+ delegate :dom_id, to: ActionView::RecordIdentifier
24
+
25
+ def render
26
+ options = @options.stringify_keys
27
+ add_default_name_and_id(options)
28
+ options["input"] ||= dom_id(object, [options["id"], :rhino_text_input].compact.join("_")) if object
29
+ options["value"] = options.fetch("value") { value&.to_editor_format }
30
+ options["serializer"] = options.fetch("serializer") { "html"}
31
+
32
+ @template_object.rhino_text_area_tag(options.delete("name"), options["value"], options.except("value"))
33
+ end
34
+ end
35
+
20
36
  module FormHelper
21
37
  def richer_text_area(object_name, method, options = {})
22
38
  Tags::Editor.new(object_name, method, self, options).render
23
39
  end
40
+
41
+ def rhino_text_area(object_name, method, options = {})
42
+ Tags::RhinoEditor.new(object_name, method, self, options).render
43
+ end
24
44
  end
25
45
 
26
46
  class FormBuilder
27
47
  def richer_text_area(method, options = {})
28
48
  @template.richer_text_area(@object_name, method, objectify_options(options))
29
49
  end
50
+
51
+ def rhino_text_area(method, options = {})
52
+ @template.rhino_text_area(@object_name, method, objectify_options(options))
53
+ end
30
54
  end
31
55
  end
32
56
 
@@ -49,5 +73,18 @@ module RicherText
49
73
 
50
74
  input_tag + editor_tag
51
75
  end
76
+
77
+ def rhino_text_area_tag(name, value = nil, options = {})
78
+ options = options.symbolize_keys
79
+ options[:input] ||= "rhino_text_input_#{RicherText::TagHelper.id += 1}"
80
+
81
+ # So we can choose the serializer to use, e.g. "html" or "json"
82
+ options[:serializer] ||= "html"
83
+
84
+ input_tag = hidden_field_tag(name, value, id: options[:input])
85
+ editor_tag = tag("rhino-editor", { input: options[:input], serializer: "json", data: { "blob-url-template": rails_service_blob_url(":signed_id", ":filename"), "direct-upload-url": rails_direct_uploads_url }}.merge(options))
86
+
87
+ input_tag + editor_tag
88
+ end
52
89
  end
53
90
  end
@@ -7,8 +7,9 @@ module RicherText
7
7
  serialize :body, JSON, default: DEFAULT_BODY.to_json
8
8
 
9
9
  has_many_attached :images
10
+ has_many_attached :rhino_attachments # For handling attachments in the Rhino Editor
10
11
 
11
- before_save :update_images
12
+ before_save :update_attachments
12
13
 
13
14
  def to_editor_format
14
15
  body
@@ -20,16 +21,28 @@ module RicherText
20
21
 
21
22
  private
22
23
 
23
- def update_images
24
+ def update_attachments
24
25
  self.images = image_nodes.map(&:signed_id)
26
+ self.rhino_attachments = rhino_attachment_records.map(&:signed_id)
25
27
  end
26
28
 
27
29
  def image_nodes
28
30
  find_nodes_of_type(RicherText::Nodes::Image).flatten.compact_blank
29
31
  end
30
32
 
33
+ def rhino_attachment_nodes
34
+ find_nodes_of_type(RicherText::Nodes::AttachmentFigure).flatten.compact_blank
35
+ end
36
+
37
+ def rhino_attachment_records
38
+ sgids = rhino_attachment_nodes.map(&:sgid).compact_blank
39
+ GlobalID::Locator.locate_many_signed(sgids, for: "attachable")
40
+ end
41
+
31
42
  def find_nodes_of_type(type, node = document)
32
43
  if node.children.any?
44
+ return node if node.is_a?(type)
45
+
33
46
  node.children.map { |child| find_nodes_of_type(type, child) }
34
47
  else
35
48
  node.is_a?(type) ? node : nil
@@ -3,10 +3,10 @@ module RicherText
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  class_methods do
6
- def has_richer_text(name, json: false)
6
+ def has_richer_text(name, store_as: :html)
7
7
  # Store if the attribute is using JSON or not.
8
8
  class_attribute :"richer_text_#{name}_json", instance_writer: false
9
- self.send(:"richer_text_#{name}_json=", json)
9
+ self.send(:"richer_text_#{name}_json=", store_as == :json)
10
10
 
11
11
  class_eval <<-CODE, __FILE__, __LINE__ + 1
12
12
  def #{name}
@@ -23,7 +23,7 @@ module RicherText
23
23
  CODE
24
24
 
25
25
  has_one :"richer_text_#{name}", -> { where(name: name) },
26
- class_name: json ? "RicherText::JsonText" : "RicherText::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy
26
+ class_name: store_as == :json ? "RicherText::JsonText" : "RicherText::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy
27
27
 
28
28
  scope :"with_richer_text_#{name}", -> { includes("richer_text_#{name}") }
29
29
  end
@@ -6,6 +6,18 @@ module RicherText
6
6
  node.accept(self)
7
7
  end
8
8
 
9
+ def visit_attachment_figure(node)
10
+ "<figure sgid=#{node.attrs["sgid"]}>
11
+ #{node_previewable?(node) ? "<img src=#{node.url} />" : "<a href=#{node.url} target='_blank'>"}
12
+ <figcaption class='attachment__caption'>#{visit_children(node).join}</figcaption>
13
+ #{node_previewable?(node) ? "" : "</a>"}
14
+ </figure>"
15
+ end
16
+
17
+ def visit_attachment_gallery(node)
18
+ "<div class='attachment-gallery'>#{visit_children(node).join}</div>"
19
+ end
20
+
9
21
  def visit_blockquote(node)
10
22
  "<blockquote>#{visit_children(node).join}</blockquote>"
11
23
  end
@@ -81,5 +93,15 @@ module RicherText
81
93
  def visit_table_header(node)
82
94
  "<th>#{visit_children(node).join}</th>"
83
95
  end
96
+
97
+ private
98
+
99
+ def previewable_regex
100
+ /^image(\/(gif|png|jpe?g)|$)/
101
+ end
102
+
103
+ def node_previewable?(node)
104
+ node.attrs["contentType"].match(previewable_regex)
105
+ end
84
106
  end
85
107
  end
@@ -8,8 +8,8 @@ module RicherText
8
8
 
9
9
  def self.build(json)
10
10
  node = json.is_a?(String) ? JSON.parse(json) : json
11
- klass = "RicherText::Nodes::#{node["type"].underscore.classify}".safe_constantize
12
- klass.new(node)
11
+ klass = "RicherText::Nodes::#{node["type"].underscore.classify}".constantize
12
+ klass.new(node) if klass
13
13
  end
14
14
 
15
15
  def initialize(json)
@@ -0,0 +1,17 @@
1
+ module RicherText
2
+ module Nodes
3
+ class AttachmentFigure < ::RicherText::Node
4
+ def previewable?
5
+ @attrs["previewable"]
6
+ end
7
+
8
+ def url
9
+ @attrs["url"].presence || @attrs["src"]
10
+ end
11
+
12
+ def sgid
13
+ @attrs["sgid"]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ module RicherText
2
+ module Nodes
3
+ class AttachmentGallery < ::RicherText::Node
4
+ end
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module RicherText
2
- VERSION = "0.6.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/richer_text.rb CHANGED
@@ -21,6 +21,8 @@ module RicherText
21
21
  module Nodes
22
22
  extend ActiveSupport::Autoload
23
23
 
24
+ autoload :AttachmentFigure # For Rhino Editor support
25
+ autoload :AttachmentGallery # For Rhino Editor support
24
26
  autoload :Blockquote
25
27
  autoload :BulletList
26
28
  autoload :Callout
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: richer_text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Fomera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -59,6 +59,8 @@ files:
59
59
  - lib/richer_text/html_visitor.rb
60
60
  - lib/richer_text/mark.rb
61
61
  - lib/richer_text/node.rb
62
+ - lib/richer_text/nodes/attachment_figure.rb
63
+ - lib/richer_text/nodes/attachment_gallery.rb
62
64
  - lib/richer_text/nodes/blockquote.rb
63
65
  - lib/richer_text/nodes/bullet_list.rb
64
66
  - lib/richer_text/nodes/callout.rb