Debian Codegraph @

extensions/VisualEditor/modules/ve-mw-collab/ve.ui.MWExportWikitextDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWExportWikitextDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Dialog for exportWikitexting CollabTarget pages
10 *
11 * @class
12 * @extends OO.ui.ProcessDialog
13 *
14 * @constructor
15 * @param {Object} [config] Config options
16 */
17 ve.ui.MWExportWikitextDialog = function VeUiMwExportWikitextDialog( config ) {
18 // Parent constructor
19 ve.ui.MWExportWikitextDialog.super.call( this, config );
20
21 // Initialization
22 this.$element.addClass( 've-ui-mwExportWikitextDialog' );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ui.MWExportWikitextDialog, OO.ui.ProcessDialog );
28
29 /* Static Properties */
30
31 ve.ui.MWExportWikitextDialog.static.name = 'mwExportWikitext';
32
33 ve.ui.MWExportWikitextDialog.static.title = ve.msg( 'visualeditor-rebase-client-export' );
34
35 ve.ui.MWExportWikitextDialog.static.actions = [
36 {
37 label: OO.ui.deferMsg( 'visualeditor-dialog-action-done' ),
38 flags: [ 'safe', 'close' ]
39 }
40 ];
41
42 ve.ui.MWExportWikitextDialog.static.size = 'larger';
43
44 /**
45 * @inheritdoc
46 */
47 ve.ui.MWExportWikitextDialog.prototype.initialize = function () {
48 // Parent method
49 ve.ui.MWExportWikitextDialog.super.prototype.initialize.call( this );
50
51 this.titleInput = new mw.widgets.TitleInputWidget( {
52 value: ve.init.target.getImportTitle()
53 }, { api: ve.init.target.getContentApi() } );
54 this.titleButton = new OO.ui.ButtonWidget( {
55 label: ve.msg( 'visualeditor-rebase-client-export-start' ),
56 flags: [ 'primary', 'progressive' ]
57 } );
58 this.titleField = new OO.ui.ActionFieldLayout( this.titleInput, this.titleButton, {
59 align: 'top',
60 label: ve.msg( 'visualeditor-rebase-client-import-name' ),
61 help: ve.msg( 'visualeditor-rebase-client-title-help' ),
62 helpInline: true
63 } );
64
65 this.titleButton.on( 'click', this.export.bind( this ) );
66
67 this.wikitextLayout = new mw.widgets.CopyTextLayout( {
68 align: 'top',
69 label: ve.msg( 'visualeditor-savedialog-review-wikitext' ),
70 multiline: true,
71 textInput: {
72 // The following classes are used here:
73 // * mw-editfont-monospace
74 // * mw-editfont-sans-serif
75 // * mw-editfont-serif
76 classes: [ 'mw-editfont-' + mw.user.options.get( 'editfont' ) ],
77 autosize: true,
78 readOnly: true,
79 // Height will be trimmed in getReadyProcess
80 rows: 99
81 }
82 } );
83
84 // TODO: Move to CSS
85 this.titleField.$element.css( 'max-width', 'none' );
86 this.titleInput.$element.css( 'max-width', 'none' );
87 this.wikitextLayout.$element.css( 'max-width', 'none' );
88 this.wikitextLayout.$field.css( 'max-width', 'none' );
89 this.wikitextLayout.textInput.$element.css( 'max-width', 'none' );
90
91 const $content = $( '<div>' );
92 $content.append(
93 this.titleField.$element,
94 this.wikitextLayout.$element
95 );
96
97 const panel = new OO.ui.PanelLayout( {
98 padded: true,
99 expanded: false,
100 $content: $content
101 } );
102 this.$body.append( panel.$element );
103 };
104
105 /**
106 * @inheritdoc
107 */
108 ve.ui.MWExportWikitextDialog.prototype.getSetupProcess = function ( data ) {
109 return ve.ui.MWExportWikitextDialog.super.prototype.getSetupProcess.call( this, data )
110 .next( () => {
111 const surface = ve.init.target.getSurface(),
112 wikitextInput = this.wikitextLayout.textInput;
113 this.titleButton.setDisabled( true );
114 this.wikitextLayout.textInput.pushPending();
115 ve.init.target.getWikitextFragment( surface.getModel().getDocument() ).then( ( wikitext ) => {
116 wikitextInput.setValue( wikitext.trim() );
117 wikitextInput.$input.scrollTop( 0 );
118 wikitextInput.popPending();
119 this.titleButton.setDisabled( false );
120 this.updateSize();
121 }, () => {
122 // TODO: Display API errors
123 wikitextInput.popPending();
124 } );
125 } );
126 };
127
128 /**
129 * @inheritdoc
130 */
131 ve.ui.MWExportWikitextDialog.prototype.getReadyProcess = function ( data ) {
132 return ve.ui.MWExportWikitextDialog.super.prototype.getReadyProcess.call( this, data )
133 .next( () => {
134 this.titleInput.focus();
135
136 // Fix height of wikitext input
137 this.wikitextLayout.textInput.$input.css( 'max-height', '' );
138 const overflow = this.$body[ 0 ].scrollHeight - this.$body[ 0 ].clientHeight;
139 if ( overflow > 0 ) {
140 // If body is too tall, take the excess height off the wikitext input
141 this.wikitextLayout.textInput.$input.css(
142 'max-height',
143 Math.max(
144 this.wikitextLayout.textInput.$input[ 0 ].clientHeight - overflow,
145 50 // minimum height
146 )
147 );
148 }
149
150 } );
151 };
152
153 /**
154 * @inheritdoc
155 */
156 ve.ui.MWExportWikitextDialog.prototype.getTeardownProcess = function ( data ) {
157 return ve.ui.MWExportWikitextDialog.super.prototype.getTeardownProcess.call( this, data )
158 .next( () => {
159 this.wikitextLayout.textInput.setValue( '' );
160 } );
161 };
162
163 /**
164 * Export the document to a specific title
165 */
166 ve.ui.MWExportWikitextDialog.prototype.export = function () {
167 const wikitext = this.wikitextLayout.textInput.getValue(),
168 title = this.titleInput.getMWTitle(),
169 importTitle = ve.init.target.getImportTitle();
170
171 const $form = $( '<form>' ).attr( { method: 'post', enctype: 'multipart/form-data' } ).addClass( 'oo-ui-element-hidden' );
172 let params = {
173 format: 'text/x-wiki',
174 model: 'wikitext',
175 wpTextbox1: wikitext,
176 wpEditToken: mw.user.tokens.get( 'csrfToken' ),
177 // MediaWiki function-verification parameters, mostly relevant to the
178 // classic editpage, but still required here:
179 wpUnicodeCheck: 'ℳ𝒲β™₯π“Šπ“ƒπ’Ύπ’Έβ„΄π’Ήβ„―',
180 wpUltimateParam: true,
181 wpDiff: true
182 };
183 if (
184 importTitle && title &&
185 importTitle.toString() === title.toString()
186 ) {
187 params = ve.extendObject( {
188 oldid: ve.init.target.revid,
189 basetimestamp: ve.init.target.baseTimeStamp,
190 starttimestamp: ve.init.target.startTimeStamp
191 }, params );
192 }
193 // Add params as hidden fields
194 for ( const key in params ) {
195 $form.append( $( '<input>' ).attr( { type: 'hidden', name: key, value: params[ key ] } ) );
196 }
197 // Submit the form, mimicking a traditional edit
198 // Firefox requires the form to be attached
199 const submitUrl = title.getUrl( {
200 action: 'submit',
201 veswitched: '1'
202 } );
203 $form.attr( 'action', submitUrl ).appendTo( 'body' ).trigger( 'submit' );
204 };
205
206 /* Registration */
207
208 ve.ui.windowFactory.register( ve.ui.MWExportWikitextDialog );