Add getHeading method to CommentItem

Change-Id: I14ccdbb432e835fe74760a52f07de172df2aba27
This commit is contained in:
Ed Sanders 2020-06-29 14:30:47 +01:00
parent ed70d49285
commit 94511c6bad
3 changed files with 32 additions and 7 deletions

View file

@ -2,6 +2,8 @@
namespace MediaWiki\Extension\DiscussionTools; namespace MediaWiki\Extension\DiscussionTools;
use MWException;
class CommentItem extends ThreadItem { class CommentItem extends ThreadItem {
private $signatureRanges; private $signatureRanges;
private $timestamp; private $timestamp;
@ -58,6 +60,20 @@ class CommentItem extends ThreadItem {
return $this->parent; return $this->parent;
} }
/**
* @return HeadingItem Closest ancestor which is a HeadingItem
*/
public function getHeading() : HeadingItem {
$parent = $this;
while ( $parent instanceof CommentItem ) {
$parent = $parent->getParent();
}
if ( !( $parent instanceof HeadingItem ) ) {
throw new MWException( 'heading parent not found' );
}
return $parent;
}
/** /**
* @return string[] Comment warnings * @return string[] Comment warnings
*/ */

View file

@ -273,7 +273,7 @@ CommentController.prototype.postReply = function ( comment ) {
}; };
CommentController.prototype.save = function ( parsoidData ) { CommentController.prototype.save = function ( parsoidData ) {
var root, summaryPrefix, summary, savePromise, var heading, summaryPrefix, summary, savePromise,
mode = this.replyWidget.getMode(), mode = this.replyWidget.getMode(),
comment = parsoidData.comment, comment = parsoidData.comment,
pageData = parsoidData.pageData, pageData = parsoidData.pageData,
@ -282,15 +282,12 @@ CommentController.prototype.save = function ( parsoidData ) {
// Update the Parsoid DOM // Update the Parsoid DOM
this.postReply( parsoidData.comment ); this.postReply( parsoidData.comment );
root = comment; heading = comment.getHeading();
while ( root && root.type !== 'heading' ) { if ( heading.placeholderHeading ) {
root = root.parent;
}
if ( root.placeholderHeading ) {
// This comment is in 0th section, there's no section title for the edit summary // This comment is in 0th section, there's no section title for the edit summary
summaryPrefix = ''; summaryPrefix = '';
} else { } else {
summaryPrefix = '/* ' + root.range.startContainer.innerText + ' */ '; summaryPrefix = '/* ' + heading.range.startContainer.innerText + ' */ ';
} }
summary = summaryPrefix + mw.msg( 'discussiontools-defaultsummary-reply' ); summary = summaryPrefix + mw.msg( 'discussiontools-defaultsummary-reply' );

View file

@ -2,6 +2,7 @@ var ThreadItem = require( './ThreadItem.js' );
/** /**
* @external moment * @external moment
* @external HeadingItem
*/ */
/** /**
@ -41,4 +42,15 @@ function CommentItem( level, range, signatureRanges, timestamp, author ) {
OO.inheritClass( CommentItem, ThreadItem ); OO.inheritClass( CommentItem, ThreadItem );
/**
* @return {HeadingItem} Closest ancestor which is a HeadingItem
*/
CommentItem.prototype.getHeading = function () {
var parent = this;
while ( parent && parent.type !== 'heading' ) {
parent = parent.parent;
}
return parent;
};
module.exports = CommentItem; module.exports = CommentItem;