action_text_overview.md 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**

Action Text Overview
====================

This guide provides you with all you need to get started in handling
rich text content.

After reading this guide, you will know:

* How to configure Action Text.
* How to handle rich text content.
* How to style rich text content.

--------------------------------------------------------------------------------

17 18
What is Action Text?
--------------------
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

Action Text brings rich text content and editing to Rails. It includes
the [Trix editor](https://trix-editor.org) that handles everything from formatting
to links to quotes to lists to embedded images and galleries.
The rich text content generated by the Trix editor is saved in its own
RichText model that's associated with any existing Active Record model in the application.
Any embedded images (or other attachments) are automatically stored using
Active Storage and associated with the included RichText model.

## Trix compared to other rich text editors

Most WYSIWYG editors are wrappers around HTML’s `contenteditable` and `execCommand` APIs,
designed by Microsoft to support live editing of web pages in Internet Explorer 5.5,
and [eventually reverse-engineered](https://blog.whatwg.org/the-road-to-html-5-contenteditable#history)
and copied by other browsers.

Because these APIs were never fully specified or documented,
and because WYSIWYG HTML editors are enormous in scope, each
browser's implementation has its own set of bugs and quirks,
and JavaScript developers are left to resolve the inconsistencies.

Trix sidesteps these inconsistencies by treating contenteditable
as an I/O device: when input makes its way to the editor, Trix converts that input
into an editing operation on its internal document model, then re-renders
that document back into the editor. This gives Trix complete control over what
happens after every keystroke, and avoids the need to use execCommand at all.

## Installation

48
Run `bin/rails action_text:install` to add the Yarn package and copy over the necessary migration. Also, you need to set up Active Storage for embedded images and other attachments. Please refer to the [Active Storage Overview](active_storage_overview.html) guide.
J
Jack Kinsella 已提交
49

50
After the installation is complete, a Rails app using Webpacker should have the following changes:
J
Jack Kinsella 已提交
51

52
1. Both `trix` and `@rails/actiontext` should be required in your JavaScript pack.
J
Jack Kinsella 已提交
53

54 55 56 57 58
    ```js
    // application.js
    require("trix")
    require("@rails/actiontext")
    ```
J
Jack Kinsella 已提交
59

60
2. The `trix` stylesheet should be imported into `actiontext.scss`.
J
Jack Kinsella 已提交
61

62 63 64
    ```scss
    @import "trix/dist/trix";
    ```
J
Jack Kinsella 已提交
65

66
    Additionally, this `actiontext.scss` file should be imported into your stylesheet pack.
J
Jack Kinsella 已提交
67

68
    ```scss
69 70 71
    // application.scss
    @import "./actiontext.scss";
    ```
72 73 74 75 76 77 78 79 80 81 82 83

## Examples

Adding a rich text field to an existing model:

```ruby
# app/models/message.rb
class Message < ApplicationRecord
  has_rich_text :content
end
```

J
Jack Kinsella 已提交
84 85
Note that you don't need to add a `content` field to your `messages` table.

86 87 88 89
Then refer to this field in the form for the model:

```erb
<%# app/views/messages/_form.html.erb %>
J
Jonathan Hefner 已提交
90
<%= form_with model: message do |form| %>
91 92 93 94 95 96 97
  <div class="field">
    <%= form.label :content %>
    <%= form.rich_text_area :content %>
  </div>
<% end %>
```

98
And finally, display the sanitized rich text on a page:
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

```erb
<%= @message.content %>
```

To accept the rich text content, all you have to do is permit the referenced attribute:

```ruby
class MessagesController < ApplicationController
  def create
    message = Message.create! params.require(:message).permit(:title, :content)
    redirect_to message
  end
end
```

115 116 117 118 119 120 121 122 123
## Avoid N+1 queries

If you wish to preload the dependent `ActionText::RichText` model, you can use the named scope:

```ruby
Message.all.with_rich_text_content # Preload the body without attachments.
Message.all.with_rich_text_content_and_embeds # Preload both body and attachments.
```

124 125 126 127
## Custom styling

By default, the Action Text editor and content is styled by the Trix defaults.
If you want to change these defaults, you'll want to remove
128
the `app/assets/stylesheets/actiontext.scss` linker and base your stylings on
129 130 131 132 133
the [contents of that file](https://raw.githubusercontent.com/basecamp/trix/master/dist/trix.css).

You can also style the HTML used for embedded images and other attachments (known as blobs).
On installation, Action Text will copy over a partial to
`app/views/active_storage/blobs/_blob.html.erb`, which you can specialize.
F
fedeagripa 已提交
134

135
## API / Backend development
F
fedeagripa 已提交
136

137
1. A backend API (for example, using JSON) needs a separate endpoint for uploading files that creates an `ActiveStorage::Blob` and returns its `attachable_sgid`:
F
fedeagripa 已提交
138

139 140 141 142 143
    ```json
    {
      "attachable_sgid": "BAh7CEkiCG…"
    }
    ```
F
fedeagripa 已提交
144

145 146 147 148 149
2. Take that `attachable_sgid` and ask your frontend to insert it in rich text content using an `<action-text-attachment>` tag:

    ```html
    <action-text-attachment sgid="BAh7CEkiCG…"></action-text-attachment>
    ```
F
fedeagripa 已提交
150

151
This is based on Basecamp, so if you still can't find what you are looking for, check this [Basecamp Doc](https://github.com/basecamp/bc3-api/blob/master/sections/rich_text.md).