Skip to content

Commit

Permalink
Reorganised and split out export templates & styles
Browse files Browse the repository at this point in the history
Moved export templates elements into their own folder for better
grouping of logical usage.
Within the base export template, added some body classes to allow easier
targeted customisation via custom head css.
Split content of export templates into smaller partials for easier
future customization.

Closes #3443
  • Loading branch information
ssddanbrown committed Jun 8, 2022
1 parent e00d88f commit 8801244
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 140 deletions.
12 changes: 6 additions & 6 deletions app/Entities/Tools/ExportFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(ImageService $imageService, PdfGenerator $pdfGenerat
public function pageToContainedHtml(Page $page)
{
$page->html = (new PageContent($page))->render();
$pageHtml = view('pages.export', [
$pageHtml = view('exports.page', [
'page' => $page,
'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(),
Expand All @@ -59,7 +59,7 @@ public function chapterToContainedHtml(Chapter $chapter)
$pages->each(function ($page) {
$page->html = (new PageContent($page))->render();
});
$html = view('chapters.export', [
$html = view('exports.chapter', [
'chapter' => $chapter,
'pages' => $pages,
'format' => 'html',
Expand All @@ -77,7 +77,7 @@ public function chapterToContainedHtml(Chapter $chapter)
public function bookToContainedHtml(Book $book)
{
$bookTree = (new BookContents($book))->getTree(false, true);
$html = view('books.export', [
$html = view('exports.book', [
'book' => $book,
'bookChildren' => $bookTree,
'format' => 'html',
Expand All @@ -95,7 +95,7 @@ public function bookToContainedHtml(Book $book)
public function pageToPdf(Page $page)
{
$page->html = (new PageContent($page))->render();
$html = view('pages.export', [
$html = view('exports.page', [
'page' => $page,
'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(),
Expand All @@ -116,7 +116,7 @@ public function chapterToPdf(Chapter $chapter)
$page->html = (new PageContent($page))->render();
});

$html = view('chapters.export', [
$html = view('exports.chapter', [
'chapter' => $chapter,
'pages' => $pages,
'format' => 'pdf',
Expand All @@ -134,7 +134,7 @@ public function chapterToPdf(Chapter $chapter)
public function bookToPdf(Book $book)
{
$bookTree = (new BookContents($book))->getTree(false, true);
$html = view('books.export', [
$html = view('exports.book', [
'book' => $book,
'bookChildren' => $bookTree,
'format' => 'pdf',
Expand Down
43 changes: 43 additions & 0 deletions resources/sass/export-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,47 @@ ul.contents ul li {
}
.chapter-hint + h1 {
margin-top: 0;
}

// PDF specific overrides
body.export-format-pdf {
font-size: 14px;
line-height: 1.2;

h1, h2, h3, h4, h5, h6 {
line-height: 1.2;
}

table {
max-width: 800px !important;
font-size: 0.8em;
width: 100% !important;
}

table td {
width: auto !important;
}

.page-content .float {
float: none !important;
}

.page-content img.align-left, .page-content img.align-right {
float: none !important;
clear: both;
display: block;
}

}

// DOMPDF pdf export specific overrides
body.export-format-pdf.export-engine-dompdf {
// Fix for full width linked image sizes on DOMPDF
.page-content a > img {
max-width: 700px;
}
// Undoes the above for table images to prevent visually worse scenario, Awaiting next DOMPDF release for patch
.page-content td a > img {
max-width: 100%;
}
}
46 changes: 0 additions & 46 deletions resources/views/books/export.blade.php

This file was deleted.

23 changes: 0 additions & 23 deletions resources/views/chapters/export.blade.php

This file was deleted.

61 changes: 0 additions & 61 deletions resources/views/common/export-styles.blade.php

This file was deleted.

20 changes: 20 additions & 0 deletions resources/views/exports/book.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@extends('layouts.export')

@section('title', $book->name)

@section('content')

<h1 style="font-size: 4.8em">{{$book->name}}</h1>
<p>{{ $book->description }}</p>

@include('exports.parts.book-contents-menu', ['children' => $bookChildren])

@foreach($bookChildren as $bookChild)
@if($bookChild->isA('chapter'))
@include('exports.parts.chapter-item', ['chapter' => $bookChild])
@else
@include('exports.parts.page-item', ['page' => $bookChild, 'chapter' => null])
@endif
@endforeach

@endsection
16 changes: 16 additions & 0 deletions resources/views/exports/chapter.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@extends('layouts.export')

@section('title', $chapter->name)

@section('content')

<h1 style="font-size: 4.8em">{{$chapter->name}}</h1>
<p>{{ $chapter->description }}</p>

@include('exports.parts.chapter-contents-menu', ['pages' => $pages])

@foreach($pages as $page)
@include('exports.parts.page-item', ['page' => $page, 'chapter' => null])
@endforeach

@endsection
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<hr>

<div class="text-muted text-small">
@include('entities.export-meta', ['entity' => $page])
@include('exports.parts.meta', ['entity' => $page])
</div>
@endsection
10 changes: 10 additions & 0 deletions resources/views/exports/parts/book-contents-menu.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@if(count($children) > 0)
<ul class="contents">
@foreach($children as $bookChild)
<li><a href="#{{$bookChild->getType()}}-{{$bookChild->id}}">{{ $bookChild->name }}</a></li>
@if($bookChild->isA('chapter') && count($bookChild->visible_pages) > 0)
@include('exports.parts.chapter-contents-menu', ['pages' => $bookChild->visible_pages])
@endif
@endforeach
</ul>
@endif
7 changes: 7 additions & 0 deletions resources/views/exports/parts/chapter-contents-menu.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@if (count($pages) > 0)
<ul class="contents">
@foreach($pages as $page)
<li><a href="#page-{{$page->id}}">{{ $page->name }}</a></li>
@endforeach
</ul>
@endif
10 changes: 10 additions & 0 deletions resources/views/exports/parts/chapter-item.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="page-break"></div>
<h1 id="chapter-{{$chapter->id}}">{{ $chapter->name }}</h1>

<p>{{ $chapter->description }}</p>

@if(count($chapter->visible_pages) > 0)
@foreach($chapter->visible_pages as $page)
@include('exports.parts.page-item', ['page' => $page, 'chapter' => $chapter])
@endforeach
@endif
8 changes: 8 additions & 0 deletions resources/views/exports/parts/page-item.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="page-break"></div>

@if (isset($chapter))
<div class="chapter-hint">{{$chapter->name}}</div>
@endif

<h1 id="page-{{$page->id}}">{{ $page->name }}</h1>
{!! $page->html !!}
20 changes: 20 additions & 0 deletions resources/views/exports/parts/styles.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{-- Fetch in our standard export styles --}}
<style>
@if (!app()->runningUnitTests())
{!! file_get_contents(public_path('/dist/export-styles.css')) !!}
@endif
</style>

{{-- Apply any additional styles that can't be applied via our standard SCSS export styles --}}
@if ($format === 'pdf')
<style>
/* Patches for CSS variable colors within PDF exports */
a {
color: {{ setting('app-color') }};
}
blockquote {
border-left-color: {{ setting('app-color') }};
}
</style>
@endif
6 changes: 3 additions & 3 deletions resources/views/layouts/export.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<meta http-equiv="Content-Security-Policy" content="{{ $cspContent }}">
@endif

@include('common.export-styles', ['format' => $format, 'engine' => $engine ?? ''])
@include('common.export-custom-head')
@include('exports.parts.styles', ['format' => $format, 'engine' => $engine ?? ''])
@include('exports.parts.custom-head')
</head>
<body>
<body class="export export-format-{{ $format }} export-engine-{{ $engine ?? 'none' }}">
<div class="page-content">
@yield('content')
</div>
Expand Down
8 changes: 8 additions & 0 deletions tests/Entity/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,12 @@ public function test_html_exports_contain_csp_meta_tag()
$resp->assertElementExists('head meta[http-equiv="Content-Security-Policy"][content*="script-src "]');
}
}

public function test_html_exports_contain_body_classes_for_export_identification()
{
$page = Page::query()->first();

$resp = $this->asEditor()->get($page->getUrl('/export/html'));
$resp->assertElementExists('body.export.export-format-html.export-engine-none');
}
}

0 comments on commit 8801244

Please sign in to comment.