1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-31 15:24:31 +03:00

Reviewed and refactored additional editor draft save warnings

- Added testing to cover warning cases.
- Refactored logic to be simpler and move much of the business out of
  the controller.
- Added new message that's more suitable to the case this was handling.
- For detecting an outdated draft, checked the draft created_at time
  instead of updated_at to better fit the scenario being checked.
- Updated some method types to align with those potentially being used
  in the logic of the code.
- Added a cache of shown messages on the front-end to prevent them
  re-showing on every save during the session, even if dismissed.
This commit is contained in:
Dan Brown
2021-10-04 20:26:55 +01:00
parent 756b55bbff
commit f99af807d0
6 changed files with 96 additions and 39 deletions

View File

@ -21,8 +21,6 @@ class PageEditActivity
/**
* Check if there's active editing being performed on this page.
*
* @return bool
*/
public function hasActiveEditing(): bool
{
@ -44,21 +42,35 @@ class PageEditActivity
}
/**
* Check if the page has been updated since the draft has been saved.
*
* @return bool
* Get any editor clash warning messages to show for the given draft revision.
* @param PageRevision|Page $draft
* @return string[]
*/
public function hasPageBeenUpdatedSinceDraftSaved(PageRevision $draft): bool
public function getWarningMessagesForDraft($draft): array
{
return $draft->page->updated_at->timestamp >= $draft->updated_at->timestamp;
$warnings = [];
if ($this->hasActiveEditing()) {
$warnings[] = $this->activeEditingMessage();
}
if ($draft instanceof PageRevision && $this->hasPageBeenUpdatedSinceDraftCreated($draft)) {
$warnings[] = trans('entities.pages_draft_page_changed_since_creation');
}
return $warnings;
}
/**
* Check if the page has been updated since the draft has been saved.
*/
protected function hasPageBeenUpdatedSinceDraftCreated(PageRevision $draft): bool
{
return $draft->page->updated_at->timestamp > $draft->created_at->timestamp;
}
/**
* Get the message to show when the user will be editing one of their drafts.
*
* @param PageRevision $draft
*
* @return string
*/
public function getEditingActiveDraftMessage(PageRevision $draft): string
{