Debian Codegraph @

extensions/ConfirmEdit/resources/ve-confirmedit/ve.init.mw.CaptchaSaveErrorHandler.js

raw symbols SCIP
1 // Captcha "errors" usually aren't errors. We simply don't know about them ahead of time,
2 // so we save once, then (if required) we get an error with a captcha back and try again after
3 // the user solved the captcha.
4 // TODO: ConfirmEdit API is horrible, there is no reliable way to know whether it is a "question"
5 // or "fancy" type of captcha. They all expose differently named properties in the
6 // API for different things in the UI. At this point we only support the SimpleCaptcha and
7 // FancyCaptcha which we very intuitively detect by the presence of a "url" property.
8 mw.loader.using( 'ext.visualEditor.targetLoader' ).then( () => {
9 mw.libs.ve.targetLoader.addPlugin( () => {
10
11 ve.init.mw.CaptchaSaveErrorHandler = function () {};
12
13 OO.inheritClass( ve.init.mw.CaptchaSaveErrorHandler, ve.init.mw.SaveErrorHandler );
14
15 ve.init.mw.CaptchaSaveErrorHandler.static.name = 'confirmEditCaptchas';
16
17 ve.init.mw.CaptchaSaveErrorHandler.static.matchFunction = function ( data ) {
18 const captchaData = ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' );
19
20 return !!( captchaData && (
21 captchaData.url ||
22 captchaData.type === 'simple' ||
23 captchaData.type === 'question'
24 ) );
25 };
26
27 ve.init.mw.CaptchaSaveErrorHandler.static.process = function ( data, target ) {
28 const captchaInput = new mw.libs.confirmEdit.CaptchaInputWidget(
29 ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' )
30 );
31 ve.targetLinksToNewWindow( captchaInput.$element[ 0 ] );
32
33 function onCaptchaLoad() {
34 target.saveDialog.updateSize();
35 captchaInput.focus();
36 captchaInput.scrollElementIntoView();
37 }
38
39 captchaInput.on( 'load', onCaptchaLoad );
40 // Save when pressing 'Enter' in captcha field as it is single line.
41 captchaInput.on( 'enter', () => {
42 target.saveDialog.executeAction( 'save' );
43 } );
44
45 // Register extra fields
46 target.saveFields.wpCaptchaId = function () {
47 return captchaInput.getCaptchaId();
48 };
49 target.saveFields.wpCaptchaWord = function () {
50 return captchaInput.getCaptchaWord();
51 };
52
53 // ProcessDialog's error system isn't great for this yet.
54 target.saveDialog.clearMessage( 'api-save-error' );
55 target.saveDialog.showMessage( 'api-save-error', captchaInput.$element, { wrap: false } );
56 target.saveDialog.popPending();
57 onCaptchaLoad();
58
59 // Emit event for tracking. TODO: This is a bad design
60 target.emit( 'saveErrorCaptcha' );
61 };
62
63 ve.init.mw.saveErrorHandlerFactory.register( ve.init.mw.CaptchaSaveErrorHandler );
64
65 } );
66 } );