Debian Codegraph @

extensions/DiscussionTools/modules/CommentItem.js

raw symbols SCIP
1 const ThreadItem = require( './ThreadItem.js' ),
2 moment = require( './lib/moment-timezone/moment-timezone-with-data-1970-2030.js' );
3
4 /**
5 * A comment item
6 *
7 * @class CommentItem
8 * @extends ThreadItem
9 * @constructor
10 * @param {number} level
11 * @param {Object} range
12 * @param {Object[]} [signatureRanges] Objects describing the extent of signatures (plus
13 * timestamps) for this comment. There is always at least one signature, but there may be
14 * multiple. The author and timestamp of the comment is determined from the first signature.
15 * @param {Object[]} [timestampRanges] Objects describing the extent of timestamps within
16 * the above signatures.
17 * @param {moment} [timestamp] Timestamp (Moment object)
18 * @param {string} [author] Comment author's username
19 * @param {string|null} [displayName] Comment author's display name
20 */
21 function CommentItem( level, range, signatureRanges, timestampRanges, timestamp, author, displayName ) {
22 // Parent constructor
23 CommentItem.super.call( this, 'comment', level, range );
24
25 this.signatureRanges = signatureRanges || [];
26 this.timestampRanges = timestampRanges || [];
27 this.timestamp = timestamp || null;
28 this.author = author || null;
29 this.displayName = displayName || null;
30
31 /**
32 * @member {ThreadItem} Parent thread item
33 */
34 this.parent = null;
35 }
36
37 OO.inheritClass( CommentItem, ThreadItem );
38
39 /**
40 * Get the comment timestamp in the format used in IDs and names.
41 *
42 * Depending on the date of the comment, this may use one of two formats:
43 *
44 * - For dates prior to 'DiscussionToolsTimestampFormatSwitchTime' (by default 2022-07-12):
45 * Uses ISO 8601 date. Almost DateTimeInterface::RFC3339_EXTENDED, but ending with 'Z' instead
46 * of '+00:00', like Date#toISOString in JavaScript.
47 *
48 * - For dates on or after 'DiscussionToolsTimestampFormatSwitchTime' (by default 2022-07-12):
49 * Uses MediaWiki timestamp (TS_MW in MediaWiki PHP code).
50 *
51 * @return {string} Comment timestamp in standard format
52 */
53 CommentItem.prototype.getTimestampString = function () {
54 const dtConfig = require( './config.json' );
55 const switchTime = moment.utc( dtConfig.switchTime );
56 if ( this.timestamp < switchTime ) {
57 return this.timestamp.utc().toISOString();
58 } else {
59 // Switch to English locale to avoid number formatting
60 return this.timestamp.utc().locale( 'en' ).format( 'YYYYMMDDHHmmss' );
61 }
62 };
63
64 /**
65 * @return {Range} Range of the comment's "body"
66 */
67 CommentItem.prototype.getBodyRange = function () {
68 if ( !this.signatureRanges.length ) {
69 return this.getRange();
70 }
71 // Exclude last signature from body
72 const lastSignature = this.signatureRanges[ this.signatureRanges.length - 1 ];
73 const doc = this.range.startContainer.ownerDocument;
74 const nativeRange = doc.createRange();
75 nativeRange.setStart( this.range.startContainer, this.range.startOffset );
76 // Subtract 1 from the end offset to exclude the signature marker itself
77 nativeRange.setEnd( lastSignature.startContainer, lastSignature.startOffset - 1 );
78 return nativeRange;
79 };
80
81 /**
82 * @return {HeadingItem} Closest ancestor which is a HeadingItem
83 */
84 CommentItem.prototype.getHeading = function () {
85 let parent = this;
86 while ( parent && parent.type !== 'heading' ) {
87 parent = parent.parent;
88 }
89 return parent;
90 };
91
92 /**
93 * @return {HeadingItem|null} Closest heading that can be used for topic subscriptions
94 */
95 CommentItem.prototype.getSubscribableHeading = function () {
96 let heading = this.getHeading();
97 while ( heading && heading.type === 'heading' && !heading.isSubscribable() ) {
98 heading = heading.parent;
99 }
100 return ( heading && heading.type === 'heading' ) ? heading : null;
101 };
102
103 // TODO: Implement getBodyRange/getBodyHTML/getBodyText/getMentions if required
104
105 module.exports = CommentItem;