Skip to content

Commit e9343a1

Browse files
committed
Introduce trix/actiontext module
In an effort to absorb responsibilities from `@rails/actiontext` (namely the [app/javascript/actiontext/index.js][] and [app/javascript/actiontext/attachment_upload.js][] files), this commit introduces a new `trix/actiontext` file that the build process will output to be consumable from the `action_text-trix` engine's asset directory. The `trix/actiontext` module will depend on two in-browser dependencies: * `trix` expected to be imported separately * `@rails/activestorage` expected to available for import. Since there is a direct dependency on `@rails/activestorage` through the `@rails/acitontext` package, the dependency will dependably (😉) be present. In support of this change, this commit also expands the system test coverage introduced by [#1258][] to also account for `direct-upload:`-prefixed events dispatched during file uploads. [app/javascript/actiontext/index.js]: https://github.com/rails/rails/blob/v8.0.3/actiontext/app/javascript/actiontext/index.js [app/javascript/actiontext/attachment_upload.js]: https://github.com/rails/rails/blob/v8.0.3/actiontext/app/javascript/actiontext/attachment_upload.js [#1258]: #1258
1 parent 835d7d5 commit e9343a1

File tree

14 files changed

+197
-4
lines changed

14 files changed

+197
-4
lines changed

action_text-trix/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ gemspec
55

66
branch = ENV.fetch("RAILS_BRANCH", "main")
77
gem "rails", github: "rails/rails", branch: branch
8+
gem "image_processing"
89
gem "importmap-rails"
910
gem "propshaft"
1011
gem "puma"

action_text-trix/app/assets/javascripts/trix.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ Copyright © 2025 37signals, LLC
8989
var engines = {
9090
node: ">= 18"
9191
};
92+
var peerDependencies = {
93+
"@rails/actiontext": "^8.1.100"
94+
};
9295
var _package = {
9396
name: name,
9497
version: version,
@@ -107,7 +110,8 @@ Copyright © 2025 37signals, LLC
107110
resolutions: resolutions,
108111
scripts: scripts,
109112
dependencies: dependencies,
110-
engines: engines
113+
engines: engines,
114+
peerDependencies: peerDependencies
111115
};
112116

113117
const attachmentSelector = "[data-trix-attachment]";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
trix/actiontext 2.1.15
3+
Copyright © 2025 37signals, LLC
4+
*/
5+
import { AttachmentUpload } from '@rails/actiontext';
6+
7+
addEventListener("trix-attachment-add", event => {
8+
const {
9+
attachment,
10+
target
11+
} = event;
12+
if (attachment.file) {
13+
const upload = new AttachmentUpload(attachment, target, attachment.file);
14+
const onProgress = event => attachment.setUploadProgress(event.detail.progress);
15+
target.addEventListener("direct-upload:progress", onProgress);
16+
upload.start().then(attributes => attachment.setAttributes(attributes)).catch(error => alert(error)).finally(() => target.removeEventListener("direct-upload:progress", onProgress));
17+
}
18+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
trix/actiontext 2.1.15
3+
Copyright © 2025 37signals, LLC
4+
*/
5+
import{AttachmentUpload as t}from"@rails/actiontext";addEventListener("trix-attachment-add",(e=>{const{attachment:r,target:a}=e;if(r.file){const e=new t(r,a,r.file),s=t=>r.setUploadProgress(t.detail.progress);a.addEventListener("direct-upload:progress",s),e.start().then((t=>r.setAttributes(t))).catch((t=>alert(t))).finally((()=>a.removeEventListener("direct-upload:progress",s)))}}));

action_text-trix/lib/action_text/trix/engine.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Trix
22
class Engine < ::Rails::Engine
33
initializer "trix.asset" do |app|
44
if app.config.respond_to?(:assets)
5-
app.config.assets.precompile += %w[ trix.js trix.css ]
5+
app.config.assets.precompile += %w[ trix.js trix.css trix/actiontext.esm.js trix/actiontext.esm.min.js ]
66
end
77
end
88
end

action_text-trix/test/application_system_test_case.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
77
js_errors: true,
88
headless: ENV["HEADLESS"] != "0"
99
}
10+
11+
def capture_events(event_names)
12+
execute_script <<~JS, *event_names
13+
window.capturedEvents = []
14+
15+
function capture({ target: { id }, type, detail }) {
16+
for (const name in detail) {
17+
detail[name] = detail[name].constructor.name
18+
}
19+
20+
capturedEvents.push({ id, type, detail })
21+
}
22+
23+
for (const eventName of arguments) {
24+
addEventListener(eventName, capture, { once: true })
25+
}
26+
JS
27+
28+
yield
29+
30+
evaluate_script("window.capturedEvents").each do |event|
31+
event["target"] = find(id: event.delete("id"))
32+
end
33+
end
34+
35+
def go_offline!
36+
page.driver.browser.network.emulate_network_conditions(offline: true)
37+
end
1038
end
1139

1240
Capybara.server = :puma, { Silent: true }
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
2-
import "trix"

action_text-trix/test/dummy/app/views/layouts/application.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
<%# Includes all stylesheet files in app/assets/stylesheets %>
2121
<%= stylesheet_link_tag :app %>
2222
<%= javascript_importmap_tags %>
23+
<script type="module">
24+
import "trix"
25+
26+
<% if ActionText.version < "8.2.0" %>
27+
import "@rails/actiontext"
28+
<% else %>
29+
import "trix/actiontext"
30+
<% end %>
31+
</script>
2332
</head>
2433

2534
<body>

action_text-trix/test/dummy/config/application.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Application < Rails::Application
2626
# config.time_zone = "Central Time (US & Canada)"
2727
# config.eager_load_paths << Rails.root.join("extras")
2828

29-
config.active_storage.variant_processor = :disabled
29+
if ActionText.version >= "8.1.0"
30+
config.active_storage.variant_processor = :disabled
31+
else
32+
config.active_storage.variant_processor = false
33+
end
3034
end
3135
end

action_text-trix/test/dummy/config/importmap.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
pin "application"
44

55
pin "trix"
6+
pin "trix/actiontext", to: "trix/actiontext.esm.js"
7+
pin "@rails/activestorage", to: "activestorage.esm.js"
8+
pin "@rails/actiontext", to: "actiontext.esm.js"

0 commit comments

Comments
 (0)