@@ -4,18 +4,39 @@ import { matchUrlInRouteConfigs } from './universal/core/route/routeUtils';
44import Component from './universal/model/Component' ;
55import Renderer from './universal/model/Renderer' ;
66import Preview from './universal/components/Preview' ;
7- import { isRequestDispatcher , isPreview , isWithoutHTML } from './universal/service/RenderService' ;
7+ import {
8+ isRequestDispatcher ,
9+ isPreview ,
10+ isWithoutHTML ,
11+ isWithoutState ,
12+ getPreviewLayout
13+ } from './universal/service/RenderService' ;
814import metrics from './metrics' ;
915import { HTTP_STATUS_CODES } from './universal/utils/constants' ;
1016import logger from './universal/utils/logger' ;
1117
18+ const previewPages = require ( '__V_PREVIEW_PAGES__' ) ;
19+
20+ const getRenderOptions = req => {
21+ const isPreviewValue = isPreview ( req . query ) || false ;
22+ const isWithoutHTMLValue = isWithoutHTML ( req . query ) || false ;
23+ const isWithoutStateValue = isWithoutState ( req . query ) || false ;
24+
25+ return {
26+ isPreview : isPreviewValue ,
27+ isWithoutHTML : isWithoutHTMLValue ,
28+ isWithoutState : isWithoutStateValue
29+ } ;
30+ } ;
31+
1232function getRenderer ( name , req ) {
1333 const { query, cookies, url, headers, params } = req ;
1434 const path = `/${ params ?. path || '' } ` ;
1535 const userAgent = headers [ 'user-agent' ] ;
1636
1737 const componentPath = Component . getComponentPath ( name ) ;
1838 const routeInfo = matchUrlInRouteConfigs ( componentPath ) ;
39+ const renderOptions = getRenderOptions ( req ) ;
1940
2041 if ( routeInfo ) {
2142 const urlWithPath = url . replace ( '/' , path ) ;
@@ -26,6 +47,7 @@ function getRenderer(name, req) {
2647 cookies,
2748 url : urlWithPath ,
2849 userAgent,
50+ ...renderOptions
2951 } ;
3052
3153 if ( Component . isExist ( componentPath ) ) {
@@ -177,11 +199,21 @@ async function getResponses(renderers) {
177199 return responses ;
178200}
179201
180- async function getPreview ( responses , requestCount ) {
181- return Preview (
182- [ ...Object . keys ( responses ) . map ( name => responses [ name ] . fullHtml ) ] . join ( '\n' ) ,
183- `${ requestCount } request!`
184- ) ;
202+ async function getPreview ( responses , requestCount , req ) {
203+ const layoutName = getPreviewLayout ( req . query ) ;
204+ const { layouts } = previewPages . default ;
205+ let PreviewFile = Preview ;
206+
207+ if ( layouts [ layoutName ] ) {
208+ PreviewFile = layouts [ layoutName ] ;
209+ }
210+
211+ const content = Object . keys ( responses ) . map ( name => {
212+ const componentName = responses ?. [ name ] ?. activeComponent ?. componentName ?? '' ;
213+ return getLayoutWithClass ( componentName , responses [ name ] . fullHtml ) ;
214+ } ) ;
215+
216+ return PreviewFile ( [ ...content ] . join ( '\n' ) , `${ requestCount } request!` ) ;
185217}
186218
187219const DEFAULT_PARTIALS = [ 'RequestDispatcher' ] ;
@@ -199,6 +231,17 @@ export const getPartials = req => {
199231 return partials ;
200232} ;
201233
234+ function cr ( condition , ok , cancel ) {
235+ return condition ? ok : cancel || '' ;
236+ }
237+
238+ const getLayoutWithClass = ( name , html , id = '' , style = null ) => {
239+ const idAttr = cr ( id !== '' , `id=${ id } ` ) ;
240+ const styleAttr = cr ( style !== null , `style=${ style } ` ) ;
241+
242+ return `<div class="${ name } " ${ idAttr } ${ styleAttr } >${ html } </div>` ;
243+ } ;
244+
202245const renderMultiple = async ( req , res ) => {
203246 const partials = getPartials ( req ) ;
204247
@@ -228,7 +271,7 @@ const renderMultiple = async (req, res) => {
228271 const responses = await getResponses ( renderers ) ;
229272
230273 if ( isPreview ( req . query ) ) {
231- const preview = await getPreview ( responses , requestCount ) ;
274+ const preview = await getPreview ( responses , requestCount , req ) ;
232275 res . html ( preview ) ;
233276 } else {
234277 res . json ( responses ) ;
0 commit comments