richer_text 0.1.0 → 0.2.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: b63576861e360399b080b94d0bc593c516688af16f6dbfd0ea70ee922deddf34
4
- data.tar.gz: a488d3031c92e1d0e34662bf942e48209030e2393a9227737e905ffb7041e60d
3
+ metadata.gz: 8ab9e5f516632d096522a7aa18daaad74e8c6b2eb5736bce0aff526e49a6f5a0
4
+ data.tar.gz: 532ff98188ba2b2425188cfcbec7cf80d5563d65339c7a247fdebb41d418247a
5
5
  SHA512:
6
- metadata.gz: 7dbebb9524973cf7a949ee78b45fa80c73c97286e17d83a901281f500fde88d9bc8b03f103552545fd0fc5c066181a0a729dc4ba2e86575306c8be423b6b65d7
7
- data.tar.gz: 45832c613c64aced07c6260163d4a1af2afe815a1af1a83beb799db24731b22e0787a262c5b0f109d0c1563510c594c1664294f5df1efe6c54a7d62b53ccd04d
6
+ metadata.gz: 39981e09d8ce208402c1c66a1541a706af868ad1563d4497b5d919a452256da00308448a6c08a5751f5cebc4b1613c165a2c0ec4d9302a8356a0f33f27f653db
7
+ data.tar.gz: 5ea9229f6bfd06ab525ab96b543dc63eed8dcbd34b3419f2dda0fe1058dba8168d881e29871917916ba1b03be7b7d7372da1b233f7c37018528f7c867383d4a6
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # RicherText
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ RicherText aims to provide a richer text editing experience than what comes out of the box with ActionText in Rails. **It is however a seperate thing from ActionText** and is **not** backwards compatible.
4
+
5
+ RicherText uses React and TipTap under the hood to create an editor, this does mean that you'll have react and react-dom in your project, but you absolutely don't need to use it outside of the RicherText editor. Additionally there's currently a hard requirement for ActiveStorage as well.
6
6
 
7
7
  ## Installation
8
+
8
9
  Add this line to your application's Gemfile:
9
10
 
10
11
  ```ruby
@@ -12,17 +13,73 @@ gem "richer_text"
12
13
  ```
13
14
 
14
15
  And then execute:
16
+
15
17
  ```bash
16
18
  $ bundle
17
19
  ```
18
20
 
