From 88012449f3ae52f8d5d4913fb994b3b6936fd505 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 8 Jun 2022 17:56:59 +0100 Subject: [PATCH] Reorganised and split out export templates & styles 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 --- app/Entities/Tools/ExportFormatter.php | 12 ++-- resources/sass/export-styles.scss | 43 +++++++++++++ resources/views/books/export.blade.php | 46 -------------- resources/views/chapters/export.blade.php | 23 ------- .../views/common/export-styles.blade.php | 61 ------------------- resources/views/exports/book.blade.php | 20 ++++++ resources/views/exports/chapter.blade.php | 16 +++++ .../page.blade.php} | 2 +- .../parts/book-contents-menu.blade.php | 10 +++ .../parts/chapter-contents-menu.blade.php | 7 +++ .../exports/parts/chapter-item.blade.php | 10 +++ .../parts/custom-head.blade.php} | 0 .../parts/meta.blade.php} | 0 .../views/exports/parts/page-item.blade.php | 8 +++ .../views/exports/parts/styles.blade.php | 20 ++++++ resources/views/layouts/export.blade.php | 6 +- tests/Entity/ExportTest.php | 8 +++ 17 files changed, 152 insertions(+), 140 deletions(-) delete mode 100644 resources/views/books/export.blade.php delete mode 100644 resources/views/chapters/export.blade.php delete mode 100644 resources/views/common/export-styles.blade.php create mode 100644 resources/views/exports/book.blade.php create mode 100644 resources/views/exports/chapter.blade.php rename resources/views/{pages/export.blade.php => exports/page.blade.php} (75%) create mode 100644 resources/views/exports/parts/book-contents-menu.blade.php create mode 100644 resources/views/exports/parts/chapter-contents-menu.blade.php create mode 100644 resources/views/exports/parts/chapter-item.blade.php rename resources/views/{common/export-custom-head.blade.php => exports/parts/custom-head.blade.php} (100%) rename resources/views/{entities/export-meta.blade.php => exports/parts/meta.blade.php} (100%) create mode 100644 resources/views/exports/parts/page-item.blade.php create mode 100644 resources/views/exports/parts/styles.blade.php diff --git a/app/Entities/Tools/ExportFormatter.php b/app/Entities/Tools/ExportFormatter.php index 99aa4536f52..97902db8219 100644 --- a/app/Entities/Tools/ExportFormatter.php +++ b/app/Entities/Tools/ExportFormatter.php @@ -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(), @@ -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', @@ -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', @@ -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(), @@ -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', @@ -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', diff --git a/resources/sass/export-styles.scss b/resources/sass/export-styles.scss index 1a8b34c5b9a..b8160b4c2df 100644 --- a/resources/sass/export-styles.scss +++ b/resources/sass/export-styles.scss @@ -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%; + } } \ No newline at end of file diff --git a/resources/views/books/export.blade.php b/resources/views/books/export.blade.php deleted file mode 100644 index 0b6b4a58c19..00000000000 --- a/resources/views/books/export.blade.php +++ /dev/null @@ -1,46 +0,0 @@ -@extends('layouts.export') - -@section('title', $book->name) - -@section('content') -

{{$book->name}}

- -

{{ $book->description }}

- - @if(count($bookChildren) > 0) - - @endif - - @foreach($bookChildren as $bookChild) -
-

{{ $bookChild->name }}

- - @if($bookChild->isA('chapter')) -

{{ $bookChild->description }}

- - @if(count($bookChild->visible_pages) > 0) - @foreach($bookChild->visible_pages as $page) -
-
{{$bookChild->name}}
-

{{ $page->name }}

- {!! $page->html !!} - @endforeach - @endif - - @else - {!! $bookChild->html !!} - @endif - - @endforeach -@endsection \ No newline at end of file diff --git a/resources/views/chapters/export.blade.php b/resources/views/chapters/export.blade.php deleted file mode 100644 index 61286ab170a..00000000000 --- a/resources/views/chapters/export.blade.php +++ /dev/null @@ -1,23 +0,0 @@ -@extends('layouts.export') - -@section('title', $chapter->name) - -@section('content') -

{{$chapter->name}}

- -

{{ $chapter->description }}

- - @if(count($pages) > 0) - - @endif - - @foreach($pages as $page) -
-

{{ $page->name }}

- {!! $page->html !!} - @endforeach -@endsection \ No newline at end of file diff --git a/resources/views/common/export-styles.blade.php b/resources/views/common/export-styles.blade.php deleted file mode 100644 index 1dfa6bb45b6..00000000000 --- a/resources/views/common/export-styles.blade.php +++ /dev/null @@ -1,61 +0,0 @@ - - -@if ($format === 'pdf') - -@endif \ No newline at end of file diff --git a/resources/views/exports/book.blade.php b/resources/views/exports/book.blade.php new file mode 100644 index 00000000000..42e03ea01bb --- /dev/null +++ b/resources/views/exports/book.blade.php @@ -0,0 +1,20 @@ +@extends('layouts.export') + +@section('title', $book->name) + +@section('content') + +

