From 501f536efd729bd509f4bd8482a22412bca91623 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Tue, 14 Feb 2023 21:52:12 -0600 Subject: [PATCH 1/3] Make the clipboard dependency an NPM dependency The rails version is out of date with the javascript version and hasn't been updated in 6 years --- app/assets/javascripts/spotlight/admin/index.js | 2 +- blacklight-spotlight.gemspec | 1 - lib/generators/spotlight/install_generator.rb | 3 +++ lib/spotlight/engine.rb | 3 ++- package.json | 1 + 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/spotlight/admin/index.js b/app/assets/javascripts/spotlight/admin/index.js index 0fe86d7dc..92918b519 100644 --- a/app/assets/javascripts/spotlight/admin/index.js +++ b/app/assets/javascripts/spotlight/admin/index.js @@ -5,7 +5,7 @@ //= require parameterize //= require bootstrap-tagsinput //= require jquery.serializejson -//= require clipboard +//= require clipboard/dist/clipboard //= require leaflet //= require leaflet-iiif //= require Leaflet.Editable diff --git a/blacklight-spotlight.gemspec b/blacklight-spotlight.gemspec index f84269a7e..5dbfea426 100644 --- a/blacklight-spotlight.gemspec +++ b/blacklight-spotlight.gemspec @@ -29,7 +29,6 @@ these collections.) s.add_dependency 'breadcrumbs_on_rails', '>= 3.0', '< 5' s.add_dependency 'cancancan' s.add_dependency 'carrierwave', '~> 2.2' - s.add_dependency 'clipboard-rails', '~> 1.5' s.add_dependency 'devise', '~> 4.1' s.add_dependency 'devise_invitable' s.add_dependency 'faraday' diff --git a/lib/generators/spotlight/install_generator.rb b/lib/generators/spotlight/install_generator.rb index 0bb82f532..d7c5c17ee 100644 --- a/lib/generators/spotlight/install_generator.rb +++ b/lib/generators/spotlight/install_generator.rb @@ -27,6 +27,9 @@ def add_js_rails6 # but since webpacker exists in the gemfile, we still need to run the # install before rails will start run 'bundle exec rails webpacker:install' + + say 'Install Clipboard' + run 'yarn add clipboard@1.7.1' end def add_js_rails7 diff --git a/lib/spotlight/engine.rb b/lib/spotlight/engine.rb index 106daae76..4b599db37 100644 --- a/lib/spotlight/engine.rb +++ b/lib/spotlight/engine.rb @@ -9,7 +9,6 @@ require 'activejob-status' require 'autoprefixer-rails' require 'blacklight' -require 'clipboard/rails' require 'faraday' require 'faraday/follow_redirects' require 'friendly_id' @@ -67,6 +66,8 @@ class Engine < ::Rails::Engine end initializer 'spotlight.assets.precompile' do |app| + config.assets.paths << Rails.root.join('node_modules') + app.config.assets.precompile += %w[spotlight/default_thumbnail.jpg spotlight/default_browse_thumbnail.jpg] Sprockets::ES6.configuration = { 'modules' => 'umd', 'moduleIds' => true } diff --git a/package.json b/package.json index f7054ba32..60ae97e4e 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,6 @@ "not IE 11" ], "dependencies": { + "clipboard": "1.7.1" } } From 2033e5e1e901f40334d374b9fa82b533b698f1f6 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Wed, 15 Feb 2023 09:58:25 -0600 Subject: [PATCH 2/3] Make leaflet an NPM dependency --- app/assets/javascripts/spotlight/admin/index.js | 2 +- blacklight-spotlight.gemspec | 1 - lib/generators/spotlight/install_generator.rb | 8 +++++--- lib/generators/spotlight/templates/spotlight.scss | 2 +- lib/spotlight/engine.rb | 1 - package.json | 3 ++- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/spotlight/admin/index.js b/app/assets/javascripts/spotlight/admin/index.js index 92918b519..5bdcbc4be 100644 --- a/app/assets/javascripts/spotlight/admin/index.js +++ b/app/assets/javascripts/spotlight/admin/index.js @@ -6,7 +6,7 @@ //= require bootstrap-tagsinput //= require jquery.serializejson //= require clipboard/dist/clipboard -//= require leaflet +//= require leaflet/dist/leaflet-src //= require leaflet-iiif //= require Leaflet.Editable //= require Path.Drag diff --git a/blacklight-spotlight.gemspec b/blacklight-spotlight.gemspec index 5dbfea426..e35b0a550 100644 --- a/blacklight-spotlight.gemspec +++ b/blacklight-spotlight.gemspec @@ -40,7 +40,6 @@ these collections.) s.add_dependency 'i18n-active_record' s.add_dependency 'iiif_manifest' s.add_dependency 'iiif-presentation' - s.add_dependency 'leaflet-rails' s.add_dependency 'legato' s.add_dependency 'mini_magick' s.add_dependency 'nokogiri' diff --git a/lib/generators/spotlight/install_generator.rb b/lib/generators/spotlight/install_generator.rb index d7c5c17ee..b77988e50 100644 --- a/lib/generators/spotlight/install_generator.rb +++ b/lib/generators/spotlight/install_generator.rb @@ -27,9 +27,6 @@ def add_js_rails6 # but since webpacker exists in the gemfile, we still need to run the # install before rails will start run 'bundle exec rails webpacker:install' - - say 'Install Clipboard' - run 'yarn add clipboard@1.7.1' end def add_js_rails7 @@ -38,6 +35,11 @@ def add_js_rails7 append_to_file 'app/assets/javascripts/application.js', "\n//= require_tree .\n" end + def add_js_deps + say 'Install Clipboard & Leaflet' + run 'yarn add clipboard@1.7.1 leaflet@^1.9.3' + end + def add_manifest append_to_file 'app/assets/config/manifest.js', "\n//= link spotlight/manifest.js" end diff --git a/lib/generators/spotlight/templates/spotlight.scss b/lib/generators/spotlight/templates/spotlight.scss index de18f5462..bdf7a50bb 100644 --- a/lib/generators/spotlight/templates/spotlight.scss +++ b/lib/generators/spotlight/templates/spotlight.scss @@ -4,5 +4,5 @@ @import 'spotlight/variables_bootstrap'; @import 'bootstrap'; @import 'sir-trevor/main'; -@import 'leaflet'; +@import 'leaflet/dist/leaflet'; @import 'spotlight/spotlight'; diff --git a/lib/spotlight/engine.rb b/lib/spotlight/engine.rb index 4b599db37..96c8d21c2 100644 --- a/lib/spotlight/engine.rb +++ b/lib/spotlight/engine.rb @@ -13,7 +13,6 @@ require 'faraday/follow_redirects' require 'friendly_id' require 'i18n/active_record' -require 'leaflet-rails' require 'paper_trail' require 'riiif' require 'spotlight/riiif_service' diff --git a/package.json b/package.json index 60ae97e4e..193008b2f 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "not IE 11" ], "dependencies": { - "clipboard": "1.7.1" + "clipboard": "1.7.1", + "leaflet": "^1.9.3" } } From 290ffffc5ecae825d3c434838674c5a5ec5c4e54 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Wed, 15 Feb 2023 16:38:53 -0600 Subject: [PATCH 3/3] Add Impormap capabilities --- app/assets/config/spotlight/manifest.js | 3 ++- app/assets/javascripts/spotlight/admin/index.js | 4 ++-- app/assets/javascripts/spotlight/application.js | 1 - app/assets/javascripts/spotlight/spotlight.js | 5 +++-- app/views/layouts/spotlight/base.html.erb | 8 +++++++- config/importmap.rb | 1 + lib/generators/spotlight/install_generator.rb | 7 ++++++- lib/generators/spotlight/templates/spotlight.scss | 1 - lib/spotlight/engine.rb | 9 ++++++++- 9 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 config/importmap.rb diff --git a/app/assets/config/spotlight/manifest.js b/app/assets/config/spotlight/manifest.js index 739edd926..47c98c451 100644 --- a/app/assets/config/spotlight/manifest.js +++ b/app/assets/config/spotlight/manifest.js @@ -1 +1,2 @@ -//= link spotlight/blocks/sir-trevor-icons.svg \ No newline at end of file +//= link spotlight/blocks/sir-trevor-icons.svg +//= link_tree ../../javascripts/spotlight .js \ No newline at end of file diff --git a/app/assets/javascripts/spotlight/admin/index.js b/app/assets/javascripts/spotlight/admin/index.js index 5bdcbc4be..4235f4eaa 100644 --- a/app/assets/javascripts/spotlight/admin/index.js +++ b/app/assets/javascripts/spotlight/admin/index.js @@ -5,8 +5,8 @@ //= require parameterize //= require bootstrap-tagsinput //= require jquery.serializejson -//= require clipboard/dist/clipboard -//= require leaflet/dist/leaflet-src +// require clipboard/dist/clipboard +// require leaflet/dist/leaflet-src //= require leaflet-iiif //= require Leaflet.Editable //= require Path.Drag diff --git a/app/assets/javascripts/spotlight/application.js b/app/assets/javascripts/spotlight/application.js index 3ad28b9ba..254fa65f0 100644 --- a/app/assets/javascripts/spotlight/application.js +++ b/app/assets/javascripts/spotlight/application.js @@ -9,6 +9,5 @@ // // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details // about supported directives. -// //= require spotlight/user/index //= require spotlight/admin/index diff --git a/app/assets/javascripts/spotlight/spotlight.js b/app/assets/javascripts/spotlight/spotlight.js index 407aacb27..74a8c578f 100644 --- a/app/assets/javascripts/spotlight/spotlight.js +++ b/app/assets/javascripts/spotlight/spotlight.js @@ -1,4 +1,4 @@ -Spotlight = function() { +const Spotlight = function() { var buffer = []; return { onLoad: function(func) { @@ -13,7 +13,7 @@ Spotlight = function() { }; }(); -Blacklight.onLoad(function() { +window.Blacklight.onLoad(function() { Spotlight.activate(); }); @@ -21,3 +21,4 @@ Spotlight.onLoad(function(){ SpotlightNestable.init(); }); +export default Spotlight; \ No newline at end of file diff --git a/app/views/layouts/spotlight/base.html.erb b/app/views/layouts/spotlight/base.html.erb index 9e6b05ac6..c29e5b91c 100644 --- a/app/views/layouts/spotlight/base.html.erb +++ b/app/views/layouts/spotlight/base.html.erb @@ -13,7 +13,13 @@ <% else %> <%= stylesheet_link_tag "application" %> <% end %> - <%= javascript_include_tag "application" %> + + + <% if defined? Importmap %> + <%= javascript_importmap_tags %> + <% else %> + <%= javascript_include_tag "application" %> + <% end %> <%= csrf_meta_tags %> <%= content_for(:head) %> <%= description %> diff --git a/config/importmap.rb b/config/importmap.rb new file mode 100644 index 000000000..9e0cd4998 --- /dev/null +++ b/config/importmap.rb @@ -0,0 +1 @@ +pin_all_from File.expand_path("../app/assets/javascripts/spotlight", __dir__), under: 'spotlight' \ No newline at end of file diff --git a/lib/generators/spotlight/install_generator.rb b/lib/generators/spotlight/install_generator.rb index b77988e50..dca663af5 100644 --- a/lib/generators/spotlight/install_generator.rb +++ b/lib/generators/spotlight/install_generator.rb @@ -37,7 +37,12 @@ def add_js_rails7 def add_js_deps say 'Install Clipboard & Leaflet' - run 'yarn add clipboard@1.7.1 leaflet@^1.9.3' + if defined?(Importmap) + run 'bin/importmap pin clipboard@1.7.1 leaflet@^1.9.3' + append_to_file 'app/javascript/application.js', "\nimport \"spotlight/application\"\n" + else + run 'yarn add clipboard@1.7.1 leaflet@^1.9.3' + end end def add_manifest diff --git a/lib/generators/spotlight/templates/spotlight.scss b/lib/generators/spotlight/templates/spotlight.scss index bdf7a50bb..86a9b949f 100644 --- a/lib/generators/spotlight/templates/spotlight.scss +++ b/lib/generators/spotlight/templates/spotlight.scss @@ -4,5 +4,4 @@ @import 'spotlight/variables_bootstrap'; @import 'bootstrap'; @import 'sir-trevor/main'; -@import 'leaflet/dist/leaflet'; @import 'spotlight/spotlight'; diff --git a/lib/spotlight/engine.rb b/lib/spotlight/engine.rb index 96c8d21c2..1bdce10dd 100644 --- a/lib/spotlight/engine.rb +++ b/lib/spotlight/engine.rb @@ -64,10 +64,13 @@ class Engine < ::Rails::Engine FactoryBot.definition_file_paths << File.expand_path('../../spec/factories', __dir__) if defined?(FactoryBot) end + PRECOMPILE_ASSETS = %w(spotlight/default_thumbnail.jpg spotlight/default_thumbnail.jpg + spotlight.js application.js).freeze + initializer 'spotlight.assets.precompile' do |app| config.assets.paths << Rails.root.join('node_modules') - app.config.assets.precompile += %w[spotlight/default_thumbnail.jpg spotlight/default_browse_thumbnail.jpg] + app.config.assets.precompile += Engine::PRECOMPILE_ASSETS Sprockets::ES6.configuration = { 'modules' => 'umd', 'moduleIds' => true } # When we upgrade to Sprockets 4, we can ditch sprockets-es6 and config AMD @@ -75,6 +78,10 @@ class Engine < ::Rails::Engine # https://github.com/rails/sprockets/issues/73#issuecomment-139113466 end + initializer "spotlight.importmap", before: "importmap" do |app| + app.config.importmap.paths << Engine.root.join("config/importmap.rb") if app.config.respond_to?(:importmap) + end + def self.user_class Spotlight::Engine.config.user_class.constantize end