From 94511c6badb864652695a7e572d37e53eced992b Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 29 Jun 2020 14:30:47 +0100 Subject: [PATCH] Add getHeading method to CommentItem Change-Id: I14ccdbb432e835fe74760a52f07de172df2aba27 --- includes/CommentItem.php | 16 ++++++++++++++++ modules/CommentController.js | 11 ++++------- modules/CommentItem.js | 12 ++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/includes/CommentItem.php b/includes/CommentItem.php index 1bce94e06..37cdccb21 100644 --- a/includes/CommentItem.php +++ b/includes/CommentItem.php @@ -2,6 +2,8 @@ namespace MediaWiki\Extension\DiscussionTools; +use MWException; + class CommentItem extends ThreadItem { private $signatureRanges; private $timestamp; @@ -58,6 +60,20 @@ class CommentItem extends ThreadItem { 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 */ diff --git a/modules/CommentController.js b/modules/CommentController.js index a5970365d..135659705 100644 --- a/modules/CommentController.js +++ b/modules/CommentController.js @@ -273,7 +273,7 @@ CommentController.prototype.postReply = function ( comment ) { }; CommentController.prototype.save = function ( parsoidData ) { - var root, summaryPrefix, summary, savePromise, + var heading, summaryPrefix, summary, savePromise, mode = this.replyWidget.getMode(), comment = parsoidData.comment, pageData = parsoidData.pageData, @@ -282,15 +282,12 @@ CommentController.prototype.save = function ( parsoidData ) { // Update the Parsoid DOM this.postReply( parsoidData.comment ); - root = comment; - while ( root && root.type !== 'heading' ) { - root = root.parent; - } - if ( root.placeholderHeading ) { + heading = comment.getHeading(); + if ( heading.placeholderHeading ) { // This comment is in 0th section, there's no section title for the edit summary summaryPrefix = ''; } else { - summaryPrefix = '/* ' + root.range.startContainer.innerText + ' */ '; + summaryPrefix = '/* ' + heading.range.startContainer.innerText + ' */ '; } summary = summaryPrefix + mw.msg( 'discussiontools-defaultsummary-reply' ); diff --git a/modules/CommentItem.js b/modules/CommentItem.js index 5ac4550bc..7721ebc41 100644 --- a/modules/CommentItem.js +++ b/modules/CommentItem.js @@ -2,6 +2,7 @@ var ThreadItem = require( './ThreadItem.js' ); /** * @external moment + * @external HeadingItem */ /** @@ -41,4 +42,15 @@ function CommentItem( level, range, signatureRanges, timestamp, author ) { 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;