richer_text 0.1.0 → 0.3.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: b63576861e360399b080b94d0bc593c516688af16f6dbfd0ea70ee922deddf34
4
- data.tar.gz: a488d3031c92e1d0e34662bf942e48209030e2393a9227737e905ffb7041e60d
3
+ metadata.gz: 4015b9a354c480f94ac845846dc9fdee7d4f9402f42175ebe8080ce2b0e66d87
4
+ data.tar.gz: 4ee8d41ce6076773f86d2703cac148fb41f8a98cdf0feedfc0da0e584575245d
5
5
  SHA512:
6
- metadata.gz: 7dbebb9524973cf7a949ee78b45fa80c73c97286e17d83a901281f500fde88d9bc8b03f103552545fd0fc5c066181a0a729dc4ba2e86575306c8be423b6b65d7
7
- data.tar.gz: 45832c613c64aced07c6260163d4a1af2afe815a1af1a83beb799db24731b22e0787a262c5b0f109d0c1563510c594c1664294f5df1efe6c54a7d62b53ccd04d
6
+ metadata.gz: 8cfe5f6dc39db89cc2327a40aa0d4198b1785850cd2dd1d1f11a2fa941dea3a68cd771a8f40295c6f8ba8f6c6a0752ab9aa43382dc19fbd3a2261693bd5a27e9
7
+ data.tar.gz: 5beab5c3cd4e36867b8d302f2cc3a08221dc739d2ab9f3812adf59f06ff1541faacd99be07947b0315f648e1bf0cfb469c3dfd1c4df5c9ac8af3551c58b6c75c
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).
@@ -0,0 +1,32 @@
1
+ .richer-text table {
2
+ border-collapse: collapse;
3
+ margin: 0;
4
+ overflow: hidden;
5
+ table-layout: fixed;
6
+ width: 100%;
7
+ }
8
+
9
+ .richer-text table td,
10
+ .richer-text table th {
11
+ border: 2px solid #ced4da;
12
+ box-sizing: border-box;
13
+ min-width: 1em;
14
+ padding: 3px 5px;
15
+ position: relative;
16
+ vertical-align: top;
17
+ }
18
+
19
+ .richer-text table td>*,
20
+ .richer-text table th>* {
21
+ margin-bottom: 0;
22
+ }
23
+
24
+ .richer-text table th {
25
+ background-color: #f1f3f5;
26
+ font-weight: bold;
27
+ text-align: left;
28
+ }
29
+
30
+ .richer-text p {
31
+ margin: 0;
32
+ }
@@ -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, tags: %w(div h1 h2 blockquote s del strong i em br pre code ol ul li table tbody th td tr p), attributes: %w(id class style data-color colspan rowspan)) %>
3
+ </div>
@@ -9,6 +9,18 @@ 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
+
18
+ copy_file(
19
+ "app/assets/stylesheets/richer_text/richer-text.css",
20
+ "app/assets/stylesheets/richer-text.css"
21
+ )
22
+ end
23
+
12
24
  def install_javascript_dependencies
13
25
  destination = Pathname(destination_root)
14
26
 
@@ -26,7 +38,10 @@ module RicherText
26
38
 
27
39
  if destination.join("app/assets/stylesheets/application.tailwind.css").exist?
28
40
  say "Adding import to application.tailwind.css", :green
29
- prepend_to_file "app/assets/stylesheets/application.tailwind.css", %(@import "@afomera/richer-text/dist/css/richer-text.css";\n@import "highlight.js/styles/github-dark.css";\n)
41
+ prepend_to_file "app/assets/stylesheets/application.tailwind.css", %(@import "@afomera/richer-text/dist/css/richer-text.css";\n@import "highlight.js/styles/github-dark.css";\n@import "richer-text.css";\n)
42
+ elsif (stylesheets = Dir.glob "#{destination_root}/app/assets/stylesheets/application.*.{scss}").length > 0
43
+ say "Adding import to #{stylesheets.first}", :green
44
+ prepend_to_file stylesheets.first, %(@import "@afomera/richer-text/dist/css/richer-text";\n@import "highlight.js/styles/github-dark";\n@import "richer-text";\n)
30
45
  end
31
46
  end
32
47
 
@@ -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.3.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,14 +1,14 @@
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.3.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-07-29 00:00:00.000000000 Z
11
+ date: 2023-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,6 +36,7 @@ files:
36
36
  - Rakefile
37
37
  - app/assets/config/richer_text_manifest.js
38
38
  - app/assets/stylesheets/richer_text/application.css
39
+ - app/assets/stylesheets/richer_text/richer-text.css
39
40
  - app/controllers/richer_text/application_controller.rb
40
41
  - app/helpers/richer_text/application_helper.rb
41
42
  - app/helpers/richer_text/tag_helper.rb
@@ -45,6 +46,7 @@ files:
45
46
  - app/models/richer_text/application_record.rb
46
47
  - app/models/richer_text/rich_text.rb
47
48
  - app/views/layouts/richer_text/application.html.erb
49
+ - app/views/richer_text/contents/_content.html.erb
48
50
  - config/routes.rb
49
51
  - db/migrate/20230107020316_create_richer_text_rich_texts.rb
50
52
  - lib/generators/richer_text/install/install_generator.rb
@@ -53,6 +55,7 @@ files:
53
55
  - lib/richer_text/content.rb
54
56
  - lib/richer_text/engine.rb
55
57
  - lib/richer_text/fragment.rb
58
+ - lib/richer_text/rendering.rb
56
59
  - lib/richer_text/serialization.rb
57
60
  - lib/richer_text/version.rb
58
61
  - lib/tasks/richer_text_tasks.rake