@@ -2,8 +2,38 @@ import {Component} from './component';
22import { getLoading , htmlToDom } from '../services/dom.ts' ;
33import { buildForInput } from '../wysiwyg-tinymce/config' ;
44
5+ export interface CommentReplyEvent extends Event {
6+ detail : {
7+ id : string ; // ID of comment being replied to
8+ element : HTMLElement ; // Container for comment replied to
9+ }
10+ }
11+
512export class PageComments extends Component {
613
14+ private elem : HTMLElement ;
15+ private pageId : number ;
16+ private container : HTMLElement ;
17+ private commentCountBar : HTMLElement ;
18+ private commentsTitle : HTMLElement ;
19+ private addButtonContainer : HTMLElement ;
20+ private replyToRow : HTMLElement ;
21+ private formContainer : HTMLElement ;
22+ private form : HTMLFormElement ;
23+ private formInput : HTMLInputElement ;
24+ private formReplyLink : HTMLAnchorElement ;
25+ private addCommentButton : HTMLElement ;
26+ private hideFormButton : HTMLElement ;
27+ private removeReplyToButton : HTMLElement ;
28+ private wysiwygLanguage : string ;
29+ private wysiwygTextDirection : string ;
30+ private wysiwygEditor : any = null ;
31+ private createdText : string ;
32+ private countText : string ;
33+ private parentId : number | null = null ;
34+ private contentReference : string = '' ;
35+ private formReplyText : string = '' ;
36+
737 setup ( ) {
838 this . elem = this . $el ;
939 this . pageId = Number ( this . $opts . pageId ) ;
@@ -15,36 +45,33 @@ export class PageComments extends Component {
1545 this . addButtonContainer = this . $refs . addButtonContainer ;
1646 this . replyToRow = this . $refs . replyToRow ;
1747 this . formContainer = this . $refs . formContainer ;
18- this . form = this . $refs . form ;
19- this . formInput = this . $refs . formInput ;
20- this . formReplyLink = this . $refs . formReplyLink ;
48+ this . form = this . $refs . form as HTMLFormElement ;
49+ this . formInput = this . $refs . formInput as HTMLInputElement ;
50+ this . formReplyLink = this . $refs . formReplyLink as HTMLAnchorElement ;
2151 this . addCommentButton = this . $refs . addCommentButton ;
2252 this . hideFormButton = this . $refs . hideFormButton ;
2353 this . removeReplyToButton = this . $refs . removeReplyToButton ;
2454
2555 // WYSIWYG options
2656 this . wysiwygLanguage = this . $opts . wysiwygLanguage ;
2757 this . wysiwygTextDirection = this . $opts . wysiwygTextDirection ;
28- this . wysiwygEditor = null ;
2958
3059 // Translations
3160 this . createdText = this . $opts . createdText ;
3261 this . countText = this . $opts . countText ;
3362
34- // Internal State
35- this . parentId = null ;
3663 this . formReplyText = this . formReplyLink ?. textContent || '' ;
3764
3865 this . setupListeners ( ) ;
3966 }
4067
41- setupListeners ( ) {
68+ protected setupListeners ( ) : void {
4269 this . elem . addEventListener ( 'page-comment-delete' , ( ) => {
4370 setTimeout ( ( ) => this . updateCount ( ) , 1 ) ;
4471 this . hideForm ( ) ;
4572 } ) ;
4673
47- this . elem . addEventListener ( 'page-comment-reply' , event => {
74+ this . elem . addEventListener ( 'page-comment-reply' , ( event : CommentReplyEvent ) => {
4875 this . setReply ( event . detail . id , event . detail . element ) ;
4976 } ) ;
5077
@@ -56,7 +83,7 @@ export class PageComments extends Component {
5683 }
5784 }
5885
59- saveComment ( event ) {
86+ protected saveComment ( event ) : void {
6087 event . preventDefault ( ) ;
6188 event . stopPropagation ( ) ;
6289
@@ -68,10 +95,11 @@ export class PageComments extends Component {
6895 const reqData = {
6996 html : this . wysiwygEditor . getContent ( ) ,
7097 parent_id : this . parentId || null ,
98+ content_reference : this . contentReference || '' ,
7199 } ;
72100
73101 window . $http . post ( `/comment/${ this . pageId } ` , reqData ) . then ( resp => {
74- const newElem = htmlToDom ( resp . data ) ;
102+ const newElem = htmlToDom ( resp . data as string ) ;
75103
76104 if ( reqData . parent_id ) {
77105 this . formContainer . after ( newElem ) ;
@@ -91,28 +119,29 @@ export class PageComments extends Component {
91119 loading . remove ( ) ;
92120 }
93121
94- updateCount ( ) {
122+ protected updateCount ( ) : void {
95123 const count = this . getCommentCount ( ) ;
96- this . commentsTitle . textContent = window . $trans . choice ( this . countText , count , { count } ) ;
124+ this . commentsTitle . textContent = window . $trans . choice ( this . countText , count ) ;
97125 }
98126
99- resetForm ( ) {
127+ protected resetForm ( ) : void {
100128 this . removeEditor ( ) ;
101129 this . formInput . value = '' ;
102130 this . parentId = null ;
131+ this . contentReference = '' ;
103132 this . replyToRow . toggleAttribute ( 'hidden' , true ) ;
104133 this . container . append ( this . formContainer ) ;
105134 }
106135
107- showForm ( ) {
136+ protected showForm ( ) : void {
108137 this . removeEditor ( ) ;
109138 this . formContainer . toggleAttribute ( 'hidden' , false ) ;
110139 this . addButtonContainer . toggleAttribute ( 'hidden' , true ) ;
111140 this . formContainer . scrollIntoView ( { behavior : 'smooth' , block : 'nearest' } ) ;
112141 this . loadEditor ( ) ;
113142 }
114143
115- hideForm ( ) {
144+ protected hideForm ( ) : void {
116145 this . resetForm ( ) ;
117146 this . formContainer . toggleAttribute ( 'hidden' , true ) ;
118147 if ( this . getCommentCount ( ) > 0 ) {
@@ -123,7 +152,7 @@ export class PageComments extends Component {
123152 this . addButtonContainer . toggleAttribute ( 'hidden' , false ) ;
124153 }
125154
126- loadEditor ( ) {
155+ protected loadEditor ( ) : void {
127156 if ( this . wysiwygEditor ) {
128157 this . wysiwygEditor . focus ( ) ;
129158 return ;
@@ -134,42 +163,49 @@ export class PageComments extends Component {
134163 containerElement : this . formInput ,
135164 darkMode : document . documentElement . classList . contains ( 'dark-mode' ) ,
136165 textDirection : this . wysiwygTextDirection ,
166+ drawioUrl : '' ,
167+ pageId : 0 ,
137168 translations : { } ,
138- translationMap : window . editor_translations ,
169+ translationMap : ( window as Record < string , Object > ) . editor_translations ,
139170 } ) ;
140171
141- window . tinymce . init ( config ) . then ( editors => {
172+ ( window as { tinymce : { init : ( Object ) => Promise < any > } } ) . tinymce . init ( config ) . then ( editors => {
142173 this . wysiwygEditor = editors [ 0 ] ;
143174 setTimeout ( ( ) => this . wysiwygEditor . focus ( ) , 50 ) ;
144175 } ) ;
145176 }
146177
147- removeEditor ( ) {
178+ protected removeEditor ( ) : void {
148179 if ( this . wysiwygEditor ) {
149180 this . wysiwygEditor . remove ( ) ;
150181 this . wysiwygEditor = null ;
151182 }
152183 }
153184
154- getCommentCount ( ) {
185+ protected getCommentCount ( ) : number {
155186 return this . container . querySelectorAll ( '[component="page-comment"]' ) . length ;
156187 }
157188
158- setReply ( commentLocalId , commentElement ) {
189+ protected setReply ( commentLocalId , commentElement ) : void {
159190 const targetFormLocation = commentElement . closest ( '.comment-branch' ) . querySelector ( '.comment-branch-children' ) ;
160191 targetFormLocation . append ( this . formContainer ) ;
161192 this . showForm ( ) ;
162193 this . parentId = commentLocalId ;
163194 this . replyToRow . toggleAttribute ( 'hidden' , false ) ;
164- this . formReplyLink . textContent = this . formReplyText . replace ( '1234' , this . parentId ) ;
195+ this . formReplyLink . textContent = this . formReplyText . replace ( '1234' , String ( this . parentId ) ) ;
165196 this . formReplyLink . href = `#comment${ this . parentId } ` ;
166197 }
167198
168- removeReplyTo ( ) {
199+ protected removeReplyTo ( ) : void {
169200 this . parentId = null ;
170201 this . replyToRow . toggleAttribute ( 'hidden' , true ) ;
171202 this . container . append ( this . formContainer ) ;
172203 this . showForm ( ) ;
173204 }
174205
206+ public startNewComment ( contentReference : string ) : void {
207+ this . removeReplyTo ( ) ;
208+ this . contentReference = contentReference ;
209+ }
210+
175211}
0 commit comments