Skip to content

Commit adf0bae

Browse files
committed
Comments: Added back-end HTML support, fixed editor focus
Also fixed handling of editors when moved in DOM, to properly remove then re-init before & after move to avoid issues.
1 parent 5c92b72 commit adf0bae

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

app/Activity/CommentRepo.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BookStack\Activity\Models\Comment;
66
use BookStack\Entities\Models\Entity;
77
use BookStack\Facades\Activity as ActivityService;
8-
use League\CommonMark\CommonMarkConverter;
8+
use BookStack\Util\HtmlDescriptionFilter;
99

1010
class CommentRepo
1111
{
@@ -20,13 +20,12 @@ public function getById(int $id): Comment
2020
/**
2121
* Create a new comment on an entity.
2222
*/
23-
public function create(Entity $entity, string $text, ?int $parent_id): Comment
23+
public function create(Entity $entity, string $html, ?int $parent_id): Comment
2424
{
2525
$userId = user()->id;
2626
$comment = new Comment();
2727

28-
$comment->text = $text;
29-
$comment->html = $this->commentToHtml($text);
28+
$comment->html = HtmlDescriptionFilter::filterFromString($html);
3029
$comment->created_by = $userId;
3130
$comment->updated_by = $userId;
3231
$comment->local_id = $this->getNextLocalId($entity);
@@ -42,11 +41,10 @@ public function create(Entity $entity, string $text, ?int $parent_id): Comment
4241
/**
4342
* Update an existing comment.
4443
*/
45-
public function update(Comment $comment, string $text): Comment
44+
public function update(Comment $comment, string $html): Comment
4645
{
4746
$comment->updated_by = user()->id;
48-
$comment->text = $text;
49-
$comment->html = $this->commentToHtml($text);
47+
$comment->html = HtmlDescriptionFilter::filterFromString($html);
5048
$comment->save();
5149

5250
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
@@ -64,20 +62,6 @@ public function delete(Comment $comment): void
6462
ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
6563
}
6664

67-
/**
68-
* Convert the given comment Markdown to HTML.
69-
*/
70-
public function commentToHtml(string $commentText): string
71-
{
72-
$converter = new CommonMarkConverter([
73-
'html_input' => 'strip',
74-
'max_nesting_level' => 10,
75-
'allow_unsafe_links' => false,
76-
]);
77-
78-
return $converter->convert($commentText);
79-
}
80-
8165
/**
8266
* Get the next local ID relative to the linked entity.
8367
*/

app/Activity/Controllers/CommentController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function __construct(
2222
*/
2323
public function savePageComment(Request $request, int $pageId)
2424
{
25-
$this->validate($request, [
26-
'text' => ['required', 'string'],
25+
$input = $this->validate($request, [
26+
'html' => ['required', 'string'],
2727
'parent_id' => ['nullable', 'integer'],
2828
]);
2929

@@ -39,7 +39,7 @@ public function savePageComment(Request $request, int $pageId)
3939

4040
// Create a new comment.
4141
$this->checkPermission('comment-create-all');
42-
$comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
42+
$comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null);
4343

4444
return view('comments.comment-branch', [
4545
'readOnly' => false,
@@ -57,17 +57,20 @@ public function savePageComment(Request $request, int $pageId)
5757
*/
5858
public function update(Request $request, int $commentId)
5959
{
60-
$this->validate($request, [
61-
'text' => ['required', 'string'],
60+
$input = $this->validate($request, [
61+
'html' => ['required', 'string'],
6262
]);
6363

6464
$comment = $this->commentRepo->getById($commentId);
6565
$this->checkOwnablePermission('page-view', $comment->entity);
6666
$this->checkOwnablePermission('comment-update', $comment);
6767

68-
$comment = $this->commentRepo->update($comment, $request->get('text'));
68+
$comment = $this->commentRepo->update($comment, $input['html']);
6969

70-
return view('comments.comment', ['comment' => $comment, 'readOnly' => false]);
70+
return view('comments.comment', [
71+
'comment' => $comment,
72+
'readOnly' => false,
73+
]);
7174
}
7275

7376
/**

resources/js/components/page-comment.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from './component';
22
import {getLoading, htmlToDom} from '../services/dom';
3-
import {buildForInput} from "../wysiwyg/config";
3+
import {buildForInput} from '../wysiwyg/config';
44

55
export class PageComment extends Component {
66

@@ -58,6 +58,7 @@ export class PageComment extends Component {
5858
this.toggleEditMode(true);
5959

6060
if (this.wysiwygEditor) {
61+
this.wysiwygEditor.focus();
6162
return;
6263
}
6364

@@ -72,6 +73,7 @@ export class PageComment extends Component {
7273

7374
window.tinymce.init(config).then(editors => {
7475
this.wysiwygEditor = editors[0];
76+
setTimeout(() => this.wysiwygEditor.focus(), 50);
7577
});
7678
}
7779

@@ -81,7 +83,7 @@ export class PageComment extends Component {
8183
this.form.toggleAttribute('hidden', true);
8284

8385
const reqData = {
84-
text: this.input.value,
86+
html: this.wysiwygEditor.getContent(),
8587
parent_id: this.parentId || null,
8688
};
8789

resources/js/components/page-comments.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from './component';
22
import {getLoading, htmlToDom} from '../services/dom';
3-
import {buildForInput} from "../wysiwyg/config";
3+
import {buildForInput} from '../wysiwyg/config';
44

55
export class PageComments extends Component {
66

@@ -65,9 +65,8 @@ export class PageComments extends Component {
6565
this.form.after(loading);
6666
this.form.toggleAttribute('hidden', true);
6767

68-
const text = this.formInput.value;
6968
const reqData = {
70-
text,
69+
html: this.wysiwygEditor.getContent(),
7170
parent_id: this.parentId || null,
7271
};
7372

@@ -92,13 +91,15 @@ export class PageComments extends Component {
9291
}
9392

9493
resetForm() {
94+
this.removeEditor();
9595
this.formInput.value = '';
9696
this.parentId = null;
9797
this.replyToRow.toggleAttribute('hidden', true);
9898
this.container.append(this.formContainer);
9999
}
100100

101101
showForm() {
102+
this.removeEditor();
102103
this.formContainer.toggleAttribute('hidden', false);
103104
this.addButtonContainer.toggleAttribute('hidden', true);
104105
this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
@@ -118,6 +119,7 @@ export class PageComments extends Component {
118119

119120
loadEditor() {
120121
if (this.wysiwygEditor) {
122+
this.wysiwygEditor.focus();
121123
return;
122124
}
123125

@@ -132,10 +134,17 @@ export class PageComments extends Component {
132134

133135
window.tinymce.init(config).then(editors => {
134136
this.wysiwygEditor = editors[0];
135-
this.wysiwygEditor.focus();
137+
setTimeout(() => this.wysiwygEditor.focus(), 50);
136138
});
137139
}
138140

141+
removeEditor() {
142+
if (this.wysiwygEditor) {
143+
this.wysiwygEditor.remove();
144+
this.wysiwygEditor = null;
145+
}
146+
}
147+
139148
getCommentCount() {
140149
return this.container.querySelectorAll('[component="page-comment"]').length;
141150
}

resources/views/comments/comment.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class="comment-box">
7777
@if(!$readOnly && userCan('comment-update', $comment))
7878
<form novalidate refs="page-comment@form" hidden class="content pt-s px-s block">
7979
<div class="form-group description-input">
80-
<textarea refs="page-comment@input" name="markdown" rows="3" placeholder="{{ trans('entities.comment_placeholder') }}">{{ $comment->text }}</textarea>
80+
<textarea refs="page-comment@input" name="html" rows="3" placeholder="{{ trans('entities.comment_placeholder') }}">{{ $comment->html }}</textarea>
8181
</div>
8282
<div class="form-group text-right">
8383
<button type="button" class="button outline" refs="page-comment@form-cancel">{{ trans('common.cancel') }}</button>

resources/views/comments/create.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<div class="content px-s pt-s">
1717
<form refs="page-comments@form" novalidate>
1818
<div class="form-group description-input">
19-
<textarea refs="page-comments@form-input" name="markdown"
19+
<textarea refs="page-comments@form-input" name="html"
2020
rows="3"
2121
placeholder="{{ trans('entities.comment_placeholder') }}"></textarea>
2222
</div>

0 commit comments

Comments
 (0)