{{$book->name}}

+

{{ $book->description }}

+ + @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 \ No newline at end of file diff --git a/resources/views/exports/chapter.blade.php b/resources/views/exports/chapter.blade.php new file mode 100644 index 00000000000..ae49fa91828 --- /dev/null +++ b/resources/views/exports/chapter.blade.php @@ -0,0 +1,16 @@ +@extends('layouts.export') + +@section('title', $chapter->name) + +@section('content') + +

{{$chapter->name}}

+

{{ $chapter->description }}

+ + @include('exports.parts.chapter-contents-menu', ['pages' => $pages]) + + @foreach($pages as $page) + @include('exports.parts.page-item', ['page' => $page, 'chapter' => null]) + @endforeach + +@endsection \ No newline at end of file diff --git a/resources/views/pages/export.blade.php b/resources/views/exports/page.blade.php similarity index 75% rename from resources/views/pages/export.blade.php rename to resources/views/exports/page.blade.php index d2f448d6e88..e9324e96b99 100644 --- a/resources/views/pages/export.blade.php +++ b/resources/views/exports/page.blade.php @@ -8,6 +8,6 @@
- @include('entities.export-meta', ['entity' => $page]) + @include('exports.parts.meta', ['entity' => $page])
@endsection \ No newline at end of file diff --git a/resources/views/exports/parts/book-contents-menu.blade.php b/resources/views/exports/parts/book-contents-menu.blade.php new file mode 100644 index 00000000000..6b75442428d --- /dev/null +++ b/resources/views/exports/parts/book-contents-menu.blade.php @@ -0,0 +1,10 @@ +@if(count($children) > 0) + +@endif \ No newline at end of file diff --git a/resources/views/exports/parts/chapter-contents-menu.blade.php b/resources/views/exports/parts/chapter-contents-menu.blade.php new file mode 100644 index 00000000000..eeaa3499471 --- /dev/null +++ b/resources/views/exports/parts/chapter-contents-menu.blade.php @@ -0,0 +1,7 @@ +@if (count($pages) > 0) + +@endif \ No newline at end of file diff --git a/resources/views/exports/parts/chapter-item.blade.php b/resources/views/exports/parts/chapter-item.blade.php new file mode 100644 index 00000000000..f58068b5e4c --- /dev/null +++ b/resources/views/exports/parts/chapter-item.blade.php @@ -0,0 +1,10 @@ +
+

{{ $chapter->name }}

+ +

{{ $chapter->description }}

+ +@if(count($chapter->visible_pages) > 0) + @foreach($chapter->visible_pages as $page) + @include('exports.parts.page-item', ['page' => $page, 'chapter' => $chapter]) + @endforeach +@endif \ No newline at end of file diff --git a/resources/views/common/export-custom-head.blade.php b/resources/views/exports/parts/custom-head.blade.php similarity index 100% rename from resources/views/common/export-custom-head.blade.php rename to resources/views/exports/parts/custom-head.blade.php diff --git a/resources/views/entities/export-meta.blade.php b/resources/views/exports/parts/meta.blade.php similarity index 100% rename from resources/views/entities/export-meta.blade.php rename to resources/views/exports/parts/meta.blade.php diff --git a/resources/views/exports/parts/page-item.blade.php b/resources/views/exports/parts/page-item.blade.php new file mode 100644 index 00000000000..1a138ae437c --- /dev/null +++ b/resources/views/exports/parts/page-item.blade.php @@ -0,0 +1,8 @@ +
+ +@if (isset($chapter)) +
{{$chapter->name}}
+@endif + +

{{ $page->name }}

+{!! $page->html !!} \ No newline at end of file diff --git a/resources/views/exports/parts/styles.blade.php b/resources/views/exports/parts/styles.blade.php new file mode 100644 index 00000000000..830b8e49a81 --- /dev/null +++ b/resources/views/exports/parts/styles.blade.php @@ -0,0 +1,20 @@ +{{-- Fetch in our standard export styles --}} + + +{{-- Apply any additional styles that can't be applied via our standard SCSS export styles --}} +@if ($format === 'pdf') + +@endif \ No newline at end of file diff --git a/resources/views/layouts/export.blade.php b/resources/views/layouts/export.blade.php index 36568fef4da..d631ad3c9bf 100644 --- a/resources/views/layouts/export.blade.php +++ b/resources/views/layouts/export.blade.php @@ -8,10 +8,10 @@ @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') - +
@yield('content')
diff --git a/tests/Entity/ExportTest.php b/tests/Entity/ExportTest.php index 08d0921111f..826b69be5d2 100644 --- a/tests/Entity/ExportTest.php +++ b/tests/Entity/ExportTest.php @@ -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'); + } }