View Blocks: Extracted default set, added labels to each

This commit is contained in:
Dan Brown
2026-08-09 18:35:10 +01:00
parent 600497b110
commit 45ff36714f
39 changed files with 185 additions and 129 deletions
+9
View File
@@ -5,6 +5,7 @@ namespace BookStack\View;
abstract class ViewBlock implements ViewBlockInterface
{
protected string $view;
protected string $labelTranslationKey;
/**
* @inheritDoc
@@ -13,4 +14,12 @@ abstract class ViewBlock implements ViewBlockInterface
{
return $this->view;
}
/**
* @inheritDoc
*/
public function getLabel(): string
{
return trans($this->labelTranslationKey);
}
}
+110
View File
@@ -0,0 +1,110 @@
<?php
namespace BookStack\View;
class ViewBlockDefaults
{
/**
* @var array<string, array<string, class-string<ViewBlockInterface>[]>>
*/
protected static array $defaults = [
'home-default' => [
'left' => [
ViewBlocks\HomeRecentDrafts::class,
ViewBlocks\HomeRecentlyViewedOrRecentBooks::class,
],
'center' => [
ViewBlocks\HomeTopFavourites::class,
ViewBlocks\HomeRecentlyUpdatedPages::class,
],
'right' => [
ViewBlocks\HomeRecentActivity::class,
],
],
'home-non-default' => [
'left' => [
ViewBlocks\HomeRecentDrafts::class,
ViewBlocks\HomeTopFavourites::class,
ViewBlocks\HomeRecentlyViewedOrRecentBooks::class,
ViewBlocks\HomeRecentlyUpdatedPages::class,
ViewBlocks\HomeRecentActivity::class,
],
'right' => [
ViewBlocks\HomeActions::class,
],
],
'shelves-index' => [
'left' => [
ViewBlocks\ShelvesIndexRecents::class,
ViewBlocks\ShelvesIndexPopular::class,
ViewBlocks\ShelvesIndexNew::class,
],
'right' => [
ViewBlocks\ShelvesIndexActions::class,
],
],
'shelves-show' => [
'left' => [
ViewBlocks\ShelvesShowTags::class,
ViewBlocks\ShelvesShowDetails::class,
ViewBlocks\ShelvesShowActivity::class,
],
'right' => [
ViewBlocks\ShelvesShowActions::class,
],
],
'books-index' => [
'left' => [
ViewBlocks\BooksIndexRecents::class,
ViewBlocks\BooksIndexPopular::class,
ViewBlocks\BooksIndexNew::class,
],
'right' => [
ViewBlocks\BooksIndexActions::class,
],
],
'books-show' => [
'left' => [
ViewBlocks\BooksShowSearchForm::class,
ViewBlocks\BooksShowTags::class,
ViewBlocks\BooksShowShelves::class,
ViewBlocks\BooksShowActivity::class,
],
'right' => [
ViewBlocks\BooksShowDetails::class,
ViewBlocks\BooksShowActions::class,
],
],
'chapters-show' => [
'left' => [
ViewBlocks\ChaptersShowSearchForm::class,
ViewBlocks\ChaptersShowTags::class,
ViewBlocks\ChaptersShowBookTree::class,
],
'right' => [
ViewBlocks\ChaptersShowDetails::class,
ViewBlocks\ChaptersShowActions::class,
],
],
'pages-show' => [
'left' => [
ViewBlocks\PagesShowTags::class,
ViewBlocks\PagesShowAttachments::class,
ViewBlocks\PagesShowPageNav::class,
ViewBlocks\PagesShowBookTree::class,
],
'right' => [
ViewBlocks\PagesShowDetails::class,
ViewBlocks\PagesShowActions::class,
],
],
];
/**
* Get the default view blocks for the given location.
*/
public static function getForLocation(string $location): array
{
return self::$defaults[$location] ?? [];
}
}
+5
View File
@@ -4,6 +4,11 @@ namespace BookStack\View;
interface ViewBlockInterface
{
/**
* Get the label for this block.
*/
public function getLabel(): string;
/**
* Get the view to render for this block.
* Is provided with the existing parent view data.
+5 -128
View File
@@ -2,41 +2,6 @@
namespace BookStack\View;
use BookStack\View\ViewBlocks\BooksIndexActions;
use BookStack\View\ViewBlocks\BooksIndexNew;
use BookStack\View\ViewBlocks\BooksIndexPopular;
use BookStack\View\ViewBlocks\BooksIndexRecents;
use BookStack\View\ViewBlocks\BooksShowActions;
use BookStack\View\ViewBlocks\BooksShowActivity;
use BookStack\View\ViewBlocks\BooksShowDetails;
use BookStack\View\ViewBlocks\BooksShowSearchForm;
use BookStack\View\ViewBlocks\BooksShowShelves;
use BookStack\View\ViewBlocks\BooksShowTags;
use BookStack\View\ViewBlocks\ChaptersShowActions;
use BookStack\View\ViewBlocks\ChaptersShowBookTree;
use BookStack\View\ViewBlocks\ChaptersShowDetails;
use BookStack\View\ViewBlocks\ChaptersShowSearchForm;
use BookStack\View\ViewBlocks\ChaptersShowTags;
use BookStack\View\ViewBlocks\HomeActions;
use BookStack\View\ViewBlocks\HomeRecentActivity;
use BookStack\View\ViewBlocks\HomeRecentDrafts;
use BookStack\View\ViewBlocks\HomeRecentlyUpdatedPages;
use BookStack\View\ViewBlocks\HomeRecentlyViewedOrRecentBooks;
use BookStack\View\ViewBlocks\HomeTopFavourites;
use BookStack\View\ViewBlocks\PagesShowActions;
use BookStack\View\ViewBlocks\PagesShowAttachments;
use BookStack\View\ViewBlocks\PagesShowBookTree;
use BookStack\View\ViewBlocks\PagesShowDetails;
use BookStack\View\ViewBlocks\PagesShowPageNav;
use BookStack\View\ViewBlocks\PagesShowTags;
use BookStack\View\ViewBlocks\ShelvesIndexActions;
use BookStack\View\ViewBlocks\ShelvesIndexNew;
use BookStack\View\ViewBlocks\ShelvesIndexPopular;
use BookStack\View\ViewBlocks\ShelvesIndexRecents;
use BookStack\View\ViewBlocks\ShelvesShowActions;
use BookStack\View\ViewBlocks\ShelvesShowActivity;
use BookStack\View\ViewBlocks\ShelvesShowDetails;
use BookStack\View\ViewBlocks\ShelvesShowTags;
use Illuminate\Contracts\Container\BindingResolutionException;
class ViewBlockManager
@@ -44,98 +9,7 @@ class ViewBlockManager
/**
* @var array<string, array<string, class-string<ViewBlockInterface>[]>>
*/
protected array $blocksByLocationAndPosition = [
'home-default' => [
'left' => [
HomeRecentDrafts::class,
HomeRecentlyViewedOrRecentBooks::class,
],
'center' => [
HomeTopFavourites::class,
HomeRecentlyUpdatedPages::class,
],
'right' => [
HomeRecentActivity::class,
],
],
'home-non-default' => [
'left' => [
HomeRecentDrafts::class,
HomeTopFavourites::class,
HomeRecentlyViewedOrRecentBooks::class,
HomeRecentlyUpdatedPages::class,
HomeRecentActivity::class,
],
'right' => [
HomeActions::class,
],
],
'shelves-index' => [
'left' => [
ShelvesIndexRecents::class,
ShelvesIndexPopular::class,
ShelvesIndexNew::class,
],
'right' => [
ShelvesIndexActions::class,
],
],
'shelves-show' => [
'left' => [
ShelvesShowTags::class,
ShelvesShowDetails::class,
ShelvesShowActivity::class,
],
'right' => [
ShelvesShowActions::class,
],
],
'books-index' => [
'left' => [
BooksIndexRecents::class,
BooksIndexPopular::class,
BooksIndexNew::class,
],
'right' => [
BooksIndexActions::class,
],
],
'books-show' => [
'left' => [
BooksShowSearchForm::class,
BooksShowTags::class,
BooksShowShelves::class,
BooksShowActivity::class,
],
'right' => [
BooksShowDetails::class,
BooksShowActions::class,
],
],
'chapters-show' => [
'left' => [
ChaptersShowSearchForm::class,
ChaptersShowTags::class,
ChaptersShowBookTree::class,
],
'right' => [
ChaptersShowDetails::class,
ChaptersShowActions::class,
],
],
'pages-show' => [
'left' => [
PagesShowTags::class,
PagesShowAttachments::class,
PagesShowPageNav::class,
PagesShowBookTree::class,
],
'right' => [
PagesShowDetails::class,
PagesShowActions::class,
],
],
];
protected array $blocksByLocationAndPosition = [];
/**
* Register a block type to be displayed at the given location and position.
@@ -162,7 +36,10 @@ class ViewBlockManager
*/
public function getForLocation(string $location, string $position): array
{
$sections = array_unique($this->blocksByLocationAndPosition[$location][$position] ?? []);
$defaults = ViewBlockDefaults::getForLocation($location)[$position] ?? [];
$registered = $this->blocksByLocationAndPosition[$location][$position] ?? [];
$sections = array_unique(array_merge($defaults, $registered));
return array_map(function (string $className) {
return app()->make($className);
}, $sections);
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class BooksIndexActions extends ViewBlock
{
protected string $view = 'books.parts.index-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
+1
View File
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class BooksIndexNew extends ViewBlock
{
protected string $view = 'books.parts.index-sidebar-section-new';
protected string $labelTranslationKey = 'entities.books_new';
public function __construct(
protected BookQueries $queries,
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class BooksIndexPopular extends ViewBlock
{
protected string $view = 'books.parts.index-sidebar-section-popular';
protected string $labelTranslationKey = 'entities.books_popular';
public function __construct(
protected BookQueries $queries,
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class BooksIndexRecents extends ViewBlock
{
protected string $view = 'books.parts.index-sidebar-section-recents';
protected string $labelTranslationKey = 'entities.recently_viewed';
public function __construct(
protected BookQueries $queries,
+1
View File
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class BooksShowActions extends ViewBlock
{
protected string $view = 'books.parts.show-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class BooksShowActivity extends ViewBlock
{
protected string $view = 'books.parts.show-sidebar-section-activity';
protected string $labelTranslationKey = 'entities.recent_activity';
public function __construct(
protected ActivityQueries $activityQueries,
+1
View File
@@ -11,6 +11,7 @@ use Illuminate\Http\Request;
class BooksShowDetails extends ViewBlock
{
protected string $view = 'books.parts.show-sidebar-section-details';
protected string $labelTranslationKey = 'common.details';
public function __construct(
protected ReferenceFetcher $referenceFetcher,
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class BooksShowSearchForm extends ViewBlock
{
protected string $view = 'entities.search-form';
protected string $labelTranslationKey = 'common.search';
public function withData(array $viewData): array
{
+1
View File
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class BooksShowShelves extends ViewBlock
{
protected string $view = 'books.parts.show-sidebar-section-shelves';
protected string $labelTranslationKey = 'entities.shelves';
public function withData(array $viewData): array
{
+1
View File
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class BooksShowTags extends ViewBlock
{
protected string $view = 'books.parts.show-sidebar-section-tags';
protected string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
{
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class ChaptersShowActions extends ViewBlock
{
protected string $view = 'chapters.parts.show-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ChaptersShowBookTree extends ViewBlock
{
protected string $view = 'entities.book-tree';
protected string $labelTranslationKey = 'entities.books_navigation';
public function withData(array $viewData): array
{
@@ -12,6 +12,7 @@ use Illuminate\Http\Request;
class ChaptersShowDetails extends ViewBlock
{
protected string $view = 'chapters.parts.show-sidebar-section-details';
protected string $labelTranslationKey = 'common.details';
public function __construct(
protected ReferenceFetcher $referenceFetcher,
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class ChaptersShowSearchForm extends ViewBlock
{
protected string $view = 'entities.search-form';
protected string $labelTranslationKey = 'common.search';
public function withData(array $viewData): array
{
+1
View File
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ChaptersShowTags extends ViewBlock
{
protected string $view = 'chapters.parts.show-sidebar-section-tags';
protected string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
{
+1 -1
View File
@@ -3,11 +3,11 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
class HomeActions extends ViewBlock
{
protected string $view = 'home.parts.configured-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
@@ -13,6 +13,11 @@ class HomeRecentActivity implements ViewBlockInterface
) {
}
public function getLabel(): string
{
return trans('entities.recent_activity');
}
public function getView(array $viewData): string
{
if ($viewData['homeView'] === 'default') {
+5
View File
@@ -12,6 +12,11 @@ class HomeRecentDrafts implements ViewBlockInterface
) {
}
public function getLabel(): string
{
return trans('entities.my_recent_drafts');
}
public function getView(array $viewData): string
{
if ($viewData['homeView'] === 'default') {
@@ -13,6 +13,11 @@ class HomeRecentlyUpdatedPages implements ViewBlockInterface
) {
}
public function getLabel(): string
{
return trans('entities.recently_updated_pages');
}
public function getView(array $viewData): string
{
if ($viewData['homeView'] === 'default') {
@@ -16,6 +16,12 @@ class HomeRecentlyViewedOrRecentBooks implements ViewBlockInterface
) {
}
public function getLabel(): string
{
$key = user()->isGuest() ? 'books_recent' : 'my_recently_viewed';
return trans("entities.{$key}");
}
public function getView(array $viewData): string
{
if ($viewData['homeView'] === 'default') {
@@ -13,6 +13,11 @@ class HomeTopFavourites implements ViewBlockInterface
) {
}
public function getLabel(): string
{
return trans('entities.my_most_viewed_favourites');
}
public function getView(array $viewData): string
{
if ($viewData['homeView'] === 'default') {
+1
View File
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class PagesShowActions extends ViewBlock
{
protected string $view = 'pages.parts.show-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class PagesShowAttachments extends ViewBlock
{
protected string $view = 'pages.parts.show-sidebar-section-attachments';
protected string $labelTranslationKey = 'entities.pages_attachments';
public function withData(array $viewData): array
{
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class PagesShowBookTree extends ViewBlock
{
protected string $view = 'entities.book-tree';
protected string $labelTranslationKey = 'entities.books_navigation';
public function withData(array $viewData): array
{
+1
View File
@@ -12,6 +12,7 @@ use Illuminate\Http\Request;
class PagesShowDetails extends ViewBlock
{
protected string $view = 'pages.parts.show-sidebar-section-details';
protected string $labelTranslationKey = 'common.details';
public function __construct(
protected ReferenceFetcher $referenceFetcher,
+1
View File
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class PagesShowPageNav extends ViewBlock
{
protected string $view = 'pages.parts.show-sidebar-section-page-nav';
protected string $labelTranslationKey = 'entities.pages_navigation';
public function withData(array $viewData): array
{
+1
View File
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class PagesShowTags extends ViewBlock
{
protected string $view = 'pages.parts.show-sidebar-section-tags';
protected string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
{
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class ShelvesIndexActions extends ViewBlock
{
protected string $view = 'shelves.parts.index-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
+1
View File
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ShelvesIndexNew extends ViewBlock
{
protected string $view = 'shelves.parts.index-sidebar-section-new';
protected string $labelTranslationKey = 'entities.shelves_new';
public function __construct(
protected BookshelfQueries $queries,
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ShelvesIndexPopular extends ViewBlock
{
protected string $view = 'shelves.parts.index-sidebar-section-popular';
protected string $labelTranslationKey = 'entities.shelves_popular';
public function __construct(
protected BookshelfQueries $queries,
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ShelvesIndexRecents extends ViewBlock
{
protected string $view = 'shelves.parts.index-sidebar-section-recents';
protected string $labelTranslationKey = 'entities.recently_viewed';
public function __construct(
protected BookshelfQueries $queries,
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
class ShelvesShowActions extends ViewBlock
{
protected string $view = 'shelves.parts.show-sidebar-section-actions';
protected string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
{
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class ShelvesShowActivity extends ViewBlock
{
protected string $view = 'shelves.parts.show-sidebar-section-activity';
protected string $labelTranslationKey = 'entities.recent_activity';
public function __construct(
protected ActivityQueries $activityQueries,
@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
class ShelvesShowDetails extends ViewBlock
{
protected string $view = 'shelves.parts.show-sidebar-section-details';
protected string $labelTranslationKey = 'common.details';
public function __construct(
protected ReferenceFetcher $referenceFetcher,
+1
View File
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
class ShelvesShowTags extends ViewBlock
{
protected string $view = 'shelves.parts.show-sidebar-section-tags';
protected string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
{