From 956eb1308fe63b0d1b3cc3765a2f2b77d2dc8396 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 17 Apr 2022 23:01:14 +0100 Subject: [PATCH] Aligned page edit controller method data usage Extracted page editor view data gathering to its own class for alignment. Updated the data used in views as part of the process to use view-specific variables instead of custom attributes added to models. Also moved tinymce library loading so it's not loaded when not using the wysiwyg editor. --- app/Entities/Tools/PageEditActivity.php | 2 +- app/Entities/Tools/PageEditorData.php | 67 +++++++++++++++++++ app/Http/Controllers/PageController.php | 50 +++----------- resources/views/pages/edit.blade.php | 8 +-- resources/views/pages/parts/form.blade.php | 17 +++-- .../pages/parts/wysiwyg-editor.blade.php | 4 ++ 6 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 app/Entities/Tools/PageEditorData.php diff --git a/app/Entities/Tools/PageEditActivity.php b/app/Entities/Tools/PageEditActivity.php index 9981a6ed7..2672de941 100644 --- a/app/Entities/Tools/PageEditActivity.php +++ b/app/Entities/Tools/PageEditActivity.php @@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Builder; class PageEditActivity { - protected $page; + protected Page $page; /** * PageEditActivity constructor. diff --git a/app/Entities/Tools/PageEditorData.php b/app/Entities/Tools/PageEditorData.php new file mode 100644 index 000000000..a6818839d --- /dev/null +++ b/app/Entities/Tools/PageEditorData.php @@ -0,0 +1,67 @@ +page = $page; + $this->pageRepo = $pageRepo; + $this->viewData = $this->build(); + } + + public function getViewData(): array + { + return $this->viewData; + } + + public function getWarnings(): array + { + return $this->warnings; + } + + protected function build(): array + { + $page = clone $this->page; + $isDraft = boolval($this->page->draft); + $templates = $this->pageRepo->getTemplates(10); + $draftsEnabled = auth()->check(); + + $isDraftRevision = false; + $this->warnings = []; + $editActivity = new PageEditActivity($page); + + if ($editActivity->hasActiveEditing()) { + $this->warnings[] = $editActivity->activeEditingMessage(); + } + + // Check for a current draft version for this user + $userDraft = $this->pageRepo->getUserDraft($page); + if ($userDraft !== null) { + $page->forceFill($userDraft->only(['name', 'html', 'markdown'])); + $isDraftRevision = true; + $this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft); + } + + return [ + 'page' => $page, + 'book' => $page->book, + 'isDraft' => $isDraft, + 'isDraftRevision' => $isDraftRevision, + 'draftsEnabled' => $draftsEnabled, + 'templates' => $templates, + 'editor' => setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown', + ]; + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index ec48e63f5..232c6b034 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -10,6 +10,7 @@ use BookStack\Entities\Tools\Cloner; use BookStack\Entities\Tools\NextPreviousContentLocator; use BookStack\Entities\Tools\PageContent; use BookStack\Entities\Tools\PageEditActivity; +use BookStack\Entities\Tools\PageEditorData; use BookStack\Entities\Tools\PermissionsUpdater; use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\PermissionsException; @@ -21,7 +22,7 @@ use Throwable; class PageController extends Controller { - protected $pageRepo; + protected PageRepo $pageRepo; /** * PageController constructor. @@ -86,19 +87,11 @@ class PageController extends Controller { $draft = $this->pageRepo->getById($pageId); $this->checkOwnablePermission('page-create', $draft->getParent()); + + $editorData = new PageEditorData($draft, $this->pageRepo); $this->setPageTitle(trans('entities.pages_edit_draft')); - $draftsEnabled = $this->isSignedIn(); - $templates = $this->pageRepo->getTemplates(10); - - return view('pages.edit', [ - 'page' => $draft, - 'book' => $draft->book, - 'isDraft' => true, - 'draftsEnabled' => $draftsEnabled, - 'templates' => $templates, - 'editor' => setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown', - ]); + return view('pages.edit', $editorData->getViewData()); } /** @@ -194,39 +187,14 @@ class PageController extends Controller $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $this->checkOwnablePermission('page-update', $page); - $page->isDraft = false; - $editActivity = new PageEditActivity($page); - - // Check for active editing - $warnings = []; - if ($editActivity->hasActiveEditing()) { - $warnings[] = $editActivity->activeEditingMessage(); + $editorData = new PageEditorData($page, $this->pageRepo); + if ($editorData->getWarnings()) { + $this->showWarningNotification(implode("\n", $editorData->getWarnings())); } - // Check for a current draft version for this user - $userDraft = $this->pageRepo->getUserDraft($page); - if ($userDraft !== null) { - $page->forceFill($userDraft->only(['name', 'html', 'markdown'])); - $page->isDraft = true; - $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft); - } - - if (count($warnings) > 0) { - $this->showWarningNotification(implode("\n", $warnings)); - } - - $templates = $this->pageRepo->getTemplates(10); - $draftsEnabled = $this->isSignedIn(); $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()])); - return view('pages.edit', [ - 'page' => $page, - 'book' => $page->book, - 'current' => $page, - 'draftsEnabled' => $draftsEnabled, - 'templates' => $templates, - 'editor' => setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown', - ]); + return view('pages.edit', $editorData->getViewData()); } /** diff --git a/resources/views/pages/edit.blade.php b/resources/views/pages/edit.blade.php index 30158e852..de7c82d21 100644 --- a/resources/views/pages/edit.blade.php +++ b/resources/views/pages/edit.blade.php @@ -1,9 +1,5 @@ @extends('layouts.base') -@section('head') - -@stop - @section('body-class', 'flexbox') @section('content') @@ -12,9 +8,7 @@
{{ csrf_field() }} - @if(!isset($isDraft)) - - @endif + @if($isDraft) {{ method_field('PUT') }} @endif @include('pages.parts.form', ['model' => $page]) @include('pages.parts.editor-toolbox')
diff --git a/resources/views/pages/parts/form.blade.php b/resources/views/pages/parts/form.blade.php index 6646d508f..3507705aa 100644 --- a/resources/views/pages/parts/form.blade.php +++ b/resources/views/pages/parts/form.blade.php @@ -8,8 +8,8 @@ @endif option:page-editor:editor-type="{{ setting('app-editor') }}" option:page-editor:page-id="{{ $model->id ?? '0' }}" - option:page-editor:page-new-draft="{{ ($model->draft ?? false) ? 'true' : 'false' }}" - option:page-editor:draft-text="{{ ($model->draft || $model->isDraft) ? trans('entities.pages_editing_draft') : trans('entities.pages_editing_page') }}" + option:page-editor:page-new-draft="{{ $isDraft ? 'true' : 'false' }}" + option:page-editor:draft-text="{{ ($isDraft || $isDraftRevision) ? trans('entities.pages_editing_draft') : trans('entities.pages_editing_page') }}" option:page-editor:autosave-fail-text="{{ trans('errors.page_draft_autosave_fail') }}" option:page-editor:editing-page-text="{{ trans('entities.pages_editing_page') }}" option:page-editor:draft-discarded-text="{{ trans('entities.pages_draft_discarded') }}" @@ -20,7 +20,7 @@
@@ -34,20 +34,19 @@
  • - @if ($model->draft) + @if($isDraft)
  • @icon('delete'){{ trans('entities.pages_edit_delete_draft') }}
  • @endif -
  • +
  • @if(userCan('editor-change'))
  • - + + @icon('swap-horizontal'){{ $editor === 'wysiwyg' ? trans('entities.pages_edit_switch_to_markdown') : trans('entities.pages_edit_switch_to_wysiwyg') }} +
  • @endif diff --git a/resources/views/pages/parts/wysiwyg-editor.blade.php b/resources/views/pages/parts/wysiwyg-editor.blade.php index 29a4b6532..d8ca74939 100644 --- a/resources/views/pages/parts/wysiwyg-editor.blade.php +++ b/resources/views/pages/parts/wysiwyg-editor.blade.php @@ -1,3 +1,7 @@ +@push('head') + +@endpush +