Debian Codegraph @

extensions/VisualEditor/modules/ve-mw-collab/ve.init.mw.CollabTarget.init.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki CollabTarget init.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /* eslint-disable no-jquery/no-global-selector */
9
10 ( function () {
11 let target,
12 $padTab = $( '#ca-pad' );
13 const $specialTab = $( '#ca-nstab-special' ),
14 conf = mw.config.get( 'wgVisualEditorConfig' ),
15 pageName = mw.config.get( 'collabPadPageName' ) || '',
16 pageTitle = mw.Title.newFromText( pageName ),
17 modules = [ OO.ui.isMobile() ? 'ext.visualEditor.collabTarget.mobile' : 'ext.visualEditor.collabTarget.desktop' ]
18 // Add modules from $wgVisualEditorPluginModules
19 .concat( conf.pluginModules.filter( mw.loader.getState ) ),
20 modulePromise = mw.loader.using( modules ),
21 progressBar = OO.ui.infuse( $( '.ve-init-mw-collabTarget-loading' ) ),
22 documentNameInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameInput' ) ),
23 documentNameButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-nameButton' ) ),
24 importInput = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importInput' ), {
25 showImages: mw.config.get( 'wgVisualEditorConfig' ).usePageImages,
26 showDescriptions: mw.config.get( 'wgVisualEditorConfig' ).usePageDescriptions
27 } ),
28 importButton = OO.ui.infuse( $( '.ve-init-mw-collabTarget-importButton' ) ),
29 // Infuse the form last to avoid recursive infusion with no config
30 form = OO.ui.infuse( $( '.ve-init-mw-collabTarget-form' ) ),
31 $targetContainer = $(
32 document.querySelector( '[data-mw-ve-target-container]' ) ||
33 document.getElementById( 'content' )
34 );
35
36 if ( !VisualEditorSupportCheck() ) {
37 // VE not supported - say something?
38 return;
39 }
40
41 function setTitle( title ) {
42 $( '#firstHeading' ).text( title );
43 document.title = title;
44 }
45
46 function showPage( title, importTitle ) {
47 const specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
48
49 setTitle( mw.msg( 'collabpad-doctitle', title.getPrefixedText() ) );
50
51 mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
52 mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
53 if ( !$padTab.length ) {
54 $padTab = $( '<li>' ).attr( 'id', 'ca-pad' ).addClass( 'selected' ).append(
55 $( '<span>' ).append(
56 $( '<a>' ).attr( 'href', '' ).text( title.getPrefixedText() )
57 )
58 );
59 }
60 $padTab.insertAfter( $specialTab.removeClass( 'selected' ) );
61
62 progressBar.toggle( true );
63 form.toggle( false );
64
65 modulePromise.done( () => {
66 target = ve.init.mw.targetFactory.create( 'collab', title, conf.rebaserUrl, { importTitle: importTitle } );
67 // If the target emits a 'close' event (via the toolbar back button on mobile) then go to the landing page.
68 target.once( 'close', () => {
69 showForm( true );
70 } );
71
72 $( 'body' ).addClass( 've-activated ve-active' );
73
74 $targetContainer.prepend( target.$element );
75
76 target.transformPage();
77 $( '#firstHeading' ).addClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
78
79 // Add a dummy surface while the doc is loading
80 const dummySurface = target.addSurface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
81 dummySurface.setReadOnly( true );
82
83 // TODO: Create the correct model surface type (ve.ui.Surface#createModel)
84 let surfaceModel = new ve.dm.Surface( ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '' ) ) );
85 const username = mw.user.getName();
86 surfaceModel.createSynchronizer(
87 mw.config.get( 'wgWikiID' ) + '/' + title.toString(),
88 {
89 server: conf.rebaserUrl,
90 // TODO: server could communicate with MW (via oauth?) to know the
91 // current-user's name. Disable changing name if logged in?
92 // Communicate an I-am-a-valid-user flag to other clients?
93 defaultName: username
94 }
95 );
96
97 const progressDeferred = ve.createDeferred();
98 dummySurface.createProgress( progressDeferred.promise(), ve.msg( 'visualeditor-rebase-client-connecting' ), true );
99
100 surfaceModel.synchronizer.once( 'initDoc', ( error ) => {
101 let initPromise;
102
103 progressDeferred.resolve();
104 // Resolving the progress bar doesn't close the window in this cycle,
105 // so wait until we call clearSurfaces which destroys the window manager.
106 setTimeout( () => {
107 target.clearSurfaces();
108 // Don't add the surface until the history has been applied
109 target.addSurface( surfaceModel );
110 if ( error ) {
111 const $errorMsg = ve.htmlMsg( 'visualeditor-rebase-corrupted-document-error', $( '<pre>' ).text( error.stack ) );
112 OO.ui.alert(
113 $( '<p>' ).append( $errorMsg ),
114 { title: ve.msg( 'visualeditor-rebase-corrupted-document-title' ), size: 'large' }
115 ).then( () => {
116 showForm( true );
117 } );
118 return;
119 }
120 target.once( 'surfaceReady', () => {
121 initPromise.then( () => {
122 target.getSurface().getView().selectFirstSelectableContentOffset();
123 const isNewAuthor = !ve.init.platform.sessionStorage.get( 've-collab-author' );
124 // For new anon users, open the author list so they can set their name
125 if ( isNewAuthor && !username ) {
126 // Something (an animation?) steals focus during load, so wait a bit
127 // before opening and focusing the authorList.
128 setTimeout( () => {
129 target.toolbar.tools.authorList.onSelect();
130 }, 500 );
131 }
132 } );
133 } );
134
135 if ( target.importTitle && !surfaceModel.getDocument().getCompleteHistoryLength() ) {
136 initPromise = mw.libs.ve.targetLoader.requestParsoidData( target.importTitle.toString(), { targetName: 'collabpad' } ).then( ( response ) => {
137 const data = response.visualeditor;
138
139 if ( data && data.content ) {
140 const doc = target.constructor.static.parseDocument( data.content );
141 const dmDoc = target.constructor.static.createModelFromDom( doc );
142 const fragment = surfaceModel.getLinearFragment( new ve.Range( 0, 2 ) );
143 fragment.insertDocument( dmDoc );
144
145 target.etag = data.etag;
146 target.baseTimeStamp = data.basetimestamp;
147 target.startTimeStamp = data.starttimestamp;
148 target.revid = data.oldid;
149
150 // Store the document metadata as a hidden meta item
151 fragment.collapseToEnd().insertContent( [
152 {
153 type: 'alienMeta',
154 attributes: {
155 importedDocument: {
156 title: target.importTitle.toString(),
157 etag: target.etag,
158 baseTimeStamp: target.baseTimeStamp,
159 startTimeStamp: target.startTimeStamp,
160 revid: target.revid
161 }
162 }
163 },
164 { type: '/alienMeta' }
165 ] );
166 } else {
167 // Import failed
168 return ve.createDeferred().reject( 'No content for ' + target.importTitle ).promise();
169 }
170 } );
171 } else {
172 // No import, or history already exists
173 initPromise = ve.createDeferred().resolve().promise();
174
175 // Look for import metadata in document
176 surfaceModel = target.getSurface().getModel();
177 surfaceModel.getDocument().getMetaList().getItemsInGroup( 'misc' ).some( ( item ) => {
178 const importedDocument = item.getAttribute( 'importedDocument' );
179 if ( importedDocument ) {
180 target.importTitle = mw.Title.newFromText( importedDocument.title );
181 target.etag = importedDocument.etag;
182 target.baseTimeStamp = importedDocument.baseTimeStamp;
183 target.startTimeStamp = importedDocument.startTimeStamp;
184 target.revid = importedDocument.revid;
185 return true;
186 }
187 return false;
188 } );
189 }
190 initPromise.fail( ( err ) => {
191 setTimeout( () => {
192 throw new Error( err );
193 } );
194 } );
195 initPromise.always( () => {
196 progressDeferred.resolve();
197 } );
198 } );
199 } );
200
201 } ).always( () => {
202 form.toggle( false );
203 progressBar.toggle( false );
204 } ).fail( ( err ) => {
205 mw.log.error( err );
206 showForm( true );
207 } );
208 }
209
210 function showForm( pushState ) {
211 const specialTitle = mw.Title.newFromText( 'Special:CollabPad' );
212
213 if ( pushState ) {
214 history.pushState( { tag: 'collabTarget' }, '', specialTitle.getUrl() );
215 }
216
217 if ( target ) {
218 $( '#firstHeading' ).removeClass( 've-init-mw-desktopArticleTarget-uneditableContent' );
219 target.restorePage();
220 target.destroy();
221
222 $( 'body' ).removeClass( 've-activated ve-active' );
223 }
224
225 setTitle( mw.msg( 'collabpad' ) );
226 mw.config.set( 'wgRelevantPageName', specialTitle.getPrefixedText() );
227 mw.config.set( 'wgPageName', specialTitle.getPrefixedText() );
228 if ( $padTab ) {
229 $padTab.detach();
230 }
231 $specialTab.addClass( 'selected' );
232
233 progressBar.toggle( false );
234 form.toggle( true );
235 }
236
237 function loadTitle( title, importTitle ) {
238 const specialTitle = mw.Title.newFromText( 'Special:CollabPad/' + title.toString() );
239 // TODO: Handle popstate
240 history.pushState( { tag: 'collabTarget', title: title.toString() }, '', specialTitle.getUrl() );
241 showPage( title, importTitle );
242 }
243
244 function getRandomTitle() {
245 return Math.random().toString( 36 ).slice( 2 );
246 }
247
248 function onNameChange() {
249 documentNameInput.getValidity().then( () => {
250 documentNameButton.setDisabled( false );
251 }, () => {
252 documentNameButton.setDisabled( true );
253 } );
254 }
255
256 function loadFromName() {
257 documentNameInput.getValidity().then( () => {
258 const title = mw.Title.newFromText(
259 documentNameInput.getValue().trim() || getRandomTitle()
260 );
261
262 if ( title ) {
263 loadTitle( title );
264 } else {
265 documentNameInput.focus();
266 }
267 } );
268 }
269
270 // Empty input will create a random document name, otherwise must be valid
271 documentNameInput.setValidation( ( value ) => value === '' || !!mw.Title.newFromText( value ) );
272 documentNameButton.setDisabled( false );
273
274 documentNameInput.on( 'change', onNameChange );
275 documentNameInput.on( 'enter', loadFromName );
276 documentNameButton.on( 'click', loadFromName );
277 onNameChange();
278
279 function onImportChange() {
280 importInput.getValidity().then( () => {
281 importButton.setDisabled( false );
282 }, () => {
283 importButton.setDisabled( true );
284 } );
285 }
286
287 function onImportSubmit() {
288 importInput.getValidity().then( () => {
289 const title = mw.Title.newFromText( importInput.getValue().trim() );
290
291 if ( title ) {
292 loadTitle( mw.Title.newFromText( getRandomTitle() ), title );
293 } else {
294 documentNameInput.focus();
295 }
296 } );
297 }
298
299 // TODO: Check page exists?
300 importInput.setValidation( ( value ) => !!mw.Title.newFromText( value ) );
301 importInput.on( 'change', onImportChange );
302 importInput.on( 'enter', onImportSubmit );
303 importButton.on( 'click', onImportSubmit );
304 onImportChange();
305
306 if ( pageTitle ) {
307 const url = new URL( location.href ),
308 importTitleText = url.searchParams.get( 'import' ),
309 importTitleParam = ( importTitleText ? mw.Title.newFromText( importTitleText ) : null );
310 showPage( pageTitle, importTitleParam );
311 } else {
312 showForm();
313 }
314
315 $specialTab.on( 'click', ( e ) => {
316 showForm( true );
317 e.preventDefault();
318 } );
319
320 // Tag current state
321 history.replaceState( { tag: 'collabTarget', title: pageName }, '', location.href );
322 window.addEventListener( 'popstate', ( e ) => {
323 if ( e.state && e.state.tag === 'collabTarget' ) {
324 if ( e.state.title ) {
325 showPage( mw.Title.newFromText( e.state.title ) );
326 } else {
327 showForm();
328 }
329 }
330 } );
331 }() );