Skip to content

Commit

Permalink
chore: register stimulus controllers from plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Jan 25, 2025
1 parent e30fc28 commit fc85b7d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<% javascripts.each do |path| %>
<%= javascript_include_tag path, "data-turbo-track": "reload", defer: true %>
<% end %>

<%# Collect all the custom StimulusJS controllers from plugins. %>
<%= javascript_tag nonce: true do %>
Avo.configuration.stimulus_controllers = <%== Avo.asset_manager.stimulus_controllers.to_a.to_json %>
<% end %>

<%# This is the last script to run so it can register custom StimulusJS controllers from plugins. %>
<%= javascript_include_tag 'late-registration', "data-turbo-track": "reload", defer: true %>
1 change: 1 addition & 0 deletions app/components/avo/asset_manager/javascript_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Avo::AssetManager::JavascriptComponent < Avo::BaseComponent
prop :asset_manager

delegate :javascripts, to: :@asset_manager
delegate :stimulus_controllers, to: :@asset_manager
end
2 changes: 1 addition & 1 deletion app/javascript/avo.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ window.Turbolinks = Turbo

let scrollTop = null
Mousetrap.bind('r r r', () => {
// Cpture scroll position
// Capture scroll position
scrollTop = document.scrollingElement.scrollTop

Turbo.visit(window.location.href, { action: 'replace' })
Expand Down
15 changes: 14 additions & 1 deletion app/javascript/js/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Alert, Popover } from 'tailwindcss-stimulus-components'
import { Application } from '@hotwired/stimulus'
import { Application, Controller } from '@hotwired/stimulus'
import Clipboard from '@stimulus-components/clipboard'
import PasswordVisibility from '@stimulus-components/password-visibility'
import TextareaAutogrow from 'stimulus-textarea-autogrow'
Expand All @@ -13,6 +13,19 @@ const application = Application.start()
application.debug = window?.localStorage.getItem('avo.debug')
window.Stimulus = application

/* Using this patter of providing a fake Stimulus object so the plugins do not have to bundle the Stimulus controller too.
Their rollup config is isntructed to use `fakeStimulus` instead of `Stimulus`.
output: {
...
globals: {
"@hotwired/stimulus": "FakeStimulus"
}
}
*/
window.FakeStimulus = {
Controller,
}

// Register stimulus-components controller
application.register('alert', Alert)
application.register('popover', Popover)
Expand Down
8 changes: 8 additions & 0 deletions app/javascript/late-registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const application = window.Stimulus

// This is going run last so it can regiser custom StimulusJS controllers from plugins.
if (window.Avo.configuration.stimulus_controllers) {
window.Avo.configuration.stimulus_controllers.forEach(([name, controller]) => {
application.register(name, window[controller])
})
}
1 change: 1 addition & 0 deletions app/views/avo/partials/_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
}
<% end %>
Avo.configuration.modal_frame_id = '<%= ::Avo::MODAL_FRAME_ID %>'
Avo.configuration.stimulus_controllers = []
<% end %>
8 changes: 8 additions & 0 deletions lib/avo/asset_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ module Avo
class AssetManager
include ActionView::Helpers::AssetTagHelper

attr_reader :stimulus_controllers

def initialize
@stylesheets = []
@javascripts = []
@stimulus_controllers = {}
end

def reset
@stylesheets = []
@javascripts = []
@stimulus_controllers = {}
end

def add_stylesheet(path)
Expand All @@ -20,6 +24,10 @@ def add_javascript(path)
@javascripts.push path
end

def register_stimulus_controller(name, controller)
@stimulus_controllers[name] = controller
end

def stylesheets
@stylesheets.uniq
end
Expand Down

0 comments on commit fc85b7d

Please sign in to comment.