Skip to content

Commit

Permalink
refactor(*): rename preview examples to scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Jan 2, 2023
1 parent 929ff42 commit c7da213
Show file tree
Hide file tree
Showing 69 changed files with 440 additions and 476 deletions.
8 changes: 4 additions & 4 deletions app/components/lookbook/embed/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module Lookbook
class Embed::Component < Lookbook::BaseComponent
ACTIONS = [:inspect, :open]

attr_reader :example, :params, :options
attr_reader :scenario, :params, :options

def initialize(example:, params: {}, options: {}, **html_attrs)
@example = example
def initialize(scenario:, params: {}, options: {}, **html_attrs)
@scenario = scenario
@params = params.to_h
@options = options.to_h
super(**html_attrs)
Expand All @@ -16,7 +16,7 @@ def id
end

def embed_path
Engine.routes.url_helpers.lookbook_embed_path(example.path, {
Engine.routes.url_helpers.lookbook_embed_path(scenario.path, {
_options: SearchParamEncoder.call(options),
**params
})
Expand Down
4 changes: 2 additions & 2 deletions app/components/lookbook/embed/inspector/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<% toolbar.section padded: true do %>
<h3 class="flex items-center space-x-1">
<strong class="text-lookbook-embed-title"><%= target.preview.label %></strong>
<% if examples.many? %>
<%= select_tag("target", options_for_select(example_select_options, helpers.lookbook_embed_path(target.path)), "x-model": "targetPath", class: "!ml-2") %>
<% if scenarios.many? %>
<%= select_tag("target", options_for_select(scenario_select_options, helpers.lookbook_embed_path(target.path)), "x-model": "targetPath", class: "!ml-2") %>
<% else %>
<span>(<%= target.label %>) </span>
<% end %>
Expand Down
12 changes: 6 additions & 6 deletions app/components/lookbook/embed/inspector/component.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module Lookbook
class Embed::Inspector::Component < Lookbook::BaseComponent
attr_reader :target, :context, :examples, :panels, :options, :actions
attr_reader :target, :context, :scenarios, :panels, :options, :actions

def initialize(target:, context: nil, options: nil, examples: nil, panels: nil, actions: nil, **html_attrs)
def initialize(target:, context: nil, options: nil, scenarios: nil, panels: nil, actions: nil, **html_attrs)
@target = target
@context = context.to_h
@options = options.to_h
@panels = Array(panels)
@actions = Array(actions).map(&:to_sym)
@examples = Array(examples)
@scenarios = Array(scenarios)
super(**html_attrs)
end

def id
Utils.id(
"embed-inspector",
"#{examples.map(&:name).join}#{options.to_json}#{actions.to_json}#{panels.to_json}".hash
"#{scenarios.map(&:name).join}#{options.to_json}#{actions.to_json}#{panels.to_json}".hash
)
end

Expand All @@ -39,8 +39,8 @@ def display_action?(name)
actions.include?(name)
end

def example_select_options
examples.map { |example| [example.label, helpers.lookbook_embed_path(example.path)] }
def scenario_select_options
scenarios.map { |scenario| [scenario.label, helpers.lookbook_embed_path(scenario.path)] }
end

def display_option_controls?
Expand Down
4 changes: 2 additions & 2 deletions app/components/lookbook/nav/entity/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Nav::Entity::Component < Nav::Item::Component
ICONS = {
page: :file,
preview: :layers,
example: :eye,
scenario: :eye,
group: :eye
}.freeze

Expand All @@ -24,7 +24,7 @@ def children
end

def type
collapsed? ? :example : node.type
collapsed? ? :scenario : node.type
end

def collapsed?
Expand Down
24 changes: 14 additions & 10 deletions app/controllers/concerns/lookbook/targetable_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def set_title
end

def lookup_entities
@target = Engine.previews.find_example_by_path(params[:path])
@target = Engine.previews.find_scenario_by_path(params[:path])
if @target.present?
@preview = @target.preview
if params[:path] == @preview&.path
redirect_to lookbook_inspect_path("#{params[:path]}/#{@preview.default_example.name}", params.permit!)
redirect_to lookbook_inspect_path("#{params[:path]}/#{@preview.default_scenario.name}", params.permit!)
end
else
@preview = Engine.previews.find_by_path(params[:path])
if @preview.present?
default_example = @preview.default_example
redirect_to lookbook_inspect_path(default_example.path, params.permit!) if default_example
default_scenario = @preview.default_scenario
redirect_to lookbook_inspect_path(default_scenario.path, params.permit!) if default_scenario
end
end
end
Expand Down Expand Up @@ -92,16 +92,20 @@ def set_params
def inspector_data
return @inspector_data if @inspector_data.present?

examples = @target ? @target.examples : []
rendered_examples = examples.map do |example|
output = preview_controller.process(:render_example_to_string, @preview, example.name)
RenderedPreviewExampleEntity.new(example, output, preview_controller.params)
scenarios = @target ? @target.scenarios : []
rendered_scenarios = scenarios.map do |scenario|
output = preview_controller.process(:render_scenario_to_string, @preview, scenario.name)
RenderedScenarioEntity.new(scenario, output, preview_controller.params)
end

@inspector_data ||= Lookbook::Store.new({
context: Store.new({params: @params, path: params[:path]}),
preview: @preview,
examples: rendered_examples,
scenarios: rendered_scenarios,
examples: -> do
Lookbook.logger.warn("The `examples` panel variable has been renamed to `scenarios`. The `examples` variable will be removed in the next major release.")
rendered_scenarios
end,
target: @target,
data: Lookbook.data,
app: Lookbook
Expand All @@ -112,7 +116,7 @@ def show_404(layout: nil)
locals = if @preview
{
message: "Example not found",
description: "The '#{@preview.label}' preview does not have an example named '#{path_segments.last}'."
description: "The '#{@preview.label}' preview does not have an scenario named '#{path_segments.last}'."
}
else
{
Expand Down
24 changes: 12 additions & 12 deletions app/controllers/lookbook/embeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class EmbedsController < ApplicationController
layout "lookbook/embed"

before_action :set_options, only: [:show]
before_action :set_example_choices, only: [:show]
before_action :set_scenario_choices, only: [:show]
before_action :set_panels, only: [:show]
before_action :set_actions, only: [:show]

Expand All @@ -19,11 +19,11 @@ def lookup
if props.preview.present?
preview = Engine.previews.find_by_preview_class(props.preview)
if preview.present?
props.examples ||= (props.example || "")
example = preview.example(Array(props.examples).first)
props.scenarios ||= (props.scenario || "")
scenario = preview.scenario(Array(props.scenarios).first)

boolean_options = ["display_option_controls"]
array_options = ["panels", "actions", "examples"]
array_options = ["panels", "actions", "scenarios"]
param_prefix = "param_"

options = {}
Expand Down Expand Up @@ -51,7 +51,7 @@ def lookup
embed_params[:_options] = SearchParamEncoder.call(options)
embed_params.symbolize_keys!

return redirect_to lookbook_embed_url(example ? example.path : preview.path, embed_params)
return redirect_to lookbook_embed_url(scenario ? scenario.path : preview.path, embed_params)
end
end

Expand All @@ -62,7 +62,7 @@ def show
@embed = true

unless @target
@target = @example_choices.first || @preview.default_example
@target = @scenario_choices.first || @preview.default_scenario
if @target
redirect_to lookbook_embed_path(@target.path, req_params)
else
Expand All @@ -74,7 +74,7 @@ def show
protected

def lookup_entities
@target = Engine.previews.find_example_by_path(params[:path])
@target = Engine.previews.find_scenario_by_path(params[:path])
@preview = @target.present? ? @target.preview : Engine.previews.find_by_path(params[:path])
end

Expand Down Expand Up @@ -107,12 +107,12 @@ def set_options
@options ||= default_options.merge(options)
end

def set_example_choices
return @example_choices ||= [] unless @preview
def set_scenario_choices
return @scenario_choices ||= [] unless @preview

named_choices = @options.fetch(:examples, [])
@example_choices = ListResolver.call(named_choices, @preview.examples.map(&:name)) do |name|
@preview.example(name)
named_choices = @options.fetch(:scenarios, [])
@scenario_choices = ListResolver.call(named_choices, @preview.scenarios.map(&:name)) do |name|
@preview.scenario(name)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/lookbook/embeds/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= lookbook_render "embed/inspector",
target: @target,
examples: @example_choices,
scenarios: @scenario_choices,
panels: @panels,
actions: @actions,
options: @options,
Expand Down
2 changes: 1 addition & 1 deletion app/views/lookbook/inspector/panels/_notes.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% items = examples.select { |example| example.notes.present? } %>
<% items = scenarios.select { |scenario| scenario.notes.present? } %>
<% if items.many? %>
<div class="divide-y divide-dashed divide-lookbook-divider bg-lookbook-prose-bg h-full w-full">
<% items.each do |item| %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/lookbook/inspector/panels/_output.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= lookbook_render :code, line_numbers: true, full_height: true do -%>
<% if examples.many? -%>
<% examples.each do |example| -%><%== "<!-- #{example.label} -->\n#{example.output}\n\n" -%><% end %>
<% if scenarios.many? -%>
<% scenarios.each do |scenario| -%><%== "<!-- #{scenario.label} -->\n#{scenario.output}\n\n" -%><% end %>
<%- else -%>
<%== examples.first.output -%>
<%== scenarios.first.output -%>
<%- end %>
<% end %>
12 changes: 6 additions & 6 deletions app/views/lookbook/inspector/panels/_source.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="h-full">
<% if examples.many? %>
<%= lookbook_render :code, language: examples.first.source_lang[:name], line_numbers: true, full_height: true do -%>
<%- examples.each.with_index(1) do |example, i| -%>
<%== "#{sprintf example.source_lang[:comment], example.label}\n#{example.source}\n#{"\n" if i < examples.size}" %><% end %>
<% if scenarios.many? %>
<%= lookbook_render :code, language: scenarios.first.source_lang[:name], line_numbers: true, full_height: true do -%>
<%- scenarios.each.with_index(1) do |scenario, i| -%>
<%== "#{sprintf scenario.source_lang[:comment], scenario.label}\n#{scenario.source}\n#{"\n" if i < scenarios.size}" %><% end %>
<% end %>
<% else %>
<% example = examples.first %>
<%= lookbook_render :code, language: example.source_lang[:name], line_numbers: true, full_height: true do %><%== example.source %><% end %>
<% scenario = scenarios.first %>
<%= lookbook_render :code, language: scenario.source_lang[:name], line_numbers: true, full_height: true do %><%== scenario.source %><% end %>
<% end %>
</div>
14 changes: 7 additions & 7 deletions app/views/lookbook/previews/group.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<% if examples.many? %>
<%# Render a group of examples %>
<% examples.each do |example| %>
<% if scenarios.many? %>
<%# Render a group of scenarios %>
<% scenarios.each do |scenario| %>
<div style="margin-bottom: 30px !important; display: block !important;">
<h6 style="all: unset; display: block; color: #999; font-family: sans-serif; font-size: 14px; margin-top: 0; margin-bottom: 10px;">
<%= example.label %>
<%= scenario.label %>
</h6>
<%= example.output %>
<%= scenario.output %>
</div>
<% end %>
<% else %>
<%# Render a single example %>
<%= examples.first.output %>
<%# Render a single scenario %>
<%= scenarios.first.output %>
<% end %>
4 changes: 2 additions & 2 deletions config/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ shared:
theme: github
dark: false

sort_examples: false
sort_scenarios: false
preview_paths: [test/components/previews]
preview_display_options: {}
preview_disable_action_view_annotations: true
Expand All @@ -34,7 +34,7 @@ shared:
panels: false
actions: [inspect, open]
display_option_controls: true
examples: []
scenarios: []

listen_paths: []
listen_extensions: [rb, html.*]
Expand Down
4 changes: 2 additions & 2 deletions config/panels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ shared:
partial: lookbook/inspector/panels/source
label: Source
hotkey: s
copy: "->(data) { data.examples.map { |e| e.source }.join(\n) }"
copy: "->(data) { data.scenarios.map { |e| e.source }.join(\n) }"

notes:
partial: lookbook/inspector/panels/notes
label: Notes
hotkey: n
disabled: "->(data) { data.examples.select { |e| e.notes.present? }.none? }"
disabled: "->(data) { data.scenarios.select { |e| e.notes.present? }.none? }"

params:
partial: lookbook/inspector/panels/params
Expand Down
7 changes: 0 additions & 7 deletions docs/src/_api/preview_example_entity.md

This file was deleted.

7 changes: 0 additions & 7 deletions docs/src/_api/preview_group_entity.md

This file was deleted.

7 changes: 7 additions & 0 deletions docs/src/_api/render_target_entity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
id: render-target-entity
label: RenderTargetEntity
title: RenderTargetEntity
layout: api
subject: render_target_entity
---
7 changes: 0 additions & 7 deletions docs/src/_api/rendered_preview_example_entity.md

This file was deleted.

7 changes: 7 additions & 0 deletions docs/src/_api/rendered_scenario_entity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
id: rendered-scenario-entity
label: RenderedScenarioEntity
title: RenderedScenarioEntity
layout: api
subject: rendered_scenario_entity
---
7 changes: 7 additions & 0 deletions docs/src/_api/scenario_entity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
id: scenario-entity
label: ScenarioEntity
title: ScenarioEntity
layout: api
subject: scenario_entity
---
7 changes: 7 additions & 0 deletions docs/src/_api/scenario_group_entity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
id: scenario-group-entity
label: ScenarioGroupEntity
title: ScenarioGroupEntity
layout: api
subject: scenario_group_entity
---
4 changes: 0 additions & 4 deletions docs/src/_components/lookbook_docs/api_method/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def example_lang

def params
tags(:param)
# .map do |param|
# param[:opts] = options_for_param(param[:name])
# param
# end
end

def options(name = :opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<% end %>
<% if show?(:params) && params.any? %>
<div>
<h4 class="text-xs uppercase tracking-wider mb-2 text-gray-400">Args:</h4>
<h4 class="text-xs uppercase tracking-wider mb-2 text-gray-400">Arguments:</h4>
<%= render LookbookDocs::OptionsList::Component.new(options: params) %>
</div>
<% end %>
Expand Down
Loading

0 comments on commit c7da213

Please sign in to comment.