Skip to content

Commit 217475a

Browse files
committed
Display generated HTML at the details page
1 parent 932d973 commit 217475a

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"/js/field.js": "/js/field.js?id=ff328aea99a66a6fe702"
2+
"/js/field.js": "/js/field.js?id=15620b3e531fb4496884"
33
}

resources/js/components/DetailField.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<panel-item :field="field">
33
<template slot="value">
44
<div class="overflow-hidden">
5-
<code>{{ field.value }}</code>
5+
<iframe :id="iframeId" sandbox="allow-scripts" importance="low" width="100%"></iframe>
66
</div>
77
</template>
88
</panel-item>
@@ -11,5 +11,34 @@
1111
<script>
1212
export default {
1313
props: ['resource', 'resourceName', 'resourceId', 'field'],
14+
15+
beforeCreate() {
16+
const uniqueId = Math.random().toString(36).slice(-5);
17+
this.iframeId = `previewUnlayerHtmlIframe-${uniqueId}`;
18+
},
19+
20+
mounted() {
21+
let iframe = document.getElementById(this.iframeId);
22+
this.setIframeContent(iframe, this.field.html);
23+
this.resizeIFrameToFitContent(iframe)
24+
},
25+
26+
methods: {
27+
/**
28+
* @param {HTMLIFrameElement} iframe
29+
* @param {string} htmlContent
30+
*/
31+
setIframeContent: function(iframe, htmlContent) {
32+
const container = document.createElement('div');
33+
container.innerHTML = htmlContent;
34+
iframe.contentWindow.document.body.appendChild(container);
35+
},
36+
/**
37+
* @param {HTMLIFrameElement} iframe
38+
*/
39+
resizeIFrameToFitContent: function (iframe) {
40+
iframe.height = iframe.contentWindow.document.body.scrollHeight;
41+
}
42+
},
1443
}
1544
</script>

src/Unlayer.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,17 @@
66
use Laravel\Nova\Fields\Code;
77
use Laravel\Nova\Http\Requests\NovaRequest;
88

9-
final class Unlayer extends Code
9+
class Unlayer extends Code
1010
{
1111
/**
1212
* The field's component.
1313
* @var string
1414
*/
1515
public $component = 'nova-unlayer-field';
1616

17-
/**
18-
* Indicates if the field is used to manipulate JSON.
19-
* @var bool
20-
*/
21-
public $json = true;
22-
2317
/** @var callable|null */
2418
public $savingCallback;
2519

26-
/**
27-
* Indicates if the element should be shown on the index view.
28-
* @var bool
29-
*/
30-
public $showOnDetail = false;
31-
3220
/**
3321
* Specify Unlayer config
3422
* @see https://docs.unlayer.com/docs/getting-started#section-configuration-options
@@ -41,7 +29,10 @@ public function config($config): Unlayer
4129
? $config()
4230
: $config;
4331

44-
return $this->withMeta(['config' => $unlayerConfig]);
32+
return $this->withMeta([
33+
'config' => $unlayerConfig,
34+
'html' => '',
35+
]);
4536
}
4637

4738
public function savingCallback(?callable $callback): Unlayer
@@ -61,6 +52,20 @@ public function height(string $height): Unlayer
6152
return $this->withMeta(['height' => $height]);
6253
}
6354

55+
/**
56+
* Set generated HTML code that can be used on details page.
57+
* @param $html
58+
* @return \IDF\NovaUnlayerField\Unlayer
59+
*/
60+
public function html($html): Unlayer
61+
{
62+
$html = is_callable($html)
63+
? $html()
64+
: $html;
65+
66+
return $this->withMeta(['html' => $html]);
67+
}
68+
6469
/**
6570
* Hydrate the given attribute on the model based on the incoming request.
6671
* @param \Laravel\Nova\Http\Requests\NovaRequest $request

0 commit comments

Comments
 (0)