Debian Codegraph @

resources/ve-cm/ve.ui.CodeMirrorAction.v6.js

raw
1 /*!
2 * VisualEditor UserInterface CodeMirrorAction class.
3 */
4
5 /**
6 * CodeMirror action
7 *
8 * @class
9 * @extends ve.ui.Action
10 * @constructor
11 * @param {ve.ui.Surface} surface Surface to act on
12 */
13 ve.ui.CodeMirrorAction = function VeUiCodeMirrorAction() {
14 // Parent constructor
15 ve.ui.CodeMirrorAction.super.apply( this, arguments );
16 };
17
18 /* Inheritance */
19
20 OO.inheritClass( ve.ui.CodeMirrorAction, ve.ui.Action );
21
22 /* Static Properties */
23
24 ve.ui.CodeMirrorAction.static.name = 'codeMirror';
25
26 /**
27 * @inheritdoc
28 */
29 ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];
30
31 /* Methods */
32
33 /**
34 * @method
35 * @param {boolean} [enable] State to force toggle to, inverts current state if undefined
36 * @return {boolean} Action was executed
37 */
38 ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
39 const action = this,
40 surface = this.surface,
41 surfaceView = surface.getView(),
42 doc = surface.getModel().getDocument();
43
44 if ( !surface.mirror && enable !== false ) {
45 surface.mirror = true;
46 mw.loader.using( [
47 'ext.CodeMirror.v6',
48 'ext.CodeMirror.v6.lib',
49 'ext.CodeMirror.v6.mode.mediawiki',
50 'jquery.client'
51 ] ).then( ( require ) => {
52 const CodeMirror = require( 'ext.CodeMirror.v6' );
53 const codeMirrorLib = require( 'ext.CodeMirror.v6.lib' );
54 const mediawikiLang = require( 'ext.CodeMirror.v6.mode.mediawiki' );
55
56 if ( !surface.mirror ) {
57 // Action was toggled to false since promise started
58 return;
59 }
60
61 // The VE/CM overlay technique only works with monospace fonts
62 // (as we use width-changing bold as a highlight) so revert any editfont user preference
63 surfaceView.$element.removeClass( 'mw-editfont-sans-serif mw-editfont-serif' )
64 .addClass( 'mw-editfont-monospace' );
65
66 if ( mw.user.options.get( 'usecodemirror-colorblind' ) ) {
67 surfaceView.$element.addClass( 'cm-mw-colorblind-colors' );
68 }
69
70 surface.mirror = new CodeMirror( surface );
71 const lineHeightExtension = codeMirrorLib.EditorView.theme( {
72 '.cm-content': {
73 lineHeight: 1.5
74 }
75 } );
76
77 const profile = $.client.profile();
78 const supportsTransparentText = 'WebkitTextFillColor' in document.body.style &&
79 // Disable on Firefox+OSX (T175223)
80 !( profile.layout === 'gecko' && profile.platform === 'mac' );
81
82 surfaceView.$documentNode.addClass(
83 supportsTransparentText ?
84 've-ce-documentNode-codeEditor-webkit-hide' :
85 've-ce-documentNode-codeEditor-hide'
86 );
87
88 // TODO: pass bidiIsolation option to mediawikiLang() when it's more stable.
89 surface.mirror.initialize( surface.mirror.defaultExtensions.concat( mediawikiLang( {
90 templateFolding: false
91 } ), lineHeightExtension ) );
92 // Force infinite viewport in CodeMirror to prevent misalignment of
93 // the VE surface and the CodeMirror view. See T357482#10076432.
94 surface.mirror.view.viewState.printing = true;
95
96 // Disable the Extension that highlights special characters.
97 surface.mirror.view.dispatch( {
98 effects: surface.mirror.specialCharsCompartment.reconfigure(
99 codeMirrorLib.EditorView.editorAttributes.of( [] )
100 )
101 } );
102
103 // Account for the gutter width in the margin.
104 action.updateGutterWidth( doc.getDir() );
105
106 // Set focus on the surface view.
107 surfaceView.focus();
108
109 /* Events */
110
111 // As the action is regenerated each time, we need to store bound listeners
112 // in the mirror for later disconnection.
113 surface.mirror.veTransactionListener = action.onDocumentPrecommit.bind( action );
114 surface.mirror.veSelectListener = action.onSelect.bind( action );
115 surface.mirror.vePositionListener = action.onPosition.bind( action );
116
117 doc.on( 'precommit', surface.mirror.veTransactionListener );
118 surface.getModel().on( 'select', surface.mirror.veSelectListener );
119 surfaceView.on( 'position', surface.mirror.vePositionListener );
120 } );
121 } else if ( surface.mirror && enable !== true ) {
122 if ( surface.mirror !== true ) {
123 surfaceView.off( 'position', surface.mirror.vePositionListener );
124 doc.off( 'precommit', surface.mirror.veTransactionListener );
125 surface.getModel().off( 'select', surface.mirror.veSelectListener );
126
127 // Restore edit-font
128 // eslint-disable-next-line mediawiki/class-doc
129 surfaceView.$element.removeClass( 'mw-editfont-monospace' )
130 .addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
131
132 surfaceView.$documentNode.removeClass(
133 've-ce-documentNode-codeEditor-webkit-hide',
134 've-ce-documentNode-codeEditor-hide'
135 );
136 // Reset gutter.
137 surfaceView.$documentNode.css( {
138 'margin-left': '',
139 'margin-right': ''
140 } );
141
142 // Set focus on the surface view.
143 surface.getView().focus();
144
145 surface.mirror.destroy();
146 surface.mirror.view = null;
147 }
148
149 surface.mirror = null;
150 }
151
152 return true;
153 };
154
155 /**
156 * Update margins to account for the CodeMirror gutter.
157 *
158 * @param {string} dir Document direction
159 */
160 ve.ui.CodeMirrorAction.prototype.updateGutterWidth = function ( dir ) {
161 const guttersWidth = this.surface.mirror.view.dom.querySelector( '.cm-gutters' ).offsetWidth;
162 this.surface.getView().$documentNode.css( {
163 'margin-left': dir === 'rtl' ? 0 : guttersWidth - 6,
164 'margin-right': dir === 'rtl' ? guttersWidth - 6 : 0
165 } );
166 // Also update width of .cm-content due to apparent Chromium bug.
167 this.surface.mirror.view.contentDOM.style.width = 'calc(100% - ' + ( guttersWidth + 1 ) + 'px)';
168 };
169
170 /**
171 * Mirror document directionality changes to CodeMirror.
172 */
173 ve.ui.CodeMirrorAction.prototype.onPosition = function () {
174 const codeMirrorLib = require( 'ext.CodeMirror.v6.lib' );
175 const veDir = this.surface.getView().getDocument().getDir();
176 const cmView = this.surface.mirror.view;
177 const cmDir = cmView.textDirection === codeMirrorLib.Direction.LTR ? 'ltr' : 'rtl';
178 if ( veDir !== cmDir ) {
179 cmView.dispatch( {
180 effects: this.surface.mirror.dirCompartment.reconfigure(
181 codeMirrorLib.EditorView.editorAttributes.of( { dir: veDir } )
182 )
183 } );
184 this.updateGutterWidth( veDir );
185 }
186 };
187
188 /**
189 * Handle select events from the surface model
190 *
191 * @param {ve.dm.Selection} selection
192 */
193 ve.ui.CodeMirrorAction.prototype.onSelect = function ( selection ) {
194 const range = selection.getCoveringRange();
195
196 // Do not re-trigger bracket matching as long as something is selected
197 if ( !range || !range.isCollapsed() ) {
198 return;
199 }
200
201 const offset = this.surface.getModel().getSourceOffsetFromOffset( range.from );
202
203 this.surface.mirror.view.dispatch( {
204 selection: {
205 anchor: offset,
206 head: offset
207 }
208 } );
209 };
210
211 /**
212 * Handle precommit events from the document.
213 *
214 * The document is still in it's 'old' state before the transaction
215 * has been applied at this point.
216 *
217 * @param {ve.dm.Transaction} tx
218 */
219 ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
220 const replacements = [],
221 action = this,
222 store = this.surface.getModel().getDocument().getStore();
223 let offset = 0;
224
225 tx.operations.forEach( ( op ) => {
226 if ( op.type === 'retain' ) {
227 offset += op.length;
228 } else if ( op.type === 'replace' ) {
229 replacements.push( {
230 from: action.surface.getModel().getSourceOffsetFromOffset( offset ),
231 to: action.surface.getModel().getSourceOffsetFromOffset( offset + op.remove.length ),
232 insert: new ve.dm.ElementLinearData( store, op.insert ).getSourceText()
233 } );
234 offset += op.remove.length;
235 }
236 } );
237
238 // Apply replacements in reverse to avoid having to shift offsets
239 for ( let i = replacements.length - 1; i >= 0; i-- ) {
240 this.surface.mirror.view.dispatch( { changes: replacements[ i ] } );
241 }
242
243 action.updateGutterWidth( this.surface.getModel().getDocument().getDir() );
244 };
245
246 /* Registration */
247
248 ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );