Skip to content

Commit e9cff0f

Browse files
authored
Merge branch 'main' into main
2 parents e94cfb1 + 50bac54 commit e9cff0f

39 files changed

+16570
-1540
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"semi": ["error", "never"],
2626
"sort-imports": ["error", { "ignoreDeclarationSort": true }]
2727
},
28-
"ignorePatterns": ["dist/**/*.js", "**/vendor/**/*.js"],
28+
"ignorePatterns": ["dist/**/*.js", "**/vendor/**/*.js", "action_text-trix/**/*.js"],
2929
"globals": {
3030
"after": true,
3131
"getComposition": true,

.github/workflows/ci.yml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
11
name: CI
22

3+
concurrency:
4+
group: "${{github.workflow}}-${{github.ref}}"
5+
cancel-in-progress: true
6+
37
on:
48
push:
5-
branches:
6-
- main
9+
branches: [ main ]
710
pull_request:
11+
types: [opened, synchronize]
12+
branches: [ '*' ]
813

914
jobs:
1015
build:
1116
name: Browser tests
1217
runs-on: ubuntu-latest
1318
steps:
14-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
1520
- uses: actions/setup-node@v3
1621
with:
17-
node-version: 16
22+
node-version: 18
1823
cache: "yarn"
1924
- name: Install Dependencies
2025
run: yarn install --frozen-lockfile
2126
- run: bin/ci
27+
rails-tests:
28+
name: Downstream Rails integration tests
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
- uses: actions/setup-node@v3
33+
with:
34+
node-version: 18
35+
cache: "yarn"
36+
- uses: ruby/setup-ruby@v1
37+
with:
38+
ruby-version: "3.4"
39+
- name: Install Dependencies
40+
run: yarn install --frozen-lockfile
41+
- name: Packaging
42+
run: yarn build
43+
- name: Clone Rails
44+
run: git clone --depth=1 https://github.com/rails/rails
45+
- name: Configure Rails
46+
run: |
47+
sudo apt install libvips-tools
48+
cd rails
49+
yarn install --frozen-lockfile
50+
bundle add action_text-trix --path ".."
51+
bundle show --paths action_text-trix
52+
- name: Action Text tests
53+
run: |
54+
cd rails/actiontext
55+
bundle exec rake test test:system
2256
2357
env:
2458
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16.14.0
1+
18.18.0

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ import Trix from "trix"
9898
Trix.elements.TrixEditorElement.formAssociated = false
9999
```
100100

101+
When Trix is configured to be compatible with `ElementInternals`, it is also
102+
capable of functioning without an `<input type="hidden">` element. To configure
103+
a `<trix-editor>` element to skip creating its `<input type="hidden">`, set the
104+
element's `willCreateInput = false`:
105+
106+
```js
107+
addEventListener("before-trix-initialize", (event) => {
108+
const trixEditor = event.target
109+
110+
trixEditor.willCreateInput = false
111+
})
112+
```
113+
114+
> [!NOTE]
115+
> Trix will *always* use an associated `<input type="hidden">` element when the
116+
> `[input]` attribute is set. To migrate to `<input>`-free support, set
117+
> `willCreateInput = false`, then render the `<trix-editor>` without the
118+
> `[input]` attribute.
119+
120+
> [!WARNING]
121+
> In the absence of an `<input type="hidden">` element, the `<trix-editor>`
122+
> element's value will not be included in `<form>` element submissions unless it
123+
> is rendered with a `[name]` attribute. Set the `[name]` attribute to the same
124+
> value that the `<input type="hidden">` element would have.
125+
101126
## Invoking Internal Trix Actions
102127

103128
Internal actions are defined in `controllers/editor_controller.js` and consist of:
@@ -156,7 +181,18 @@ To populate a `<trix-editor>` with stored content, include that content in the a
156181
</form>
157182
```
158183

159-
Always use an associated input element to safely populate an editor. Trix won’t load any HTML content inside a `<trix-editor>…</trix-editor>` tag.
184+
Use an associated input element to initially populate an editor. When an associated input element is absent, Trix will safely sanitize then load any HTML content inside a `<trix-editor>…</trix-editor>` tag.
185+
186+
```html
187+
<form >
188+
<trix-editor>Editor content goes here</trix-editor>
189+
</form>
190+
```
191+
192+
> [!WARNING]
193+
> When a `<trix-editor>` element initially connects with both HTML content *and*
194+
> an associated input element, Trix will *always* disregard the HTML content and
195+
> load its initial content from the associated input element.
160196
161197
## Validating the Editor
162198

@@ -300,6 +336,27 @@ To store attachments, listen for the `trix-attachment-add` event. Upload the att
300336
301337
If you don’t want to accept dropped or pasted files, call `preventDefault()` on the `trix-file-accept` event, which Trix dispatches just before the `trix-attachment-add` event.
302338
339+
## Previewing Attached Files
340+
341+
Trix automatically previews attached image files. To determine whether or not to preview an attached file, Trix compares the file's content type against the [Trix.Attachment.previewablePattern](./src/trix/models/attachment.js#L7). By default, Trix will preview the following content types:
342+
343+
* `image/gif`
344+
* `image/png`
345+
* `image/webp`
346+
* `image/jpg`
347+
* `image/jpeg`
348+
349+
To customize an attachment's preview, listen for the `trix-attachment-add` event. When handling the event, set the attachment's `previewable` attribute, then change its preview URL by calling `setPreviewURL`:
350+
351+
```js
352+
addEventListener("trix-attachment-add", (event) => {
353+
if (event.attachment.file instanceof File) {
354+
event.attachment.setAttribute("previewable", true)
355+
event.attachment.setPreviewURL("...")
356+
}
357+
})
358+
```
359+
303360
# Editing Text Programmatically
304361
305362
You can manipulate a Trix editor programmatically through the `Trix.Editor` interface, available on each `<trix-editor>` element through its `editor` property.
@@ -558,6 +615,36 @@ For example if you want to keep a custom tag, you can access do that with:
558615
Trix.config.dompurify.ADD_TAGS = [ "my-custom-tag" ]
559616
```
560617
618+
## HTML Rendering
619+
620+
Trix renders changes to editor content by replacing existing nodes with new nodes.
621+
622+
To customize how Trix renders changes, set the `<trix-editor>` element's
623+
`render` property to a function that accepts a `<trix-editor>` instance and a
624+
[DocumentFragment][]:
625+
626+
```js
627+
document.addEventListener("trix-before-render", (event) => {
628+
const defaultRender = event.render
629+
630+
event.render = function(editorElement, documentFragment) {
631+
// modify the documentFragment…
632+
customize(documentFragment)
633+
634+
// render it with the default rendering function
635+
defaultRender(editorElement, documentFragment)
636+
}
637+
})
638+
```
639+
640+
> [!CAUTION]
641+
> By the time that `render(editorElement, documentFragment)` is
642+
> invoked, Trix will have finalized modifications to the HTML content (like HTML
643+
> sanitization, for example). If you make further modifications to the content,
644+
> be sure that they are safe.
645+
646+
[DocumentFragment]: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
647+
561648
## Observing Editor Changes
562649
563650
The `<trix-editor>` element emits several events which you can use to observe and respond to changes in editor state.
@@ -568,6 +655,10 @@ The `<trix-editor>` element emits several events which you can use to observe an
568655
569656
* `trix-change` fires whenever the editor’s contents have changed.
570657
658+
* `trix-before-render` fires before the editor’s new contents are rendered. You can override the function used to render the content through the `render` property on the event. The `render` function expects two positional arguments: the `<trix-editor>` element that will render and a [DocumentFragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment) instance that contains the new content. Read [HTML Rendering](#html-rendering) to learn more.
659+
660+
* `trix-before-paste` fires just before text is pasted into the editor. You can use this to modify the content being pasted or prevent the paste event from happening at all. The `paste` property on the event contains the pasted `string` or `html`, and the `range` of the inserted text.
661+
571662
* `trix-paste` fires whenever text is pasted into the editor. The `paste` property on the event contains the pasted `string` or `html`, and the `range` of the inserted text.
572663
573664
* `trix-selection-change` fires any time the selected range changes in the editor.
@@ -578,6 +669,8 @@ The `<trix-editor>` element emits several events which you can use to observe an
578669
579670
* `trix-attachment-add` fires after an attachment is added to the document. You can access the Trix attachment object through the `attachment` property on the event. If the `attachment` object has a `file` property, you should store this file remotely and set the attachment’s URL attribute. See the [attachment example](http://trix-editor.org/js/attachments.js) for detailed information.
580671
672+
* `trix-attachment-edit` fires after an attachment is edited in the document. You can access the Trix attachment object through the `attachment` property on the event.
673+
581674
* `trix-attachment-remove` fires when an attachment is removed from the document. You can access the Trix attachment object through the `attachment` property on the event. You may wish to use this event to clean up remotely stored files.
582675
583676
* `trix-action-invoke` fires when a Trix action is invoked. You can access the `<trix-editor>` element through the event's `target` property, the element responsible for invoking the action through the `invokingElement` property, and the action's name through the `actionName` property. The `trix-action-invoke` event will only fire for [custom](#invoking-external-custom-actions) actions and not for [built-in](#invoking-internal-trix-actions).

action_text-trix/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Gemfile.lock
2+
pkg

action_text-trix/Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
# Specify your gem's dependencies in trix.gemspec.
4+
gemspec

action_text-trix/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 37signals, LLC
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

action_text-trix/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Building the Trix Ruby gem
2+
3+
1. `cd action_text-trix`
4+
2. `bundle exec rake sync` (updates files which must be committed to git)
5+
3. `bundle exec rake build`
6+
4. `gem push pkg/*.gem`

action_text-trix/Rakefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "bundler/gem_tasks"
2+
require "rake/clean"
3+
4+
task :sync do
5+
require "json"
6+
7+
FileUtils.cp File.expand_path("../LICENSE", __dir__), __dir__, verbose: true
8+
9+
package_json = JSON.load(File.read(File.join(__dir__, "../package.json")))
10+
version = package_json["version"]
11+
File.write(File.join(__dir__, "lib", "action_text", "trix", "version.rb"), <<~RUBY)
12+
module Trix
13+
VERSION = "#{version}"
14+
end
15+
RUBY
16+
puts "Updated gem version to #{version}"
17+
end
18+
19+
CLEAN.add "pkg"
20+
CLOBBER.add "app/assets/javascripts/trix.js", "app/assets/stylesheets/trix.css"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative "lib/action_text/trix/version"
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "action_text-trix"
5+
spec.version = Trix::VERSION
6+
spec.authors = "37signals, LLC"
7+
spec.summary = "A rich text editor for everyday writing"
8+
spec.license = "MIT"
9+
10+
spec.homepage = "https://github.com/basecamp/trix"
11+
spec.metadata["homepage_uri"] = spec.homepage
12+
spec.metadata["source_code_uri"] = spec.homepage
13+
spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
14+
15+
spec.metadata["rubygems_mfa_required"] = "true"
16+
17+
spec.files = [
18+
"LICENSE",
19+
"app/assets/javascripts/trix.js",
20+
"app/assets/stylesheets/trix.css",
21+
"lib/action_text/trix.rb",
22+
"lib/action_text/trix/engine.rb",
23+
"lib/action_text/trix/version.rb"
24+
]
25+
26+
spec.add_dependency "railties"
27+
end

0 commit comments

Comments
 (0)