Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Add Support of cover image for pages and also grid view for list of pages #21

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/App/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,18 @@ function sortUrl(string $path, array $data, array $overrideData = []): string

return url($path . '?' . implode('&', $queryStringSections));
}

function getCover($entity, $type)
{
if (!in_array($type, ['page', 'book', 'bookshelf'])) {
return '';
}

if ($type === 'page') {
return $entity->getPageCover();
}

if ($type === 'book' || $type === 'bookshelf') {
return $entity->getBookCover();
}
}
1 change: 1 addition & 0 deletions app/Config/setting-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'bookshelves_view_type' => env('APP_VIEWS_BOOKSHELVES', 'grid'),
'bookshelf_view_type' => env('APP_VIEWS_BOOKSHELF', 'grid'),
'books_view_type' => env('APP_VIEWS_BOOKS', 'grid'),
'pages_view_type' => env('APP_VIEWS_BOOKS', 'grid'),
],

];
2 changes: 2 additions & 0 deletions app/Entities/Controllers/ChapterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function store(Request $request, string $bookSlug)
*/
public function show(string $bookSlug, string $chapterSlug)
{
$view = setting()->getForCurrentUser(key: 'pages_view_type');
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-view', $chapter);

Expand All @@ -96,6 +97,7 @@ public function show(string $bookSlug, string $chapterSlug)
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),
'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
'view' => $view,
]);
}

Expand Down
6 changes: 6 additions & 0 deletions app/Entities/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ public function update(Request $request, string $bookSlug, string $pageSlug)
$page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-update', $page);

if ($request->has('image_reset')) {
$request['image'] = null;
} elseif (array_key_exists('image', $request->all()) && is_null($request['image'])) {
unset($request['image']);
}

$this->pageRepo->update($page, $request->all());

return redirect($page->getUrl());
Expand Down
29 changes: 28 additions & 1 deletion app/Entities/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace BookStack\Entities\Models;

use BookStack\Uploads\Image;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\PageEditorType;
use BookStack\Permissions\PermissionApplicator;
use BookStack\Uploads\Attachment;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,12 +26,13 @@
* @property bool $draft
* @property int $revision_count
* @property string $editor
* @property Image|null $cover
* @property Chapter $chapter
* @property Collection $attachments
* @property Collection $revisions
* @property PageRevision $currentRevision
*/
class Page extends BookChild
class Page extends BookChild implements HasCoverImage
{
use HasFactory;

Expand Down Expand Up @@ -143,4 +146,28 @@ public function forJsonDisplay(): self

return $refreshed;
}

public function getPageCover(int $width = 440, int $height = 250): string
{
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brijm-improwised default should be configurable using env

Copy link
Collaborator Author

@brijm-improwised brijm-improwised Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They implement this for book and for book they give this way only
that why i give this way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

book they give this way only - @brijm-improwised Can you give example?

if (!$this->image_id || !$this->cover) {
return $default;
}

try {
return $this->cover->getThumb($width, $height, false) ?? $default;
} catch (Exception $err) {
return $default;
}
}

public function cover(): BelongsTo
{
return $this->belongsTo(Image::class, 'image_id');
}

public function coverImageTypeKey(): string
{
return 'cover_page';
}
}
9 changes: 7 additions & 2 deletions app/Entities/Queries/PageQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PageQueries implements ProvidesEntityQueries
];
protected static array $listAttributes = [
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
'template', 'text', 'created_at', 'updated_at', 'priority', 'owned_by',
'template', 'text', 'created_at', 'updated_at', 'priority', 'owned_by','image_id',
];

public function start(): Builder
Expand Down Expand Up @@ -75,7 +75,7 @@ public function visibleForList(): Builder

public function visibleForChapterList(int $chapterId): Builder
{
return $this->visibleForList()
return $this->visibleForListWithCover()
->where('chapter_id', '=', $chapterId)
->orderBy('draft', 'desc')
->orderBy('priority', 'asc');
Expand Down Expand Up @@ -109,4 +109,9 @@ protected function mergeBookSlugForSelect(array $columns): array
->whereColumn('books.id', '=', 'pages.book_id');
}]);
}

public function visibleForListWithCover(): Builder
{
return $this->visibleForList()->with('cover');
}
}
8 changes: 8 additions & 0 deletions app/Entities/Repos/PageRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function publishDraft(Page $draft, array $input): Page
$this->updateTemplateStatusAndContentFromInput($draft, $input);
$this->baseRepo->update($draft, $input);

if (array_key_exists('image', $input)) {
$this->baseRepo->updateCoverImage($draft, $input['image'], $input['image'] === null);
}

$summary = trim($input['summary'] ?? '') ?: trans('entities.pages_initial_revision');
$this->revisionRepo->storeNewForPage($draft, $summary);
$draft->refresh();
Expand Down Expand Up @@ -116,6 +120,10 @@ public function update(Page $page, array $input): Page
$this->revisionRepo->storeNewForPage($page, $summary);
}

