richer_text 0.8.0 → 0.9.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: 03e9732ed2320f56a2c30cbcd6af7614b6840b601aab70d790c96e6e8412babf
4
+ data.tar.gz: 4413b9240e46d0153dff22c7b4dd7161a555f572ada62b69159f0c267ae7b0b4
5
5
  SHA512:
6
- metadata.gz: 6d4faf62c78eb8bc372f11c2f0c9209eb14d0c0c622b79cab2c19de14a01da6c9fd4705a7384548bdb810fea8ad7cce4ca87c28fb9e7181974962054af217746
7
- data.tar.gz: e462e01e68d7edbb0cea0882e28469fdf8b9aaed92df8f7df7ed405abda49b78e9bb1f7c9dff62df081bbdfff30d0d8dbade9b7c9cc88a71aac77c8cdc6a15f8
6
+ metadata.gz: 39f20dfc3cecd2f74f4cc4d01b396d9678ceeed635ff4dc2a35bc62ba0dc8f6361f882dfe1df67b9286d63b1c86fbd6b7e73b729175e8fd3948435eb041a7ade
7
+ data.tar.gz: 4718871930d84edd4de392d9ea2d321f6a04782e5f0c774906e468e8c70ecac6ea9c78fb4ed92dcf7aa8598fea600dea3d13a117beef8287d5b709a8547d2318
@@ -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
@@ -42,6 +42,17 @@ 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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RicherText
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/richer_text.rb CHANGED
@@ -33,8 +33,9 @@ module RicherText
33
33
  autoload :HorizontalRule
34
34
  autoload :Image
35
35
  autoload :ListItem
36
- autoload :Paragraph
36
+ autoload :Mention
37
37
  autoload :OrderedList
38
+ autoload :Paragraph
38
39
  autoload :Text
39
40
  autoload :Table
40
41
  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.9.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-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -71,6 +71,7 @@ files:
71
71
  - lib/richer_text/nodes/horizontal_rule.rb
72
72
  - lib/richer_text/nodes/image.rb
73
73
  - lib/richer_text/nodes/list_item.rb
74
+ - lib/richer_text/nodes/mention.rb
74
75
  - lib/richer_text/nodes/ordered_list.rb
75
76
  - lib/richer_text/nodes/paragraph.rb
76
77
  - lib/richer_text/nodes/table.rb