2020-04-30 13:20:41 +00:00
|
|
|
var
|
|
|
|
controller = require( './controller.js' ),
|
|
|
|
modifier = require( './modifier.js' ),
|
|
|
|
logger = require( './logger.js' ),
|
2021-03-15 14:29:19 +00:00
|
|
|
dtConf = require( './config.json' ),
|
2022-02-02 16:18:01 +00:00
|
|
|
CommentItem = require( './CommentItem.js' ),
|
2022-09-24 11:37:39 +00:00
|
|
|
scrollPadding = {
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
2023-01-31 15:51:12 +00:00
|
|
|
top: 10 + ( $( document.documentElement ).hasClass( 'vector-feature-sticky-header-enabled' ) ? 50 : 0 ),
|
2022-09-24 11:37:39 +00:00
|
|
|
bottom: 10
|
|
|
|
},
|
2020-05-07 19:16:14 +00:00
|
|
|
defaultEditMode = mw.user.options.get( 'discussiontools-editmode' ) || mw.config.get( 'wgDiscussionToolsFallbackEditMode' ),
|
2020-07-13 14:36:41 +00:00
|
|
|
defaultVisual = defaultEditMode === 'visual',
|
2021-03-08 13:25:59 +00:00
|
|
|
featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {},
|
|
|
|
enable2017Wikitext = featuresEnabled.sourcemodetoolbar,
|
2020-06-24 18:09:27 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' ),
|
|
|
|
visualModules = [ 'ext.discussionTools.ReplyWidgetVisual' ]
|
|
|
|
.concat( conf.pluginModules.filter( mw.loader.getState ) ),
|
|
|
|
plainModules = [ 'ext.discussionTools.ReplyWidgetPlain' ];
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-12-15 19:45:05 +00:00
|
|
|
if ( OO.ui.isMobile() ) {
|
|
|
|
visualModules = [
|
|
|
|
'ext.visualEditor.core.mobile',
|
|
|
|
'ext.visualEditor.mwextensions'
|
|
|
|
].concat( visualModules );
|
|
|
|
} else {
|
|
|
|
visualModules = [
|
|
|
|
'ext.visualEditor.core.desktop',
|
|
|
|
'ext.visualEditor.desktopTarget',
|
|
|
|
'ext.visualEditor.mwextensions.desktop'
|
|
|
|
].concat( visualModules );
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
// Start loading reply widget code
|
2020-09-21 20:13:35 +00:00
|
|
|
if ( defaultVisual || enable2017Wikitext ) {
|
2020-06-24 18:09:27 +00:00
|
|
|
mw.loader.using( visualModules );
|
2020-04-30 13:20:41 +00:00
|
|
|
} else {
|
2020-06-24 18:09:27 +00:00
|
|
|
mw.loader.using( plainModules );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Handles setup, save and teardown of commenting widgets
|
|
|
|
*
|
|
|
|
* @param {jQuery} $pageContainer Page container
|
|
|
|
* @param {ThreadItem} threadItem Thread item to attach new comment to
|
2022-02-19 06:31:34 +00:00
|
|
|
* @param {ThreadItemSet} threadItemSet
|
2021-12-20 17:06:24 +00:00
|
|
|
*/
|
2022-02-19 06:31:34 +00:00
|
|
|
function CommentController( $pageContainer, threadItem, threadItemSet ) {
|
2021-01-25 22:21:34 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
2022-12-01 21:09:42 +00:00
|
|
|
this.isTornDown = false;
|
2020-04-30 13:20:41 +00:00
|
|
|
this.$pageContainer = $pageContainer;
|
2021-12-20 17:06:24 +00:00
|
|
|
this.threadItem = threadItem;
|
2022-02-19 06:31:34 +00:00
|
|
|
this.threadItemSet = threadItemSet;
|
2020-04-30 13:20:41 +00:00
|
|
|
this.newListItem = null;
|
|
|
|
this.replyWidgetPromise = null;
|
2022-02-02 16:18:01 +00:00
|
|
|
this.newComments = [];
|
2022-02-08 15:47:28 +00:00
|
|
|
this.parentRemoved = false;
|
2022-02-02 16:18:01 +00:00
|
|
|
this.oldId = mw.config.get( 'wgRevisionId' );
|
|
|
|
this.pollTimeout = null;
|
|
|
|
this.onVisibilityChangeHandler = this.onVisibilityChange.bind( this );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OO.initClass( CommentController );
|
2021-01-25 22:21:34 +00:00
|
|
|
OO.mixinClass( CommentController, OO.EventEmitter );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-05-12 11:51:20 +00:00
|
|
|
/* CommentController private utilities */
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the latest revision ID of the page.
|
|
|
|
*
|
|
|
|
* @param {string} pageName
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
function getLatestRevId( pageName ) {
|
2020-10-23 11:02:18 +00:00
|
|
|
return controller.getApi().get( {
|
2020-04-30 13:20:41 +00:00
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
rvprop: 'ids',
|
|
|
|
rvlimit: 1,
|
2020-06-03 22:22:32 +00:00
|
|
|
titles: pageName
|
2020-04-30 13:20:41 +00:00
|
|
|
} ).then( function ( resp ) {
|
|
|
|
return resp.query.pages[ 0 ].revisions[ 0 ].revid;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-20 17:06:24 +00:00
|
|
|
* Like #checkThreadItemOnPage, but assumes the comment was found on the current page,
|
2020-04-30 13:20:41 +00:00
|
|
|
* and then follows transclusions to determine the source page where it is written.
|
|
|
|
*
|
2021-06-09 23:22:28 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves with a CommentDetails object, or rejects with an error
|
2020-04-30 13:20:41 +00:00
|
|
|
*/
|
2021-12-20 17:06:24 +00:00
|
|
|
CommentController.prototype.getTranscludedFromSource = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var pageName = mw.config.get( 'wgRelevantPageName' ),
|
2021-12-20 17:06:24 +00:00
|
|
|
oldId = mw.config.get( 'wgCurRevisionId' ),
|
|
|
|
threadItem = this.getThreadItem();
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
function followTransclusion( recursionLimit, code, data ) {
|
|
|
|
var errorData;
|
|
|
|
if ( recursionLimit > 0 && code === 'comment-is-transcluded' ) {
|
|
|
|
errorData = data.errors[ 0 ].data;
|
|
|
|
if ( errorData.follow && typeof errorData.transcludedFrom === 'string' ) {
|
|
|
|
return getLatestRevId( errorData.transcludedFrom ).then( function ( latestRevId ) {
|
|
|
|
// Fetch the transcluded page, until we cross the recursion limit
|
2021-12-20 17:06:24 +00:00
|
|
|
return controller.checkThreadItemOnPage( errorData.transcludedFrom, latestRevId, threadItem )
|
2020-04-30 13:20:41 +00:00
|
|
|
.catch( followTransclusion.bind( null, recursionLimit - 1 ) );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $.Deferred().reject( code, data );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arbitrary limit of 10 steps, which should be more than anyone could ever need
|
|
|
|
// (there are reasonable use cases for at least 2)
|
2021-12-20 17:06:24 +00:00
|
|
|
var promise = controller.checkThreadItemOnPage( pageName, oldId, threadItem )
|
2020-04-30 13:20:41 +00:00
|
|
|
.catch( followTransclusion.bind( null, 10 ) );
|
|
|
|
|
|
|
|
return promise;
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2021-01-12 06:34:24 +00:00
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
CommentController.static.initType = 'page';
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
/**
|
|
|
|
* Create and setup the reply widget
|
|
|
|
*
|
|
|
|
* @param {string} [mode] Optionally force a mode, 'visual' or 'source'
|
2021-12-20 15:23:46 +00:00
|
|
|
* @param {boolean} [hideErrors] Suppress errors, e.g. when restoring auto-save
|
2022-02-22 23:10:43 +00:00
|
|
|
* @param {boolean} [suppressNotifications] Don't notify the user if recovering auto-save
|
2020-04-29 16:43:11 +00:00
|
|
|
*/
|
2022-02-22 23:10:43 +00:00
|
|
|
CommentController.prototype.setup = function ( mode, hideErrors, suppressNotifications ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
var threadItem = this.getThreadItem(),
|
2020-04-30 13:20:41 +00:00
|
|
|
commentController = this;
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
if ( mode === undefined ) {
|
2020-07-13 14:36:41 +00:00
|
|
|
mode = mw.user.options.get( 'discussiontools-editmode' ) ||
|
2020-05-28 20:12:38 +00:00
|
|
|
( defaultVisual ? 'visual' : 'source' );
|
2020-04-29 16:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
logger( {
|
|
|
|
action: 'init',
|
2021-01-12 06:34:24 +00:00
|
|
|
type: this.constructor.static.initType || 'page',
|
2020-04-30 13:20:41 +00:00
|
|
|
mechanism: 'click',
|
|
|
|
// eslint-disable-next-line camelcase
|
2020-09-21 20:13:35 +00:00
|
|
|
editor_interface: mode === 'visual' ? 'visualeditor' :
|
|
|
|
( enable2017Wikitext ? 'wikitext-2017' : 'wikitext' )
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !this.replyWidgetPromise ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
this.replyWidgetPromise = this.getTranscludedFromSource().then( function ( commentDetails ) {
|
|
|
|
return commentController.createReplyWidget( commentDetails, { mode: mode } );
|
2020-04-30 13:20:41 +00:00
|
|
|
}, function ( code, data ) {
|
|
|
|
commentController.teardown();
|
|
|
|
|
2021-01-26 01:50:25 +00:00
|
|
|
if ( !hideErrors ) {
|
|
|
|
OO.ui.alert(
|
|
|
|
code instanceof Error ? code.toString() : controller.getApi().getErrorMessage( data ),
|
|
|
|
{ size: 'medium' }
|
|
|
|
);
|
2022-09-14 06:23:07 +00:00
|
|
|
mw.track( 'dt.commentSetupError', code );
|
2021-01-26 01:50:25 +00:00
|
|
|
}
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
logger( {
|
|
|
|
action: 'abort',
|
|
|
|
type: 'preinit'
|
|
|
|
} );
|
|
|
|
|
|
|
|
commentController.replyWidgetPromise = null;
|
|
|
|
|
|
|
|
return $.Deferred().reject();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// On first load, add a placeholder list item
|
2021-12-20 17:06:24 +00:00
|
|
|
commentController.newListItem = modifier.addListItem( threadItem, dtConf.replyIndentation );
|
2022-01-19 19:41:49 +00:00
|
|
|
if ( commentController.newListItem.tagName.toLowerCase() === 'li' ) {
|
|
|
|
// When using bullet syntax, hide the marker. (T259864#7634107)
|
|
|
|
$( commentController.newListItem ).addClass( 'ext-discussiontools-init-noMarker' );
|
|
|
|
}
|
2021-10-05 19:11:50 +00:00
|
|
|
$( commentController.newListItem ).append(
|
|
|
|
// Microsoft Edge's built-in translation feature replaces the entire element when it finishes
|
|
|
|
// translating it, which often happens after our interface has loaded, clobbering it, unless
|
|
|
|
// we wrap this loading message in another element.
|
|
|
|
$( '<span>' ).text( mw.msg( 'discussiontools-replywidget-loading' ) )
|
|
|
|
);
|
2022-09-24 12:13:55 +00:00
|
|
|
var scrollPaddingCollapsed = OO.copy( scrollPadding );
|
|
|
|
// We don't know exactly how tall the widge will be, but leave room for one line
|
|
|
|
// of preview in source mode (~270px). Visual mode is ~250px.
|
|
|
|
scrollPaddingCollapsed.bottom += 270;
|
|
|
|
OO.ui.Element.static.scrollIntoView( commentController.newListItem, {
|
|
|
|
padding: scrollPaddingCollapsed
|
|
|
|
} );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 16:18:01 +00:00
|
|
|
if (
|
|
|
|
this.threadItem instanceof CommentItem &&
|
|
|
|
this.threadItem.getSubscribableHeading()
|
|
|
|
) {
|
|
|
|
// Use the revision ID of the content on the page, not wgCurRevisionId
|
|
|
|
// This means you will more likely get a refresh warning when deliberately
|
|
|
|
// viewing old revisions, which is helpful.
|
|
|
|
this.startPoll();
|
|
|
|
$( document ).on( 'visibilitychange', this.onVisibilityChangeHandler );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replyWidgetPromise.then( function ( replyWidget ) {
|
2020-04-27 16:23:27 +00:00
|
|
|
if ( !commentController.newListItem ) {
|
|
|
|
// On subsequent loads, there's no list item yet, so create one now
|
2021-12-20 17:06:24 +00:00
|
|
|
commentController.newListItem = modifier.addListItem( threadItem, dtConf.replyIndentation );
|
2022-01-19 19:41:49 +00:00
|
|
|
if ( commentController.newListItem.tagName.toLowerCase() === 'li' ) {
|
|
|
|
// When using bullet syntax, hide the marker. (T259864#7634107)
|
|
|
|
$( commentController.newListItem ).addClass( 'ext-discussiontools-init-noMarker' );
|
|
|
|
}
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
|
|
|
$( commentController.newListItem ).empty().append( replyWidget.$element );
|
|
|
|
|
2022-02-22 23:10:43 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget, {}, suppressNotifications );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
commentController.showAndFocus();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
logger( { action: 'ready' } );
|
|
|
|
logger( { action: 'loaded' } );
|
|
|
|
} );
|
2020-04-30 13:20:41 +00:00
|
|
|
};
|
|
|
|
|
2022-02-02 16:18:01 +00:00
|
|
|
/**
|
|
|
|
* Handle document visibilitychange events
|
|
|
|
*
|
|
|
|
* This allows us to pause polling when the user switches to another tab
|
|
|
|
*/
|
|
|
|
CommentController.prototype.onVisibilityChange = function () {
|
|
|
|
if ( document.hidden ) {
|
|
|
|
this.stopPoll();
|
|
|
|
} else if ( !this.pollTimeout ) {
|
|
|
|
this.pollTimeout = setTimeout( this.startPoll.bind( this ), 5000 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CommentController.prototype.startPoll = function () {
|
2022-02-08 15:47:28 +00:00
|
|
|
var threadItemId = this.threadItem.id;
|
2022-02-02 16:18:01 +00:00
|
|
|
var subscribableHeadingId = this.threadItem.getSubscribableHeading().id;
|
|
|
|
var commentController = this;
|
|
|
|
|
|
|
|
this.pollApiRequest = controller.getApi().get( {
|
|
|
|
action: 'discussiontoolscompare',
|
|
|
|
fromrev: this.oldId,
|
|
|
|
totitle: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} );
|
|
|
|
this.pollApiRequest.then( function ( response ) {
|
|
|
|
function relevantCommentFilter( cmt ) {
|
|
|
|
return cmt.subscribableHeadingId === subscribableHeadingId &&
|
|
|
|
// Ignore posts by yourself, if logged in
|
|
|
|
cmt.author !== mw.user.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = OO.getProp( response, 'discussiontoolscompare' ) || {};
|
|
|
|
var addedComments = result.addedcomments.filter( relevantCommentFilter );
|
|
|
|
var removedComments = result.removedcomments.filter( relevantCommentFilter );
|
|
|
|
|
|
|
|
if ( addedComments.length || removedComments.length ) {
|
|
|
|
commentController.updateNewCommentsWarning( addedComments, removedComments );
|
|
|
|
}
|
|
|
|
|
2022-02-08 15:47:28 +00:00
|
|
|
// Parent comment was deleted
|
|
|
|
var isParentRemoved = result.removedcomments.some( function ( cmt ) {
|
|
|
|
return cmt.id === threadItemId;
|
|
|
|
} );
|
|
|
|
// Parent comment was deleted then added back (e.g. reverted vandalism)
|
|
|
|
var isParentAdded = result.addedcomments.some( function ( cmt ) {
|
|
|
|
return cmt.id === threadItemId;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( isParentAdded ) {
|
|
|
|
commentController.setParentRemoved( false );
|
|
|
|
} else if ( isParentRemoved ) {
|
|
|
|
commentController.setParentRemoved( true );
|
|
|
|
}
|
|
|
|
|
2022-02-02 16:18:01 +00:00
|
|
|
commentController.oldId = result.torevid;
|
|
|
|
} ).always( function () {
|
2022-12-01 21:09:42 +00:00
|
|
|
if ( commentController.isTornDown ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-02 16:18:01 +00:00
|
|
|
commentController.pollTimeout = setTimeout( commentController.startPoll.bind( commentController ), 5000 );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
CommentController.prototype.stopPoll = function () {
|
|
|
|
if ( this.pollTimeout ) {
|
|
|
|
clearTimeout( this.pollTimeout );
|
|
|
|
this.pollTimeout = null;
|
|
|
|
}
|
|
|
|
if ( this.pollApiRequest ) {
|
|
|
|
this.pollApiRequest.abort();
|
|
|
|
this.pollApiRequest = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Get thread item this controller is attached to
|
|
|
|
*
|
|
|
|
* @return {ThreadItem} Thread item
|
|
|
|
*/
|
|
|
|
CommentController.prototype.getThreadItem = function () {
|
|
|
|
return this.threadItem;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the reply widget class to use in this controller
|
|
|
|
*
|
|
|
|
* @param {boolean} visual Prefer the VE-based class
|
|
|
|
* @return {jQuery.Promise} Promise which resolves with a Function: the reply widget class
|
|
|
|
*/
|
2020-04-30 13:20:41 +00:00
|
|
|
CommentController.prototype.getReplyWidgetClass = function ( visual ) {
|
2020-09-21 20:13:35 +00:00
|
|
|
// If 2017WTE mode is enabled, always use ReplyWidgetVisual.
|
|
|
|
visual = visual || enable2017Wikitext;
|
|
|
|
|
2020-06-24 18:09:27 +00:00
|
|
|
return mw.loader.using( visual ? visualModules : plainModules ).then( function () {
|
|
|
|
return require( visual ? 'ext.discussionTools.ReplyWidgetVisual' : 'ext.discussionTools.ReplyWidgetPlain' );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-06-09 23:22:28 +00:00
|
|
|
/**
|
2021-12-20 17:06:24 +00:00
|
|
|
* Create a reply widget
|
|
|
|
*
|
2021-06-09 23:22:28 +00:00
|
|
|
* @param {CommentDetails} commentDetails
|
|
|
|
* @param {Object} config
|
|
|
|
* @return {jQuery.Promise} Promise resolved with a ReplyWidget
|
|
|
|
*/
|
2021-12-20 17:06:24 +00:00
|
|
|
CommentController.prototype.createReplyWidget = function ( commentDetails, config ) {
|
2020-04-30 13:20:41 +00:00
|
|
|
var commentController = this;
|
2021-12-20 17:06:24 +00:00
|
|
|
|
2021-03-18 14:14:23 +00:00
|
|
|
return this.getReplyWidgetClass( config.mode === 'visual' ).then( function ( ReplyWidget ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
return new ReplyWidget( commentController, commentDetails, config );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-02-22 23:10:43 +00:00
|
|
|
CommentController.prototype.setupReplyWidget = function ( replyWidget, data, suppressNotifications ) {
|
2022-02-02 16:18:01 +00:00
|
|
|
replyWidget.connect( this, {
|
|
|
|
teardown: 'teardown',
|
|
|
|
reloadPage: this.emit.bind( this, 'reloadPage' )
|
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2022-02-22 23:10:43 +00:00
|
|
|
replyWidget.setup( data, suppressNotifications );
|
2022-02-02 16:18:01 +00:00
|
|
|
replyWidget.updateNewCommentsWarning( this.newComments );
|
2022-02-08 15:47:28 +00:00
|
|
|
replyWidget.updateParentRemovedError( this.parentRemoved );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
this.replyWidget = replyWidget;
|
2020-04-30 13:20:41 +00:00
|
|
|
};
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
/**
|
|
|
|
* Focus the first input field inside the controller.
|
|
|
|
*/
|
|
|
|
CommentController.prototype.focus = function () {
|
|
|
|
this.replyWidget.focus();
|
|
|
|
};
|
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
CommentController.prototype.showAndFocus = function () {
|
2021-03-24 12:58:28 +00:00
|
|
|
var commentController = this;
|
2022-07-07 12:42:12 +00:00
|
|
|
this.replyWidget.scrollElementIntoView( { padding: scrollPadding } )
|
|
|
|
.then( function () {
|
|
|
|
commentController.focus();
|
|
|
|
} );
|
2021-01-25 22:21:34 +00:00
|
|
|
};
|
|
|
|
|
2022-02-02 16:15:50 +00:00
|
|
|
CommentController.prototype.teardown = function ( mode ) {
|
2022-02-02 16:18:01 +00:00
|
|
|
if ( mode === 'refresh' ) {
|
|
|
|
$( this.newListItem ).empty().append(
|
|
|
|
$( '<span>' ).text( mw.msg( 'discussiontools-replywidget-loading' ) )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
modifier.removeAddedListItem( this.newListItem );
|
|
|
|
this.newListItem = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stopPoll();
|
|
|
|
$( document ).off( 'visibilitychange', this.onVisibilityChangeHandler );
|
|
|
|
|
2022-12-01 21:09:42 +00:00
|
|
|
this.isTornDown = true;
|
2022-02-02 16:15:50 +00:00
|
|
|
this.emit( 'teardown', mode );
|
2020-04-30 13:20:41 +00:00
|
|
|
};
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
/**
|
|
|
|
* Get the parameters of the API query that can be used to post this comment.
|
|
|
|
*
|
|
|
|
* @param {string} pageName Title of the page to post on
|
|
|
|
* @param {Object} checkboxes Value of the promise returned by controller#getCheckboxesPromise
|
2021-12-20 17:25:54 +00:00
|
|
|
* @return {Object.<string,string>} API query data
|
2020-08-29 12:00:51 +00:00
|
|
|
*/
|
2021-12-20 17:06:24 +00:00
|
|
|
CommentController.prototype.getApiQuery = function ( pageName, checkboxes ) {
|
|
|
|
var threadItem = this.getThreadItem();
|
2021-04-08 13:46:09 +00:00
|
|
|
var replyWidget = this.replyWidget;
|
2022-02-19 06:31:34 +00:00
|
|
|
var sameNameComments = this.threadItemSet.findCommentsByName( threadItem.name );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2021-04-16 16:04:57 +00:00
|
|
|
var mode = replyWidget.getMode();
|
|
|
|
var tags = [
|
|
|
|
'discussiontools',
|
|
|
|
'discussiontools-reply',
|
|
|
|
'discussiontools-' + mode
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( mode === 'source' && enable2017Wikitext ) {
|
|
|
|
tags.push( 'discussiontools-source-enhanced' );
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var data = {
|
2020-08-29 12:00:51 +00:00
|
|
|
action: 'discussiontoolsedit',
|
|
|
|
paction: 'addcomment',
|
|
|
|
page: pageName,
|
2021-12-20 17:06:24 +00:00
|
|
|
commentname: threadItem.name,
|
2021-02-12 19:16:13 +00:00
|
|
|
// Only specify this if necessary to disambiguate, to avoid errors if the parent changes
|
2021-12-20 17:06:24 +00:00
|
|
|
commentid: sameNameComments.length > 1 ? threadItem.id : undefined,
|
2020-08-29 12:00:51 +00:00
|
|
|
summary: replyWidget.getEditSummary(),
|
2021-10-18 22:18:07 +00:00
|
|
|
formtoken: replyWidget.getFormToken(),
|
2020-08-29 12:00:51 +00:00
|
|
|
assert: mw.user.isAnon() ? 'anon' : 'user',
|
|
|
|
assertuser: mw.user.getName() || undefined,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
2021-10-18 08:32:17 +00:00
|
|
|
dttags: tags.join( ',' ),
|
|
|
|
editingStatsId: logger.getSessionId()
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ( replyWidget.getMode() === 'source' ) {
|
|
|
|
data.wikitext = replyWidget.getValue();
|
|
|
|
} else {
|
|
|
|
data.html = replyWidget.getValue();
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var captchaInput = replyWidget.captchaInput;
|
2020-08-29 12:00:51 +00:00
|
|
|
if ( captchaInput ) {
|
|
|
|
data.captchaid = captchaInput.getCaptchaId();
|
|
|
|
data.captchaword = captchaInput.getCaptchaWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( checkboxes.checkboxesByName.wpWatchthis ) {
|
|
|
|
data.watchlist = checkboxes.checkboxesByName.wpWatchthis.isSelected() ?
|
|
|
|
'watch' :
|
|
|
|
'unwatch';
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Save the comment in the comment controller
|
|
|
|
*
|
|
|
|
* @param {string} pageName Page title
|
|
|
|
* @return {jQuery.Promise} Promise which resolves when the save is complete
|
|
|
|
*/
|
|
|
|
CommentController.prototype.save = function ( pageName ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
var replyWidget = this.replyWidget,
|
2021-12-20 17:06:24 +00:00
|
|
|
commentController = this,
|
|
|
|
threadItem = this.getThreadItem();
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-06-26 21:47:56 +00:00
|
|
|
return this.replyWidget.checkboxesPromise.then( function ( checkboxes ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
var data = commentController.getApiQuery( pageName, checkboxes );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-10-23 11:02:18 +00:00
|
|
|
// No timeout. Huge talk pages can take a long time to save, and falsely reporting an error
|
|
|
|
// could result in duplicate messages if the user retries. (T249071)
|
2021-04-08 13:46:09 +00:00
|
|
|
var defaults = OO.copy( controller.getApi().defaults );
|
2021-09-09 21:37:28 +00:00
|
|
|
defaults.ajax.timeout = 0;
|
2021-04-08 13:46:09 +00:00
|
|
|
var noTimeoutApi = new mw.Api( defaults );
|
2020-10-23 11:02:18 +00:00
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
return mw.libs.ve.targetSaver.postContent(
|
2020-10-23 11:02:18 +00:00
|
|
|
data, { api: noTimeoutApi }
|
2020-08-26 22:46:34 +00:00
|
|
|
).catch( function ( code, responseData ) {
|
2020-09-01 20:20:24 +00:00
|
|
|
// Better user-facing error messages
|
|
|
|
if ( code === 'editconflict' ) {
|
2021-02-12 19:16:13 +00:00
|
|
|
return $.Deferred().reject( code, { errors: [ {
|
|
|
|
code: code,
|
2020-09-01 20:20:24 +00:00
|
|
|
html: mw.message( 'discussiontools-error-comment-conflict' ).parse()
|
|
|
|
} ] } ).promise();
|
|
|
|
}
|
2021-02-12 19:16:13 +00:00
|
|
|
if (
|
|
|
|
code === 'discussiontools-commentid-notfound' ||
|
|
|
|
code === 'discussiontools-commentname-ambiguous' ||
|
|
|
|
code === 'discussiontools-commentname-notfound'
|
|
|
|
) {
|
|
|
|
return $.Deferred().reject( code, { errors: [ {
|
|
|
|
code: code,
|
2020-08-19 20:03:41 +00:00
|
|
|
html: mw.message( 'discussiontools-error-comment-disappeared' ).parse()
|
|
|
|
} ] } ).promise();
|
2020-07-28 19:57:41 +00:00
|
|
|
}
|
2020-08-26 22:46:34 +00:00
|
|
|
return $.Deferred().reject( code, responseData ).promise();
|
|
|
|
} ).then( function ( responseData ) {
|
2021-12-20 17:06:24 +00:00
|
|
|
controller.update( responseData, threadItem, pageName, replyWidget );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-02-02 16:18:01 +00:00
|
|
|
/**
|
|
|
|
* Add a list of comment objects that are new on the page since it was last refreshed
|
|
|
|
*
|
|
|
|
* @param {Object[]} addedComments Array of JSON-serialized CommentItem's
|
|
|
|
* @param {Object[]} removedComments Array of JSON-serialized CommentItem's
|
|
|
|
*/
|
|
|
|
CommentController.prototype.updateNewCommentsWarning = function ( addedComments, removedComments ) {
|
|
|
|
var commentController = this;
|
|
|
|
// Add new comments
|
|
|
|
this.newComments.push.apply( this.newComments, addedComments );
|
|
|
|
|
|
|
|
// Delete any comments which have since been deleted (e.g. posted then reverted)
|
|
|
|
var removedCommentIds = removedComments.filter( function ( cmt ) {
|
|
|
|
return cmt.id;
|
|
|
|
} );
|
|
|
|
this.newComments = this.newComments.filter( function ( cmt ) {
|
|
|
|
// If comment ID is not in removedCommentIds, keep it
|
|
|
|
return removedCommentIds.indexOf( cmt.id ) === -1;
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.replyWidgetPromise.then( function ( replyWidget ) {
|
|
|
|
replyWidget.updateNewCommentsWarning( commentController.newComments );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-02-08 15:47:28 +00:00
|
|
|
/**
|
|
|
|
* Record whether the parent thread item has been removed
|
|
|
|
*
|
|
|
|
* @param {boolean} parentRemoved
|
|
|
|
*/
|
|
|
|
CommentController.prototype.setParentRemoved = function ( parentRemoved ) {
|
|
|
|
var commentController = this;
|
|
|
|
this.parentRemoved = parentRemoved;
|
|
|
|
|
|
|
|
this.replyWidgetPromise.then( function ( replyWidget ) {
|
|
|
|
replyWidget.updateParentRemovedError( commentController.parentRemoved );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Switch reply widget to wikitext input
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise which resolves when switch is complete
|
|
|
|
*/
|
2020-04-27 16:23:27 +00:00
|
|
|
CommentController.prototype.switchToWikitext = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var oldWidget = this.replyWidget,
|
2020-04-27 16:23:27 +00:00
|
|
|
target = oldWidget.replyBodyWidget.target,
|
2020-09-25 17:08:08 +00:00
|
|
|
oldShowAdvanced = oldWidget.showAdvanced,
|
|
|
|
oldEditSummary = oldWidget.getEditSummary(),
|
2020-05-21 21:11:01 +00:00
|
|
|
previewDeferred = $.Deferred(),
|
2020-04-27 16:23:27 +00:00
|
|
|
commentController = this;
|
|
|
|
|
2020-06-20 12:07:49 +00:00
|
|
|
// TODO: We may need to pass oldid/etag when editing is supported
|
2021-04-08 13:46:09 +00:00
|
|
|
var wikitextPromise = target.getWikitextFragment( target.getSurface().getModel().getDocument() );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.replyWidgetPromise = this.createReplyWidget(
|
2021-06-09 23:22:28 +00:00
|
|
|
oldWidget.commentDetails,
|
2021-03-18 14:14:23 +00:00
|
|
|
{ mode: 'source' }
|
2020-08-25 12:31:54 +00:00
|
|
|
);
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
return $.when( wikitextPromise, this.replyWidgetPromise ).then( function ( wikitext, replyWidget ) {
|
2020-05-21 21:11:01 +00:00
|
|
|
// To prevent the "Reply" / "Cancel" buttons from shifting when the preview loads,
|
|
|
|
// wait for the preview (but no longer than 500 ms) before swithing the editors.
|
|
|
|
replyWidget.preparePreview( wikitext ).then( previewDeferred.resolve );
|
|
|
|
setTimeout( previewDeferred.resolve, 500 );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2020-05-21 21:11:01 +00:00
|
|
|
return previewDeferred.then( function () {
|
|
|
|
// Teardown the old widget
|
|
|
|
oldWidget.disconnect( commentController );
|
|
|
|
oldWidget.teardown();
|
|
|
|
|
2021-02-15 20:59:32 +00:00
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
2020-09-25 17:08:08 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget, {
|
|
|
|
value: wikitext,
|
|
|
|
showAdvanced: oldShowAdvanced,
|
|
|
|
editSummary: oldEditSummary
|
|
|
|
} );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
// Focus the editor
|
|
|
|
replyWidget.focus();
|
2020-05-21 21:11:01 +00:00
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
/**
|
|
|
|
* Remove empty lines and add indent characters to convert the paragraphs in given wikitext to
|
|
|
|
* definition list items, as customary in discussions.
|
|
|
|
*
|
|
|
|
* @param {string} wikitext
|
|
|
|
* @param {string} indent Indent character, ':' or '*'
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2021-02-17 23:42:57 +00:00
|
|
|
CommentController.prototype.doIndentReplacements = function ( wikitext, indent ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
wikitext = modifier.sanitizeWikitextLinebreaks( wikitext );
|
|
|
|
|
|
|
|
wikitext = wikitext.split( '\n' ).map( function ( line ) {
|
|
|
|
return indent + line;
|
|
|
|
} ).join( '\n' );
|
|
|
|
|
|
|
|
return wikitext;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn definition list items, customary in discussions, back into normal paragraphs, suitable for
|
|
|
|
* the editing interface.
|
|
|
|
*
|
|
|
|
* @param {Node} rootNode Node potentially containing definition lists (modified in-place)
|
|
|
|
*/
|
2021-02-17 23:42:57 +00:00
|
|
|
CommentController.prototype.undoIndentReplacements = function ( rootNode ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
var children = Array.prototype.slice.call( rootNode.childNodes );
|
|
|
|
// There may be multiple lists when some lines are template generated
|
|
|
|
children.forEach( function ( child ) {
|
|
|
|
if ( child.nodeType === Node.ELEMENT_NODE ) {
|
|
|
|
// Unwrap list
|
|
|
|
modifier.unwrapList( child );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of selectors that match nodes that can't be inserted in the comment. (We disallow
|
|
|
|
* things that generate wikitext syntax that may conflict with list item syntax.)
|
|
|
|
*
|
2021-12-20 17:25:54 +00:00
|
|
|
* @return {Object.<string,string>} Map of type used for error messages (string) to CSS selector (string)
|
2020-08-29 12:00:51 +00:00
|
|
|
*/
|
|
|
|
CommentController.prototype.getUnsupportedNodeSelectors = function () {
|
|
|
|
return {
|
|
|
|
// Tables are almost always multi-line
|
|
|
|
table: 'table',
|
|
|
|
// Headings are converted to plain text before we can detect them:
|
|
|
|
// `:==h2==` -> `<p>==h2==</p>`
|
|
|
|
// heading: 'h1, h2, h3, h4, h5, h6',
|
|
|
|
// Templates can be multiline
|
|
|
|
template: '[typeof*="mw:Transclusion"]',
|
|
|
|
// Extensions (includes references) can be multiline, could be supported later (T251633)
|
|
|
|
extension: '[typeof*="mw:Extension"]'
|
|
|
|
// Images are probably fine unless a multi-line caption was used (rare)
|
|
|
|
// image: 'figure, figure-inline'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Switch reply widget to visual input
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise which resolves when switch is complete
|
|
|
|
*/
|
2020-04-27 16:23:27 +00:00
|
|
|
CommentController.prototype.switchToVisual = function () {
|
2021-04-08 13:46:09 +00:00
|
|
|
var oldWidget = this.replyWidget,
|
2020-09-25 17:08:08 +00:00
|
|
|
oldShowAdvanced = oldWidget.showAdvanced,
|
|
|
|
oldEditSummary = oldWidget.getEditSummary(),
|
2020-04-27 16:23:27 +00:00
|
|
|
wikitext = oldWidget.getValue(),
|
|
|
|
commentController = this;
|
|
|
|
|
2020-04-08 18:23:22 +00:00
|
|
|
// Replace wikitext signatures with a special marker recognized by DtDmMWSignatureNode
|
|
|
|
// to render them as signature nodes in visual mode.
|
|
|
|
wikitext = wikitext.replace(
|
|
|
|
// Replace ~~~~ (four tildes), but not ~~~~~ (five tildes)
|
|
|
|
/([^~]|^)~~~~([^~]|$)/g,
|
|
|
|
'$1<span data-dtsignatureforswitching="1"></span>$2'
|
|
|
|
);
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var parsePromise;
|
2020-04-27 16:23:27 +00:00
|
|
|
if ( wikitext ) {
|
2021-03-15 14:29:19 +00:00
|
|
|
wikitext = this.doIndentReplacements( wikitext, dtConf.replyIndentation === 'invisible' ? ':' : '*' );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
// Based on ve.init.mw.Target#parseWikitextFragment
|
2020-10-23 11:02:18 +00:00
|
|
|
parsePromise = controller.getApi().post( {
|
2020-04-27 16:23:27 +00:00
|
|
|
action: 'visualeditor',
|
|
|
|
paction: 'parsefragment',
|
2020-08-19 20:03:41 +00:00
|
|
|
page: oldWidget.pageName,
|
2020-06-22 19:01:09 +00:00
|
|
|
wikitext: wikitext,
|
|
|
|
pst: true
|
2020-04-27 16:23:27 +00:00
|
|
|
} ).then( function ( response ) {
|
|
|
|
return response && response.visualeditor.content;
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
parsePromise = $.Deferred().resolve( '' ).promise();
|
|
|
|
}
|
2020-08-25 12:31:54 +00:00
|
|
|
this.replyWidgetPromise = this.createReplyWidget(
|
2021-06-09 23:22:28 +00:00
|
|
|
oldWidget.commentDetails,
|
2021-03-18 14:14:23 +00:00
|
|
|
{ mode: 'visual' }
|
2020-08-25 12:31:54 +00:00
|
|
|
);
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
return $.when( parsePromise, this.replyWidgetPromise ).then( function ( html, replyWidget ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var unsupportedSelectors = commentController.getUnsupportedNodeSelectors();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var doc;
|
2020-04-27 16:23:27 +00:00
|
|
|
if ( html ) {
|
|
|
|
doc = replyWidget.replyBodyWidget.target.parseDocument( html );
|
2020-06-10 19:34:07 +00:00
|
|
|
// Remove RESTBase IDs (T253584)
|
|
|
|
mw.libs.ve.stripRestbaseIds( doc );
|
2020-08-29 12:00:51 +00:00
|
|
|
// Check for tables, headings, images, templates
|
2021-04-08 13:46:09 +00:00
|
|
|
for ( var type in unsupportedSelectors ) {
|
2020-06-24 20:07:34 +00:00
|
|
|
if ( doc.querySelector( unsupportedSelectors[ type ] ) ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var $msg = $( '<div>' ).html(
|
2020-06-24 20:07:34 +00:00
|
|
|
mw.message(
|
|
|
|
'discussiontools-error-noswitchtove',
|
|
|
|
// The following messages are used here:
|
|
|
|
// * discussiontools-error-noswitchtove-extension
|
|
|
|
// * discussiontools-error-noswitchtove-table
|
|
|
|
// * discussiontools-error-noswitchtove-template
|
|
|
|
mw.msg( 'discussiontools-error-noswitchtove-' + type )
|
|
|
|
).parse()
|
|
|
|
);
|
|
|
|
$msg.find( 'a' ).attr( {
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener'
|
|
|
|
} );
|
|
|
|
OO.ui.alert(
|
|
|
|
$msg.contents(),
|
|
|
|
{
|
|
|
|
title: mw.msg( 'discussiontools-error-noswitchtove-title' ),
|
|
|
|
size: 'medium'
|
|
|
|
}
|
|
|
|
);
|
2020-07-28 17:19:36 +00:00
|
|
|
mw.track( 'dt.schemaVisualEditorFeatureUse', {
|
|
|
|
feature: 'editor-switch',
|
2020-09-17 21:52:32 +00:00
|
|
|
action: 'dialog-prevent-show'
|
2020-07-28 17:19:36 +00:00
|
|
|
} );
|
|
|
|
|
2020-06-24 20:07:34 +00:00
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 23:42:57 +00:00
|
|
|
commentController.undoIndentReplacements( doc.body );
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 20:07:34 +00:00
|
|
|
// Teardown the old widget
|
|
|
|
oldWidget.disconnect( commentController );
|
|
|
|
oldWidget.teardown();
|
|
|
|
|
2021-02-15 20:59:32 +00:00
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
2020-09-25 17:08:08 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget, {
|
|
|
|
value: doc,
|
|
|
|
showAdvanced: oldShowAdvanced,
|
|
|
|
editSummary: oldEditSummary
|
|
|
|
} );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
// Focus the editor
|
|
|
|
replyWidget.focus();
|
2020-04-27 16:23:27 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
module.exports = CommentController;
|