1 import {Component} from "./component";
2 import {findTargetNodeAndOffset, hashElement} from "../services/dom";
3 import {el} from "../wysiwyg/utils/dom";
4 import commentIcon from "@icons/comment.svg";
5 import closeIcon from "@icons/close.svg";
6 import {debounce, scrollAndHighlightElement} from "../services/util";
9 * Track the close function for the current open marker so it can be closed
10 * when another is opened so we only show one marker comment thread at one time.
12 let openMarkerClose: Function|null = null;
14 export class PageCommentReference extends Component {
15 protected link: HTMLLinkElement;
16 protected reference: string;
17 protected markerWrap: HTMLElement|null = null;
19 protected viewCommentText: string;
20 protected jumpToThreadText: string;
21 protected closeText: string;
24 this.link = this.$el as HTMLLinkElement;
25 this.reference = this.$opts.reference;
26 this.viewCommentText = this.$opts.viewCommentText;
27 this.jumpToThreadText = this.$opts.jumpToThreadText;
28 this.closeText = this.$opts.closeText;
30 // Show within page display area if seen
31 this.showForDisplay();
33 // Handle editor view to show on comments toolbox view
34 window.addEventListener('editor-toolbox-change', (event) => {
35 const tabName: string = (event as {detail: {tab: string, open: boolean}}).detail.tab;
36 const isOpen = (event as {detail: {tab: string, open: boolean}}).detail.open;
37 if (tabName === 'comments' && isOpen && this.link.checkVisibility()) {
44 // Handle visibility changes within editor toolbox archived details dropdown
45 window.addEventListener('toggle', event => {
46 if (event.target instanceof HTMLElement && event.target.contains(this.link)) {
47 window.requestAnimationFrame(() => {
48 if (this.link.checkVisibility()) {
57 // Handle comments tab changes to hide/show markers & indicators
58 window.addEventListener('tabs-change', event => {
59 const sectionId = (event as {detail: {showing: string}}).detail.showing;
60 if (!sectionId.startsWith('comment-tab-panel')) {
64 const panel = document.getElementById(sectionId);
65 if (panel?.contains(this.link)) {
66 this.showForDisplay();
73 public showForDisplay() {
74 const pageContentArea = document.querySelector('.page-content');
75 if (pageContentArea instanceof HTMLElement && this.link.checkVisibility()) {
76 this.updateMarker(pageContentArea);
80 protected showForEditor() {
81 const contentWrap = document.querySelector('.editor-content-wrap');
82 if (contentWrap instanceof HTMLElement) {
83 this.updateMarker(contentWrap);
86 const onChange = () => {
89 window.$events.remove('editor-html-change', onChange);
93 window.$events.listen('editor-html-change', onChange);
96 protected updateMarker(contentContainer: HTMLElement) {
97 // Reset link and existing marker
98 this.link.classList.remove('outdated', 'missing');
99 if (this.markerWrap) {
100 this.markerWrap.remove();
103 const [refId, refHash, refRange] = this.reference.split(':');
104 const refEl = document.getElementById(refId);
106 this.link.classList.add('outdated', 'missing');
110 const actualHash = hashElement(refEl);
111 if (actualHash !== refHash) {
112 this.link.classList.add('outdated');
115 const marker = el('button', {
117 class: 'content-comment-marker',
118 title: this.viewCommentText,
120 marker.innerHTML = <string>commentIcon;
121 marker.addEventListener('click', event => {
122 this.showCommentAtMarker(marker);
125 this.markerWrap = el('div', {
126 class: 'content-comment-highlight',
129 contentContainer.append(this.markerWrap);
130 this.positionMarker(refEl, refRange);
132 this.link.href = `#${refEl.id}`;
133 this.link.addEventListener('click', (event: MouseEvent) => {
134 event.preventDefault();
135 scrollAndHighlightElement(refEl);
138 const debouncedReposition = debounce(() => {
139 this.positionMarker(refEl, refRange);
140 }, 50, false).bind(this);
141 window.addEventListener('resize', debouncedReposition);
144 protected positionMarker(targetEl: HTMLElement, range: string) {
145 if (!this.markerWrap) {
149 const markerParent = this.markerWrap.parentElement as HTMLElement;
150 const parentBounds = markerParent.getBoundingClientRect();
151 let targetBounds = targetEl.getBoundingClientRect();
152 const [rangeStart, rangeEnd] = range.split('-');
153 if (rangeStart && rangeEnd) {
154 const range = new Range();
155 const relStart = findTargetNodeAndOffset(targetEl, Number(rangeStart));
156 const relEnd = findTargetNodeAndOffset(targetEl, Number(rangeEnd));
157 if (relStart && relEnd) {
158 range.setStart(relStart.node, relStart.offset);
159 range.setEnd(relEnd.node, relEnd.offset);
160 targetBounds = range.getBoundingClientRect();
164 const relLeft = targetBounds.left - parentBounds.left;
165 const relTop = (targetBounds.top - parentBounds.top) + markerParent.scrollTop;
167 this.markerWrap.style.left = `${relLeft}px`;
168 this.markerWrap.style.top = `${relTop}px`;
169 this.markerWrap.style.width = `${targetBounds.width}px`;
170 this.markerWrap.style.height = `${targetBounds.height}px`;
173 public hideMarker() {
174 // Hide marker and close existing marker windows
175 if (openMarkerClose) {
178 this.markerWrap?.remove();
179 this.markerWrap = null;
182 protected showCommentAtMarker(marker: HTMLElement): void {
183 // Hide marker and close existing marker windows
184 if (openMarkerClose) {
187 marker.hidden = true;
189 // Locate relevant comment
190 const commentBox = this.link.closest('.comment-box') as HTMLElement;
192 // Build comment window
193 const readClone = (commentBox.closest('.comment-branch') as HTMLElement).cloneNode(true) as HTMLElement;
194 const toRemove = readClone.querySelectorAll('.actions, form');
195 for (const el of toRemove) {
199 const close = el('button', {type: 'button', title: this.closeText});
200 close.innerHTML = (closeIcon as string);
201 const jump = el('button', {type: 'button', 'data-action': 'jump'}, [this.jumpToThreadText]);
203 const commentWindow = el('div', {
204 class: 'content-comment-window'
207 class: 'content-comment-window-actions',
210 class: 'content-comment-window-content comment-container-compact comment-container-super-compact',
214 marker.parentElement?.append(commentWindow);
216 // Handle interaction within window
217 const closeAction = () => {
218 commentWindow.remove();
219 marker.hidden = false;
220 window.removeEventListener('click', windowCloseAction);
221 openMarkerClose = null;
224 const windowCloseAction = (event: MouseEvent) => {
225 if (!(marker.parentElement as HTMLElement).contains(event.target as HTMLElement)) {
229 window.addEventListener('click', windowCloseAction);
231 openMarkerClose = closeAction;
232 close.addEventListener('click', closeAction.bind(this));
233 jump.addEventListener('click', () => {
235 commentBox.scrollIntoView({behavior: 'smooth'});
236 const highlightTarget = commentBox.querySelector('.header') as HTMLElement;
237 highlightTarget.classList.add('anim-highlight');
238 highlightTarget.addEventListener('animationend', () => highlightTarget.classList.remove('anim-highlight'))
241 // Position window within bounds
242 const commentWindowBounds = commentWindow.getBoundingClientRect();
243 const contentBounds = document.querySelector('.page-content')?.getBoundingClientRect();
244 if (contentBounds && commentWindowBounds.right > contentBounds.right) {
245 const diff = commentWindowBounds.right - contentBounds.right;
246 commentWindow.style.left = `-${diff}px`;