app/controllers/lookbook/previews_controller.rb in lookbook-0.9.8 vs app/controllers/lookbook/previews_controller.rb in lookbook-1.0.0.beta.0
- old
+ new
@@ -1,7 +1,9 @@
module Lookbook
class PreviewsController < ApplicationController
+ layout "lookbook/inspector"
+
def self.controller_path
"lookbook/previews"
end
before_action :lookup_entities, only: [:preview, :show]
@@ -12,49 +14,73 @@
set_params
begin
render html: render_examples(examples_data)
rescue => exception
render_in_layout "lookbook/error",
- layout: "lookbook/basic",
- error: prettify_error(exception),
- disable_header: true
+ layout: "lookbook/standalone",
+ error: prettify_error(exception)
end
else
- render_in_layout "not_found"
+ show_404 layout: "lookbook/standalone"
end
end
def show
if @example
begin
set_params
- @examples = examples_data
- @drawer_panels = drawer_panels.select { |name, panel| panel[:show] }
- @preview_panels = preview_panels.select { |name, panel| panel[:show] }
+ @rendered_examples = examples_data
+ @drawer_panels = drawer_panels.filter { |name, panel| panel[:show] }
+ @preview_panels = preview_panels.filter { |name, panel| panel[:show] }
rescue => exception
- render_in_layout "lookbook/error", error: prettify_error(exception)
+ render_in_layout "lookbook/error", layout: "lookbook/inspector", error: prettify_error(exception)
end
else
- render_in_layout "not_found"
+ show_404
end
end
+ def show_legacy
+ Lookbook.logger.warn("Legacy URL path detected. These paths are deprecated and will be removed in a future version")
+ redirect_to inspect_path params[:path]
+ end
+
private
def lookup_entities
@example = Lookbook.previews.find_example(params[:path])
- if @example
+ if @example.present?
@preview = @example.preview
if params[:path] == @preview&.lookup_path
- redirect_to lookbook_inspect_path "#{params[:path]}/#{@preview.default_example.name}"
+ redirect_to inspect_path "#{params[:path]}/#{@preview.default_example.name}"
end
else
- first_example = Lookbook.previews.find(params[:path])&.examples&.first
- redirect_to lookbook_inspect_path(first_example.lookup_path) if first_example
+ @preview = Lookbook.previews.find(params[:path])
+ if @preview.present?
+ first_example = @preview.examples.first
+ redirect_to inspect_path(first_example.lookup_path) if first_example
+ else
+ @preview = Lookbook.previews.find(path_segments.slice(0, path_segments.size - 1).join("/"))
+ end
end
end
+ 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}'."
+ }
+ else
+ {
+ message: "Not found",
+ description: "Looked for '#{params[:path]}'.<br>The preview may have been renamed or deleted."
+ }
+ end
+ render_in_layout "lookbook/404", layout: layout, **locals
+ end
+
def set_title
@title = @example.present? ? [@example&.label, @preview&.label].compact.join(" :: ") : "Not found"
end
def examples_data
@@ -70,16 +96,21 @@
label: example.label,
notes: example.notes,
html: preview_controller.process(:render_example_to_string, @preview, example.name),
source: has_template ? example.template_source(render_args[:template]) : example.method_source,
source_lang: has_template ? example.template_lang(render_args[:template]) : example.source_lang,
- params: example.params
+ params: example.params,
+ display: example.display_params
}
end
def render_examples(examples)
- preview_controller.process(:render_in_layout_to_string, "layouts/lookbook/preview", {examples: examples}, @preview.layout)
+ opts = {layout: @preview.layout}
+ if params[:lookbook_embed] == "true"
+ opts[:append_html] = "<script src=\"/lookbook-assets/js/embed.js?v=#{Lookbook.version}\"></script>".html_safe
+ end
+ preview_controller.process(:render_in_layout_to_string, "lookbook/preview", {examples: examples}, **opts)
end
def set_params
# cast known params to type
@example.params.each do |param|
@@ -97,52 +128,58 @@
end
def preview_panels
{
preview: {
+ id: "preview-panel-preview",
label: "Preview",
template: "lookbook/previews/panels/preview",
- srcdoc: Lookbook.config.preview_srcdoc ? render_examples(examples_data).gsub("\"", """) : nil,
hotkey: "v",
show: true,
disabled: false,
copy: false
},
output: {
+ id: "preview-panel-html",
label: "HTML",
template: "lookbook/previews/panels/output",
- hotkey: "o",
+ hotkey: "h",
show: true,
disabled: false,
- copy: true
+ copy: false
}
}
end
def drawer_panels
{
source: {
+ id: "drawer-panel-source",
label: "Source",
template: "lookbook/previews/panels/source",
hotkey: "s",
show: true,
disabled: false,
- copy: true
+ copy: @rendered_examples.map { |e| e[:source] }.join("\n")
},
notes: {
+ id: "drawer-panel-notes",
label: "Notes",
template: "lookbook/previews/panels/notes",
hotkey: "n",
show: true,
- disabled: @examples.select { |e| e[:notes].present? }.none?
+ copy: false,
+ disabled: @rendered_examples.filter { |e| e[:notes].present? }.none?
},
params: {
+ id: "drawer-panel-params",
label: "Params",
template: "lookbook/previews/panels/params",
hotkey: "p",
show: true,
- disabled: @example.type == :group || @example.params.none?
+ disabled: @example.params.none?,
+ copy: false
}
}
end
def preview_controller
@@ -151,14 +188,10 @@
controller.request = request
controller.response = response
@preview_controller ||= controller
end
- def render_in_layout(path, layout: nil, **locals)
- render path, layout: layout.presence || (params[:lookbook_embed] ? "lookbook/basic" : "lookbook/application"), locals: locals
- end
-
def prettify_error(exception)
error_params = if exception.is_a?(ViewComponent::PreviewTemplateError)
{
file_path: @preview&.full_path,
line_number: 0,
@@ -175,8 +208,12 @@
line_number: method.source_location[1]
}
end
end
Lookbook::Error.new(exception, **(error_params || {}))
+ end
+
+ def path_segments
+ params[:path].split("/")
end
end
end