| 1 |
const { |
| 2 |
EditorSelection, |
| 3 |
EditorView, |
| 4 |
Extension, |
| 5 |
LanguageSupport, |
| 6 |
openSearchPanel |
| 7 |
} = require( 'ext.CodeMirror.v6.lib' ); |
| 8 |
const CodeMirror = require( 'ext.CodeMirror.v6' ); |
| 9 |
|
| 10 |
/** |
| 11 |
* CodeMirror integration with |
| 12 |
* [WikiEditor](https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:WikiEditor). |
| 13 |
* |
| 14 |
* Use this class if you want WikiEditor's toolbar. If you don't need the toolbar, |
| 15 |
* using {@link CodeMirror} directly will be considerably more efficient. |
| 16 |
* |
| 17 |
* @example |
| 18 |
* mw.loader.using( [ |
| 19 |
* 'ext.wikiEditor', |
| 20 |
* 'ext.CodeMirror.v6.WikiEditor', |
| 21 |
* 'ext.CodeMirror.v6.mode.mediawiki' |
| 22 |
* ] ).then( ( require ) => { |
| 23 |
* mw.addWikiEditor( myTextarea ); |
| 24 |
* const CodeMirrorWikiEditor = require( 'ext.CodeMirror.v6.WikiEditor' ); |
| 25 |
* const mediawikiLang = require( 'ext.CodeMirror.v6.mode.mediawiki' ); |
| 26 |
* const cmWe = new CodeMirrorWikiEditor( myTextarea ); |
| 27 |
* cmWe.initialize( [ cmWe.defaultExtensions, mediawikiLang() ] ); |
| 28 |
* cmWe.addCodeMirrorToWikiEditor(); |
| 29 |
* } ); |
| 30 |
* @extends CodeMirror |
| 31 |
*/ |
| 32 |
class CodeMirrorWikiEditor extends CodeMirror { |
| 33 |
/** |
| 34 |
* @constructor |
| 35 |
* @param {jQuery} $textarea The textarea to replace with CodeMirror. |
| 36 |
* @param {LanguageSupport|Extension} langExtension Language support and its extension(s). |
| 37 |
* @stable to call and override |
| 38 |
*/ |
| 39 |
constructor( $textarea, langExtension ) { |
| 40 |
super( $textarea ); |
| 41 |
/** |
| 42 |
* Language support and its extension(s). |
| 43 |
* |
| 44 |
* @type {LanguageSupport|Extension} |
| 45 |
*/ |
| 46 |
this.langExtension = langExtension; |
| 47 |
/** |
| 48 |
* Whether CodeMirror is currently enabled. |
| 49 |
* |
| 50 |
* @type {boolean} |
| 51 |
*/ |
| 52 |
this.useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0; |
| 53 |
/** |
| 54 |
* The [Realtime Preview](https://w.wiki/9XgX) handler. |
| 55 |
* |
| 56 |
* @type {Function|null} |
| 57 |
*/ |
| 58 |
this.realtimePreviewHandler = null; |
| 59 |
/** |
| 60 |
* The old WikiEditor search button, to be restored if CodeMirror is disabled. |
| 61 |
* |
| 62 |
* @type {jQuery} |
| 63 |
*/ |
| 64 |
this.$oldSearchBtn = null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @inheritDoc |
| 69 |
*/ |
| 70 |
setCodeMirrorPreference( prefValue ) { |
| 71 |
// Save state for function updateToolbarButton() |
| 72 |
this.useCodeMirror = prefValue; |
| 73 |
CodeMirror.setCodeMirrorPreference( prefValue ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Replaces the default textarea with CodeMirror. |
| 78 |
* |
| 79 |
* @fires CodeMirrorWikiEditor~'ext.CodeMirror.switch' |
| 80 |
* @stable to call |
| 81 |
*/ |
| 82 |
enableCodeMirror() { |
| 83 |
// If CodeMirror is already loaded, abort. |
| 84 |
if ( this.view ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
const selectionStart = this.$textarea.prop( 'selectionStart' ), |
| 89 |
selectionEnd = this.$textarea.prop( 'selectionEnd' ), |
| 90 |
scrollTop = this.$textarea.scrollTop(), |
| 91 |
hasFocus = this.$textarea.is( ':focus' ); |
| 92 |
|
| 93 |
/* |
| 94 |
* Default configuration, which we may conditionally add to later. |
| 95 |
* @see https://codemirror.net/docs/ref/#state.Extension |
| 96 |
*/ |
| 97 |
const extensions = [ |
| 98 |
this.defaultExtensions, |
| 99 |
this.langExtension, |
| 100 |
EditorView.updateListener.of( ( update ) => { |
| 101 |
if ( update.docChanged && typeof this.realtimePreviewHandler === 'function' ) { |
| 102 |
this.realtimePreviewHandler(); |
| 103 |
} |
| 104 |
} ) |
| 105 |
]; |
| 106 |
|
| 107 |
this.initialize( extensions ); |
| 108 |
this.addRealtimePreviewHandler(); |
| 109 |
|
| 110 |
// Sync scroll position, selections, and focus state. |
| 111 |
requestAnimationFrame( () => { |
| 112 |
this.view.scrollDOM.scrollTop = scrollTop; |
| 113 |
} ); |
| 114 |
if ( selectionStart !== 0 || selectionEnd !== 0 ) { |
| 115 |
const range = EditorSelection.range( selectionStart, selectionEnd ), |
| 116 |
scrollEffect = EditorView.scrollIntoView( range ); |
| 117 |
scrollEffect.value.isSnapshot = true; |
| 118 |
this.view.dispatch( { |
| 119 |
selection: EditorSelection.create( [ range ] ), |
| 120 |
effects: scrollEffect |
| 121 |
} ); |
| 122 |
} |
| 123 |
if ( hasFocus ) { |
| 124 |
this.view.focus(); |
| 125 |
} |
| 126 |
|
| 127 |
// Hijack the search button to open the CodeMirror search panel |
| 128 |
// instead of the WikiEditor search dialog. |
| 129 |
// eslint-disable-next-line no-jquery/no-global-selector |
| 130 |
const $searchBtn = $( '.wikiEditor-ui .group-search a' ); |
| 131 |
this.$oldSearchBtn = $searchBtn.clone( true ); |
| 132 |
$searchBtn.off( 'click keydown keypress' ) |
| 133 |
.on( 'click keydown', ( e ) => { |
| 134 |
if ( e.type === 'click' || ( e.type === 'keydown' && e.key === 'Enter' ) ) { |
| 135 |
openSearchPanel( this.view ); |
| 136 |
e.preventDefault(); |
| 137 |
} |
| 138 |
} ); |
| 139 |
|
| 140 |
/** |
| 141 |
* Called after CodeMirror is enabled or disabled in WikiEditor. |
| 142 |
* |
| 143 |
* @event CodeMirrorWikiEditor~'ext.CodeMirror.switch' |
| 144 |
* @param {boolean} enabled Whether CodeMirror is enabled. |
| 145 |
* @param {jQuery} $textarea The current "editor", either the |
| 146 |
* original textarea or the `.cm-editor` element. |
| 147 |
* @stable to use |
| 148 |
*/ |
| 149 |
mw.hook( 'ext.CodeMirror.switch' ).fire( true, $( this.view.dom ) ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Adds the Realtime Preview handler. Realtime Preview reads from the textarea |
| 154 |
* via jQuery.textSelection, which will bubble up to CodeMirror automatically. |
| 155 |
* |
| 156 |
* @private |
| 157 |
*/ |
| 158 |
addRealtimePreviewHandler() { |
| 159 |
mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( ( realtimePreview ) => { |
| 160 |
this.realtimePreviewHandler = realtimePreview.getEventHandler().bind( realtimePreview ); |
| 161 |
} ); |
| 162 |
mw.hook( 'ext.WikiEditor.realtimepreview.disable' ).add( () => { |
| 163 |
this.realtimePreviewHandler = null; |
| 164 |
} ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Adds the CodeMirror button to WikiEditor. |
| 169 |
* |
| 170 |
* @stable to call |
| 171 |
*/ |
| 172 |
addCodeMirrorToWikiEditor() { |
| 173 |
const context = this.$textarea.data( 'wikiEditor-context' ); |
| 174 |
const toolbar = context && context.modules && context.modules.toolbar; |
| 175 |
|
| 176 |
// Guard against something having removed WikiEditor (T271457) |
| 177 |
if ( !toolbar ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
this.$textarea.wikiEditor( |
| 182 |
'addToToolbar', |
| 183 |
{ |
| 184 |
section: 'main', |
| 185 |
groups: { |
| 186 |
codemirror: { |
| 187 |
tools: { |
| 188 |
CodeMirror: { |
| 189 |
type: 'element', |
| 190 |
element: () => { |
| 191 |
// OOUI has already been loaded by WikiEditor. |
| 192 |
const button = new OO.ui.ToggleButtonWidget( { |
| 193 |
label: mw.msg( 'codemirror-toggle-label-short' ), |
| 194 |
icon: 'syntax-highlight', |
| 195 |
value: this.useCodeMirror, |
| 196 |
framed: false, |
| 197 |
classes: [ 'tool', 'cm-mw-toggle-wikieditor' ] |
| 198 |
} ); |
| 199 |
button.on( 'change', this.switchCodeMirror.bind( this ) ); |
| 200 |
return button.$element; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
); |
| 208 |
|
| 209 |
const $codeMirrorButton = toolbar.$toolbar.find( '.tool[rel=CodeMirror]' ); |
| 210 |
$codeMirrorButton.attr( 'id', 'mw-editbutton-codemirror' ); |
| 211 |
|
| 212 |
// Hide non-applicable buttons until WikiEditor better supports a read-only mode (T188817). |
| 213 |
if ( this.readOnly ) { |
| 214 |
this.$textarea.data( 'wikiEditor-context' ).$ui.addClass( 'ext-codemirror-readonly' ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( this.useCodeMirror ) { |
| 218 |
this.enableCodeMirror(); |
| 219 |
} |
| 220 |
this.updateToolbarButton(); |
| 221 |
|
| 222 |
CodeMirror.logUsage( { |
| 223 |
editor: 'wikitext', |
| 224 |
enabled: this.useCodeMirror, |
| 225 |
toggled: false, |
| 226 |
// eslint-disable-next-line no-jquery/no-global-selector,camelcase |
| 227 |
edit_start_ts_ms: parseInt( $( 'input[name="wpStarttime"]' ).val(), 10 ) * 1000 || 0 |
| 228 |
} ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Updates CodeMirror button on the toolbar according to the current state (on/off). |
| 233 |
* |
| 234 |
* @private |
| 235 |
*/ |
| 236 |
updateToolbarButton() { |
| 237 |
// eslint-disable-next-line no-jquery/no-global-selector |
| 238 |
const $button = $( '#mw-editbutton-codemirror' ); |
| 239 |
$button.toggleClass( 'mw-editbutton-codemirror-active', this.useCodeMirror ); |
| 240 |
|
| 241 |
// WikiEditor2010 OOUI ToggleButtonWidget |
| 242 |
if ( $button.data( 'setActive' ) ) { |
| 243 |
$button.data( 'setActive' )( this.useCodeMirror ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Enables or disables CodeMirror. |
| 249 |
* |
| 250 |
* @fires CodeMirrorWikiEditor~'ext.CodeMirror.switch' |
| 251 |
* @stable to call |
| 252 |
*/ |
| 253 |
switchCodeMirror() { |
| 254 |
if ( this.view ) { |
| 255 |
this.setCodeMirrorPreference( false ); |
| 256 |
this.destroy(); |
| 257 |
// eslint-disable-next-line no-jquery/no-global-selector |
| 258 |
$( '.wikiEditor-ui .group-search a' ).replaceWith( this.$oldSearchBtn ); |
| 259 |
mw.hook( 'ext.CodeMirror.switch' ).fire( false, this.$textarea ); |
| 260 |
} else { |
| 261 |
this.enableCodeMirror(); |
| 262 |
this.setCodeMirrorPreference( true ); |
| 263 |
} |
| 264 |
this.updateToolbarButton(); |
| 265 |
|
| 266 |
CodeMirror.logUsage( { |
| 267 |
editor: 'wikitext', |
| 268 |
enabled: this.useCodeMirror, |
| 269 |
toggled: true, |
| 270 |
// eslint-disable-next-line no-jquery/no-global-selector,camelcase |
| 271 |
edit_start_ts_ms: parseInt( $( 'input[name="wpStarttime"]' ).val(), 10 ) * 1000 || 0 |
| 272 |
} ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
module.exports = CodeMirrorWikiEditor; |