rails/actiontext
Sean Doyle 68bb2650d5 Action Text: forward form: option to hidden input
Trix's `<trix-editor>` doesn't support the [form][] property like
`<textarea>` or other form fields.

For example, consider the following HTML and event listener:

```html
<form action="/articles" method="post">
  <textarea name="content"></textarea>

  <button type="submit">Save</button>
</form>

<script>
  addEventListener("keydown", ({ key, metaKey, target }) => {
    if (target.form && key == "Enter" && (metaKey || ctrlKey)) {
      form.requestSubmit()
    }
  })
</script>
```

The `target` (an instance of `HTMLTextAreaElement` relies on the
[HTMLTextAreaElement.form][] property for access to its associated
`<form>`. While it's usually equivalent to `target.closest("form")`,
that isn't always the case. Declaring a `[form]` attribute with another
`<form>` element's `[id]` value can associate a field to a `<form>` that
is _not an ancestor_. That means that the event listener from above
would continue to work with this HTML:

```html
<textarea name="content" form="new_article"></textarea>

<!-- elsewhere -->
<form id="new_article" action="/articles" method="post">
  <button type="submit">Save</button>
</form>
```

Unfortunately, if the `<textarea>` element were replaced with a
`<trix-editor>`, the event listener's reliance on accessing the form as
a property would break, since the `<trix-editor>` custom element doesn't
declare that property. There is currently a pull request
([basecamp/trix#899][]) to add support for accessing the `form` as a
property of the `<trix-editor>` element.

The [feedback][] provided on that pull request suggests that we
implement the `form` property by delegating to the `<input
type="hidden">` element. Currently, `<input type="hidden">` elements
constructed by Action Text helpers cannot declare the `[form]`
attribute.

This commit adds support by special-casing the `options[:form]` key
within `ActionText::TagHelper#rich_text_area_tag`.

[form]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement#properties
[basecamp/trix#899]: https://github.com/basecamp/trix/pull/899#discussion_r618543357
[feedback]: https://github.com/basecamp/trix/pull/899#discussion_r618543357
2021-04-25 20:27:24 -04:00
..
app Action Text: forward form: option to hidden input 2021-04-25 20:27:24 -04:00
bin Import Action Text 2019-01-04 22:22:49 -05:00
db/migrate Allow changing text and blob size without giving the limit option 2019-01-29 06:49:32 +09:00
lib Enable Performance/MapCompact cop 2021-04-23 16:33:02 +09:00
test Action Text: forward form: option to hidden input 2021-04-25 20:27:24 -04:00
.gitignore Remove redundant .gitignore entries 2020-02-07 14:05:23 -06:00
actiontext.gemspec Rails 7 requires Ruby 2.7 and prefer Ruby 3+ 2021-02-04 16:34:53 +00:00
CHANGELOG.md Action Text: forward form: option to hidden input 2021-04-25 20:27:24 -04:00
MIT-LICENSE Bump license years to 2021 [ci skip] 2021-01-01 12:21:20 +09:00
package.json @rails/actiontext: depend on released @rails/activestorage 2021-03-06 11:32:44 -05:00
Rakefile Add ActionDispatch::SystemTestCase#fill_in_rich_text_area 2019-05-13 12:44:06 -04:00
README.md Add Action Text to guides [ci skip] 2019-01-05 13:30:37 +02:00

Action Text

Action Text brings rich text content and editing to Rails. It includes the Trix editor 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.

You can read more about Action Text in the Action Text Overview guide.

License

Action Text is released under the MIT License.