Skip to content

Commit 15c79c3

Browse files
committed
Comments: Addressed a range of edge cases and ux issues for references
Handles only display and handling references when they're in the active tab, while handling proper removal when made not visible.
1 parent e7dcc2d commit 15c79c3

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {findTargetNodeAndOffset, hashElement} from "../services/dom";
33
import {el} from "../wysiwyg/utils/dom";
44
import commentIcon from "@icons/comment.svg";
55
import closeIcon from "@icons/close.svg";
6-
import {scrollAndHighlightElement} from "../services/util";
6+
import {debounce, scrollAndHighlightElement} from "../services/util";
77

88
/**
99
* Track the close function for the current open marker so it can be closed
@@ -29,7 +29,7 @@ export class PageCommentReference extends Component {
2929

3030
// Show within page display area if seen
3131
const pageContentArea = document.querySelector('.page-content');
32-
if (pageContentArea instanceof HTMLElement) {
32+
if (pageContentArea instanceof HTMLElement && this.link.checkVisibility()) {
3333
this.updateMarker(pageContentArea);
3434
}
3535

@@ -43,6 +43,21 @@ export class PageCommentReference extends Component {
4343
this.hideMarker();
4444
}
4545
});
46+
47+
// Handle comments tab changes to hide/show markers & indicators
48+
window.addEventListener('tabs-change', event => {
49+
const sectionId = (event as {detail: {showing: string}}).detail.showing;
50+
if (!sectionId.startsWith('comment-tab-panel') || !(pageContentArea instanceof HTMLElement)) {
51+
return;
52+
}
53+
54+
const panel = document.getElementById(sectionId);
55+
if (panel?.contains(this.link)) {
56+
this.updateMarker(pageContentArea);
57+
} else {
58+
this.hideMarker();
59+
}
60+
});
4661
}
4762

4863
protected showForEditor() {
@@ -111,9 +126,10 @@ export class PageCommentReference extends Component {
111126
scrollAndHighlightElement(refEl);
112127
});
113128

114-
window.addEventListener('resize', () => {
129+
const debouncedReposition = debounce(() => {
115130
this.positionMarker(refEl, refRange);
116-
});
131+
}, 50, false).bind(this);
132+
window.addEventListener('resize', debouncedReposition);
117133
}
118134

119135
protected positionMarker(targetEl: HTMLElement, range: string) {
@@ -145,12 +161,13 @@ export class PageCommentReference extends Component {
145161
this.markerWrap.style.height = `${targetBounds.height}px`;
146162
}
147163

148-
protected hideMarker() {
164+
public hideMarker() {
149165
// Hide marker and close existing marker windows
150166
if (openMarkerClose) {
151167
openMarkerClose();
152168
}
153169
this.markerWrap?.remove();
170+
this.markerWrap = null;
154171
}
155172

156173
protected showCommentAtMarker(marker: HTMLElement): void {

resources/js/components/page-comment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component} from './component';
22
import {getLoading, htmlToDom} from '../services/dom.ts';
33
import {buildForInput} from '../wysiwyg-tinymce/config';
4+
import {PageCommentReference} from "./page-comment-reference";
45

56
export class PageComment extends Component {
67

@@ -142,7 +143,13 @@ export class PageComment extends Component {
142143
const response = await window.$http.put(`/comment/${this.commentId}/${action}`);
143144
window.$events.success(this.archiveText);
144145
this.$emit(action, {new_thread_dom: htmlToDom(response.data as string)});
145-
this.container.closest('.comment-branch')?.remove();
146+
147+
const branch = this.container.closest('.comment-branch') as HTMLElement;
148+
const references = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
149+
for (const reference of references) {
150+
reference.hideMarker();
151+
}
152+
branch.remove();
146153
}
147154

148155
protected showLoading(): HTMLElement {

resources/js/services/components.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export class ComponentStore {
139139
/**
140140
* Get all the components of the given name.
141141
*/
142-
public get(name: string): Component[] {
143-
return this.components[name] || [];
142+
public get<T extends Component>(name: string): T[] {
143+
return (this.components[name] || []) as T[];
144144
}
145145

146146
/**
@@ -150,4 +150,9 @@ export class ComponentStore {
150150
const elComponents = this.elementComponentMap.get(element) || {};
151151
return elComponents[name] || null;
152152
}
153+
154+
public allWithinElement<T extends Component>(element: HTMLElement, name: string): T[] {
155+
const components = this.get<T>(name);
156+
return components.filter(c => element.contains(c.$el));
157+
}
153158
}

0 commit comments

Comments
 (0)