Skip to content

Commit c82fa33

Browse files
committed
Comments: Further range of content reference ux improvements
- Added reference indicator to comment create form. - Added remove action. - Extracted reference text to translations. - Changed reference hash to be text-based instead of HTML based. - Added reference display for newly added comments. - Handled reference marker delete on comment delete.
1 parent 15c79c3 commit c82fa33

File tree

9 files changed

+74
-28
lines changed

9 files changed

+74
-28
lines changed

lang/en/entities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@
410410
'comment_jump_to_thread' => 'Jump to thread',
411411
'comment_delete_confirm' => 'Are you sure you want to delete this comment?',
412412
'comment_in_reply_to' => 'In reply to :commentId',
413+
'comment_reference' => 'Reference',
414+
'comment_reference_outdated' => '(Outdated)',
413415
'comment_editor_explain' => 'Here are the comments that have been left on this page. Comments can be added & managed when viewing the saved page.',
414416

415417
// Revision

resources/js/components/page-comment-reference.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ export class PageCommentReference extends Component {
2828
this.closeText = this.$opts.closeText;
2929

3030
// Show within page display area if seen
31-
const pageContentArea = document.querySelector('.page-content');
32-
if (pageContentArea instanceof HTMLElement && this.link.checkVisibility()) {
33-
this.updateMarker(pageContentArea);
34-
}
31+
this.showForDisplay();
3532

3633
// Handle editor view to show on comments toolbox view
3734
window.addEventListener('editor-toolbox-change', (event) => {
@@ -47,19 +44,26 @@ export class PageCommentReference extends Component {
4744
// Handle comments tab changes to hide/show markers & indicators
4845
window.addEventListener('tabs-change', event => {
4946
const sectionId = (event as {detail: {showing: string}}).detail.showing;
50-
if (!sectionId.startsWith('comment-tab-panel') || !(pageContentArea instanceof HTMLElement)) {
47+
if (!sectionId.startsWith('comment-tab-panel')) {
5148
return;
5249
}
5350

5451
const panel = document.getElementById(sectionId);
5552
if (panel?.contains(this.link)) {
56-
this.updateMarker(pageContentArea);
53+
this.showForDisplay();
5754
} else {
5855
this.hideMarker();
5956
}
6057
});
6158
}
6259

60+
public showForDisplay() {
61+
const pageContentArea = document.querySelector('.page-content');
62+
if (pageContentArea instanceof HTMLElement && this.link.checkVisibility()) {
63+
this.updateMarker(pageContentArea);
64+
}
65+
}
66+
6367
protected showForEditor() {
6468
const contentWrap = document.querySelector('.editor-content-wrap');
6569
if (contentWrap instanceof HTMLElement) {
@@ -90,15 +94,7 @@ export class PageCommentReference extends Component {
9094
return;
9195
}
9296

93-
const refCloneToAssess = refEl.cloneNode(true) as HTMLElement;
94-
const toRemove = refCloneToAssess.querySelectorAll('[data-lexical-text]');
95-
refCloneToAssess.removeAttribute('style');
96-
for (const el of toRemove) {
97-
el.after(...el.childNodes);
98-
el.remove();
99-
}
100-
101-
const actualHash = hashElement(refCloneToAssess);
97+
const actualHash = hashElement(refEl);
10298
if (actualHash !== refHash) {
10399
this.link.classList.add('outdated');
104100
}

resources/js/components/page-comment.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ export class PageComment extends Component {
131131

132132
await window.$http.delete(`/comment/${this.commentId}`);
133133
this.$emit('delete');
134-
this.container.closest('.comment-branch')?.remove();
134+
135+
const branch = this.container.closest('.comment-branch');
136+
if (branch instanceof HTMLElement) {
137+
const refs = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
138+
for (const ref of refs) {
139+
ref.hideMarker();
140+
}
141+
branch.remove();
142+
}
143+
135144
window.$events.success(this.deletedText);
136145
}
137146

resources/js/components/page-comments.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Component} from './component';
22
import {getLoading, htmlToDom} from '../services/dom.ts';
33
import {buildForInput} from '../wysiwyg-tinymce/config';
44
import {Tabs} from "./tabs";
5+
import {PageCommentReference} from "./page-comment-reference";
6+
import {scrollAndHighlightElement} from "../services/util";
57

68
export interface CommentReplyEvent extends Event {
79
detail: {
@@ -27,13 +29,16 @@ export class PageComments extends Component {
2729
private addButtonContainer: HTMLElement;
2830
private archiveContainer: HTMLElement;
2931
private replyToRow: HTMLElement;
32+
private referenceRow: HTMLElement;
3033
private formContainer: HTMLElement;
3134
private form: HTMLFormElement;
3235
private formInput: HTMLInputElement;
3336
private formReplyLink: HTMLAnchorElement;
37+
private formReferenceLink: HTMLAnchorElement;
3438
private addCommentButton: HTMLElement;
3539
private hideFormButton: HTMLElement;
3640
private removeReplyToButton: HTMLElement;
41+
private removeReferenceButton: HTMLElement;
3742
private wysiwygLanguage: string;
3843
private wysiwygTextDirection: string;
3944
private wysiwygEditor: any = null;
@@ -56,13 +61,16 @@ export class PageComments extends Component {
5661
this.addButtonContainer = this.$refs.addButtonContainer;
5762
this.archiveContainer = this.$refs.archiveContainer;
5863
this.replyToRow = this.$refs.replyToRow;
64+
this.referenceRow = this.$refs.referenceRow;
5965
this.formContainer = this.$refs.formContainer;
6066
this.form = this.$refs.form as HTMLFormElement;
6167
this.formInput = this.$refs.formInput as HTMLInputElement;
6268
this.formReplyLink = this.$refs.formReplyLink as HTMLAnchorElement;
69+
this.formReferenceLink = this.$refs.formReferenceLink as HTMLAnchorElement;
6370
this.addCommentButton = this.$refs.addCommentButton;
6471
this.hideFormButton = this.$refs.hideFormButton;
6572
this.removeReplyToButton = this.$refs.removeReplyToButton;
73+
this.removeReferenceButton = this.$refs.removeReferenceButton;
6674

6775
// WYSIWYG options
6876
this.wysiwygLanguage = this.$opts.wysiwygLanguage;
@@ -100,6 +108,7 @@ export class PageComments extends Component {
100108

101109
if (this.form) {
102110
this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
111+
this.removeReferenceButton.addEventListener('click', () => this.setContentReference(''));
103112
this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
104113
this.addCommentButton.addEventListener('click', this.showForm.bind(this));
105114
this.form.addEventListener('submit', this.saveComment.bind(this));
@@ -118,7 +127,7 @@ export class PageComments extends Component {
118127
const reqData = {
119128
html: this.wysiwygEditor.getContent(),
120129
parent_id: this.parentId || null,
121-
content_ref: this.contentReference || '',
130+
content_ref: this.contentReference,
122131
};
123132

124133
window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
@@ -130,6 +139,11 @@ export class PageComments extends Component {
130139
this.container.append(newElem);
131140
}
132141

142+
const refs = window.$components.allWithinElement<PageCommentReference>(newElem, 'page-comment-reference');
143+
for (const ref of refs) {
144+
ref.showForDisplay();
145+
}
146+
133147
window.$events.success(this.createdText);
134148
this.hideForm();
135149
this.updateCount();
@@ -152,10 +166,8 @@ export class PageComments extends Component {
152166
protected resetForm(): void {
153167
this.removeEditor();
154168
this.formInput.value = '';
155-
this.parentId = null;
156-
this.contentReference = '';
157-
this.replyToRow.toggleAttribute('hidden', true);
158-
this.container.append(this.formContainer);
169+
this.setContentReference('');
170+
this.removeReplyTo();
159171
}
160172

161173
protected showForm(): void {
@@ -240,7 +252,21 @@ export class PageComments extends Component {
240252

241253
public startNewComment(contentReference: string): void {
242254
this.removeReplyTo();
243-
this.contentReference = contentReference;
255+
this.setContentReference(contentReference);
256+
}
257+
258+
protected setContentReference(reference: string): void {
259+
this.contentReference = reference;
260+
this.referenceRow.toggleAttribute('hidden', !Boolean(reference));
261+
const [id] = reference.split(':');
262+
this.formReferenceLink.href = `#${id}`;
263+
this.formReferenceLink.onclick = function(event) {
264+
event.preventDefault();
265+
const el = document.getElementById(id);
266+
if (el) {
267+
scrollAndHighlightElement(el);
268+
}
269+
};
244270
}
245271

246272
}

resources/js/services/dom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ export function findTargetNodeAndOffset(parentNode: HTMLElement, offset: number)
251251
}
252252

253253
/**
254-
* Create a hash for the given HTML element.
254+
* Create a hash for the given HTML element content.
255255
*/
256256
export function hashElement(element: HTMLElement): string {
257-
const normalisedElemHtml = element.outerHTML.replace(/\s{2,}/g, '');
258-
return cyrb53(normalisedElemHtml);
257+
const normalisedElemText = (element.textContent || '').replace(/\s{2,}/g, '');
258+
return cyrb53(normalisedElemText);
259259
}

resources/sass/_components.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
569569
border-bottom: 0;
570570
padding: 0 vars.$xs;
571571
}
572+
.tab-container [role="tabpanel"].no-outline:focus {
573+
outline: none;
574+
}
572575

573576
.image-picker .none {
574577
display: none;

resources/views/comments/comment.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class="text-button text-muted hover-underline text-small p-xs">@icon('archive')
8787
option:page-comment-reference:view-comment-text="{{ trans('entities.comment_view') }}"
8888
option:page-comment-reference:jump-to-thread-text="{{ trans('entities.comment_jump_to_thread') }}"
8989
option:page-comment-reference:close-text="{{ trans('common.close') }}"
90-
href="#">@icon('bookmark')Reference <span>- Outdated</span></a>
90+
href="#">@icon('bookmark'){{ trans('entities.comment_reference') }} <span>{{ trans('entities.comment_reference_outdated') }}</span></a>
9191
</div>
9292
@endif
9393
{!! $commentHtml !!}

resources/views/comments/comments.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class="button outline mb-m">{{ trans('entities.comment_add') }}</button>
3636
tabindex="0"
3737
role="tabpanel"
3838
aria-labelledby="comment-tab-active"
39-
class="comment-container">
39+
class="comment-container no-outline">
4040
<div refs="page-comments@comment-container">
4141
@foreach($commentTree->getActive() as $branch)
4242
@include('comments.comment-branch', ['branch' => $branch, 'readOnly' => false])
@@ -63,7 +63,7 @@ class="button outline ml-auto">{{ trans('entities.comment_add') }}</button>
6363
role="tabpanel"
6464
aria-labelledby="comment-tab-archived"
6565
hidden="hidden"
66-
class="comment-container">
66+
class="comment-container no-outline">
6767
@foreach($commentTree->getArchived() as $branch)
6868
@include('comments.comment-branch', ['branch' => $branch, 'readOnly' => false])
6969
@endforeach

resources/views/comments/create.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
</div>
1313
</div>
1414
</div>
15+
<div refs="page-comments@reference-row" hidden class="primary-background-light text-muted px-s py-xs">
16+
<div class="grid left-focus v-center">
17+
<div>
18+
<a refs="page-comments@formReferenceLink" href="#">{{ trans('entities.comment_reference') }}</a>
19+
</div>
20+
<div class="text-right">
21+
<button refs="page-comments@remove-reference-button" class="text-button">{{ trans('common.remove') }}</button>
22+
</div>
23+
</div>
24+
</div>
1525

1626
<div class="content px-s pt-s">
1727
<form refs="page-comments@form" novalidate>

0 commit comments

Comments
 (0)