@@ -2,6 +2,8 @@ import {Component} from './component';
22import { getLoading , htmlToDom } from '../services/dom.ts' ;
33import { buildForInput } from '../wysiwyg-tinymce/config' ;
44import { Tabs } from "./tabs" ;
5+ import { PageCommentReference } from "./page-comment-reference" ;
6+ import { scrollAndHighlightElement } from "../services/util" ;
57
68export 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}
0 commit comments