99use SolutionForest \InspireCms \Dtos \ContentDto ;
1010use SolutionForest \InspireCms \Dtos \PropertyTypeDto ;
1111use SolutionForest \InspireCms \Helpers \PropertyTypeHelper ;
12+ use SolutionForest \InspireCms \Helpers \TemplateHelper ;
1213use SolutionForest \InspireCms \InspireCmsConfig ;
1314use SolutionForest \InspireCms \Models \Contracts \Content ;
1415use SolutionForest \InspireCms \Models \Contracts \DocumentType ;
@@ -19,7 +20,51 @@ class DefaultPreviewProvider implements PreviewProviderInterface
1920 'isPeekPreviewModal ' => true ,
2021 ];
2122
23+ protected static $ previewType = 'internalUrl ' ; // internalUrl, view
24+
25+ public function getPeekPreviewType (): string
26+ {
27+ return static ::$ previewType ;
28+ }
29+
30+ public function configureFilamentPeekAsInternalLink (): void
31+ {
32+ if ($ this ->getPeekPreviewType () === 'internalUrl ' ) {
33+ config ()->set ('filament-peek.builderEditor.useInternalPreviewUrl ' , true );
34+ config ()->set ('filament-peek.internalPreviewUrl.enabled ' , true );
35+ }
36+ }
37+
2238 public function renderContentPreview ($ documentType , $ content , $ template , $ locale = null , $ propertyData = [], $ data = [])
39+ {
40+ [$ htmlContent , $ viewData ] = $ this ->prepareContentPreviewContentAndData (
41+ documentType: $ documentType ,
42+ content: $ content ,
43+ template: $ template ,
44+ locale: $ locale ,
45+ propertyData: $ propertyData ,
46+ data: $ data
47+ );
48+
49+ return $ this ->renderBuilderPreview (
50+ Blade::render ($ htmlContent ,$ viewData , true )
51+ );
52+ }
53+
54+ public function renderTemplatePreview ($ templateContent , $ documentType , $ theme = null , $ locale = null , $ data = [])
55+ {
56+ if (empty ($ templateContent )) {
57+ return '' ;
58+ }
59+
60+ [$ htmlContent , $ viewData ] = $ this ->prepareTemplatePreviewContentAndData ($ templateContent , $ documentType , $ theme , $ locale , $ data );
61+
62+ return $ this ->renderBuilderPreview (
63+ Blade::render ($ templateContent , $ viewData )
64+ );
65+ }
66+
67+ protected function prepareContentPreviewContentAndData ($ documentType , $ content , $ template , $ locale = null , $ propertyData = [], $ data = [])
2368 {
2469 $ documentType = $ this ->findDocumentType ($ documentType );
2570 if (! $ documentType ) {
@@ -30,7 +75,7 @@ public function renderContentPreview($documentType, $content, $template, $locale
3075 ->seconds (60 )
3176 ->send ();
3277
33- return $ this -> renderBuilderPreview ( 'Document type not found ' ) ;
78+ return [ 'Document type not found ' , []] ;
3479 }
3580
3681 $ locale ??= $ data ['activeLocale ' ] ?? $ data ['locale ' ] ?? null ;
@@ -53,8 +98,7 @@ public function renderContentPreview($documentType, $content, $template, $locale
5398 ->danger ()
5499 ->seconds (60 )
55100 ->send ();
56-
57- return $ this ->renderBuilderPreview ('Content not found ' );
101+ return ['Content not found ' , []];
58102 }
59103
60104 $ templateContent = $ this ->findTemplateContent ($ template ) ?? $ this ->findTemplateContent ($ documentType ->getDefaultTemplate ());
@@ -66,33 +110,23 @@ public function renderContentPreview($documentType, $content, $template, $locale
66110 ->seconds (60 )
67111 ->send ();
68112
69- return $ this -> renderBuilderPreview ( 'Template not found ' ) ;
113+ return [ 'Template not found ' , []] ;
70114 }
71115
72116 if ($ contentDTO instanceof ContentDto) {
73117 // Set the locale of the content dto to the active locale
74118 $ contentDTO = $ contentDTO ->setLocale ($ locale );
75119 }
76120
77- return $ this ->renderBuilderPreview (
78- Blade::render (
79- $ templateContent ,
80- array_merge ([
81- 'locale ' => $ locale ,
82- 'content ' => $ contentDTO ,
83- ... self ::PREVIEW_DATA ,
84- ], $ data ),
85- true
86- )
87- );
121+ return [$ templateContent , array_merge ([
122+ 'locale ' => $ locale ,
123+ 'content ' => $ contentDTO ,
124+ ... self ::PREVIEW_DATA ,
125+ ], $ data )];
88126 }
89127
90- public function renderTemplatePreview ( $ templateContent , $ documentType , $ theme = null , $ locale = null , $ data = [])
128+ protected function prepareTemplatePreviewContentAndData ( $ htmlContent , $ documentType , $ theme = null , $ locale = null , $ data = [])
91129 {
92- if (empty ($ templateContent )) {
93- return '' ;
94- }
95-
96130 $ documentType = $ this ->findDocumentType ($ documentType );
97131 if (! $ documentType ) {
98132 Notification::make ()
@@ -101,8 +135,7 @@ public function renderTemplatePreview($templateContent, $documentType, $theme =
101135 ->danger ()
102136 ->seconds (60 )
103137 ->send ();
104-
105- return $ this ->renderBuilderPreview ('Document type not found ' );
138+ return ['Document type not found ' , []];
106139 }
107140
108141 $ contentDTO = $ this ->buildFakeContentDto ($ documentType , $ locale );
@@ -116,29 +149,33 @@ public function renderTemplatePreview($templateContent, $documentType, $theme =
116149 if ($ documentType ->isDataType () && ! preg_match ("/getComponentWithTheme\(\'(.*?)\'\)/ " , $ templateContent )) {
117150
118151 // get the layout
119- $ layoutName = inspirecms_templates ()->getComponentWithTheme (' layout ' );
152+ $ layoutName = inspirecms_templates ()->getComponentWithTheme (TemplateHelper:: getDefaultThemedLayoutComponentName () );
120153
121154 if (view ()->exists ('components. ' . $ layoutName )) {
122155
123- $ newHtmlContent = Blade::render (
124- "@extends('components. $ layoutName') " . $ templateContent ,
125- array_merge ($ viewData , ['slot ' => '' , 'layoutName ' => $ layoutName ]),
126- );
156+ $ viewData = array_merge ($ viewData , ['slot ' => '' , 'layoutName ' => $ layoutName ]);
127157
128- return $ this -> renderBuilderPreview ( $ newHtmlContent ) ;
158+ return [ " @extends('components. $ layoutName ') " . $ htmlContent , $ viewData ] ;
129159 }
130160 }
131161
132- return $ this ->renderBuilderPreview (
133- Blade::render ($ templateContent , $ viewData )
134- );
162+ return [$ htmlContent , $ viewData ];
135163 }
136164
137165 /**
138166 * @param int|Model|null $template
139167 * @return ?string
140168 */
141169 protected function findTemplateContent ($ template )
170+ {
171+ return $ this ->findTemplate ($ template )?->getContent() ?? null ;
172+ }
173+
174+ /**
175+ * @param int|Model|null $template
176+ * @return \Illuminate\Database\Eloquent\Model|null
177+ */
178+ protected function findTemplate ($ template )
142179 {
143180 if (is_null ($ template )) {
144181 return null ;
@@ -147,7 +184,7 @@ protected function findTemplateContent($template)
147184 $ template = InspireCmsConfig::getTemplateModelClass ()::find ($ template );
148185 }
149186
150- return $ template?->getContent() ?? null ;
187+ return $ template ;
151188 }
152189
153190 /**
0 commit comments