mediawiki-extensions-Discus.../modules/dt.ui.ReplyWidgetVisual.js
Ed Sanders 2f1cf65233 Option to integrate VisualEditor instead of textarea
* Add config option $wgDiscussionToolsUseVisualEditor (default false).
* Add new modules ext.discussionTools.ReplyWidgetPlain and ...ReplyWidgetVisual,
  replacing ...ReplyWidget. Load only one of them depending on the config.

TODO:
* Also add the visual mode of VisualEditor, this only uses NWE now.
  There is already code to support saving from it, but no mode
  switcher tool

Co-Authored-By: Ed Sanders <esanders@wikimedia.org>
Co-Authored-By: Bartosz Dziewoński <matma.rex@gmail.com>
Change-Id: I9b6db865d51baf400fb715dc7aa68ccd8cdd4905
2020-01-07 22:15:03 +00:00

98 lines
2.9 KiB
JavaScript

var
controller = require( 'ext.discussionTools.controller' ),
modifier = require( 'ext.discussionTools.modifier' ),
CommentTargetWidget = require( './CommentTargetWidget.js' );
/**
* DiscussionTools ReplyWidgetVisual class
*
* @class mw.dt.ReplyWidgetVisual
* @extends mw.dt.ReplyWidget
* @constructor
* @param {Object} comment Parsed comment object
* @param {Object} [config] Configuration options
*/
function ReplyWidgetVisual( comment, config ) {
// Parent constructor
ReplyWidgetVisual.super.call( this, comment, config );
}
/* Inheritance */
OO.inheritClass( ReplyWidgetVisual, require( 'ext.discussionTools.ReplyWidget' ) );
/* Methods */
ReplyWidgetVisual.prototype.createReplyBodyWidget = function ( config ) {
return new CommentTargetWidget( $.extend( {
defaultMode: 'source'
}, config ) );
};
ReplyWidgetVisual.prototype.getValue = function () {
return this.replyBodyWidget.target.getSurface().getModel().getDom();
};
ReplyWidgetVisual.prototype.clear = function () {
this.replyBodyWidget.clear();
};
ReplyWidgetVisual.prototype.isEmpty = function () {
var surface = this.replyBodyWidget.target.getSurface();
return !surface || !surface.getModel().hasBeenModified();
};
ReplyWidgetVisual.prototype.setup = function () {
// Parent method
ReplyWidgetVisual.super.prototype.setup.call( this );
this.replyBodyWidget.setDocument( '<p></p>' );
this.mode = this.replyBodyWidget.target.getSurface().getMode();
// Events
this.replyBodyWidget.target.getSurface().getModel().getDocument().connect( this, { transact: this.onInputChangeThrottled } );
this.replyBodyWidget.target.getSurface().connect( this, { submit: 'onReplyClick' } );
};
ReplyWidgetVisual.prototype.focus = function () {
var targetWidget = this.replyBodyWidget;
setTimeout( function () {
targetWidget.getSurface().getModel().selectLastContentOffset();
targetWidget.focus();
} );
};
ReplyWidgetVisual.prototype.setPending = function ( pending ) {
ReplyWidgetVisual.super.prototype.setPending.call( this, pending );
if ( pending ) {
// TODO
// this.replyBodyWidget.pushPending();
this.replyBodyWidget.setReadOnly( true );
} else {
// this.replyBodyWidget.popPending();
this.replyBodyWidget.setReadOnly( false );
}
};
ReplyWidgetVisual.prototype.insertNewNodes = function ( parentNode ) {
var wikitext, newParsoidItem,
surface = this.replyBodyWidget.getSurface();
if ( surface.getMode() === 'source' ) {
wikitext = controller.autoSignWikitext( this.getValue() );
wikitext.split( '\n' ).forEach( function ( line ) {
var lineItem = modifier.addListItem( parentNode );
lineItem.appendChild( modifier.createWikitextNode( line ) );
} );
} else {
newParsoidItem = modifier.addListItem( parentNode );
// TODO: Support multi-line comments in visual mode
newParsoidItem.innerHTML = surface.getHtml();
newParsoidItem.children[ 0 ].appendChild( modifier.createWikitextNode( ' ~~~~' ) );
}
};
module.exports = ReplyWidgetVisual;