2020-08-29 12:00:51 +00:00
|
|
|
/**
|
|
|
|
* @external ThreadItem
|
|
|
|
*/
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
var
|
|
|
|
controller = require( './controller.js' ),
|
|
|
|
modifier = require( './modifier.js' ),
|
|
|
|
logger = require( './logger.js' ),
|
|
|
|
storage = mw.storage.session,
|
|
|
|
scrollPadding = { top: 10, 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
|
|
|
|
|
|
|
// 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-02-12 19:16:13 +00:00
|
|
|
function CommentController( $pageContainer, $replyLink, comment, parser ) {
|
2020-04-29 16:43:11 +00:00
|
|
|
var mode;
|
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
this.$pageContainer = $pageContainer;
|
|
|
|
this.comment = comment;
|
2021-02-12 19:16:13 +00:00
|
|
|
this.parser = parser;
|
2020-04-30 13:20:41 +00:00
|
|
|
this.newListItem = null;
|
|
|
|
this.replyWidgetPromise = null;
|
2020-08-29 12:00:51 +00:00
|
|
|
this.onReplyLinkClickHandler = this.onReplyLinkClick.bind( this );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
// Reply
|
2020-08-29 12:00:51 +00:00
|
|
|
this.$replyLink = $replyLink.on( 'click keypress', this.onReplyLinkClickHandler );
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$replyLinkButtons = $replyLink.closest( '.ext-discussiontools-init-replylink-buttons, .dt-init-replylink-buttons' );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
if ( storage.get( 'reply/' + comment.id + '/saveable' ) ) {
|
|
|
|
mode = storage.get( 'reply/' + comment.id + '/mode' );
|
2021-01-26 01:50:25 +00:00
|
|
|
this.setup( mode, true );
|
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;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-19 20:03:41 +00:00
|
|
|
* Like #checkCommentOnPage, 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.
|
|
|
|
*
|
2020-08-19 20:03:41 +00:00
|
|
|
* @param {string} commentId Comment ID
|
|
|
|
* @return {jQuery.Promise} Promise which resolves with pageName+oldId, or rejects with an error
|
2020-04-30 13:20:41 +00:00
|
|
|
*/
|
2020-08-29 12:00:51 +00:00
|
|
|
CommentController.prototype.getTranscludedFromSource = function ( commentId ) {
|
2020-04-30 13:20:41 +00:00
|
|
|
var promise,
|
|
|
|
pageName = mw.config.get( 'wgRelevantPageName' ),
|
|
|
|
oldId = mw.config.get( 'wgCurRevisionId' );
|
|
|
|
|
|
|
|
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
|
2020-08-19 20:03:41 +00:00
|
|
|
return controller.checkCommentOnPage( errorData.transcludedFrom, latestRevId, commentId )
|
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)
|
2020-08-19 20:03:41 +00:00
|
|
|
promise = controller.checkCommentOnPage( pageName, oldId, commentId )
|
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 */
|
|
|
|
|
|
|
|
CommentController.prototype.onReplyLinkClick = function ( e ) {
|
|
|
|
if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) {
|
|
|
|
// Only handle keypresses on the "Enter" or "Space" keys
|
|
|
|
return;
|
|
|
|
}
|
2021-01-19 19:45:42 +00:00
|
|
|
if ( e.type === 'click' && ( e.which !== OO.ui.MouseButtons.LEFT || e.shiftKey || e.altKey || e.ctrlKey || e.metaKey ) ) {
|
|
|
|
// Only handle unmodified left clicks
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
2021-01-25 22:21:34 +00:00
|
|
|
this.emit( 'link-click' );
|
2020-04-30 13:20:41 +00:00
|
|
|
};
|
|
|
|
|
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-01-26 01:50:25 +00:00
|
|
|
* @param {boolean} [hideErrors]
|
2020-04-29 16:43:11 +00:00
|
|
|
*/
|
2021-01-26 01:50:25 +00:00
|
|
|
CommentController.prototype.setup = function ( mode, hideErrors ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
var comment = this.comment,
|
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
|
|
|
}
|
|
|
|
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$pageContainer.addClass( 'ext-discussiontools-init-replylink-open' );
|
2020-04-30 13:20:41 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2021-03-13 14:39:39 +00:00
|
|
|
$( '.ext-discussiontools-init-replylink-reply, .dt-init-replylink-reply' ).attr( {
|
2020-04-30 13:20:41 +00:00
|
|
|
tabindex: '-1'
|
|
|
|
} );
|
2020-04-15 06:35:00 +00:00
|
|
|
// Suppress page takeover behavior for VE editing so that our unload
|
|
|
|
// handler can warn of data loss.
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-edit, #ca-ve-edit, .mw-editsection a, #ca-addsection' ).off( '.ve-target' );
|
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
|
|
|
} );
|
|
|
|
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$replyLinkButtons.addClass( 'ext-discussiontools-init-replylink-active' );
|
2020-04-30 13:20:41 +00:00
|
|
|
|
|
|
|
if ( !this.replyWidgetPromise ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
this.replyWidgetPromise = this.getTranscludedFromSource( comment.id ).then( function ( pageData ) {
|
2021-03-18 14:14:23 +00:00
|
|
|
return commentController.createReplyWidget( comment, pageData.pageName, pageData.oldId, { 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' }
|
|
|
|
);
|
|
|
|
}
|
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
|
2020-08-19 20:03:41 +00:00
|
|
|
commentController.newListItem = modifier.addListItem( comment );
|
2020-04-30 13:20:41 +00:00
|
|
|
$( commentController.newListItem ).text( mw.msg( 'discussiontools-replywidget-loading' ) );
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
commentController.replyWidgetPromise.then( function ( replyWidget ) {
|
|
|
|
if ( !commentController.newListItem ) {
|
|
|
|
// On subsequent loads, there's no list item yet, so create one now
|
2020-08-19 20:03:41 +00:00
|
|
|
commentController.newListItem = modifier.addListItem( comment );
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
|
|
|
$( commentController.newListItem ).empty().append( replyWidget.$element );
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget );
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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-03-18 14:14:23 +00:00
|
|
|
CommentController.prototype.createReplyWidget = function ( comment, pageName, oldId, config ) {
|
2020-04-30 13:20:41 +00:00
|
|
|
var commentController = this;
|
2021-03-18 14:14:23 +00:00
|
|
|
return this.getReplyWidgetClass( config.mode === 'visual' ).then( function ( ReplyWidget ) {
|
2020-08-25 12:31:54 +00:00
|
|
|
return new ReplyWidget( commentController, comment, pageName, oldId, config );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
CommentController.prototype.setupReplyWidget = function ( replyWidget, data ) {
|
2020-04-27 16:23:27 +00:00
|
|
|
replyWidget.connect( this, { teardown: 'teardown' } );
|
|
|
|
|
2020-09-25 17:08:08 +00:00
|
|
|
replyWidget.setup( data );
|
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;
|
2021-01-25 22:21:34 +00:00
|
|
|
this.focus();
|
2021-03-24 12:58:28 +00:00
|
|
|
// Calling can trigger a scroll-into-view withing VE, so wait for this to
|
|
|
|
// happen so it doesn't cancel our scrolling of the whole widget into view
|
|
|
|
setTimeout( function () {
|
|
|
|
commentController.replyWidget.scrollElementIntoView( { padding: scrollPadding } );
|
|
|
|
} );
|
2021-01-25 22:21:34 +00:00
|
|
|
};
|
|
|
|
|
2020-06-11 16:36:00 +00:00
|
|
|
CommentController.prototype.teardown = function ( abandoned ) {
|
2021-03-13 14:39:39 +00:00
|
|
|
this.$replyLinkButtons.removeClass( 'ext-discussiontools-init-replylink-active' );
|
|
|
|
this.$pageContainer.removeClass( 'ext-discussiontools-init-replylink-open' );
|
2020-04-30 13:20:41 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2021-03-13 14:39:39 +00:00
|
|
|
$( '.ext-discussiontools-init-replylink-reply, .dt-init-replylink-reply' ).attr( {
|
2020-04-30 13:20:41 +00:00
|
|
|
tabindex: '0'
|
|
|
|
} );
|
2020-04-15 06:35:00 +00:00
|
|
|
// We deliberately mangled edit links earlier so VE can't steal our page;
|
|
|
|
// have it redo setup to fix those.
|
|
|
|
if ( mw.libs.ve && mw.libs.ve.setupEditLinks ) {
|
|
|
|
mw.libs.ve.setupEditLinks();
|
2020-08-29 12:00:51 +00:00
|
|
|
// Disable VisualEditor's new section editor (in wikitext mode / NWE), to allow our own
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-addsection' ).off( '.ve-target' );
|
2020-04-15 06:35:00 +00:00
|
|
|
}
|
2020-05-15 00:23:50 +00:00
|
|
|
modifier.removeAddedListItem( this.newListItem );
|
2020-04-30 13:20:41 +00:00
|
|
|
this.newListItem = null;
|
2020-06-11 16:36:00 +00:00
|
|
|
if ( abandoned ) {
|
|
|
|
this.$replyLink.trigger( 'focus' );
|
|
|
|
}
|
2021-01-25 22:21:34 +00:00
|
|
|
this.emit( 'teardown' );
|
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 {ThreadItem} comment Parent comment
|
|
|
|
* @param {string} pageName Title of the page to post on
|
|
|
|
* @param {Object} checkboxes Value of the promise returned by controller#getCheckboxesPromise
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
CommentController.prototype.getApiQuery = function ( comment, pageName, checkboxes ) {
|
2021-02-12 19:16:13 +00:00
|
|
|
var captchaInput, replyWidget, data, sameNameComments;
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
replyWidget = this.replyWidget;
|
2021-02-12 19:16:13 +00:00
|
|
|
sameNameComments = this.parser.findCommentsByName( comment.name );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
action: 'discussiontoolsedit',
|
|
|
|
paction: 'addcomment',
|
|
|
|
page: pageName,
|
2021-02-12 19:16:13 +00:00
|
|
|
commentname: comment.name,
|
|
|
|
// Only specify this if necessary to disambiguate, to avoid errors if the parent changes
|
|
|
|
commentid: sameNameComments.length > 1 ? comment.id : undefined,
|
2020-08-29 12:00:51 +00:00
|
|
|
summary: replyWidget.getEditSummary(),
|
|
|
|
assert: mw.user.isAnon() ? 'anon' : 'user',
|
|
|
|
assertuser: mw.user.getName() || undefined,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
|
|
|
// HACK: Always display reply links afterwards, ignoring preferences etc., in case this was
|
|
|
|
// a page view with reply links forced with ?dtenable=1 or otherwise
|
|
|
|
dtenable: '1',
|
|
|
|
dttags: [
|
|
|
|
'discussiontools',
|
|
|
|
'discussiontools-reply',
|
|
|
|
'discussiontools-' + replyWidget.getMode()
|
|
|
|
].join( ',' )
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( replyWidget.getMode() === 'source' ) {
|
|
|
|
data.wikitext = replyWidget.getValue();
|
|
|
|
} else {
|
|
|
|
data.html = replyWidget.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
captchaInput = replyWidget.captchaInput;
|
|
|
|
if ( captchaInput ) {
|
|
|
|
data.captchaid = captchaInput.getCaptchaId();
|
|
|
|
data.captchaword = captchaInput.getCaptchaWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( checkboxes.checkboxesByName.wpWatchthis ) {
|
|
|
|
data.watchlist = checkboxes.checkboxesByName.wpWatchthis.isSelected() ?
|
|
|
|
'watch' :
|
|
|
|
'unwatch';
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
CommentController.prototype.save = function ( comment, pageName ) {
|
|
|
|
var replyWidget = this.replyWidget,
|
2020-04-30 13:20:41 +00:00
|
|
|
commentController = this;
|
|
|
|
|
2020-06-26 21:47:56 +00:00
|
|
|
return this.replyWidget.checkboxesPromise.then( function ( checkboxes ) {
|
2020-10-23 11:02:18 +00:00
|
|
|
var defaults, noTimeoutApi,
|
2020-08-29 12:00:51 +00:00
|
|
|
data = commentController.getApiQuery( comment, 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)
|
|
|
|
defaults = OO.copy( controller.getApi().defaults );
|
|
|
|
defaults.timeout = 0;
|
|
|
|
noTimeoutApi = new mw.Api( defaults );
|
|
|
|
|
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 ) {
|
|
|
|
controller.update( responseData, comment, pageName, replyWidget );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
CommentController.prototype.switchToWikitext = function () {
|
|
|
|
var wikitextPromise,
|
|
|
|
oldWidget = this.replyWidget,
|
|
|
|
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
|
|
|
|
wikitextPromise = target.getWikitextFragment( target.getSurface().getModel().getDocument() );
|
2020-08-25 12:31:54 +00:00
|
|
|
this.replyWidgetPromise = this.createReplyWidget(
|
|
|
|
oldWidget.comment,
|
|
|
|
oldWidget.pageName,
|
|
|
|
oldWidget.oldId,
|
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.)
|
|
|
|
*
|
|
|
|
* @return {Object} Map of type used for error messages (string) to CSS selector (string)
|
|
|
|
*/
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
CommentController.prototype.switchToVisual = function () {
|
|
|
|
var parsePromise,
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
|
2020-04-27 16:23:27 +00:00
|
|
|
if ( wikitext ) {
|
2021-02-17 23:42:57 +00:00
|
|
|
wikitext = this.doIndentReplacements( wikitext, ':' );
|
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(
|
|
|
|
oldWidget.comment,
|
|
|
|
oldWidget.pageName,
|
|
|
|
oldWidget.oldId,
|
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 ) {
|
2020-08-29 12:00:51 +00:00
|
|
|
var doc, type, $msg,
|
|
|
|
unsupportedSelectors = commentController.getUnsupportedNodeSelectors();
|
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
|
2020-06-24 20:07:34 +00:00
|
|
|
for ( type in unsupportedSelectors ) {
|
|
|
|
if ( doc.querySelector( unsupportedSelectors[ type ] ) ) {
|
|
|
|
$msg = $( '<div>' ).html(
|
|
|
|
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;
|