Debian Codegraph @

resources/legacy/modules/ve-cm/ve.ui.CodeMirrorTool.js

raw
1 /**
2 * VisualEditor UserInterface CodeMirror tool.
3 *
4 * @class
5 * @abstract
6 * @extends ve.ui.Tool
7 * @constructor
8 * @param {OO.ui.ToolGroup} toolGroup
9 * @param {Object} [config] Configuration options
10 */
11 ve.ui.CodeMirrorTool = function VeUiCodeMirrorTool() {
12 // Parent constructor
13 ve.ui.CodeMirrorTool.super.apply( this, arguments );
14
15 this.extCodeMirror = require( 'ext.CodeMirror' );
16
17 // Events
18 this.toolbar.connect( this, { surfaceChange: 'onSurfaceChange' } );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.CodeMirrorTool, ve.ui.Tool );
24
25 /* Static properties */
26
27 ve.ui.CodeMirrorTool.static.name = 'codeMirror';
28 ve.ui.CodeMirrorTool.static.autoAddToCatchall = false;
29 ve.ui.CodeMirrorTool.static.title = OO.ui.deferMsg( 'codemirror-toggle-label' );
30 ve.ui.CodeMirrorTool.static.icon = 'highlight';
31 ve.ui.CodeMirrorTool.static.group = 'utility';
32 ve.ui.CodeMirrorTool.static.commandName = 'codeMirror';
33 ve.ui.CodeMirrorTool.static.deactivateOnSelect = false;
34
35 /**
36 * @inheritdoc
37 */
38 ve.ui.CodeMirrorTool.prototype.onSelect = function () {
39 // Parent method
40 ve.ui.CodeMirrorTool.super.prototype.onSelect.apply( this, arguments );
41
42 const useCodeMirror = !!this.toolbar.surface.mirror;
43 this.setActive( useCodeMirror );
44
45 new mw.Api().saveOption( 'usecodemirror', useCodeMirror ? 1 : 0 );
46 mw.user.options.set( 'usecodemirror', useCodeMirror ? 1 : 0 );
47
48 this.extCodeMirror.logUsage( {
49 editor: 'wikitext-2017',
50 enabled: useCodeMirror,
51 toggled: true,
52 // eslint-disable-next-line camelcase
53 edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
54 } );
55 };
56
57 /**
58 * @inheritdoc
59 */
60 ve.ui.CodeMirrorTool.prototype.onSurfaceChange = function ( oldSurface, newSurface ) {
61 const isDisabled = newSurface.getMode() !== 'source';
62
63 this.setDisabled( isDisabled );
64 if ( !isDisabled ) {
65 const command = this.getCommand();
66 const surface = this.toolbar.getSurface();
67 const useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
68 command.execute( surface, [ useCodeMirror ] );
69 this.setActive( useCodeMirror );
70
71 if ( this.toolbar.target.startTimeStamp ) {
72 this.extCodeMirror.logUsage( {
73 editor: 'wikitext-2017',
74 enabled: useCodeMirror,
75 toggled: false,
76 // eslint-disable-next-line camelcase
77 edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
78 } );
79 }
80 }
81 };
82
83 ve.ui.CodeMirrorTool.prototype.onUpdateState = function () {};
84
85 {
86 // eslint-disable-next-line no-jquery/no-global-selector
87 const contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
88 // New pages will use wgPageContentLanguage which is set on the html element.
89 document.documentElement.dir;
90
91 if ( contentDir === 'ltr' ) {
92 /* Registration */
93 ve.ui.toolFactory.register( ve.ui.CodeMirrorTool );
94
95 /* Command */
96 ve.ui.commandRegistry.register(
97 new ve.ui.Command(
98 'codeMirror', 'codeMirror', 'toggle'
99 )
100 );
101 }
102 }