if (array_key_exists('image', $input)) {
$this->baseRepo->updateCoverImage($page, $input['image'], $input['image'] === null);
}

Activity::add(ActivityType::PAGE_UPDATE, $page);

return $page;
Expand Down
2 changes: 1 addition & 1 deletion app/Users/Controllers/UserPreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
*/
public function changeView(Request $request, string $type)
{
$valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
$valueViewTypes = ['books', 'bookshelves', 'bookshelf','pages'];
if (!in_array($type, $valueViewTypes)) {
return $this->redirectToRequest($request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('pages', function (Blueprint $table) {
$table->integer('image_id')->after('priority')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('page', function (Blueprint $table) {
//
});
}
};
3 changes: 3 additions & 0 deletions resources/sass/_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@
.bg-bookshelf {
background-color: var(--color-bookshelf);
}
.bg-page {
background-color: var(--color-page);
}
20 changes: 15 additions & 5 deletions resources/views/chapters/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@
<div refs="entity-search@contentView" class="chapter-content">
<div class="text-muted break-text">{!! $chapter->descriptionHtml() !!}</div>
@if(count($pages) > 0)
<div class="entity-list book-contents">
@foreach($pages as $page)
@include('pages.parts.list-item', ['page' => $page])
@endforeach
</div>
@if($view === 'list')
<div class="entity-list book-contents">
@foreach($pages as $page)
@include('pages.parts.list-item', ['page' => $page])
@endforeach
</div>
@else
<div class="grid third">
@foreach($pages as $page)
@include('entities.grid-item', ['entity' => $page])
@endforeach
</div>
@endif
@else
<div class="mt-xl">
<hr>
Expand Down Expand Up @@ -107,6 +115,8 @@
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-link">

@include('entities.view-toggle', ['view' => $view, 'type' => 'pages'])

@if(userCan('page-create', $chapter))
<a href="{{ $chapter->getUrl('/create-page') }}" data-shortcut="new" class="icon-list-item">
<span>@icon('add')</span>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/entities/grid-item.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a href="{{ $entity->getUrl() }}" class="grid-card"
data-entity-type="{{ $entity->getType() }}" data-entity-id="{{ $entity->id }}">
<div class="bg-{{ $entity->getType() }} featured-image-container-wrap">
<div class="featured-image-container" @if($entity->cover) style="background-image: url('{{ $entity->getBookCover() }}')"@endif>
<div class="featured-image-container" @if($entity->cover) style="background-image: url('{{ getCover($entity,$entity->getType()) }}')"@endif>
</div>
@icon($entity->getType())
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/pages/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@section('content')

<div id="main-content" class="flex-fill flex height-fill">
<form action="{{ $page->getUrl() }}" autocomplete="off" data-page-id="{{ $page->id }}" method="POST" class="flex flex-fill">
<form action="{{ $page->getUrl() }}" autocomplete="off" data-page-id="{{ $page->id }}" method="POST" class="flex flex-fill" enctype="multipart/form-data">
{{ csrf_field() }}

@if(!$isDraft) {{ method_field('PUT') }} @endif
Expand Down
15 changes: 15 additions & 0 deletions resources/views/pages/parts/editor-toolbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@if($comments->enabled())
<button type="button" refs="editor-toolbox@tab-button" data-tab="comments" title="{{ trans('entities.comments') }}">@icon('comment')</button>
@endif
<button type="button" refs="editor-toolbox@tab-button" data-tab="cover_image" title="{{ trans('common.cover_image') }}">@icon('image')</button>
</div>
</div>

Expand All @@ -37,4 +38,18 @@
@include('pages.parts.toolbox-comments')
@endif

<div refs="editor-toolbox@tab-content" data-tab-content="cover_image" class="toolbox-tab-content">
<h4>{{ trans('common.cover_image') }}</h4>
<div class="px-l">
<p class="small">{{ trans('common.cover_image_description') }}</p>

@include('form.image-picker', [
'defaultImage' => url('/book_default_cover.png'),
'currentImage' => (isset($page) && $page->cover) ? $page->getPageCover() : url('/book_default_cover.png') ,
'name' => 'image',
'imageClass' => 'cover'
])
</div>
</div>

</div>
15 changes: 11 additions & 4 deletions resources/views/pages/parts/list-item.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
@component('entities.list-item-basic', ['entity' => $page])
<div class="entity-item-snippet">
<p class="text-muted break-text">{{ $page->getExcerpt() }}</p>
<a href="{{ $page->getUrl() }}" class="chapter entity-list-item " data-entity-type="chapter" data-entity-id="{{$page->id}}">
<span class="icon text-page">@icon('page')</span>
<div class="entity-list-item-image bg-page mr-xxs" style="background-image: url('{{$page->getPageCover()}}');width: 120px;">
@icon('page')
</div>
@endcomponent
<div class="content">
<h4 class="entity-list-item-name break-text">{{ $page->name }}</h4>
<div class="entity-item-snippet">
<p class="text-muted break-text">{{ $page->getExcerpt() }}</p>
</div>
</div>
</a>
Loading
Loading