Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/nodes/ve.ce.MWSignatureNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWSignatureNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki signature node. This defines the behavior of the signature node
10 * inserted into the ContentEditable document.
11 *
12 * @class
13 * @extends ve.ce.LeafNode
14 *
15 * @constructor
16 * @param {ve.dm.MWSignatureNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWSignatureNode = function VeCeMWSignatureNode() {
20 // Parent constructor
21 ve.ce.MWSignatureNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.ce.GeneratedContentNode.call( this );
25 ve.ce.FocusableNode.call( this );
26
27 // DOM changes
28 this.$element.addClass( 've-ce-mwSignatureNode' );
29
30 if ( this.isGenerating() ) {
31 // Use an initial rendering of '~~~~' as a placeholder to avoid
32 // the width changing when using the Sequence.
33 this.$element.text( '~~~~' );
34 }
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( ve.ce.MWSignatureNode, ve.ce.LeafNode );
40 OO.mixinClass( ve.ce.MWSignatureNode, ve.ce.GeneratedContentNode );
41 OO.mixinClass( ve.ce.MWSignatureNode, ve.ce.FocusableNode );
42
43 /* Static Properties */
44
45 ve.ce.MWSignatureNode.static.name = 'mwSignature';
46
47 ve.ce.MWSignatureNode.static.tagName = 'span';
48
49 ve.ce.MWSignatureNode.static.primaryCommandName = 'mwSignature';
50
51 ve.ce.MWSignatureNode.static.liveSignatures = [];
52
53 // Set a description for focusable node tooltip
54 ve.ce.MWSignatureNode.static.getDescription = function () {
55 return ve.msg( 'visualeditor-mwsignature-tool' );
56 };
57
58 // Update the timestamp on inserted signatures every minute.
59 setInterval( () => {
60 const liveSignatures = ve.ce.MWSignatureNode.static.liveSignatures;
61
62 const updatedSignatures = [];
63 for ( let i = 0; i < liveSignatures.length; i++ ) {
64 const sig = liveSignatures[ i ];
65 try {
66 sig.forceUpdate();
67 updatedSignatures.push( sig );
68 } catch ( er ) {
69 // Do nothing
70 }
71 }
72 // Stop updating signatures that failed once
73 ve.ce.MWSignatureNode.static.liveSignatures = updatedSignatures;
74 }, 60 * 1000 );
75
76 /* Methods */
77
78 /**
79 * @inheritdoc
80 */
81 ve.ce.MWSignatureNode.prototype.onSetup = function () {
82 // Parent method
83 ve.ce.MWSignatureNode.super.prototype.onSetup.call( this );
84
85 // Keep track for regular updating of timestamp
86 this.constructor.static.liveSignatures.push( this );
87 };
88
89 /**
90 * @inheritdoc
91 */
92 ve.ce.MWSignatureNode.prototype.onTeardown = function () {
93 const liveSignatures = this.constructor.static.liveSignatures;
94
95 // Parent method
96 ve.ce.MWSignatureNode.super.prototype.onTeardown.call( this );
97
98 // Stop tracking
99 const index = liveSignatures.indexOf( this );
100 if ( index !== -1 ) {
101 liveSignatures.splice( index, 1 );
102 }
103 };
104
105 /**
106 * @inheritdoc ve.ce.GeneratedContentNode
107 */
108 ve.ce.MWSignatureNode.prototype.generateContents = function () {
109 const doc = this.getModel().getDocument();
110 let abortable, aborted;
111 const abortedPromise = ve.createDeferred().reject( 'http',
112 { textStatus: 'abort', exception: 'abort' } ).promise();
113
114 function abort() {
115 aborted = true;
116 if ( abortable && abortable.abort ) {
117 abortable.abort();
118 }
119 }
120
121 // Acquire a temporary user username before previewing, so that signatures
122 // display the temp user instead of IP user. (T331397)
123 return mw.user.acquireTempUserName()
124 .then( () => {
125 if ( aborted ) {
126 return abortedPromise;
127 }
128
129 // We must have only one top-level node, this is the easiest way.
130 const wikitext = '<span>~~~~</span>';
131
132 // Parsoid doesn't support pre-save transforms. PHP parser doesn't support Parsoid's
133 // meta attributes (that may or may not be required).
134 // We could try hacking up one (or even both) of these, but just calling the two parsers
135 // in order seems slightly saner.
136 return ( abortable = ve.init.target.getContentApi( doc ).post( {
137 action: 'parse',
138 text: wikitext,
139 contentmodel: 'wikitext',
140 prop: 'text',
141 onlypst: true
142 } ) );
143 } )
144 .then( ( pstResponse ) => {
145 if ( aborted ) {
146 return abortedPromise;
147 }
148 const wikitext = ve.getProp( pstResponse, 'parse', 'text' );
149 if ( !wikitext ) {
150 return ve.createDeferred().reject();
151 }
152 return ( abortable = ve.init.target.parseWikitextFragment( wikitext, true, doc ) );
153 } )
154 .then( ( parseResponse ) => {
155 if ( aborted ) {
156 return abortedPromise;
157 }
158 if ( ve.getProp( parseResponse, 'visualeditor', 'result' ) !== 'success' ) {
159 return ve.createDeferred().reject();
160 }
161 // Simplified case of template rendering, don't need to worry about filtering etc
162 return $( parseResponse.visualeditor.content ).contents().toArray();
163 } )
164 .promise( { abort: abort } );
165 };
166
167 /* Registration */
168
169 ve.ce.nodeFactory.register( ve.ce.MWSignatureNode );