Debian Codegraph @

extensions/WikiEditor/modules/ext.wikiEditor.js

raw symbols SCIP
1 /*
2 * JavaScript for WikiEditor
3 */
4
5 /**
6 * @module module:ext.wikiEditor
7 */
8
9 var editingSessionId;
10
11 // This sets $.wikiEditor and $.fn.wikiEditor
12 require( './jquery.wikiEditor.js' );
13
14 function logEditEvent( data ) {
15 if ( mw.config.get( 'wgMFMode' ) !== null ) {
16 // Visiting a ?action=edit URL can, depending on user settings, result
17 // in the MobileFrontend overlay appearing on top of WikiEditor. In
18 // these cases, don't log anything.
19 return;
20 }
21 mw.track( 'editAttemptStep', Object.assign( {
22 // eslint-disable-next-line camelcase
23 editor_interface: 'wikitext',
24 platform: 'desktop', // FIXME T249944
25 integration: 'page'
26 }, data ) );
27 }
28
29 function logEditFeature( feature, action ) {
30 if ( mw.config.get( 'wgMFMode' ) !== null ) {
31 // Visiting a ?action=edit URL can, depending on user settings, result
32 // in the MobileFrontend overlay appearing on top of WikiEditor. In
33 // these cases, don't log anything.
34 return;
35 }
36 mw.track( 'visualEditorFeatureUse', {
37 feature: feature,
38 action: action,
39 // eslint-disable-next-line camelcase
40 editor_interface: 'wikitext',
41 platform: 'desktop', // FIXME T249944
42 integration: 'page'
43 } );
44 }
45
46 function logAbort( switchingToVE, unmodified ) {
47 if ( switchingToVE ) {
48 logEditFeature( 'editor-switch', 'visual-desktop' );
49 }
50
51 var abortType;
52 if ( switchingToVE && unmodified ) {
53 abortType = 'switchnochange';
54 } else if ( switchingToVE ) {
55 abortType = 'switchwithout';
56 } else if ( unmodified ) {
57 abortType = 'nochange';
58 } else {
59 abortType = 'abandon';
60 }
61
62 logEditEvent( {
63 action: 'abort',
64 type: abortType
65 } );
66 }
67
68 $( function () {
69 var $textarea = $( '#wpTextbox1' ),
70 $editingSessionIdInput = $( '#editingStatsId' ),
71 origText = $textarea.val();
72
73 // T263505, T249038
74 $( '#wikieditorUsed' ).val( 'yes' );
75
76 if ( $editingSessionIdInput.length ) {
77 editingSessionId = $editingSessionIdInput.val();
78 if ( window.performance && window.performance.timing ) {
79 // We want to track from the time the user started to try to
80 // launch the editor which navigationStart approximates. All
81 // of our supported browsers *should* allow this. Rather than
82 // fall back to the timestamp when the page loaded for those
83 // that don't, we just ignore them, so as to not skew the
84 // results towards better-performance in those cases.
85 var readyTime = Date.now();
86 logEditEvent( {
87 action: 'ready',
88 timing: readyTime - window.performance.timing.navigationStart
89 } );
90 $textarea.on( 'wikiEditor-toolbar-doneInitialSections', function () {
91 logEditEvent( {
92 action: 'loaded',
93 timing: Date.now() - window.performance.timing.navigationStart
94 } );
95 } ).one( 'input', function () {
96 logEditEvent( {
97 action: 'firstChange',
98 timing: Date.now() - readyTime
99 } );
100 } );
101 }
102 var $form = $textarea.closest( 'form' );
103 if ( mw.user.options.get( 'uselivepreview' ) ) {
104 $form.find( '#wpPreview' ).on( 'click', function () {
105 logEditFeature( 'preview', 'preview-live' );
106 } );
107 }
108
109 var submitting;
110 $form.on( 'submit', function () {
111 submitting = true;
112 } );
113 var onUnloadFallback = window.onunload;
114
115 window.onunload = function () {
116 var unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val(),
117 caVeEdit = $( '#ca-ve-edit' )[ 0 ],
118 switchingToVE = caVeEdit && (
119 document.activeElement === caVeEdit ||
120 $.contains( caVeEdit, document.activeElement )
121 );
122
123 var fallbackResult;
124 if ( onUnloadFallback ) {
125 fallbackResult = onUnloadFallback();
126 }
127
128 if ( !submitting ) {
129 logAbort( switchingToVE, unmodified );
130 }
131
132 // If/when the user uses the back button to go back to the edit form
133 // and the browser serves this from bfcache, regenerate the session ID
134 // so we don't use the same ID twice. Ideally we'd do this by listening to the pageshow
135 // event and checking e.originalEvent.persisted, but that doesn't work in Chrome:
136 // https://code.google.com/p/chromium/issues/detail?id=344507
137 // So instead we modify the DOM here, after sending the abort event.
138 editingSessionId = mw.user.generateRandomSessionId();
139 $editingSessionIdInput.val( editingSessionId );
140
141 return fallbackResult;
142 };
143 $textarea.on( 'wikiEditor-switching-visualeditor', function () {
144 var unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val();
145 // A non-navigation switch to VE has occurred. As such, avoid eventually
146 // double-logging an abort when VE is done.
147 window.onunload = onUnloadFallback;
148
149 logAbort( true, unmodified );
150 } );
151
152 // Add logging for Realtime Preview.
153 mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( function () {
154 logEditFeature( 'preview', 'preview-realtime-on' );
155 } );
156 mw.hook( 'ext.WikiEditor.realtimepreview.inuse' ).add( function () {
157 logEditFeature( 'preview', 'preview-realtime-inuse' );
158 } );
159 mw.hook( 'ext.WikiEditor.realtimepreview.disable' ).add( function () {
160 logEditFeature( 'preview', 'preview-realtime-off' );
161 } );
162 mw.hook( 'ext.WikiEditor.realtimepreview.loaded' ).add( function () {
163 logEditFeature( 'preview', 'preview-realtime-loaded' );
164 } );
165 mw.hook( 'ext.WikiEditor.realtimepreview.stop' ).add( function () {
166 logEditFeature( 'preview', 'preview-realtime-error-stopped' );
167 } );
168 mw.hook( 'ext.WikiEditor.realtimepreview.reloadError' ).add( function () {
169 logEditFeature( 'preview', 'preview-realtime-reload-error' );
170 } );
171 mw.hook( 'ext.WikiEditor.realtimepreview.reloadHover' ).add( function () {
172 logEditFeature( 'preview', 'preview-realtime-reload-hover' );
173 } );
174 mw.hook( 'ext.WikiEditor.realtimepreview.reloadManual' ).add( function () {
175 logEditFeature( 'preview', 'preview-realtime-reload-manual' );
176 } );
177 }
178
179 // The old toolbar is still in place and needs to be removed so there aren't two toolbars
180 $( '#toolbar' ).remove();
181 // Add toolbar module
182 // TODO: Implement .wikiEditor( 'remove' )
183 mw.addWikiEditor( $textarea );
184 } );
185
186 mw.addWikiEditor = function ( $textarea ) {
187 if ( $textarea.css( 'display' ) === 'none' ) {
188 return;
189 }
190
191 $textarea.wikiEditor(
192 'addModule', require( './jquery.wikiEditor.toolbar.config.js' )
193 );
194
195 var dialogsConfig = require( './jquery.wikiEditor.dialogs.config.js' );
196 // Replace icons
197 dialogsConfig.replaceIcons( $textarea );
198 // Add dialogs module
199 $textarea.wikiEditor( 'addModule', dialogsConfig.getDefaultConfig() );
200
201 };