1 import {Component} from './component';
2 import {findTargetNodeAndOffset, getLoading, hashElement, htmlToDom} from '../services/dom.ts';
3 import {buildForInput} from '../wysiwyg-tinymce/config';
4 import {el} from "../wysiwyg/utils/dom";
6 import commentIcon from "@icons/comment.svg"
7 import closeIcon from "@icons/close.svg"
10 * Track the close function for the current open marker so it can be closed
11 * when another is opened so we only show one marker comment thread at one time.
13 let openMarkerClose: Function|null = null;
15 export class PageComment extends Component {
17 protected commentId: string;
18 protected commentLocalId: string;
19 protected commentContentRef: string;
20 protected deletedText: string;
21 protected updatedText: string;
22 protected viewCommentText: string;
23 protected jumpToThreadText: string;
24 protected closeText: string;
26 protected wysiwygEditor: any = null;
27 protected wysiwygLanguage: string;
28 protected wysiwygTextDirection: string;
30 protected container: HTMLElement;
31 protected contentContainer: HTMLElement;
32 protected form: HTMLFormElement;
33 protected formCancel: HTMLElement;
34 protected editButton: HTMLElement;
35 protected deleteButton: HTMLElement;
36 protected replyButton: HTMLElement;
37 protected input: HTMLInputElement;
41 this.commentId = this.$opts.commentId;
42 this.commentLocalId = this.$opts.commentLocalId;
43 this.commentContentRef = this.$opts.commentContentRef;
44 this.deletedText = this.$opts.deletedText;
45 this.updatedText = this.$opts.updatedText;
46 this.viewCommentText = this.$opts.viewCommentText;
47 this.jumpToThreadText = this.$opts.jumpToThreadText;
48 this.closeText = this.$opts.closeText;
50 // Editor reference and text options
51 this.wysiwygLanguage = this.$opts.wysiwygLanguage;
52 this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
55 this.container = this.$el;
56 this.contentContainer = this.$refs.contentContainer;
57 this.form = this.$refs.form as HTMLFormElement;
58 this.formCancel = this.$refs.formCancel;
59 this.editButton = this.$refs.editButton;
60 this.deleteButton = this.$refs.deleteButton;
61 this.replyButton = this.$refs.replyButton;
62 this.input = this.$refs.input as HTMLInputElement;
64 this.setupListeners();
65 this.positionForReference();
68 protected setupListeners(): void {
69 if (this.replyButton) {
70 this.replyButton.addEventListener('click', () => this.$emit('reply', {
71 id: this.commentLocalId,
72 element: this.container,
76 if (this.editButton) {
77 this.editButton.addEventListener('click', this.startEdit.bind(this));
78 this.form.addEventListener('submit', this.update.bind(this));
79 this.formCancel.addEventListener('click', () => this.toggleEditMode(false));
82 if (this.deleteButton) {
83 this.deleteButton.addEventListener('click', this.delete.bind(this));
87 protected toggleEditMode(show: boolean) : void {
88 this.contentContainer.toggleAttribute('hidden', show);
89 this.form.toggleAttribute('hidden', !show);
92 protected startEdit() : void {
93 this.toggleEditMode(true);
95 if (this.wysiwygEditor) {
96 this.wysiwygEditor.focus();
100 const config = buildForInput({
101 language: this.wysiwygLanguage,
102 containerElement: this.input,
103 darkMode: document.documentElement.classList.contains('dark-mode'),
104 textDirection: this.wysiwygTextDirection,
108 translationMap: (window as Record<string, Object>).editor_translations,
111 (window as {tinymce: {init: (Object) => Promise<any>}}).tinymce.init(config).then(editors => {
112 this.wysiwygEditor = editors[0];
113 setTimeout(() => this.wysiwygEditor.focus(), 50);
117 protected async update(event: Event): Promise<void> {
118 event.preventDefault();
119 const loading = this.showLoading();
120 this.form.toggleAttribute('hidden', true);
123 html: this.wysiwygEditor.getContent(),
127 const resp = await window.$http.put(`/comment/${this.commentId}`, reqData);
128 const newComment = htmlToDom(resp.data as string);
129 this.container.replaceWith(newComment);
130 window.$events.success(this.updatedText);
133 window.$events.showValidationErrors(err);
134 this.form.toggleAttribute('hidden', false);
139 protected async delete(): Promise<void> {
142 await window.$http.delete(`/comment/${this.commentId}`);
143 this.$emit('delete');
144 this.container.closest('.comment-branch')?.remove();
145 window.$events.success(this.deletedText);
148 protected showLoading(): HTMLElement {
149 const loading = getLoading();
150 loading.classList.add('px-l');
151 this.container.append(loading);
155 protected positionForReference() {
156 if (!this.commentContentRef) {
160 const [refId, refHash, refRange] = this.commentContentRef.split(':');
161 const refEl = document.getElementById(refId);
163 // TODO - Show outdated marker for comment
167 const actualHash = hashElement(refEl);
168 if (actualHash !== refHash) {
169 // TODO - Show outdated marker for comment
173 const refElBounds = refEl.getBoundingClientRect();
174 let bounds = refElBounds;
175 const [rangeStart, rangeEnd] = refRange.split('-');
176 if (rangeStart && rangeEnd) {
177 const range = new Range();
178 const relStart = findTargetNodeAndOffset(refEl, Number(rangeStart));
179 const relEnd = findTargetNodeAndOffset(refEl, Number(rangeEnd));
180 if (relStart && relEnd) {
181 range.setStart(relStart.node, relStart.offset);
182 range.setEnd(relEnd.node, relEnd.offset);
183 bounds = range.getBoundingClientRect();
187 const relLeft = bounds.left - refElBounds.left;
188 const relTop = bounds.top - refElBounds.top;
190 const marker = el('button', {
192 class: 'content-comment-marker',
193 title: this.viewCommentText,
195 marker.innerHTML = <string>commentIcon;
196 marker.addEventListener('click', event => {
197 this.showCommentAtMarker(marker);
200 const markerWrap = el('div', {
201 class: 'content-comment-highlight',
202 style: `left: ${relLeft}px; top: ${relTop}px; width: ${bounds.width}px; height: ${bounds.height}px;`
205 refEl.style.position = 'relative';
206 refEl.append(markerWrap);
209 protected showCommentAtMarker(marker: HTMLElement): void {
210 // Hide marker and close existing marker windows
211 if (openMarkerClose) {
214 marker.hidden = true;
216 // Build comment window
217 const readClone = (this.container.closest('.comment-branch') as HTMLElement).cloneNode(true) as HTMLElement;
218 const toRemove = readClone.querySelectorAll('.actions, form');
219 for (const el of toRemove) {
223 const close = el('button', {type: 'button', title: this.closeText});
224 close.innerHTML = (closeIcon as string);
225 const jump = el('button', {type: 'button', 'data-action': 'jump'}, [this.jumpToThreadText]);
227 const commentWindow = el('div', {
228 class: 'content-comment-window'
231 class: 'content-comment-window-actions',
234 class: 'content-comment-window-content comment-container-compact comment-container-super-compact',
238 marker.parentElement?.append(commentWindow);
240 // Handle interaction within window
241 const closeAction = () => {
242 commentWindow.remove();
243 marker.hidden = false;
244 window.removeEventListener('click', windowCloseAction);
245 openMarkerClose = null;
248 const windowCloseAction = (event: MouseEvent) => {
249 if (!(marker.parentElement as HTMLElement).contains(event.target as HTMLElement)) {
253 window.addEventListener('click', windowCloseAction);
255 openMarkerClose = closeAction;
256 close.addEventListener('click', closeAction.bind(this));
257 jump.addEventListener('click', () => {
259 this.container.scrollIntoView({behavior: 'smooth'});
260 const highlightTarget = this.container.querySelector('.header') as HTMLElement;
261 highlightTarget.classList.add('anim-highlight');
262 highlightTarget.addEventListener('animationend', () => highlightTarget.classList.remove('anim-highlight'))
265 // Position window within bounds
266 const commentWindowBounds = commentWindow.getBoundingClientRect();
267 const contentBounds = document.querySelector('.page-content')?.getBoundingClientRect();
268 if (contentBounds && commentWindowBounds.right > contentBounds.right) {
269 const diff = commentWindowBounds.right - contentBounds.right;
270 commentWindow.style.left = `-${diff}px`;