Skip to content

Commit 89f84c9

Browse files
committed
Pages: Updated editor field to always be set
- Migration for setting on existing pages - Added test to cover simple new page scenario For #5117
1 parent 6103a22 commit 89f84c9

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

app/Entities/Repos/PageRepo.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use BookStack\Entities\Queries\EntityQueries;
1212
use BookStack\Entities\Tools\BookContents;
1313
use BookStack\Entities\Tools\PageContent;
14-
use BookStack\Entities\Tools\PageEditorData;
1514
use BookStack\Entities\Tools\PageEditorType;
1615
use BookStack\Entities\Tools\TrashCan;
1716
use BookStack\Exceptions\MoveOperationException;
@@ -44,6 +43,7 @@ public function getNewDraftPage(Entity $parent)
4443
'owned_by' => user()->id,
4544
'updated_by' => user()->id,
4645
'draft' => true,
46+
'editor' => PageEditorType::getSystemDefault()->value,
4747
]);
4848

4949
if ($parent instanceof Chapter) {
@@ -146,8 +146,10 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
146146
$pageContent->setNewHTML($input['html'], user());
147147
}
148148

149-
if ($newEditor !== $currentEditor && userCan('editor-change')) {
149+
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
150150
$page->editor = $newEditor->value;
151+
} elseif (empty($page->editor)) {
152+
$page->editor = $defaultEditor->value;
151153
}
152154
}
153155

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\DB;
5+
6+
return new class extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up(): void
12+
{
13+
// Ensure we have an "editor" value set for pages
14+
15+
// Get default
16+
$default = DB::table('settings')
17+
->where('setting_key', '=', 'app-editor')
18+
->first()
19+
->value ?? 'wysiwyg';
20+
$default = ($default === 'markdown') ? 'markdown' : 'wysiwyg';
21+
22+
// We set it to 'markdown' for pages currently with markdown content
23+
DB::table('pages')
24+
->where('editor', '=', '')
25+
->where('markdown', '!=', '')
26+
->update(['editor' => 'markdown']);
27+
28+
// We set it to 'wysiwyg' where we have HTML but no markdown
29+
DB::table('pages')
30+
->where('editor', '=', '')
31+
->where('markdown', '=', '')
32+
->where('html', '!=', '')
33+
->update(['editor' => 'wysiwyg']);
34+
35+
// Otherwise, where still empty, set to the current default
36+
DB::table('pages')
37+
->where('editor', '=', '')
38+
->update(['editor' => $default]);
39+
}
40+
41+
/**
42+
* Reverse the migrations.
43+
*/
44+
public function down(): void
45+
{
46+
// Can't reverse due to not knowing what would have been empty before
47+
}
48+
};

tests/Entity/PageEditorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ public function test_default_editor_is_wysiwyg_for_new_pages()
2424
$this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor');
2525
}
2626

27+
public function test_editor_set_for_new_pages()
28+
{
29+
$book = $this->page->book;
30+
31+
$this->asEditor()->get($book->getUrl('/create-page'));
32+
$newPage = $book->pages()->orderBy('id', 'desc')->first();
33+
$this->assertEquals('wysiwyg', $newPage->editor);
34+
35+
$this->setSettings(['app-editor' => PageEditorType::Markdown->value]);
36+
37+
$this->asEditor()->get($book->getUrl('/create-page'));
38+
$newPage = $book->pages()->orderBy('id', 'desc')->first();
39+
$this->assertEquals('markdown', $newPage->editor);
40+
}
41+
2742
public function test_markdown_setting_shows_markdown_editor_for_new_pages()
2843
{
2944
$this->setSettings(['app-editor' => PageEditorType::Markdown->value]);

0 commit comments

Comments
 (0)