Skip to content

Commit f74f4a2

Browse files
author
Thomas Hanke
committed
add resizeable iframe
1 parent a496dfc commit f74f4a2

8 files changed

Lines changed: 110 additions & 21 deletions

File tree

config/documentation.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,30 @@
1010
use Qossmic\TwigDocBundle\Twig\TwigDocExtension;
1111

1212
return static function (ContainerConfigurator $container) {
13-
$container->services()
14-
->set('twig_doc.controller.documentation', TwigDocController::class)
15-
->public()
16-
->args([
17-
service('twig'),
18-
service('twig_doc.service.component')
19-
])
13+
$container->services() ->set('twig_doc.controller.documentation', TwigDocController::class)
14+
->public()
15+
->autoconfigure()
16+
->autowire()
17+
->arg('$profiler', service('profiler')->nullOnInvalid())
2018
->set('twig_doc.service.category', CategoryService::class)
19+
->alias(CategoryService::class, 'twig_doc.service.category')
2120

2221
->set('twig_doc.service.component_factory', ComponentItemFactory::class)
23-
->public()
24-
->args([service('validator'), service('twig_doc.service.category')])
22+
->public()
23+
->autoconfigure()
24+
->autowire()
25+
->alias(ComponentItemFactory::class, 'twig_doc.service.component_factory')
2526

2627
->set('twig_doc.service.component', ComponentService::class)
27-
->public()
28-
->args([service('twig_doc.service.component_factory'), service('twig_doc.service.category')])
28+
->public()
29+
->autoconfigure()
30+
->autowire()
31+
->alias(ComponentService::class, 'twig_doc.service.component')
2932

3033
->set('twig_doc.twig.extension', TwigDocExtension::class)
31-
->args([
32-
service('ux.twig_component.component_renderer')->nullOnInvalid(),
33-
service('twig_doc.service.component'),
34-
service('twig_doc.service.category'),
35-
service('twig')]
36-
)
37-
->tag('twig.extension');
34+
->autoconfigure()
35+
->autowire()
36+
->tag('twig.extension')
37+
->alias(TwigDocExtension::class, 'twig_doc.twig.extension')
38+
;
3839
};

config/routing/documentation.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
<route id="_documentation_invalid_components" path="/invalid">
1111
<default key="_controller">twig_doc.controller.documentation::invalidComponents</default>
1212
</route>
13+
<route id="_documentation_component_view" path="/component-view">
14+
<default key="_controller">twig_doc.controller.documentation::componentView</default>
15+
</route>
1316

1417
</routes>

src/Controller/TwigDocController.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use Symfony\Component\HttpFoundation\Request;
77
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9+
use Symfony\Component\HttpKernel\Profiler\Profiler;
810
use Twig\Environment;
911
use Qossmic\TwigDocBundle\Component\ComponentItem;
1012
use Qossmic\TwigDocBundle\Service\ComponentService;
@@ -13,7 +15,8 @@ class TwigDocController
1315
{
1416
public function __construct(
1517
private readonly Environment $twig,
16-
private readonly ComponentService $componentService
18+
private readonly ComponentService $componentService,
19+
private readonly ?Profiler $profiler = null
1720
)
1821
{
1922
}
@@ -42,4 +45,25 @@ public function invalidComponents(): Response
4245
$this->twig->render('@TwigDoc/pages/invalid_components.html.twig')
4346
);
4447
}
48+
49+
public function componentView(Request $request): Response
50+
{
51+
$name = $request->query->get('name');
52+
$component = $this->componentService->getComponent($name);
53+
if (!$component) {
54+
throw new NotFoundHttpException(sprintf('Component %s is unknown', $name));
55+
}
56+
$breakpoint = $request->query->get('breakpoint');
57+
// disable profiler to get rid of toolbar in dev
58+
if($this->profiler) {
59+
$this->profiler->disable();
60+
}
61+
return new Response(
62+
$this->twig->render('@TwigDoc/component.html.twig', [
63+
'component' => $component,
64+
'componentData' => $request->query->all('data'),
65+
'quantity' => $request->query->get('quantity')
66+
])
67+
);
68+
}
4569
}

src/Service/ComponentService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,9 @@ public function getInvalidComponents(): array
131131
{
132132
return $this->invalidComponents;
133133
}
134+
135+
public function getComponent(string $name): ?ComponentItem
136+
{
137+
return array_values($this->filterComponents($name, 'name'))[0] ?? null;
138+
}
134139
}

templates/component.html.twig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html lang="en">
2+
{% block head %}
3+
{% block stylesheets %}
4+
{% endblock %}
5+
<title>{% block title %}Twig Component Documentation{% endblock %}</title>
6+
{% block javascripts %}
7+
{% endblock %}
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
{% endblock %}
10+
</html>
11+
12+
<body>
13+
{% for i in 1..quantity %}
14+
{{ renderComponent(component, componentData ?? [])|raw }}
15+
{% endfor %}
16+
</body>

templates/component/_item.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
<h4>Variations</h4>
4949
{% for variation in component.variations %}
5050
<div class="twig-doc-variation">
51-
<div class="twig-doc-component-variation">
52-
{{ renderComponent(component, variation ?? [])|raw }}
51+
<div class="twig-doc-viewport-container">
52+
{% include '@TwigDoc/component/_viewport.html.twig' with {component: component, componentData: variation ?? [], width: '1280px'} %}
5353
</div>
5454
<div class="twig-doc-variation-description">
5555
<ul>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="twig-doc-viewport" style="width: {{ width }}px">
2+
<iframe class="twig-doc-iframe"
3+
src="{{ path('_documentation_component_view', {name: component.name, data: componentData, quantity: 1}) }}"
4+
>
5+
</iframe>
6+
</div>

templates/style/style.css.twig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,37 @@
114114
#twig-doc-container .twig-doc-variation .twig-doc-variation-description {
115115
padding-left: 20px;
116116
}
117+
118+
.twig-doc-body {
119+
padding: 15px;
120+
}
121+
122+
.twig-doc-viewport-container {
123+
display: flex;
124+
flex-direction: column;
125+
flex-grow: 1;
126+
border: none;
127+
padding: 10px;
128+
position: relative;
129+
overflow-x: scroll;
130+
}
131+
.twig-doc-viewport {
132+
padding: 0;
133+
display: flex;
134+
flex-direction: column;
135+
flex-grow: 1;
136+
min-width: 240px;
137+
margin: 0 auto;
138+
white-space: nowrap;
139+
text-align: center;
140+
position: relative;
141+
resize: horizontal;
142+
overflow-x: auto;
143+
outline: 1px solid #000000;
144+
}
145+
146+
.twig-doc-iframe {
147+
width: 100%;
148+
flex-grow: 1;
149+
border: 0;
150+
}

0 commit comments

Comments
 (0)