2018-11-12 13:19:41 +00:00
|
|
|
( function () {
|
2018-10-25 20:41:01 +00:00
|
|
|
var useCodeMirror, codeMirror, api, originHooksTextarea, cmTextSelection,
|
2019-02-23 18:04:17 +00:00
|
|
|
$textbox1,
|
2018-01-04 12:35:36 +00:00
|
|
|
enableContentEditable = true;
|
2017-03-21 10:13:18 +00:00
|
|
|
|
2015-11-18 05:34:36 +00:00
|
|
|
if ( mw.config.get( 'wgCodeEditorCurrentLanguage' ) ) { // If the CodeEditor is used then just exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-25 20:41:01 +00:00
|
|
|
// Exit if WikiEditor is disabled
|
|
|
|
// usebetatoolbar can be the string "0" if the user disabled the preference - Bug T54542#555387
|
|
|
|
if ( !( mw.loader.getState( 'ext.wikiEditor' ) && mw.user.options.get( 'usebetatoolbar' ) > 0 ) ) {
|
2018-01-04 12:35:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-21 13:21:48 +00:00
|
|
|
useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
|
2017-03-21 10:13:18 +00:00
|
|
|
api = new mw.Api();
|
2017-09-11 14:43:32 +00:00
|
|
|
|
2017-03-21 10:13:18 +00:00
|
|
|
originHooksTextarea = $.valHooks.textarea;
|
2017-09-11 14:43:32 +00:00
|
|
|
// define jQuery hook for searching and replacing text using JS if CodeMirror is enabled, see Bug: T108711
|
|
|
|
$.valHooks.textarea = {
|
|
|
|
get: function ( elem ) {
|
|
|
|
if ( elem.id === 'wpTextbox1' && codeMirror ) {
|
|
|
|
return codeMirror.doc.getValue();
|
|
|
|
} else if ( originHooksTextarea ) {
|
|
|
|
return originHooksTextarea.get( elem );
|
|
|
|
}
|
|
|
|
return elem.value;
|
|
|
|
},
|
|
|
|
set: function ( elem, value ) {
|
|
|
|
if ( elem.id === 'wpTextbox1' && codeMirror ) {
|
|
|
|
return codeMirror.doc.setValue( value );
|
|
|
|
} else if ( originHooksTextarea ) {
|
|
|
|
return originHooksTextarea.set( elem, value );
|
|
|
|
}
|
|
|
|
elem.value = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-03 17:46:21 +00:00
|
|
|
// Disable spellchecking for Firefox users on non-Mac systems (Bug T95104)
|
|
|
|
if ( navigator.userAgent.indexOf( 'Firefox' ) > -1 &&
|
|
|
|
navigator.userAgent.indexOf( 'Mac' ) === -1
|
|
|
|
) {
|
|
|
|
enableContentEditable = false;
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:37:38 +00:00
|
|
|
// T174055: Do not redefine the browser history navigation keys (T175378: for PC only)
|
|
|
|
CodeMirror.keyMap.pcDefault[ 'Alt-Left' ] = false;
|
|
|
|
CodeMirror.keyMap.pcDefault[ 'Alt-Right' ] = false;
|
|
|
|
|
2018-01-22 02:16:40 +00:00
|
|
|
// jQuery.textSelection overrides for CodeMirror.
|
|
|
|
// See jQuery.textSelection.js for method documentation
|
|
|
|
cmTextSelection = {
|
|
|
|
getContents: function () {
|
|
|
|
return codeMirror.doc.getValue();
|
|
|
|
},
|
|
|
|
setContents: function ( content ) {
|
|
|
|
codeMirror.doc.setValue( content );
|
2018-02-26 11:47:43 +00:00
|
|
|
return this;
|
2018-01-22 02:16:40 +00:00
|
|
|
},
|
|
|
|
getSelection: function () {
|
|
|
|
return codeMirror.doc.getSelection();
|
|
|
|
},
|
|
|
|
setSelection: function ( options ) {
|
2018-02-09 10:31:47 +00:00
|
|
|
codeMirror.focus();
|
2018-06-17 16:10:24 +00:00
|
|
|
codeMirror.doc.setSelection( codeMirror.doc.posFromIndex( options.start ), codeMirror.doc.posFromIndex( options.end ) );
|
2018-02-26 11:47:43 +00:00
|
|
|
return this;
|
2018-01-22 02:16:40 +00:00
|
|
|
},
|
2018-02-09 10:31:47 +00:00
|
|
|
replaceSelection: function ( value ) {
|
|
|
|
codeMirror.doc.replaceSelection( value );
|
2018-02-26 11:47:43 +00:00
|
|
|
return this;
|
2018-01-22 02:16:40 +00:00
|
|
|
},
|
|
|
|
getCaretPosition: function ( options ) {
|
|
|
|
var caretPos = codeMirror.doc.indexFromPos( codeMirror.doc.getCursor( true ) ),
|
|
|
|
endPos = codeMirror.doc.indexFromPos( codeMirror.doc.getCursor( false ) );
|
|
|
|
if ( options.startAndEnd ) {
|
|
|
|
return [ caretPos, endPos ];
|
2015-03-16 05:00:43 +00:00
|
|
|
}
|
2018-01-22 02:16:40 +00:00
|
|
|
return caretPos;
|
|
|
|
},
|
|
|
|
scrollToCaretPosition: function () {
|
2018-02-09 10:31:47 +00:00
|
|
|
codeMirror.scrollIntoView( null );
|
2018-02-26 11:47:43 +00:00
|
|
|
return this;
|
2017-03-22 11:47:16 +00:00
|
|
|
}
|
2018-01-22 02:16:40 +00:00
|
|
|
};
|
2017-03-21 10:13:18 +00:00
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* Save CodeMirror enabled pref.
|
|
|
|
*
|
2015-11-24 16:03:00 +00:00
|
|
|
* @param {boolean} prefValue True, if CodeMirror should be enabled by default, otherwise false.
|
2015-03-16 05:00:43 +00:00
|
|
|
*/
|
|
|
|
function setCodeEditorPreference( prefValue ) {
|
2018-03-22 14:52:27 +00:00
|
|
|
useCodeMirror = prefValue; // Save state for function updateToolbarButton()
|
2017-03-27 08:23:21 +00:00
|
|
|
|
2015-10-29 03:15:29 +00:00
|
|
|
if ( mw.user.isAnon() ) { // Skip it for anon users
|
|
|
|
return;
|
|
|
|
}
|
2017-03-21 13:21:48 +00:00
|
|
|
api.saveOption( 'usecodemirror', prefValue ? 1 : 0 );
|
|
|
|
mw.user.options.set( 'usecodemirror', prefValue ? 1 : 0 );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:00:43 +00:00
|
|
|
/**
|
|
|
|
* Replaces the default textarea with CodeMirror
|
|
|
|
*/
|
2017-04-18 07:51:32 +00:00
|
|
|
function enableCodeMirror() {
|
2017-04-28 13:42:30 +00:00
|
|
|
var config = mw.config.get( 'extCodeMirrorConfig' );
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2017-05-04 20:18:37 +00:00
|
|
|
mw.loader.using( config.pluginModules, function () {
|
2017-04-28 13:42:30 +00:00
|
|
|
var $codeMirror,
|
2017-09-03 16:14:51 +00:00
|
|
|
selectionStart = $textbox1.prop( 'selectionStart' ),
|
|
|
|
selectionEnd = $textbox1.prop( 'selectionEnd' ),
|
|
|
|
scrollTop = $textbox1.scrollTop();
|
|
|
|
|
2017-11-06 22:48:50 +00:00
|
|
|
// If CodeMirror is already loaded or wikEd gadget is enabled, abort. See T178348.
|
|
|
|
// FIXME: Would be good to replace the wikEd check with something more generic.
|
2017-12-05 13:45:12 +00:00
|
|
|
if ( codeMirror || mw.user.options.get( 'gadget-wikEd' ) > 0 ) {
|
2017-09-03 16:14:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-28 13:42:30 +00:00
|
|
|
|
|
|
|
codeMirror = CodeMirror.fromTextArea( $textbox1[ 0 ], {
|
|
|
|
mwConfig: config,
|
|
|
|
// styleActiveLine: true, // disabled since Bug: T162204, maybe should be optional
|
|
|
|
lineWrapping: true,
|
|
|
|
readOnly: $textbox1[ 0 ].readOnly,
|
|
|
|
// select mediawiki as text input mode
|
|
|
|
mode: 'text/mediawiki',
|
|
|
|
extraKeys: {
|
2017-08-24 17:58:13 +00:00
|
|
|
Tab: false,
|
2018-06-17 15:50:12 +00:00
|
|
|
'Shift-Tab': false,
|
2017-09-04 09:36:34 +00:00
|
|
|
// T174514: Move the cursor at the beginning/end of the current wrapped line
|
|
|
|
Home: 'goLineLeft',
|
2017-09-11 16:37:38 +00:00
|
|
|
End: 'goLineRight'
|
2017-09-05 19:33:41 +00:00
|
|
|
},
|
2017-10-03 17:46:21 +00:00
|
|
|
inputStyle: enableContentEditable ? 'contenteditable' : 'textarea',
|
|
|
|
spellcheck: enableContentEditable,
|
2017-09-05 19:33:41 +00:00
|
|
|
viewportMargin: Infinity
|
2017-04-28 13:42:30 +00:00
|
|
|
} );
|
|
|
|
$codeMirror = $( codeMirror.getWrapperElement() );
|
2018-01-22 02:16:40 +00:00
|
|
|
|
|
|
|
// Allow textSelection() functions to work with CodeMirror editing field.
|
|
|
|
$codeMirror.textSelection( 'register', cmTextSelection );
|
|
|
|
// Also override textSelection() functions for the "real" hidden textarea to route to
|
|
|
|
// CodeMirror. We unregister this when switching to normal textarea mode.
|
|
|
|
$textbox1.textSelection( 'register', cmTextSelection );
|
2017-03-21 13:21:48 +00:00
|
|
|
|
2017-11-06 23:42:05 +00:00
|
|
|
$codeMirror.resizable( {
|
|
|
|
handles: 'se',
|
|
|
|
resize: function ( event, ui ) {
|
|
|
|
ui.size.width = ui.originalSize.width;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2017-09-03 16:14:51 +00:00
|
|
|
codeMirror.doc.setSelection( codeMirror.doc.posFromIndex( selectionEnd ), codeMirror.doc.posFromIndex( selectionStart ) );
|
|
|
|
codeMirror.scrollTo( null, scrollTop );
|
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
// HACK: <textarea> font size varies by browser (chrome/FF/IE)
|
|
|
|
$codeMirror.css( {
|
|
|
|
'font-size': $textbox1.css( 'font-size' ),
|
|
|
|
'line-height': $textbox1.css( 'line-height' )
|
|
|
|
} );
|
|
|
|
|
2017-09-09 12:31:24 +00:00
|
|
|
// use direction and language of the original textbox
|
|
|
|
$codeMirror.attr( {
|
|
|
|
dir: $textbox1.attr( 'dir' ),
|
|
|
|
lang: $textbox1.attr( 'lang' )
|
|
|
|
} );
|
|
|
|
|
2018-05-26 01:24:46 +00:00
|
|
|
// T194102: UniversalLanguageSelector integration is buggy, disabling it completely
|
|
|
|
$( codeMirror.getInputField() ).addClass( 'noime' );
|
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
// set the height of the textarea
|
|
|
|
codeMirror.setSize( null, $textbox1.height() );
|
|
|
|
} );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 14:43:32 +00:00
|
|
|
/**
|
|
|
|
* Updates CodeMirror button on the toolbar according to the current state (on/off)
|
|
|
|
*/
|
|
|
|
function updateToolbarButton() {
|
2018-03-22 14:52:27 +00:00
|
|
|
var $button = $( '#mw-editbutton-codemirror' );
|
|
|
|
|
2018-10-25 20:41:01 +00:00
|
|
|
$button.toggleClass( 'mw-editbutton-codemirror-active', !!useCodeMirror );
|
|
|
|
|
2018-07-24 23:11:08 +00:00
|
|
|
// WikiEditor2010 OOUI ToggleButtonWidget
|
2018-06-17 19:41:59 +00:00
|
|
|
if ( $button.data( 'setActive' ) ) {
|
|
|
|
$button.data( 'setActive' )( !!useCodeMirror );
|
|
|
|
}
|
2017-09-11 14:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables CodeMirror
|
|
|
|
*/
|
|
|
|
function switchCodeMirror() {
|
2019-02-23 18:04:17 +00:00
|
|
|
var selectionObj, selectionStart, selectionEnd, scrollTop, hasFocus, $codeMirror;
|
2017-09-11 14:43:32 +00:00
|
|
|
|
|
|
|
if ( codeMirror ) {
|
|
|
|
scrollTop = codeMirror.getScrollInfo().top;
|
|
|
|
selectionObj = codeMirror.doc.listSelections()[ 0 ];
|
|
|
|
selectionStart = codeMirror.doc.indexFromPos( selectionObj.head );
|
|
|
|
selectionEnd = codeMirror.doc.indexFromPos( selectionObj.anchor );
|
|
|
|
hasFocus = codeMirror.hasFocus();
|
2018-01-22 02:16:40 +00:00
|
|
|
$codeMirror = $( codeMirror.getWrapperElement() );
|
2017-09-11 14:43:32 +00:00
|
|
|
setCodeEditorPreference( false );
|
2018-01-22 02:16:40 +00:00
|
|
|
$codeMirror.textSelection( 'unregister' );
|
|
|
|
$textbox1.textSelection( 'unregister' );
|
2017-09-11 14:43:32 +00:00
|
|
|
codeMirror.toTextArea();
|
|
|
|
codeMirror = null;
|
|
|
|
if ( hasFocus ) {
|
|
|
|
$textbox1.focus();
|
|
|
|
}
|
|
|
|
$textbox1.prop( 'selectionStart', selectionStart );
|
|
|
|
$textbox1.prop( 'selectionEnd', selectionEnd );
|
|
|
|
$textbox1.scrollTop( scrollTop );
|
|
|
|
} else {
|
|
|
|
enableCodeMirror();
|
|
|
|
setCodeEditorPreference( true );
|
|
|
|
}
|
|
|
|
updateToolbarButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the CodeMirror button to WikiEditor
|
|
|
|
*/
|
|
|
|
function addCodeMirrorToWikiEditor() {
|
|
|
|
var $codeMirrorButton;
|
|
|
|
|
2019-02-23 18:04:17 +00:00
|
|
|
$textbox1.wikiEditor(
|
2017-09-11 14:43:32 +00:00
|
|
|
'addToToolbar',
|
|
|
|
{
|
|
|
|
section: 'main',
|
|
|
|
groups: {
|
|
|
|
codemirror: {
|
|
|
|
tools: {
|
|
|
|
CodeMirror: {
|
|
|
|
label: mw.msg( 'codemirror-toggle-label' ),
|
2018-07-24 23:11:08 +00:00
|
|
|
type: 'toggle',
|
2018-03-22 14:52:27 +00:00
|
|
|
oouiIcon: 'highlight',
|
2017-09-11 14:43:32 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: function () {
|
|
|
|
switchCodeMirror();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-02-23 18:04:17 +00:00
|
|
|
$codeMirrorButton = $textbox1.data( 'wikiEditor-context' ).modules.toolbar.$toolbar.find( '.tool[rel=CodeMirror]' );
|
2018-06-17 19:41:59 +00:00
|
|
|
$codeMirrorButton
|
2018-07-24 23:11:08 +00:00
|
|
|
.attr( 'id', 'mw-editbutton-codemirror' );
|
2017-09-11 14:43:32 +00:00
|
|
|
|
|
|
|
updateToolbarButton();
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:25:26 +00:00
|
|
|
/**
|
|
|
|
* Adds CodeMirror button to the toolbar
|
|
|
|
*/
|
|
|
|
function addToolbarButton() {
|
2018-10-25 20:41:01 +00:00
|
|
|
// They are using WikiEditor
|
|
|
|
mw.loader.using( 'ext.wikiEditor', function () {
|
|
|
|
// Add CodeMirror button to the enhanced editing toolbar.
|
2019-02-23 18:04:17 +00:00
|
|
|
// Add an additional $() to avoid a race condition with WikiEditor.
|
|
|
|
// FIXME: Solve this with an explicit dependency.
|
2018-10-25 20:41:01 +00:00
|
|
|
$( addCodeMirrorToWikiEditor );
|
|
|
|
} );
|
2017-11-06 22:25:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 18:04:17 +00:00
|
|
|
$( function () {
|
|
|
|
$textbox1 = $( '#wpTextbox1' );
|
|
|
|
// If view is in edit mode, add the button to the toolbar.
|
|
|
|
if ( $textbox1.length ) {
|
|
|
|
addToolbarButton();
|
|
|
|
}
|
2015-03-16 05:00:43 +00:00
|
|
|
|
2019-02-23 18:04:17 +00:00
|
|
|
// enable CodeMirror
|
|
|
|
if ( useCodeMirror ) {
|
|
|
|
$textbox1.on( 'wikiEditor-toolbar-doneInitialSections', enableCodeMirror.bind( this ) );
|
|
|
|
}
|
|
|
|
} );
|
2017-06-26 23:37:48 +00:00
|
|
|
|
2017-09-11 04:47:39 +00:00
|
|
|
// Synchronize textarea with CodeMirror before leaving
|
|
|
|
window.addEventListener( 'beforeunload', function () {
|
|
|
|
if ( codeMirror ) {
|
|
|
|
codeMirror.save();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2018-11-12 13:19:41 +00:00
|
|
|
}() );
|