2022-04-17 18:01:14 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
|
|
|
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
2022-04-18 12:39:28 -04:00
|
|
|
use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
|
|
|
|
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
|
2022-04-17 18:01:14 -04:00
|
|
|
|
|
|
|
class PageEditorData
|
|
|
|
{
|
|
|
|
protected Page $page;
|
|
|
|
protected PageRepo $pageRepo;
|
2022-04-18 12:39:28 -04:00
|
|
|
protected string $requestedEditor;
|
2022-04-17 18:01:14 -04:00
|
|
|
|
|
|
|
protected array $viewData;
|
|
|
|
protected array $warnings;
|
|
|
|
|
2022-04-18 12:39:28 -04:00
|
|
|
public function __construct(Page $page, PageRepo $pageRepo, string $requestedEditor)
|
2022-04-17 18:01:14 -04:00
|
|
|
{
|
|
|
|
$this->page = $page;
|
|
|
|
$this->pageRepo = $pageRepo;
|
2022-04-18 12:39:28 -04:00
|
|
|
$this->requestedEditor = $requestedEditor;
|
|
|
|
|
2022-04-17 18:01:14 -04:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:39:28 -04:00
|
|
|
$editorType = $this->getEditorType($page);
|
|
|
|
$this->updateContentForEditor($page, $editorType);
|
|
|
|
|
2022-04-17 18:01:14 -04:00
|
|
|
return [
|
|
|
|
'page' => $page,
|
|
|
|
'book' => $page->book,
|
|
|
|
'isDraft' => $isDraft,
|
|
|
|
'isDraftRevision' => $isDraftRevision,
|
|
|
|
'draftsEnabled' => $draftsEnabled,
|
|
|
|
'templates' => $templates,
|
2022-04-18 12:39:28 -04:00
|
|
|
'editor' => $editorType,
|
2022-04-17 18:01:14 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:39:28 -04:00
|
|
|
protected function updateContentForEditor(Page $page, string $editorType): void
|
|
|
|
{
|
|
|
|
$isHtml = !empty($page->html) && empty($page->markdown);
|
|
|
|
|
|
|
|
// HTML to markdown-clean conversion
|
|
|
|
if ($editorType === 'markdown' && $isHtml && $this->requestedEditor === 'markdown-clean') {
|
|
|
|
$page->markdown = (new HtmlToMarkdown($page->html))->convert();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Markdown to HTML conversion if we don't have HTML
|
|
|
|
if ($editorType === 'wysiwyg' && !$isHtml) {
|
|
|
|
$page->html = (new MarkdownToHtml($page->markdown))->convert();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of editor to show for editing the given page.
|
|
|
|
* Defaults based upon the current content of the page otherwise will fall back
|
|
|
|
* to system default but will take a requested type (if provided) if permissions allow.
|
|
|
|
*/
|
|
|
|
protected function getEditorType(Page $page): string
|
|
|
|
{
|
2022-04-23 18:20:46 -04:00
|
|
|
$editorType = $page->editor ?: self::getSystemDefaultEditor();
|
2022-04-18 12:39:28 -04:00
|
|
|
|
|
|
|
// Use requested editor if valid and if we have permission
|
|
|
|
$requestedType = explode('-', $this->requestedEditor)[0];
|
|
|
|
if (($requestedType === 'markdown' || $requestedType === 'wysiwyg') && userCan('editor-change')) {
|
|
|
|
$editorType = $requestedType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $editorType;
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
/**
|
|
|
|
* Get the configured system default editor.
|
|
|
|
*/
|
|
|
|
public static function getSystemDefaultEditor(): string
|
|
|
|
{
|
|
|
|
return setting('app-editor') === 'markdown' ? 'markdown' : 'wysiwyg';
|
|
|
|
}
|
2022-04-24 13:22:40 -04:00
|
|
|
}
|