2024-05-24 12:31:30 +00:00
|
|
|
const
|
2020-04-30 13:20:41 +00:00
|
|
|
controller = require( './controller.js' ),
|
|
|
|
modifier = require( './modifier.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
|
|
|
|
},
|
2023-05-21 06:57:49 +00:00
|
|
|
defaultVisual = controller.defaultVisual,
|
|
|
|
enable2017Wikitext = controller.enable2017Wikitext;
|
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
|
2023-06-13 22:55:08 +00:00
|
|
|
* @param {MemoryStorage} storage Storage object for autosave
|
2021-12-20 17:06:24 +00:00
|
|
|
*/
|
2023-06-13 22:55:08 +00:00
|
|
|
function CommentController( $pageContainer, threadItem, threadItemSet, storage ) {
|
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;
|
2023-06-13 22:55:08 +00:00
|
|
|
this.storage = storage;
|
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
|
2024-04-19 22:07:35 +00:00
|
|
|
} ).then( ( resp ) => resp.query.pages[ 0 ].revisions[ 0 ].revid );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 () {
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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 ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
let errorData;
|
2020-04-30 13:20:41 +00:00
|
|
|
if ( recursionLimit > 0 && code === 'comment-is-transcluded' ) {
|
|
|
|
errorData = data.errors[ 0 ].data;
|
|
|
|
if ( errorData.follow && typeof errorData.transcludedFrom === 'string' ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
return getLatestRevId( errorData.transcludedFrom ).then(
|
2020-04-30 13:20:41 +00:00
|
|
|
// Fetch the transcluded page, until we cross the recursion limit
|
2024-04-19 22:07:35 +00:00
|
|
|
( latestRevId ) => controller.checkThreadItemOnPage( errorData.transcludedFrom, latestRevId, threadItem )
|
|
|
|
.catch( followTransclusion.bind( null, recursionLimit - 1 ) )
|
|
|
|
);
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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';
|
|
|
|
|
2024-01-05 15:45:46 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current widget has been torn down
|
|
|
|
*
|
|
|
|
* @event teardown
|
|
|
|
* @param string mode Teardown mode. See dt.ui.ReplyWidget#teardown.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
/**
|
|
|
|
* Create and setup the reply widget
|
|
|
|
*
|
2024-10-02 13:42:56 +00:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {string} [options.mode] Optionally force a mode, 'visual' or 'source'
|
|
|
|
* @param {boolean} [options.hideErrors] Suppress errors, e.g. when restoring auto-save
|
|
|
|
* @param {boolean} [options.suppressNotifications] Don't notify the user if recovering auto-save
|
2020-04-29 16:43:11 +00:00
|
|
|
*/
|
2024-10-02 13:42:56 +00:00
|
|
|
CommentController.prototype.setup = function ( options ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const threadItem = this.getThreadItem();
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2024-10-02 13:42:56 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if ( options.mode === undefined ) {
|
|
|
|
options.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
|
|
|
}
|
|
|
|
|
2023-03-18 02:29:27 +00:00
|
|
|
mw.track( 'editAttemptStep', {
|
2020-04-30 13:20:41 +00:00
|
|
|
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',
|
2023-03-18 02:29:27 +00:00
|
|
|
integration: 'discussiontools',
|
2020-04-30 13:20:41 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
2024-10-02 13:42:56 +00:00
|
|
|
editor_interface: options.mode === 'visual' ? 'visualeditor' :
|
2020-09-21 20:13:35 +00:00
|
|
|
( enable2017Wikitext ? 'wikitext-2017' : 'wikitext' )
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !this.replyWidgetPromise ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
this.replyWidgetPromise = this.getTranscludedFromSource().then(
|
2024-10-02 13:42:56 +00:00
|
|
|
( commentDetails ) => this.createReplyWidget( commentDetails, { mode: options.mode } ),
|
2024-04-19 22:07:35 +00:00
|
|
|
( code, data ) => {
|
|
|
|
this.onReplyWidgetTeardown();
|
|
|
|
|
2024-10-02 13:42:56 +00:00
|
|
|
if ( !options.hideErrors ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
OO.ui.alert(
|
|
|
|
code instanceof Error ? code.toString() : controller.getApi().getErrorMessage( data ),
|
|
|
|
{ size: 'medium' }
|
|
|
|
);
|
|
|
|
mw.track( 'dt.commentSetupError', code );
|
|
|
|
}
|
|
|
|
|
|
|
|
mw.track( 'editAttemptStep', {
|
|
|
|
action: 'abort',
|
|
|
|
type: 'preinit'
|
|
|
|
} );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2024-04-19 22:07:35 +00:00
|
|
|
this.replyWidgetPromise = null;
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2024-04-19 22:07:35 +00:00
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
|
|
|
);
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
// On first load, add a placeholder list item
|
2024-04-18 18:37:58 +00:00
|
|
|
this.newListItem = modifier.addListItem( threadItem, dtConf.replyIndentation );
|
|
|
|
if ( this.newListItem.tagName.toLowerCase() === 'li' ) {
|
2022-01-19 19:41:49 +00:00
|
|
|
// When using bullet syntax, hide the marker. (T259864#7634107)
|
2024-04-18 18:37:58 +00:00
|
|
|
$( this.newListItem ).addClass( 'ext-discussiontools-init-noMarker' );
|
2022-01-19 19:41:49 +00:00
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
$( this.newListItem ).append(
|
2021-10-05 19:11:50 +00:00
|
|
|
// 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' ) )
|
|
|
|
);
|
2024-05-24 12:20:50 +00:00
|
|
|
const scrollPaddingCollapsed = OO.copy( scrollPadding );
|
2022-09-24 12:13:55 +00:00
|
|
|
// 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;
|
2024-04-18 18:37:58 +00:00
|
|
|
OO.ui.Element.static.scrollIntoView( this.newListItem, {
|
2022-09-24 12:13:55 +00:00
|
|
|
padding: scrollPaddingCollapsed
|
|
|
|
} );
|
2022-10-16 12:20:23 +00:00
|
|
|
|
|
|
|
// Disable section collapsing on mobile. If the section were collapsed it would be hard to
|
|
|
|
// find your comment again. The "Return to reply" tool is broken by section collapsing as
|
|
|
|
// the reply widget is hidden and therefore not measureable. It's also possible the page is
|
|
|
|
// not long enough to trigger the "Return to reply" tool.
|
|
|
|
$( this.newListItem ).parents( '.collapsible-block' ).prev().addClass( 'collapsible-heading-disabled' );
|
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 );
|
|
|
|
}
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.replyWidgetPromise.then( ( replyWidget ) => {
|
|
|
|
if ( !this.newListItem ) {
|
2020-04-27 16:23:27 +00:00
|
|
|
// On subsequent loads, there's no list item yet, so create one now
|
2024-04-18 18:37:58 +00:00
|
|
|
this.newListItem = modifier.addListItem( threadItem, dtConf.replyIndentation );
|
|
|
|
if ( this.newListItem.tagName.toLowerCase() === 'li' ) {
|
2022-01-19 19:41:49 +00:00
|
|
|
// When using bullet syntax, hide the marker. (T259864#7634107)
|
2024-04-18 18:37:58 +00:00
|
|
|
$( this.newListItem ).addClass( 'ext-discussiontools-init-noMarker' );
|
2022-01-19 19:41:49 +00:00
|
|
|
}
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
$( this.newListItem ).empty().append( replyWidget.$element );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2024-10-02 13:42:56 +00:00
|
|
|
this.setupReplyWidget( replyWidget, {}, options.suppressNotifications );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.showAndFocus();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2023-03-18 02:29:27 +00:00
|
|
|
mw.track( 'editAttemptStep', { action: 'ready' } );
|
|
|
|
mw.track( 'editAttemptStep', { action: 'loaded' } );
|
2020-04-27 16:23:27 +00:00
|
|
|
} );
|
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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-26 02:35:12 +00:00
|
|
|
CommentController.prototype.startPoll = function ( nextDelay ) {
|
|
|
|
nextDelay = nextDelay || 5000;
|
|
|
|
|
2024-09-17 00:24:54 +00:00
|
|
|
if ( !(
|
|
|
|
this.threadItem instanceof CommentItem &&
|
|
|
|
this.threadItem.getSubscribableHeading()
|
|
|
|
) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const threadItemId = this.threadItem.id;
|
|
|
|
const subscribableHeadingId = this.threadItem.getSubscribableHeading().id;
|
2024-08-28 16:42:12 +00:00
|
|
|
let aborted = false;
|
2022-02-02 16:18:01 +00:00
|
|
|
|
|
|
|
this.pollApiRequest = controller.getApi().get( {
|
|
|
|
action: 'discussiontoolscompare',
|
|
|
|
fromrev: this.oldId,
|
|
|
|
totitle: mw.config.get( 'wgRelevantPageName' )
|
|
|
|
} );
|
2024-04-18 18:37:58 +00:00
|
|
|
this.pollApiRequest.then( ( response ) => {
|
2022-02-02 16:18:01 +00:00
|
|
|
function relevantCommentFilter( cmt ) {
|
|
|
|
return cmt.subscribableHeadingId === subscribableHeadingId &&
|
|
|
|
// Ignore posts by yourself, if logged in
|
|
|
|
cmt.author !== mw.user.getName();
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const result = OO.getProp( response, 'discussiontoolscompare' ) || {};
|
|
|
|
const addedComments = result.addedcomments.filter( relevantCommentFilter );
|
|
|
|
const removedComments = result.removedcomments.filter( relevantCommentFilter );
|
2022-02-02 16:18:01 +00:00
|
|
|
|
|
|
|
if ( addedComments.length || removedComments.length ) {
|
2024-04-18 18:37:58 +00:00
|
|
|
this.updateNewCommentsWarning( addedComments, removedComments );
|
2022-02-02 16:18:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 15:47:28 +00:00
|
|
|
// Parent comment was deleted
|
2024-05-24 12:20:50 +00:00
|
|
|
const isParentRemoved = result.removedcomments.some( ( cmt ) => cmt.id === threadItemId );
|
2022-02-08 15:47:28 +00:00
|
|
|
// Parent comment was deleted then added back (e.g. reverted vandalism)
|
2024-05-24 12:20:50 +00:00
|
|
|
const isParentAdded = result.addedcomments.some( ( cmt ) => cmt.id === threadItemId );
|
2022-02-08 15:47:28 +00:00
|
|
|
|
|
|
|
if ( isParentAdded ) {
|
2024-04-18 18:37:58 +00:00
|
|
|
this.setParentRemoved( false );
|
2022-02-08 15:47:28 +00:00
|
|
|
} else if ( isParentRemoved ) {
|
2024-04-18 18:37:58 +00:00
|
|
|
this.setParentRemoved( true );
|
2022-02-08 15:47:28 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.oldId = result.torevid;
|
2024-04-26 02:35:12 +00:00
|
|
|
nextDelay = 5000;
|
2024-08-28 16:42:12 +00:00
|
|
|
}, ( code, data ) => {
|
|
|
|
if ( code === 'http' && data.textStatus === 'abort' ) {
|
|
|
|
aborted = true;
|
|
|
|
} else {
|
|
|
|
// Wait longer next time in case of error
|
|
|
|
nextDelay = nextDelay * 1.5;
|
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
} ).always( () => {
|
2024-08-28 16:42:12 +00:00
|
|
|
if ( this.isTornDown || aborted ) {
|
2022-12-01 21:09:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-26 02:35:12 +00:00
|
|
|
// Stop polling after too many errors
|
|
|
|
if ( nextDelay < 1000 * 60 * 60 ) {
|
|
|
|
this.pollTimeout = setTimeout( this.startPoll.bind( this, nextDelay ), nextDelay );
|
|
|
|
}
|
2022-02-02 16:18:01 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-04-19 22:07:35 +00:00
|
|
|
return mw.loader.using( controller.getReplyWidgetModules() )
|
|
|
|
.then( () => require( 'ext.discussionTools.ReplyWidget' )[ visual ? 'ReplyWidgetVisual' : '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 ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
return this.getReplyWidgetClass( config.mode === 'visual' )
|
|
|
|
.then( ( ReplyWidget ) => new ReplyWidget( this, 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, {
|
2024-01-05 15:45:46 +00:00
|
|
|
teardown: 'onReplyWidgetTeardown',
|
2022-02-02 16:18:01 +00:00
|
|
|
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 );
|
2024-05-02 14:05:45 +00:00
|
|
|
replyWidget.connect( this, { submit: 'onReplySubmit' } );
|
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
|
|
|
};
|
|
|
|
|
2023-01-24 12:11:45 +00:00
|
|
|
CommentController.prototype.storeEditSummary = function () {
|
|
|
|
if ( this.replyWidget ) {
|
2023-06-13 22:55:08 +00:00
|
|
|
this.replyWidget.storage.set( 'summary', this.replyWidget.getEditSummary() );
|
2023-01-24 12:11:45 +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();
|
|
|
|
};
|
|
|
|
|
2024-01-05 15:26:19 +00:00
|
|
|
/**
|
|
|
|
* Scroll the widget into view and focus it
|
|
|
|
*/
|
2021-01-25 22:21:34 +00:00
|
|
|
CommentController.prototype.showAndFocus = function () {
|
2024-01-05 15:26:19 +00:00
|
|
|
if ( this.replyWidgetPromise ) {
|
2024-04-18 18:37:58 +00:00
|
|
|
this.replyWidgetPromise.then( ( replyWidget ) => {
|
2024-01-05 15:26:19 +00:00
|
|
|
replyWidget.scrollElementIntoView( { padding: scrollPadding } )
|
2024-04-18 18:37:58 +00:00
|
|
|
.then( () => {
|
|
|
|
this.focus();
|
2024-01-05 15:26:19 +00:00
|
|
|
} );
|
2022-07-07 12:42:12 +00:00
|
|
|
} );
|
2024-01-05 15:26:19 +00:00
|
|
|
}
|
2021-01-25 22:21:34 +00:00
|
|
|
};
|
|
|
|
|
2024-01-05 15:51:49 +00:00
|
|
|
/**
|
|
|
|
* Try to tear down the reply widget, if it is setup
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Resolves when the widget is torn down, rejects if it fails.
|
|
|
|
*/
|
|
|
|
CommentController.prototype.tryTeardown = function () {
|
|
|
|
if ( this.replyWidgetPromise ) {
|
2024-04-19 22:07:35 +00:00
|
|
|
return this.replyWidgetPromise.then( ( replyWidget ) => replyWidget.tryTeardown() );
|
2024-01-05 15:51:49 +00:00
|
|
|
}
|
|
|
|
return $.Deferred().resolve().promise();
|
|
|
|
};
|
|
|
|
|
2024-01-05 15:45:46 +00:00
|
|
|
/**
|
|
|
|
* Handle teardown events from the reply widget
|
|
|
|
*
|
|
|
|
* @param {string} mode Teardown mode. See dt.ui.ReplyWidget#teardown
|
|
|
|
*/
|
|
|
|
CommentController.prototype.onReplyWidgetTeardown = function ( mode ) {
|
2022-10-16 12:20:23 +00:00
|
|
|
$( this.newListItem ).parents( '.collapsible-block' ).prev().removeClass( 'collapsible-heading-disabled' );
|
|
|
|
|
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
|
2024-05-02 15:24:20 +00:00
|
|
|
* @param {Object} extraParams Extra params to pass to the API
|
2021-12-20 17:25:54 +00:00
|
|
|
* @return {Object.<string,string>} API query data
|
2020-08-29 12:00:51 +00:00
|
|
|
*/
|
2024-05-02 15:24:20 +00:00
|
|
|
CommentController.prototype.getApiQuery = function ( pageName, checkboxes, extraParams ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const threadItem = this.getThreadItem();
|
|
|
|
const replyWidget = this.replyWidget;
|
|
|
|
const sameNameComments = this.threadItemSet.findCommentsByName( threadItem.name );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const mode = replyWidget.getMode();
|
|
|
|
const tags = [
|
2021-04-16 16:04:57 +00:00
|
|
|
'discussiontools',
|
|
|
|
'discussiontools-reply',
|
|
|
|
'discussiontools-' + mode
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( mode === 'source' && enable2017Wikitext ) {
|
|
|
|
tags.push( 'discussiontools-source-enhanced' );
|
|
|
|
}
|
|
|
|
|
2024-05-02 15:24:20 +00:00
|
|
|
const data = Object.assign( {
|
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' ),
|
2024-03-25 18:26:53 +00:00
|
|
|
// Pass through dtenable query string param from original request
|
|
|
|
dtenable: new URLSearchParams( location.search ).get( 'dtenable' ) ? '1' : undefined,
|
2023-03-18 02:29:27 +00:00
|
|
|
dttags: tags.join( ',' )
|
2024-05-02 15:24:20 +00:00
|
|
|
}, extraParams );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
if ( replyWidget.getMode() === 'source' ) {
|
|
|
|
data.wikitext = replyWidget.getValue();
|
|
|
|
} else {
|
|
|
|
data.html = replyWidget.getValue();
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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;
|
|
|
|
};
|
2024-05-02 14:05:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the reply widget being submitted
|
2024-05-02 15:24:20 +00:00
|
|
|
*
|
|
|
|
* @param {Object} extraParams Extra params to pass to the API
|
2024-05-02 14:05:45 +00:00
|
|
|
*/
|
2024-05-02 15:24:20 +00:00
|
|
|
CommentController.prototype.onReplySubmit = function ( extraParams ) {
|
2024-05-02 14:05:45 +00:00
|
|
|
if ( !this.replyWidget ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replyWidget.clearSaveErrorMessage();
|
|
|
|
|
|
|
|
this.saveInitiated = mw.now();
|
|
|
|
this.replyWidget.setPending( true );
|
|
|
|
|
|
|
|
mw.track( 'editAttemptStep', { action: 'saveIntent' } );
|
|
|
|
mw.track( 'editAttemptStep', { action: 'saveAttempt' } );
|
|
|
|
|
|
|
|
// TODO: When editing a transcluded page, VE API returning the page HTML is a waste, since we won't use it
|
2024-05-02 15:24:20 +00:00
|
|
|
this.save( this.replyWidget.pageName, extraParams )
|
2024-05-02 14:05:45 +00:00
|
|
|
.then( null, this.saveFail.bind( this ) )
|
|
|
|
.always( () => {
|
|
|
|
this.replyWidget.setPending( false );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle save failures from the API
|
|
|
|
*
|
|
|
|
* @param {string} code Error code
|
|
|
|
* @param {Object} data Error data
|
|
|
|
*/
|
|
|
|
CommentController.prototype.saveFail = function ( code, data ) {
|
2024-08-28 16:42:12 +00:00
|
|
|
this.startPoll();
|
|
|
|
|
2024-05-02 14:05:45 +00:00
|
|
|
const captchaData = OO.getProp( data, 'discussiontoolsedit', 'edit', 'captcha' );
|
|
|
|
|
|
|
|
if ( captchaData ) {
|
|
|
|
code = 'captcha';
|
2024-08-27 21:53:54 +00:00
|
|
|
this.replyWidget.clearCaptcha();
|
2024-05-02 14:05:45 +00:00
|
|
|
this.replyWidget.setCaptcha( captchaData );
|
|
|
|
} else {
|
2024-08-26 14:15:21 +00:00
|
|
|
this.replyWidget.setSaveErrorMessage( code, data );
|
2024-05-02 14:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( code instanceof Error ) {
|
|
|
|
code = 'exception';
|
|
|
|
}
|
|
|
|
// Log more precise error codes, mw.Api just gives us 'http' in all of these cases
|
|
|
|
if ( data ) {
|
|
|
|
if ( data.textStatus === 'timeout' || data.textStatus === 'abort' || data.textStatus === 'parsererror' ) {
|
|
|
|
code = data.textStatus;
|
|
|
|
} else if ( data.xhr ) {
|
|
|
|
code = 'http-' + ( data.xhr.status || 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare to ve.init.mw.ArticleTargetEvents.js in VisualEditor.
|
|
|
|
const typeMap = {
|
|
|
|
badtoken: 'userBadToken',
|
|
|
|
assertanonfailed: 'userNewUser',
|
|
|
|
assertuserfailed: 'userNewUser',
|
|
|
|
assertnameduserfailed: 'userNewUser',
|
|
|
|
'abusefilter-disallowed': 'extensionAbuseFilter',
|
|
|
|
'abusefilter-warning': 'extensionAbuseFilter',
|
|
|
|
captcha: 'extensionCaptcha',
|
|
|
|
spamblacklist: 'extensionSpamBlacklist',
|
|
|
|
'titleblacklist-forbidden': 'extensionTitleBlacklist',
|
|
|
|
pagedeleted: 'editPageDeleted',
|
|
|
|
editconflict: 'editConflict'
|
|
|
|
};
|
|
|
|
mw.track( 'editAttemptStep', {
|
|
|
|
action: 'saveFailure',
|
|
|
|
timing: mw.now() - this.saveInitiated,
|
|
|
|
message: code,
|
|
|
|
type: typeMap[ code ] || 'responseUnknown'
|
|
|
|
} );
|
|
|
|
};
|
2020-08-29 12:00:51 +00:00
|
|
|
|
2021-12-20 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* Save the comment in the comment controller
|
|
|
|
*
|
|
|
|
* @param {string} pageName Page title
|
2024-05-02 15:24:20 +00:00
|
|
|
* @param {Object} extraParams Extra params to pass to the API
|
2021-12-20 17:06:24 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves when the save is complete
|
|
|
|
*/
|
2024-05-02 15:24:20 +00:00
|
|
|
CommentController.prototype.save = function ( pageName, extraParams ) {
|
2024-08-28 16:42:12 +00:00
|
|
|
this.stopPoll();
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
const replyWidget = this.replyWidget,
|
2021-12-20 17:06:24 +00:00
|
|
|
threadItem = this.getThreadItem();
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
return this.replyWidget.checkboxesPromise.then( ( checkboxes ) => {
|
2024-05-02 15:24:20 +00:00
|
|
|
const data = this.getApiQuery( pageName, checkboxes, extraParams );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2023-06-12 20:19:12 +00:00
|
|
|
if (
|
|
|
|
// We're saving the first comment on a page that previously didn't exist.
|
|
|
|
// Don't fetch the new revision's HTML content, because we will reload the whole page.
|
|
|
|
!mw.config.get( 'wgRelevantArticleId' ) ||
|
|
|
|
// We're saving a comment on a different page than the one being viewed.
|
|
|
|
// Don't fetch the new revision's HTML content, because we can't use it anyway.
|
|
|
|
pageName !== mw.config.get( 'wgRelevantPageName' )
|
|
|
|
) {
|
|
|
|
data.nocontent = true;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:07:55 +00:00
|
|
|
if ( replyWidget.commentDetails.wouldAutoCreate ) {
|
|
|
|
// This means that we might need to redirect to an opaque URL,
|
|
|
|
// so we must set up query parameters we want ahead of time.
|
|
|
|
data.returnto = pageName;
|
2024-05-24 12:20:50 +00:00
|
|
|
const params = new URLSearchParams();
|
2024-04-18 18:37:58 +00:00
|
|
|
params.set( 'dtrepliedto', this.getThreadItem().id );
|
2023-08-24 15:36:44 +00:00
|
|
|
params.set( 'dttempusercreated', '1' );
|
2023-06-15 00:07:55 +00:00
|
|
|
data.returntoquery = params.toString();
|
|
|
|
}
|
|
|
|
|
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)
|
2024-05-24 12:20:50 +00:00
|
|
|
const defaults = OO.copy( controller.getApi().defaults );
|
2021-09-09 21:37:28 +00:00
|
|
|
defaults.ajax.timeout = 0;
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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 }
|
2024-04-18 18:37:58 +00:00
|
|
|
).catch( ( code, responseData ) => {
|
2024-08-27 21:44:51 +00:00
|
|
|
if ( code === 'assertanonfailed' ) {
|
|
|
|
// Reattempt the save when something already created a temporary account (T368263)
|
|
|
|
return controller.getApi().get( {
|
|
|
|
meta: 'userinfo'
|
|
|
|
} ).then( ( resp ) => {
|
|
|
|
if ( !resp.query.userinfo.temp ) {
|
|
|
|
// Return the original error response
|
|
|
|
return $.Deferred().reject( code, responseData ).promise();
|
|
|
|
}
|
|
|
|
// Set new parameters and retry
|
|
|
|
data.assert = 'user';
|
|
|
|
data.assertuser = resp.query.userinfo.name;
|
|
|
|
return mw.libs.ve.targetSaver.postContent(
|
|
|
|
data, { api: noTimeoutApi }
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return $.Deferred().reject( code, responseData ).promise();
|
|
|
|
} ).catch( ( 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();
|
2024-04-18 18:37:58 +00:00
|
|
|
} ).then( ( 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 ) {
|
|
|
|
// Add new comments
|
|
|
|
this.newComments.push.apply( this.newComments, addedComments );
|
|
|
|
|
|
|
|
// Delete any comments which have since been deleted (e.g. posted then reverted)
|
2024-05-24 12:20:50 +00:00
|
|
|
const removedCommentIds = removedComments.filter( ( cmt ) => cmt.id );
|
2024-04-19 22:07:35 +00:00
|
|
|
this.newComments = this.newComments.filter(
|
2022-02-02 16:18:01 +00:00
|
|
|
// If comment ID is not in removedCommentIds, keep it
|
2024-04-19 22:07:35 +00:00
|
|
|
( cmt ) => removedCommentIds.indexOf( cmt.id ) === -1
|
|
|
|
);
|
2022-02-02 16:18:01 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.replyWidgetPromise.then( ( replyWidget ) => {
|
|
|
|
replyWidget.updateNewCommentsWarning( this.newComments );
|
2022-02-02 16:18:01 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
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 ) {
|
|
|
|
this.parentRemoved = parentRemoved;
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.replyWidgetPromise.then( ( replyWidget ) => {
|
|
|
|
replyWidget.updateParentRemovedError( this.parentRemoved );
|
2022-02-08 15:47:28 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
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 () {
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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(),
|
2024-04-18 18:37:58 +00:00
|
|
|
previewDeferred = $.Deferred();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2020-06-20 12:07:49 +00:00
|
|
|
// TODO: We may need to pass oldid/etag when editing is supported
|
2024-05-24 12:20:50 +00:00
|
|
|
const 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
return $.when( wikitextPromise, this.replyWidgetPromise ).then( ( 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
return previewDeferred.then( () => {
|
2020-05-21 21:11:01 +00:00
|
|
|
// Teardown the old widget
|
2024-04-18 18:37:58 +00:00
|
|
|
oldWidget.disconnect( this );
|
2020-05-21 21:11:01 +00:00
|
|
|
oldWidget.teardown();
|
|
|
|
|
2021-02-15 20:59:32 +00:00
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.setupReplyWidget( replyWidget, {
|
2020-09-25 17:08:08 +00:00
|
|
|
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 );
|
|
|
|
|
2024-04-19 22:07:35 +00:00
|
|
|
wikitext = wikitext.split( '\n' ).map( ( line ) => indent + line ).join( '\n' );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
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 ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const children = Array.prototype.slice.call( rootNode.childNodes );
|
2020-08-29 12:00:51 +00:00
|
|
|
// There may be multiple lists when some lines are template generated
|
2024-04-18 18:37:58 +00:00
|
|
|
children.forEach( ( child ) => {
|
2020-08-29 12:00:51 +00:00
|
|
|
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 () {
|
2024-05-24 12:43:07 +00:00
|
|
|
const oldWidget = this.replyWidget,
|
2020-09-25 17:08:08 +00:00
|
|
|
oldShowAdvanced = oldWidget.showAdvanced,
|
2024-05-24 12:43:07 +00:00
|
|
|
oldEditSummary = oldWidget.getEditSummary();
|
|
|
|
let wikitext = oldWidget.getValue();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let 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
|
2024-04-19 22:07:35 +00:00
|
|
|
} ).then( ( response ) => response && response.visualeditor.content );
|
2020-04-27 16:23:27 +00:00
|
|
|
} 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
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
return $.when( parsePromise, this.replyWidgetPromise ).then( ( html, replyWidget ) => {
|
2024-05-24 12:20:50 +00:00
|
|
|
const unsupportedSelectors = this.getUnsupportedNodeSelectors();
|
2020-04-27 16:23:27 +00:00
|
|
|
|
2024-05-24 12:20:50 +00:00
|
|
|
let 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
|
2024-05-24 12:20:50 +00:00
|
|
|
for ( const type in unsupportedSelectors ) {
|
2020-06-24 20:07:34 +00:00
|
|
|
if ( doc.querySelector( unsupportedSelectors[ type ] ) ) {
|
2024-05-24 12:20:50 +00:00
|
|
|
const $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'
|
|
|
|
}
|
|
|
|
);
|
2023-03-18 02:29:27 +00:00
|
|
|
mw.track( 'visualEditorFeatureUse', {
|
2020-07-28 17:19:36 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 18:37:58 +00:00
|
|
|
this.undoIndentReplacements( doc.body );
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 20:07:34 +00:00
|
|
|
// Teardown the old widget
|
2024-04-18 18:37:58 +00:00
|
|
|
oldWidget.disconnect( this );
|
2020-06-24 20:07:34 +00:00
|
|
|
oldWidget.teardown();
|
|
|
|
|
2021-02-15 20:59:32 +00:00
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
this.setupReplyWidget( replyWidget, {
|
2020-09-25 17:08:08 +00:00
|
|
|
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;
|