Skip to content

Commit 224d0e3

Browse files
committed
Merge pull request #157 from sul-dlss/json
Use andypf json viewer.
2 parents 367e607 + 18c3d33 commit 224d0e3

17 files changed

Lines changed: 187 additions & 49 deletions

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ gem 'okcomputer'
5151
gem 'roo' # work with newer Excel files and other types (xlsx, ods, csv).
5252
# Note that dropping roo-xls for older xls support since not updated for roo 3.x.
5353
gem 'rsolr'
54-
gem 'sdr_view_components'
54+
gem 'sdr_view_components', github: 'sul-dlss/sdr_view_components', branch: 'lookbook'
5555
gem 'view_component'
5656

5757
group :development, :test do
@@ -76,6 +76,7 @@ end
7676

7777
group :development do
7878
gem 'erb_lint', require: false
79+
gem 'lookbook'
7980
gem 'overmind'
8081
# Use console on exceptions pages [https://github.com/rails/web-console]
8182
gem 'web-console'

Gemfile.lock

Lines changed: 58 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%= tag.andypf_json_viewer indent: 3,
2+
expanded:,
3+
theme: 'cupertino',
4+
'show-data-types': false,
5+
'show-toolbar': show_toolbar,
6+
'expand-icon-type': 'arrow',
7+
'show-copy': true,
8+
'show-size': false,
9+
'preserve-expanded': false,
10+
'expand-empty': false,
11+
data: %>
12+
<% if show_tip %>
13+
<p class="mt-2"><small><em>Tip: Shift + Click to expand/collapse all nodes.</em></small></p>
14+
<% end %>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Elements
4+
# Component to display a JSON hash in a pretty format.
5+
# Wrapper around the @andypf/json-viewer library.
6+
# See https://github.com/andypf/json-viewer for more details and documentation of options.
7+
class JsonViewerComponent < ApplicationComponent
8+
def initialize(hash:, expanded: true, show_toolbar: true, show_tip: false, compact: true)
9+
@hash = hash
10+
@expanded = expanded
11+
@show_toolbar = show_toolbar
12+
@show_tip = show_tip
13+
@compact = compact
14+
super()
15+
end
16+
17+
attr_reader :hash, :expanded, :show_toolbar, :show_tip, :compact
18+
19+
def data
20+
helpers.format_hash(hash, compact:, pretty: false)
21+
end
22+
end
23+
end

app/helpers/application_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ def format_datetime(datetime, format: :long)
2020

2121
I18n.l(datetime.in_time_zone('Pacific Time (US & Canada)'), format:)
2222
end
23+
24+
def format_hash(hash, compact: true, pretty: true)
25+
formatted_hash = compact ? CocinaDisplay::Utils.deep_compact_blank(hash) : hash
26+
if pretty
27+
JSON.pretty_generate(formatted_hash)
28+
else
29+
JSON.generate(formatted_hash)
30+
end
31+
end
2332
end

app/javascript/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
import '@hotwired/turbo-rails'
33
import 'controllers'
44
import * as bootstrap from 'bootstrap' // eslint-disable-line no-unused-vars
5+
import '@andypf/json-viewer'

app/views/layouts/application.html.erb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828

2929
<link rel="apple-touch-icon" href="/icon.png">
3030

31-
<% component_library_url = 'https://cdn.jsdelivr.net/gh/sul-dlss/component-library@v2025-09-11' %>
32-
<%= tag.link rel: 'icon', href: "#{component_library_url}/styles/icon.png", type: 'image/png' %>
33-
<%= tag.link rel: 'icon', href: "#{component_library_url}/styles/icon.svg", type: 'image/svg+xml' %>
34-
<%= tag.link rel: 'apple-touch-icon', href: "#{component_library_url}/styles/icon.png" %>
31+
<%= tag.link rel: 'icon', href: "#{Settings.component_library.url}/styles/icon.png", type: 'image/png' %>
32+
<%= tag.link rel: 'icon', href: "#{Settings.component_library.url}/styles/icon.svg", type: 'image/svg+xml' %>
33+
<%= tag.link rel: 'apple-touch-icon', href: "#{Settings.component_library.url}/styles/icon.png" %>
3534

3635
<%# Includes all stylesheet files in app/assets/stylesheets %>
3736
<%= stylesheet_link_tag :app, 'data-turbo-track': 'reload' %>
38-
<%= tag.link rel: 'stylesheet', href: "#{component_library_url}/styles/sul.css" %>
37+
<%= tag.link rel: 'stylesheet', href: "#{Settings.component_library.url}/styles/sul.css" %>
3938
<%= javascript_importmap_tags %>
4039
</head>
4140

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<%= turbo_frame_tag(@cocina_hash[:externalIdentifier], 'cocina_json') do %>
2-
<pre><%= JSON.pretty_generate(@cocina_hash) %></pre>
2+
<%= render Elements::JsonViewerComponent.new(hash: @cocina_hash, expanded: 1, show_tip: true) %>
33
<% end %>

config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,6 @@
7575

7676
# Raise error when a before_action's only/except options reference missing actions.
7777
config.action_controller.raise_on_missing_callback_actions = true
78+
79+
# config.view_component.previews.default_layout = "lookbook"
7880
end

config/importmap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
pin_all_from 'app/javascript/controllers', under: 'controllers'
1010
pin 'bootstrap', to: 'bootstrap.bundle.min.js'
1111
pin 'stimulus-autocomplete' # @3.1.0
12+
pin '@andypf/json-viewer', to: '@andypf--json-viewer.js' # @2.4.0

0 commit comments

Comments
 (0)