mediawiki-extensions-Visual.../modules/ve-mw/ce/nodes/ve.ce.MWSignatureNode.js
Bartosz Dziewoński 9be5c85db4 Provide a tool to insert a signature in namespaces that need it
VisualEditor is usually not enabled in talk namespaces... but
sometimes it is. And when users see the button to edit with VE,
they're going to click it and expect to be able to sign their posts.

This tool is only loaded on talk pages and pages in additional
namespaces defined in $wgExtraSignatureNamespaces.

Code adapted with small tweaks from my own gadget
<https://meta.wikimedia.org/wiki/User:Matma_Rex/visualeditor-signature.js?oldid=13461327>,
which is already available under the MIT license.

Changes include:
* The tool is now always visible if the wiki allows signatures in any
  VE namespaces, but disabled when not allowed in the current namespace.
* Register '~~~~' sequence to insert the signature.
* Code style tweaks for stricter lint checks in this repository.
* Documentation corrections.

Swedish translation provided by André Costa (already credited
as a translator as Lokal_Profil).

Depends on changes in VisualEditor core:
* I89fe53890ab59d12260ea6b41de802c38c24e8b9
* I14cd7efac521687ea38580341ae08ddc522edeeb

Bug: T53154
Change-Id: I6be5fb2118cf3eef5098d4c5320228aa81411ccb
2015-11-16 18:06:32 +01:00

113 lines
2.9 KiB
JavaScript

/*!
* VisualEditor ContentEditable MWSignatureNode class.
*
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
// Update the timestamp on inserted signatures every minute.
var liveSignatures = [];
setInterval( function () {
var updatedSignatures, i, sig;
updatedSignatures = [];
for ( i = 0; i < liveSignatures.length; i++ ) {
sig = liveSignatures[ i ];
try {
sig.forceUpdate();
updatedSignatures.push( sig );
} catch ( er ) {
// Do nothing
}
}
liveSignatures = updatedSignatures;
}, 60 * 1000 );
/**
* ContentEditable MediaWiki signature node. This defines the behavior of the signature node
* inserted into the ContentEditable document.
*
* @class
* @extends ve.ce.MWTransclusionInlineNode
*
* @constructor
* @param {ve.dm.MWSignatureNode} model Model to observe
* @param {Object} [config] Configuration options
*/
ve.ce.MWSignatureNode = function VeCeMWSignatureNode( model ) {
// Parent constructor
ve.ce.MWTransclusionInlineNode.call( this, model );
// DOM changes
this.$element.addClass( 've-ce-mwSignatureNode' );
if ( this.isGenerating() ) {
// Use an initial rendering of '~~~~' as a placeholder to avoid
// the width changing when using the Sequence.
this.$element.text( '~~~~' );
}
// Keep track for magical text updating
liveSignatures.push( this );
};
/* Inheritance */
OO.inheritClass( ve.ce.MWSignatureNode, ve.ce.MWTransclusionInlineNode );
/* Static Properties */
ve.ce.MWSignatureNode.static.name = 'mwSignature';
ve.ce.MWSignatureNode.static.tagName = 'span';
ve.ce.MWSignatureNode.static.primaryCommandName = 'mwSignature';
/* Methods */
/**
* @inheritdoc
*/
ve.ce.MWSignatureNode.prototype.generateContents = function () {
var wikitext, signatureNode, api, deferred, xhr;
// Parsoid doesn't support pre-save transforms. PHP parser doesn't support Parsoid's
// meta attributes (that may or may not be required).
// We could try hacking up one (or even both) of these, but just calling the two parsers
// in order seems slightly saner.
// We must have only one top-level node, this is the easiest way.
wikitext = '<span>~~~~</span>';
signatureNode = this;
api = new mw.Api();
deferred = $.Deferred();
xhr = api.post( {
action: 'parse',
text: wikitext,
contentmodel: 'wikitext',
prop: 'text',
onlypst: true
} )
.done( function ( resp ) {
var wikitext = ve.getProp( resp, 'parse', 'text', '*' );
if ( wikitext ) {
// Call parent method with the wikitext with PST applied.
ve.ce.MWSignatureNode.parent.prototype.generateContents.call(
signatureNode,
{ wikitext: wikitext }
).done( function ( nodes ) {
deferred.resolve( nodes );
} );
} else {
signatureNode.onParseError( deferred );
}
} )
.fail( this.onParseError.bind( this, deferred ) );
return deferred.promise( { abort: xhr.abort } );
};
/* Registration */
ve.ce.nodeFactory.register( ve.ce.MWSignatureNode );