richer_text 0.8.0 → 0.10.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: a91f47881a33b61d264e59fe2500da26b0ba346f3fe1eda0b2f2a3e8759e6700
4
- data.tar.gz: a5a605c07462c979d3b30cce5c9756cb65a7d4a271ca463edb2e75e81192a859
3
+ metadata.gz: 587dab9004a19d9fdd0d5d1c191f01f2d128e73f20a4c10b38b1b43fc6206a94
4
+ data.tar.gz: 60ca0263af5e39723bfab6cb3700623aed408bd34e55d4fc6cd81dbebf0f823b
5
5
  SHA512:
6
- metadata.gz: 6d4faf62c78eb8bc372f11c2f0c9209eb14d0c0c622b79cab2c19de14a01da6c9fd4705a7384548bdb810fea8ad7cce4ca87c28fb9e7181974962054af217746
7
- data.tar.gz: e462e01e68d7edbb0cea0882e28469fdf8b9aaed92df8f7df7ed405abda49b78e9bb1f7c9dff62df081bbdfff30d0d8dbade9b7c9cc88a71aac77c8cdc6a15f8
6
+ metadata.gz: 7d43de3d31186bf8e5c19b1194d817440c10e05e2b4ebfb689882233a4ac7680fb64761476305535c3a1bc5b7f3896116f87419cb824b174dc2e9c7ef5108222
7
+ data.tar.gz: 41d039dc8fe7b478d7f106562812c1871c9341438f04441cff205ac6f0df35d7b535571c1e3d6dffd1296c61d369828ab1efcd169b82020a18c65a0f1819184b
@@ -19,6 +19,12 @@ module RicherText
19
19
  RicherText.default_renderer.visit(document)
20
20
  end
21
21
 
22
+ def mentionees
23
+ gids = mention_nodes.map(&:id).compact_blank
24
+
25
+ GlobalID::Locator.locate_many(gids).uniq # Only return unique records
26
+ end
27
+
22
28
  private
23
29
 
24
30
  def update_attachments
@@ -30,6 +36,10 @@ module RicherText
30
36
  find_nodes_of_type(RicherText::Nodes::Image).flatten.compact_blank
31
37
  end
32
38
 
39
+ def mention_nodes
40
+ find_nodes_of_type(RicherText::Nodes::Mention).flatten.compact_blank
41
+ end
42
+
33
43
  def rhino_attachment_nodes
34
44
  find_nodes_of_type(RicherText::Nodes::AttachmentFigure).flatten.compact_blank
35
45
  end
@@ -19,6 +19,12 @@ module RicherText
19
19
  body&.to_html&.to_s
20
20
  end
21
21
 
22
+ def mentionees
23
+ global_ids = body&.mentionees_global_ids
24
+
25
+ GlobalID::Locator.locate_many(global_ids)
26
+ end
27
+
22
28
  private
23
29
 
24
30
  def update_images
@@ -28,6 +28,12 @@ module RicherText
28
28
  end.uniq
29
29
  end
30
30
 
31
+ def mentionees_global_ids
32
+ global_ids = fragment.find_all("span[data-type=mention]").map do |span|
33
+ span.attributes["data-id"].value
34
+ end.uniq
35
+ end
36
+
31
37
  def image_blobs
32
38
  image_blob_ids.map { |id| ActiveStorage::Blob.find_signed(id) }
33
39
  end
@@ -0,0 +1,27 @@
1
+ module RicherText
2
+ class Embed
3
+ include Rendering
4
+
5
+ def self.find(sgid)
6
+ @embed = GlobalID::Locator.locate_signed(sgid, for: "embeddable")
7
+ new(@embed) if @embed
8
+ end
9
+
10
+ attr_reader :embed
11
+
12
+ def initialize(embed)
13
+ @embed = embed
14
+ end
15
+
16
+ def object
17
+ @embed
18
+ end
19
+
20
+ delegate :to_embeddable_partial_path, to: :@embed
21
+ delegate :model_name, to: :@embed
22
+
23
+ def html
24
+ render partial: to_embeddable_partial_path, object: object, as: model_name.element
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module RicherText
2
+ module Embeddable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def embeddable_sgid
7
+ to_sgid(expires_in: nil, for: "embeddable").to_s
8
+ end
9
+
10
+ def to_embeddable_partial_path
11
+ to_partial_path
12
+ end
13
+ end
14
+ end
15
+ end
@@ -42,10 +42,25 @@ module RicherText
42
42
  "<div class='richer-text'>#{visit_children(node).join("\n")}</div>".html_safe
43
43
  end
44
44
 
45
+ def visit_mention(node, marks)
46
+ if marks.any?
47
+ content_tag(marks[0].tag, visit_mention(node, marks[1..]), marks[0].attrs)
48
+ else
49
+ tag.span(
50
+ tag.img(src: node.avatar_url, class: "richer-text--mention-img") +
51
+ tag.span(node.name, class: "richer-text--mention-label"),
52
+ class: "richer-text--mention")
53
+ end
54
+ end
55
+
45
56
  def visit_paragraph(node)
46
57
  "<p style='#{node.style}'>#{visit_children(node).join}</p>"