19
- Or install it yourself as:
21
+ Once you've installed the gem the next step is to run the Generator and install all required libraries.
22
+
20
23
  ```bash
21
- $ gem install richer_text
24
+ $ rails richer_text:install
25
+ ```
26
+
27
+ > [!IMPORTANT]
28
+ > If you wish to use highlight.js outside of RicherText you'll need to add some code to your javascript entry point.
29
+
30
+ ```js
31
+ const hljs = require("highlight.js");
32
+
33
+ document.addEventListener("turbo:load", (event) => {
34
+ document.querySelectorAll("pre").forEach((block) => {
35
+ hljs.highlightElement(block);
36
+ });
37
+ });
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ > [!WARNING]
43
+ > You probably shouldn't use this gem / npm package (yet!) for anything serious. It's still undergoing development. But please do try it on a non-production app!
44
+
45
+ To use RicherText, once you've completed the installation process is a matter of doing the following:
46
+
47
+ **Add has_richer_text to a model**
48
+
49
+ Take a Post model where you'd like to add RicherText to write, and edit the body attribute:
50
+
51
+ ```ruby
52
+ class Post < ApplicationRecord
53
+ has_richer_text :body
54
+ end
55
+ ```
56
+
57
+ **Add the form helper to the form**
58
+
59
+ Inside of your form partial:
60
+
61
+ ```erb
62
+ <%= form.label :body %>
63
+ <%= form.richer_text_area :body %>
64
+ ```
65
+
66
+ Optionally you can pass arguments to the RicherText editor...
67
+
68
+ ```erb
69
+ <%= form.label :body %>
70
+ <%= form.richer_text_area :body, callouts: true, placeholder: "Write something..." %>
71
+ ```
72
+
73
+ **Render the richer text content**
74
+
75
+ ```erb
76
+ <%= @post.body %>
22
77
  ```
23
78
 
24
79
  ## Contributing
80
+
25
81
  Contribution directions go here.
26
82
 
27
83
  ## License
84
+
28
85
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,17 +1,18 @@
1
1
  module RicherText
2
2
  class RichText < ApplicationRecord
3
- belongs_to :record, polymorphic: true
3
+ belongs_to :record, polymorphic: true, touch: true
4
4
 
5
5
  serialize :body, RicherText::Content
6
6
 
7
7
  delegate :to_s, :nil?, to: :body
8
+ delegate :blank?, :empty?, :present?, to: :to_html
8
9
 
9
10
  has_many_attached :images
10
11
 
11
12
  before_save :update_images
12
13
 
13
14
  def to_html
14
- body&.to_html
15
+ body&.to_html&.to_s
15
16
  end
16
17
 
17
18
  private
@@ -0,0 +1,3 @@
1
+ <div class="richer-text">
2
+ <%= sanitize(content.to_html, attributes: %w(id class style data-color)) %>
3
+ </div>
@@ -9,6 +9,13 @@ module RicherText
9
9
  rails_command "railties:install:migrations FROM=active_storage,richer_text", inline: true
10
10
  end
11
11
 
12
+ def copy_files
13
+ copy_file(
14
+ "app/views/richer_text/contents/_content.html.erb",
15
+ "app/views/richer_text/contents/_content.html.erb"
16
+ )
17
+ end
18
+
12
19
  def install_javascript_dependencies
13
20
  destination = Pathname(destination_root)
14
21
 
@@ -1,6 +1,6 @@
1
1
  module RicherText
2
2
  class Content
3
- include Serialization
3
+ include ActiveModel::Conversion, Serialization, Rendering
4
4
 
5
5
  delegate :blank?, :empty?, :html_safe, :present?, to: :to_html
6
6
 
@@ -15,7 +15,7 @@ module RicherText
15
15
  end
16
16
 
17
17
  def to_s
18
- to_html.html_safe # TODO: add some kind of sanitization
18
+ render partial: to_partial_path, layout: false, locals: { content: self }
19
19
  end
20
20
 
21
21
  def to_html
@@ -41,6 +41,7 @@ module RicherText
41
41
  end
42
42
  end
43
43
 
44
- attr_reader :body, :fragment
44
+ attr_reader :fragment
45
+ attr_accessor :body
45
46
  end
46
47
  end
@@ -0,0 +1,13 @@
1
+ module RicherText
2
+ module Rendering
3
+ private
4
+
5
+ def render(*args, &block)
6
+ action_controller_renderer.render(*args, &block)
7
+ end
8
+
9
+ def action_controller_renderer
10
+ ActionController::Base.renderer
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module RicherText
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/richer_text.rb CHANGED
@@ -9,6 +9,7 @@ module RicherText
9
9
 
10
10
  autoload :Attribute
11
11
  autoload :Content
12
+ autoload :Rendering
12
13
  autoload :Fragment
13
14
  autoload :Serialization
14
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: richer_text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Fomera
@@ -45,6 +45,7 @@ files:
45
45
  - app/models/richer_text/application_record.rb
46
46
  - app/models/richer_text/rich_text.rb
47
47
  - app/views/layouts/richer_text/application.html.erb
48
+ - app/views/richer_text/contents/_content.html.erb
48
49
  - config/routes.rb
49
50
  - db/migrate/20230107020316_create_richer_text_rich_texts.rb
50
51
  - lib/generators/richer_text/install/install_generator.rb
@@ -53,6 +54,7 @@ files:
53
54
  - lib/richer_text/content.rb
54
55
  - lib/richer_text/engine.rb
55
56
  - lib/richer_text/fragment.rb
57
+ - lib/richer_text/rendering.rb
56
58
  - lib/richer_text/serialization.rb
57
59
  - lib/richer_text/version.rb
58
60
  - lib/tasks/richer_text_tasks.rake