app/controllers/lookbook/previews_controller.rb in lookbook-2.3.4 vs app/controllers/lookbook/previews_controller.rb in lookbook-3.0.0.alpha.0

- old
+ new

@@ -1,74 +1,105 @@ module Lookbook - class PreviewsController < Lookbook::ApplicationController - include TargetableConcern - include WithPreviewControllerConcern + class PreviewsController < ApplicationController + include Lookbook::PreviewActions - layout false + before_action :assign_preview + before_action :assign_target, only: %i[inspect embed preview] + before_action :assign_display_options, only: %i[inspect embed preview], unless: :json_request? + before_action :prerender_target, only: %i[inspect embed preview], unless: :json_request? + after_action :persist_display_options, only: %i[inspect embed preview], unless: :json_request? + skip_before_action :assign_preview, only: [:index] - before_action :permit_framing, only: [:show] - - def self.controller_path - "lookbook/previews" + def index + respond_to do |format| + format.html { redirect_to root_path } + format.json do + render json: Previews.to_json(format: (params[:tree] == "true") ? "tree" : "list") + end + end end - def index + def inspect respond_to do |format| + format.html do + @preview_panels = Inspector.preview_panels(**panel_context) + @drawer_panels = Inspector.drawer_panels(**panel_context) + @preview_html = render_target_in_layout(@target) + end + format.json do - render( - json: Lookbook.previews.map do |preview| - { - name: preview.name, - scenarios: preview.scenarios.map { |scenario| - case scenario - when Lookbook::ScenarioEntity - scenario_json(scenario) - when Lookbook::ScenarioGroupEntity - { - name: scenario.name, - examples: scenario.scenarios.map { |s| scenario_json(s) } - } - end - } - } - end - ) + render json: @target.to_json end end end - def show - raise_not_found("Preview not found") unless @target + def embed + embed_params = params[:_embed] || {} + target_names = Array(embed_params[:targets]).map(&:to_sym) - @preview_html = preview_controller.process( - :render_in_layout_to_string, - "lookbook/previews/group", - inspector_data, - layout: @preview.layout, - append_html: (iframe_content_scripts if embedded?) - ) + @targets = @preview.inspector_targets.select { _1.name.to_sym.in?(target_names) } + @panels = Inspector.embed_panels(embed_params[:panels], **panel_context) + @actions = embed_params.fetch(:actions, []) + @preview_html = render_target_in_layout(@target, embed: true) + + render layout: "lookbook/embed" end + def preview + render html: render_target_in_layout(@target) + end + private - def embedded? - params[:lookbook_embed] == "true" + def prerender_target + controller = preview_controller + @target.render_scenarios do |scenario| + controller.request = scenario_render_request(scenario) + controller.response = ActionDispatch::Response.new + html = controller.process(:lookbook_render_scenario, scenario) + CodeBeautifier.call(WhitespaceStripper.call(html)) + end end - def iframe_content_scripts - render_to_string("lookbook/partials/_iframe_content_scripts", layout: nil) + def render_target_in_layout(target, embed: false) + controller = preview_controller + controller.request = scenario_render_request(target.scenarios.first) + controller.response = ActionDispatch::Response.new + + append_html = if embed || params[:_lookbook_embed] == "true" + render_to_string("lookbook/application/_iframe_scripts", layout: nil) + end + + controller.process(:lookbook_render_template, + target.preview_template, + { + preview: target.preview, + target: target, + display_options: @display_options + }, + layout: target.preview.layout, + append_html: append_html) end - def scenario_json(scenario) - { - name: scenario.name, - inspect_path: scenario.url_path, - preview_path: scenario.preview_path - } + def scenario_render_request(scenario) + render_request = ActionDispatch::Request.new(request.env) + render_request.path_parameters = scenario_path_params(scenario) + render_request end - def permit_framing - headers["X-Frame-Options"] = Lookbook.config.preview_embeds.policy if embedded? - headers["X-Frame-Options"] = "SAMEORIGIN" if headers["X-Frame-Options"]&.upcase == "DENY" + def preview_controller + Previews.preview_controller.new + end + + def scenario_path_params(scenario) + Rails.application.routes.recognize_path(main_app.lookbook_render_scenario_path(scenario.preview, scenario)) + end + + def panel_context + { + preview: @preview, + target: @target, + request: request + } end end end