Skip to content

Commit 5c92b72

Browse files
committed
Comments: Added input wysiwyg for creating/updating comments
Not supporting old content, existing HTML or updating yet.
1 parent 24e6dc4 commit 5c92b72

File tree

8 files changed

+85
-12
lines changed

8 files changed

+85
-12
lines changed

app/Activity/Tools/CommentTree.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public function get(): array
4141
return $this->tree;
4242
}
4343

44+
public function canUpdateAny(): bool
45+
{
46+
foreach ($this->comments as $comment) {
47+
if (userCan('comment-update', $comment)) {
48+
return true;
49+
}
50+
}
51+
52+
return false;
53+
}
54+
4455
/**
4556
* @param Comment[] $comments
4657
*/

resources/js/components/page-comment.js

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

45
export class PageComment extends Component {
56

@@ -11,7 +12,12 @@ export class PageComment extends Component {
1112
this.deletedText = this.$opts.deletedText;
1213
this.updatedText = this.$opts.updatedText;
1314

14-
// Element References
15+
// Editor reference and text options
16+
this.wysiwygEditor = null;
17+
this.wysiwygLanguage = this.$opts.wysiwygLanguage;
18+
this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
19+
20+
// Element references
1521
this.container = this.$el;
1622
this.contentContainer = this.$refs.contentContainer;
1723
this.form = this.$refs.form;
@@ -50,8 +56,23 @@ export class PageComment extends Component {
5056

5157
startEdit() {
5258
this.toggleEditMode(true);
53-
const lineCount = this.$refs.input.value.split('\n').length;
54-
this.$refs.input.style.height = `${(lineCount * 20) + 40}px`;
59+
60+
if (this.wysiwygEditor) {
61+
return;
62+
}
63+
64+
const config = buildForInput({
65+
language: this.wysiwygLanguage,
66+
containerElement: this.input,
67+
darkMode: document.documentElement.classList.contains('dark-mode'),
68+
textDirection: this.wysiwygTextDirection,
69+
translations: {},
70+
translationMap: window.editor_translations,
71+
});
72+
73+
window.tinymce.init(config).then(editors => {
74+
this.wysiwygEditor = editors[0];
75+
});
5576
}
5677

5778
async update(event) {

resources/js/components/page-comments.js

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

45
export class PageComments extends Component {
56

@@ -21,6 +22,11 @@ export class PageComments extends Component {
2122
this.hideFormButton = this.$refs.hideFormButton;
2223
this.removeReplyToButton = this.$refs.removeReplyToButton;
2324

25+
// WYSIWYG options
26+
this.wysiwygLanguage = this.$opts.wysiwygLanguage;
27+
this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
28+
this.wysiwygEditor = null;
29+
2430
// Translations
2531
this.createdText = this.$opts.createdText;
2632
this.countText = this.$opts.countText;
@@ -96,9 +102,7 @@ export class PageComments extends Component {
96102
this.formContainer.toggleAttribute('hidden', false);
97103
this.addButtonContainer.toggleAttribute('hidden', true);
98104
this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
99-
setTimeout(() => {
100-
this.formInput.focus();
101-
}, 100);
105+
this.loadEditor();
102106
}
103107

104108
hideForm() {
@@ -112,6 +116,26 @@ export class PageComments extends Component {
112116
this.addButtonContainer.toggleAttribute('hidden', false);
113117
}
114118

119+
loadEditor() {
120+
if (this.wysiwygEditor) {
121+
return;
122+
}
123+
124+
const config = buildForInput({
125+
language: this.wysiwygLanguage,
126+
containerElement: this.formInput,
127+
darkMode: document.documentElement.classList.contains('dark-mode'),
128+
textDirection: this.wysiwygTextDirection,
129+
translations: {},
130+
translationMap: window.editor_translations,
131+
});
132+
133+
window.tinymce.init(config).then(editors => {
134+
this.wysiwygEditor = editors[0];
135+
this.wysiwygEditor.focus();
136+
});
137+
}
138+
115139
getCommentCount() {
116140
return this.container.querySelectorAll('[component="page-comment"]').length;
117141
}

resources/js/components/wysiwyg-input.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ export class WysiwygInput extends Component {
1010
language: this.$opts.language,
1111
containerElement: this.elem,
1212
darkMode: document.documentElement.classList.contains('dark-mode'),
13-
textDirection: this.textDirection,
14-
translations: {
15-
imageUploadErrorText: this.$opts.imageUploadErrorText,
16-
serverUploadLimitText: this.$opts.serverUploadLimitText,
17-
},
13+
textDirection: this.$opts.textDirection,
14+
translations: {},
1815
translationMap: window.editor_translations,
1916
});
2017

resources/sass/_tinymce.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
display: block;
3131
}
3232

33+
.wysiwyg-input.mce-content-body:before {
34+
padding: 1rem;
35+
top: 4px;
36+
font-style: italic;
37+
color: rgba(34,47,62,.5)
38+
}
39+
3340
// Default styles for our custom root nodes
3441
.page-content.mce-content-body doc-root {
3542
display: block;

resources/views/comments/comment.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
option:page-comment:comment-parent-id="{{ $comment->parent_id }}"
55
option:page-comment:updated-text="{{ trans('entities.comment_updated_success') }}"
66
option:page-comment:deleted-text="{{ trans('entities.comment_deleted_success') }}"
7+
option:page-comment:wysiwyg-language="{{ $locale->htmlLang() }}"
8+
option:page-comment:wysiwyg-text-direction="{{ $locale->htmlDirection() }}"
79
id="comment{{$comment->local_id}}"
810
class="comment-box">
911
<div class="header">

resources/views/comments/comments.blade.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
option:page-comments:page-id="{{ $page->id }}"
33
option:page-comments:created-text="{{ trans('entities.comment_created_success') }}"
44
option:page-comments:count-text="{{ trans('entities.comment_count') }}"
5+
option:page-comments:wysiwyg-language="{{ $locale->htmlLang() }}"
6+
option:page-comments:wysiwyg-text-direction="{{ $locale->htmlDirection() }}"
57
class="comments-list"
68
aria-label="{{ trans('entities.comments') }}">
79

@@ -24,7 +26,6 @@ class="button outline">{{ trans('entities.comment_add') }}</button>
2426

2527
@if(userCan('comment-create-all'))
2628
@include('comments.create')
27-
2829
@if (!$commentTree->empty())
2930
<div refs="page-comments@addButtonContainer" class="text-right">
3031
<button type="button"
@@ -34,4 +35,11 @@ class="button outline">{{ trans('entities.comment_add') }}</button>
3435
@endif
3536
@endif
3637

38+
@if(userCan('comment-create-all') || $commentTree->canUpdateAny())
39+
@push('post-app-scripts')
40+
<script src="{{ versioned_asset('libs/tinymce/tinymce.min.js') }}" nonce="{{ $cspNonce }}"></script>
41+
@include('form.editor-translations')
42+
@endpush
43+
@endif
44+
3745
</section>

resources/views/layouts/base.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ class="@stack('body-class')">
6868
</div>
6969

7070
@yield('bottom')
71+
72+
7173
@if($cspNonce ?? false)
7274
<script src="{{ versioned_asset('dist/app.js') }}" nonce="{{ $cspNonce }}"></script>
7375
@endif
7476
@yield('scripts')
77+
@stack('post-app-scripts')
7578

7679
@include('layouts.parts.base-body-end')
7780
</body>

0 commit comments

Comments
 (0)