2020-04-30 13:20:41 +00:00
|
|
|
var
|
2020-06-11 17:27:30 +00:00
|
|
|
api = new mw.Api( { parameters: { formatversion: 2 } } ),
|
2020-04-30 13:20:41 +00:00
|
|
|
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',
|
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-05-07 19:16:14 +00:00
|
|
|
if ( defaultVisual ) {
|
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
|
|
|
}
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
function CommentController( $pageContainer, comment ) {
|
2020-04-29 16:43:11 +00:00
|
|
|
var mode;
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
this.$pageContainer = $pageContainer;
|
|
|
|
this.comment = comment;
|
|
|
|
this.newListItem = null;
|
|
|
|
this.replyWidgetPromise = null;
|
|
|
|
|
|
|
|
this.$replyLinkButtons = $( '<span>' )
|
|
|
|
.addClass( 'dt-init-replylink-buttons' );
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
this.$replyLink = $( '<a>' )
|
|
|
|
.addClass( 'dt-init-replylink-reply' )
|
|
|
|
.text( mw.msg( 'discussiontools-replylink' ) )
|
|
|
|
.attr( {
|
|
|
|
role: 'button',
|
|
|
|
tabindex: '0'
|
|
|
|
} )
|
|
|
|
.on( 'click keypress', this.onReplyLinkClick.bind( this ) );
|
|
|
|
|
2020-06-09 18:55:14 +00:00
|
|
|
this.$replyLinkButtons.append(
|
|
|
|
$( '<span>' ).addClass( 'dt-init-replylink-bracket' ).text( '[' ),
|
|
|
|
this.$replyLink,
|
|
|
|
$( '<span>' ).addClass( 'dt-init-replylink-bracket' ).text( ']' )
|
|
|
|
);
|
2020-04-30 13:20:41 +00:00
|
|
|
modifier.addReplyLink( comment, this.$replyLinkButtons[ 0 ] );
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
if ( storage.get( 'reply/' + comment.id + '/saveable' ) ) {
|
|
|
|
mode = storage.get( 'reply/' + comment.id + '/mode' );
|
|
|
|
this.setup( mode );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.initClass( CommentController );
|
|
|
|
|
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-06-03 22:22:32 +00:00
|
|
|
return api.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-19 20:03:41 +00:00
|
|
|
function getTranscludedFromSource( 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
this.setup();
|
|
|
|
};
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
/**
|
|
|
|
* Create and setup the reply widget
|
|
|
|
*
|
|
|
|
* @param {string} [mode] Optionally force a mode, 'visual' or 'source'
|
|
|
|
*/
|
|
|
|
CommentController.prototype.setup = function ( mode ) {
|
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
|
|
|
}
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
// TODO: Allow users to use multiple reply widgets simultaneously.
|
|
|
|
// Currently submitting a reply from one widget would also destroy the other ones.
|
|
|
|
// eslint-disable-next-line no-jquery/no-class-state
|
|
|
|
if ( this.$pageContainer.hasClass( 'dt-init-replylink-open' ) ) {
|
|
|
|
// Support: IE 11
|
|
|
|
// On other browsers, the link is made unclickable using 'pointer-events' in CSS
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.$pageContainer.addClass( 'dt-init-replylink-open' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '.dt-init-replylink-reply' ).attr( {
|
|
|
|
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',
|
|
|
|
type: 'page',
|
|
|
|
mechanism: 'click',
|
2020-04-29 18:45:20 +00:00
|
|
|
// TODO: Use 'wikitext-2017' when config.enable2017Wikitext is set
|
2020-04-30 13:20:41 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
2020-06-08 22:12:55 +00:00
|
|
|
editor_interface: mode === 'visual' ? 'visualeditor' : 'wikitext'
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.$replyLinkButtons.addClass( 'dt-init-replylink-active' );
|
|
|
|
|
|
|
|
if ( !this.replyWidgetPromise ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
this.replyWidgetPromise = getTranscludedFromSource( comment.id ).then( function ( pageData ) {
|
|
|
|
return commentController.createReplyWidget( comment, pageData.pageName, pageData.oldId, mode === 'visual' );
|
2020-04-30 13:20:41 +00:00
|
|
|
}, function ( code, data ) {
|
|
|
|
commentController.teardown();
|
|
|
|
|
|
|
|
OO.ui.alert(
|
2020-06-03 22:22:32 +00:00
|
|
|
code instanceof Error ? code.toString() : api.getErrorMessage( data ),
|
2020-04-30 13:20:41 +00:00
|
|
|
{ size: 'medium' }
|
|
|
|
);
|
|
|
|
|
|
|
|
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-04-29 16:43:11 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget, null, true );
|
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 ) {
|
|
|
|
if ( visual === undefined ) {
|
2020-04-29 18:45:20 +00:00
|
|
|
visual = defaultVisual;
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
CommentController.prototype.createReplyWidget = function ( comment, pageName, oldId, visual ) {
|
2020-04-30 13:20:41 +00:00
|
|
|
var commentController = this;
|
|
|
|
|
|
|
|
return this.getReplyWidgetClass( visual ).then( function ( ReplyWidget ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
return new ReplyWidget( commentController, comment, pageName, oldId );
|
2020-04-30 13:20:41 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
CommentController.prototype.setupReplyWidget = function ( replyWidget, initialValue, scrollIntoView ) {
|
2020-04-27 16:23:27 +00:00
|
|
|
replyWidget.connect( this, { teardown: 'teardown' } );
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
replyWidget.setup( initialValue );
|
2020-04-27 16:23:27 +00:00
|
|
|
if ( scrollIntoView ) {
|
|
|
|
replyWidget.scrollElementIntoView( { padding: scrollPadding } );
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
2020-04-27 16:23:27 +00:00
|
|
|
replyWidget.focus();
|
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-06-11 16:36:00 +00:00
|
|
|
CommentController.prototype.teardown = function ( abandoned ) {
|
2020-04-30 13:20:41 +00:00
|
|
|
this.$replyLinkButtons.removeClass( 'dt-init-replylink-active' );
|
|
|
|
this.$pageContainer.removeClass( 'dt-init-replylink-open' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '.dt-init-replylink-reply' ).attr( {
|
|
|
|
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-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' );
|
|
|
|
}
|
2020-04-30 13:20:41 +00:00
|
|
|
};
|
|
|
|
|
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-04-30 13:20:41 +00:00
|
|
|
var captchaInput = commentController.replyWidget.captchaInput,
|
|
|
|
data = {
|
2020-08-19 20:03:41 +00:00
|
|
|
action: 'discussiontoolsedit',
|
|
|
|
paction: 'addcomment',
|
|
|
|
page: pageName,
|
|
|
|
commentid: comment.id,
|
2020-04-30 13:20:41 +00:00
|
|
|
assert: mw.user.isAnon() ? 'anon' : 'user',
|
|
|
|
assertuser: mw.user.getName() || undefined,
|
|
|
|
dttags: [
|
|
|
|
'discussiontools',
|
|
|
|
'discussiontools-reply',
|
2020-08-19 20:03:41 +00:00
|
|
|
'discussiontools-' + replyWidget.getMode()
|
2020-04-30 13:20:41 +00:00
|
|
|
].join( ',' )
|
|
|
|
};
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
if ( replyWidget.getMode() === 'source' ) {
|
|
|
|
data.wikitext = replyWidget.getValue();
|
|
|
|
} else {
|
|
|
|
data.html = replyWidget.getValue();
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
if ( captchaInput ) {
|
|
|
|
data.captchaid = captchaInput.getCaptchaId();
|
|
|
|
data.captchaword = captchaInput.getCaptchaWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( checkboxes.checkboxesByName.wpWatchthis ) {
|
|
|
|
data.watchlist = checkboxes.checkboxesByName.wpWatchthis.isSelected() ?
|
|
|
|
'watch' :
|
|
|
|
'unwatch';
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:03:41 +00:00
|
|
|
return mw.libs.ve.targetSaver.postContent(
|
2020-04-30 13:20:41 +00:00
|
|
|
data,
|
|
|
|
{
|
|
|
|
// No timeout. Huge talk pages take a long time to save, and falsely reporting an error can
|
|
|
|
// result in duplicate messages when the user retries. (T249071)
|
2020-06-11 17:27:30 +00:00
|
|
|
api: new mw.Api( { ajax: { timeout: 0 }, parameters: { formatversion: 2 } } )
|
2020-04-30 13:20:41 +00:00
|
|
|
}
|
2020-08-26 22:46:34 +00:00
|
|
|
).catch( function ( code, responseData ) {
|
2020-08-19 20:03:41 +00:00
|
|
|
// Better user-facing error message
|
|
|
|
if ( code === 'discussiontools-commentid-notfound' ) {
|
|
|
|
return $.Deferred().reject( 'discussiontools-commentid-notfound', { errors: [ {
|
|
|
|
code: 'discussiontools-commentid-notfound',
|
|
|
|
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 ) {
|
2020-08-26 22:50:06 +00:00
|
|
|
// Pass through watchlist state.
|
|
|
|
responseData.watchlist = data.watchlist;
|
2020-08-26 22:46:34 +00:00
|
|
|
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-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-19 20:03:41 +00:00
|
|
|
this.replyWidgetPromise = this.createReplyWidget( oldWidget.comment, oldWidget.pageName, oldWidget.oldId, false );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
return $.when( wikitextPromise, this.replyWidgetPromise ).then( function ( wikitext, replyWidget ) {
|
2020-06-26 22:24:14 +00:00
|
|
|
wikitext = modifier.sanitizeWikitextLinebreaks( wikitext );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
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 () {
|
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
|
|
|
// Teardown the old widget
|
|
|
|
oldWidget.disconnect( commentController );
|
|
|
|
oldWidget.teardown();
|
|
|
|
|
|
|
|
commentController.setupReplyWidget( replyWidget, wikitext );
|
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
CommentController.prototype.switchToVisual = function () {
|
|
|
|
var parsePromise,
|
|
|
|
oldWidget = this.replyWidget,
|
|
|
|
wikitext = oldWidget.getValue(),
|
|
|
|
commentController = this;
|
|
|
|
|
2020-07-15 21:43:56 +00:00
|
|
|
wikitext = modifier.sanitizeWikitextLinebreaks( wikitext );
|
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 ) {
|
|
|
|
wikitext = wikitext.split( '\n' ).map( function ( line ) {
|
|
|
|
return ':' + line;
|
|
|
|
} ).join( '\n' );
|
|
|
|
|
|
|
|
// Based on ve.init.mw.Target#parseWikitextFragment
|
2020-06-03 22:22:32 +00:00
|
|
|
parsePromise = api.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-19 20:03:41 +00:00
|
|
|
this.replyWidgetPromise = this.createReplyWidget( oldWidget.comment, oldWidget.pageName, oldWidget.oldId, true );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
return $.when( parsePromise, this.replyWidgetPromise ).then( function ( html, replyWidget ) {
|
2020-06-24 20:07:34 +00:00
|
|
|
var doc, bodyChildren, type, $msg,
|
|
|
|
unsupportedSelectors = {
|
|
|
|
// 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
|
|
|
|
|
|
|
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-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',
|
|
|
|
action: 'dialog-prevent-' + type + '-show'
|
|
|
|
} );
|
|
|
|
|
2020-06-24 20:07:34 +00:00
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for tables, headings, images, templates
|
2020-05-26 20:47:46 +00:00
|
|
|
bodyChildren = Array.prototype.slice.call( doc.body.childNodes );
|
|
|
|
// There may be multiple lists when some lines are template generated
|
|
|
|
bodyChildren.forEach( function ( child ) {
|
|
|
|
if ( child.nodeType === Node.ELEMENT_NODE ) {
|
|
|
|
// Unwrap list
|
|
|
|
modifier.unwrapList( child );
|
|
|
|
}
|
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 20:07:34 +00:00
|
|
|
// Swap out the DOM nodes
|
|
|
|
oldWidget.$element.replaceWith( replyWidget.$element );
|
|
|
|
|
|
|
|
// Teardown the old widget
|
|
|
|
oldWidget.disconnect( commentController );
|
|
|
|
oldWidget.teardown();
|
|
|
|
|
2020-04-29 16:43:11 +00:00
|
|
|
commentController.setupReplyWidget( replyWidget, doc );
|
2020-04-27 16:23:27 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-04-30 13:20:41 +00:00
|
|
|
module.exports = CommentController;
|