2015-11-08 17:05:33 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWSignatureNode class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2015-11-08 17:05:33 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki signature node. This defines the behavior of the signature node
|
|
|
|
* inserted into the ContentEditable document.
|
|
|
|
*
|
|
|
|
* @class
|
2020-06-20 12:00:25 +00:00
|
|
|
* @extends ve.ce.LeafNode
|
2015-11-08 17:05:33 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWSignatureNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ce.MWSignatureNode = function VeCeMWSignatureNode() {
|
2015-11-08 17:05:33 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ce.MWSignatureNode.super.apply( this, arguments );
|
2015-11-08 17:05:33 +00:00
|
|
|
|
2020-06-20 12:00:25 +00:00
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
ve.ce.FocusableNode.call( this );
|
|
|
|
|
2015-11-08 17:05:33 +00:00
|
|
|
// 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( '~~~~' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2020-06-20 12:00:25 +00:00
|
|
|
OO.inheritClass( ve.ce.MWSignatureNode, ve.ce.LeafNode );
|
|
|
|
OO.mixinClass( ve.ce.MWSignatureNode, ve.ce.GeneratedContentNode );
|
|
|
|
OO.mixinClass( ve.ce.MWSignatureNode, ve.ce.FocusableNode );
|
2015-11-08 17:05:33 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.MWSignatureNode.static.name = 'mwSignature';
|
|
|
|
|
|
|
|
ve.ce.MWSignatureNode.static.tagName = 'span';
|
|
|
|
|
|
|
|
ve.ce.MWSignatureNode.static.primaryCommandName = 'mwSignature';
|
|
|
|
|
2017-04-29 11:37:28 +00:00
|
|
|
ve.ce.MWSignatureNode.static.liveSignatures = [];
|
|
|
|
|
2020-06-20 12:00:25 +00:00
|
|
|
// Set a description for focusable node tooltip
|
|
|
|
ve.ce.MWSignatureNode.static.getDescription = function () {
|
|
|
|
return ve.msg( 'visualeditor-mwsignature-tool' );
|
|
|
|
};
|
|
|
|
|
2017-04-29 11:37:28 +00:00
|
|
|
// Update the timestamp on inserted signatures every minute.
|
2024-04-30 16:44:25 +00:00
|
|
|
setInterval( () => {
|
2022-02-21 18:07:07 +00:00
|
|
|
var liveSignatures = ve.ce.MWSignatureNode.static.liveSignatures;
|
2017-04-29 11:37:28 +00:00
|
|
|
|
2022-02-21 18:07:07 +00:00
|
|
|
var updatedSignatures = [];
|
|
|
|
for ( var i = 0; i < liveSignatures.length; i++ ) {
|
|
|
|
var sig = liveSignatures[ i ];
|
2017-04-29 11:37:28 +00:00
|
|
|
try {
|
|
|
|
sig.forceUpdate();
|
|
|
|
updatedSignatures.push( sig );
|
|
|
|
} catch ( er ) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 15:35:43 +00:00
|
|
|
// Stop updating signatures that failed once
|
|
|
|
ve.ce.MWSignatureNode.static.liveSignatures = updatedSignatures;
|
2017-04-29 11:37:28 +00:00
|
|
|
}, 60 * 1000 );
|
|
|
|
|
2015-11-08 17:05:33 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2017-04-29 11:37:28 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWSignatureNode.prototype.onSetup = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ce.MWSignatureNode.super.prototype.onSetup.call( this );
|
|
|
|
|
|
|
|
// Keep track for regular updating of timestamp
|
|
|
|
this.constructor.static.liveSignatures.push( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWSignatureNode.prototype.onTeardown = function () {
|
2021-10-13 12:57:45 +00:00
|
|
|
var liveSignatures = this.constructor.static.liveSignatures;
|
2017-04-29 11:37:28 +00:00
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ce.MWSignatureNode.super.prototype.onTeardown.call( this );
|
|
|
|
|
|
|
|
// Stop tracking
|
2021-10-13 12:57:45 +00:00
|
|
|
var index = liveSignatures.indexOf( this );
|
2017-04-29 11:37:28 +00:00
|
|
|
if ( index !== -1 ) {
|
|
|
|
liveSignatures.splice( index, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-08 17:05:33 +00:00
|
|
|
/**
|
2020-06-20 12:00:25 +00:00
|
|
|
* @inheritdoc ve.ce.GeneratedContentNode
|
2015-11-08 17:05:33 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWSignatureNode.prototype.generateContents = function () {
|
2021-10-13 12:57:45 +00:00
|
|
|
var doc = this.getModel().getDocument();
|
2023-07-18 02:14:09 +00:00
|
|
|
var abortable, aborted;
|
|
|
|
var abortedPromise = ve.createDeferred().reject( 'http',
|
|
|
|
{ textStatus: 'abort', exception: 'abort' } ).promise();
|
|
|
|
|
|
|
|
function abort() {
|
|
|
|
aborted = true;
|
|
|
|
if ( abortable && abortable.abort ) {
|
|
|
|
abortable.abort();
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 17:05:33 +00:00
|
|
|
|
2023-07-18 02:14:09 +00:00
|
|
|
// Acquire a temporary user username before previewing, so that signatures
|
|
|
|
// display the temp user instead of IP user. (T331397)
|
|
|
|
return mw.user.acquireTempUserName()
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( () => {
|
2023-07-18 02:14:09 +00:00
|
|
|
if ( aborted ) {
|
|
|
|
return abortedPromise;
|
2015-11-08 17:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 02:14:09 +00:00
|
|
|
// We must have only one top-level node, this is the easiest way.
|
|
|
|
var wikitext = '<span>~~~~</span>';
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
return ( abortable = ve.init.target.getContentApi( doc ).post( {
|
|
|
|
action: 'parse',
|
|
|
|
text: wikitext,
|
|
|
|
contentmodel: 'wikitext',
|
|
|
|
prop: 'text',
|
|
|
|
onlypst: true
|
|
|
|
} ) );
|
|
|
|
} )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( ( pstResponse ) => {
|
2023-07-18 02:14:09 +00:00
|
|
|
if ( aborted ) {
|
|
|
|
return abortedPromise;
|
|
|
|
}
|
|
|
|
var wikitext = ve.getProp( pstResponse, 'parse', 'text' );
|
|
|
|
if ( !wikitext ) {
|
|
|
|
return ve.createDeferred().reject();
|
|
|
|
}
|
|
|
|
return ( abortable = ve.init.target.parseWikitextFragment( wikitext, true, doc ) );
|
|
|
|
} )
|
2024-04-30 16:44:25 +00:00
|
|
|
.then( ( parseResponse ) => {
|
2023-07-18 02:14:09 +00:00
|
|
|
if ( aborted ) {
|
|
|
|
return abortedPromise;
|
|
|
|
}
|
|
|
|
if ( ve.getProp( parseResponse, 'visualeditor', 'result' ) !== 'success' ) {
|
|
|
|
return ve.createDeferred().reject();
|
|
|
|
}
|
|
|
|
// Simplified case of template rendering, don't need to worry about filtering etc
|
|
|
|
return $( parseResponse.visualeditor.content ).contents().toArray();
|
|
|
|
} )
|
|
|
|
.promise( { abort: abort } );
|
2015-11-08 17:05:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ce.nodeFactory.register( ve.ce.MWSignatureNode );
|