47
58
  end
48
59
 
60
+ def visit_richer_text_embed(node)
61
+ node.html
62
+ end
63
+
49
64
  def visit_list_item(node)
50
65
  "<li>#{visit_children(node).join}</li>"
51
66
  end
@@ -1,6 +1,7 @@
1
1
  module RicherText
2
2
  class Node
3
3
  attr_reader :json, :attrs, :children, :type
4
+ include RicherText::Rendering
4
5
 
5
6
  STYLES = {
6
7
  "textAlign" => "text-align",
@@ -0,0 +1,30 @@
1
+ module RicherText
2
+ module Nodes
3
+ class Mention < ::RicherText::Node
4
+ def initialize(json)
5
+ @marks = json.fetch("marks", []).map { |mark| RicherText::Mark.new(mark) }
6
+ super(json)
7
+ end
8
+
9
+ def accept(visitor)
10
+ visitor.visit_mention(self, @marks)
11
+ end
12
+
13
+ def user
14
+ @user ||= GlobalID::Locator.locate(@attrs["id"])
15
+ end
16
+
17
+ def id
18
+ @attrs["id"]
19
+ end
20
+
21
+ def name
22
+ @user.respond_to?(:name) ? @user.name : @attrs["label"]
23
+ end
24
+
25
+ def avatar_url
26
+ @user.respond_to?(:avatar_url) ? @user.avatar_url : @attrs["avatarUrl"]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module RicherText
2
+ module Nodes
3
+ class RicherTextEmbed < ::RicherText::Node
4
+ def sgid
5
+ attrs["sgid"]
6
+ end
7
+
8
+ def html
9
+ render partial: embeddable.to_embeddable_partial_path, object: embeddable, as: embeddable.model_name.element
10
+ rescue ActiveRecord::RecordNotFound
11
+ "" # TODO: handle this better
12
+ end
13
+
14
+ def embeddable
15
+ @embeddable ||= GlobalID::Locator.locate_signed(sgid, for: "embeddable")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,13 +1,23 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/module/attribute_accessors_per_thread"
3
+
1
4
  module RicherText
2
5
  module Rendering
3
- private
6
+ extend ActiveSupport::Concern
4
7
 
5
- def render(*args, &block)
6
- action_controller_renderer.render(*args, &block)
8
+ included do
9
+ thread_cattr_accessor :renderer, instance_accessor: false
10
+ delegate :render, to: :class
7
11
  end
8
12
 
9
- def action_controller_renderer
10
- ActionController::Base.renderer
13
+ class_methods do
14
+ def action_controller_renderer
15
+ @action_controller_renderer ||= Class.new(ActionController::Base).renderer
16
+ end
17
+
18
+ def render(*args, &block)
19
+ (renderer || action_controller_renderer).render_to_string(*args, &block)
20
+ end
11
21
  end
12
22
  end
13
23
  end
@@ -1,3 +1,3 @@
1
1
  module RicherText
2
- VERSION = "0.8.0"
2
+ VERSION = "0.10.0"
3
3
  end
data/lib/richer_text.rb CHANGED
@@ -14,6 +14,9 @@ module RicherText
14
14
  autoload :Serialization
15
15
  autoload :TagHelper
16
16
 
17
+ autoload :Embed
18
+ autoload :Embeddable
19
+
17
20
  autoload :Node
18
21
  autoload :Mark
19
22
  autoload :HTMLVisitor
@@ -33,8 +36,10 @@ module RicherText
33
36
  autoload :HorizontalRule
34
37
  autoload :Image
35
38
  autoload :ListItem
36
- autoload :Paragraph
39
+ autoload :Mention
37
40
  autoload :OrderedList
41
+ autoload :Paragraph
42
+ autoload :RicherTextEmbed
38
43
  autoload :Text
39
44
  autoload :Table
40
45
  autoload :TableCell
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.8.0
4
+ version: 0.10.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-20 00:00:00.000000000 Z
11
+ date: 2023-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -54,6 +54,8 @@ files:
54
54
  - lib/richer_text.rb
55
55
  - lib/richer_text/attribute.rb
56
56
  - lib/richer_text/content.rb
57
+ - lib/richer_text/embed.rb
58
+ - lib/richer_text/embeddable.rb
57
59
  - lib/richer_text/engine.rb
58
60
  - lib/richer_text/fragment.rb
59
61
  - lib/richer_text/html_visitor.rb
@@ -71,8 +73,10 @@ files:
71
73
  - lib/richer_text/nodes/horizontal_rule.rb
72
74
  - lib/richer_text/nodes/image.rb
73
75
  - lib/richer_text/nodes/list_item.rb
76
+ - lib/richer_text/nodes/mention.rb
74
77
  - lib/richer_text/nodes/ordered_list.rb
75
78
  - lib/richer_text/nodes/paragraph.rb
79
+ - lib/richer_text/nodes/richer_text_embed.rb
76
80
  - lib/richer_text/nodes/table.rb
77
81
  - lib/richer_text/nodes/table_cell.rb
78
82
  - lib/richer_text/nodes/table_header.rb