2011-06-10 01:11:18 +00:00
|
|
|
/* Ace syntax-highlighting code editor extension for wikiEditor */
|
2016-12-09 13:50:28 +00:00
|
|
|
/* global ace */
|
2019-02-06 01:34:32 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2018-11-12 13:16:11 +00:00
|
|
|
( function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
$.wikiEditor.modules.codeEditor = {
|
|
|
|
/**
|
|
|
|
* Core Requirements
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
req: [ 'codeEditor' ],
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Configuration
|
|
|
|
*/
|
|
|
|
cfg: {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* API accessible functions
|
|
|
|
*/
|
|
|
|
api: {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Event handlers
|
|
|
|
*/
|
|
|
|
evt: {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Internally used functions
|
|
|
|
*/
|
|
|
|
fn: {
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$.wikiEditor.extensions.codeEditor = function ( context ) {
|
2014-10-16 18:37:18 +00:00
|
|
|
var saveAndExtend,
|
2014-07-28 20:50:19 +00:00
|
|
|
textSelectionFn,
|
2015-03-29 14:20:40 +00:00
|
|
|
hasErrorsOnSave = false,
|
2015-08-27 19:22:09 +00:00
|
|
|
selectedLine = 0,
|
2013-12-30 15:57:47 +00:00
|
|
|
returnFalse = function () { return false; },
|
2016-01-27 11:07:38 +00:00
|
|
|
api = new mw.Api();
|
2013-12-30 15:57:47 +00:00
|
|
|
|
|
|
|
// Initialize state
|
2021-10-06 17:00:20 +00:00
|
|
|
var cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-showInvisibleChars' ), 10 );
|
2013-12-30 15:57:47 +00:00
|
|
|
context.showInvisibleChars = ( cookieEnabled === 1 );
|
|
|
|
cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-lineWrappingActive' ), 10 );
|
|
|
|
context.lineWrappingActive = ( cookieEnabled === 1 );
|
2013-12-31 12:23:48 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/*
|
|
|
|
* Event Handlers
|
|
|
|
*
|
2014-10-16 18:37:18 +00:00
|
|
|
* WikiEditor inspects the 'evt' object for event names and uses them if present as additional
|
|
|
|
* event handlers that fire before the default handling.
|
|
|
|
* To prevent WikiEditor from running its own handling, handlers should return false.
|
|
|
|
*
|
|
|
|
* This is also where we can attach some extra information to the events.
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
|
|
|
context.evt = $.extend( context.evt, {
|
2015-06-27 20:22:54 +00:00
|
|
|
keydown: returnFalse,
|
|
|
|
change: returnFalse,
|
|
|
|
delayedChange: returnFalse,
|
|
|
|
cut: returnFalse,
|
|
|
|
paste: returnFalse,
|
|
|
|
ready: returnFalse,
|
|
|
|
codeEditorSubmit: function () {
|
2016-12-09 13:50:28 +00:00
|
|
|
var form = this;
|
2014-03-28 10:47:21 +00:00
|
|
|
context.evt.codeEditorSync();
|
2015-03-29 14:20:40 +00:00
|
|
|
if ( hasErrorsOnSave ) {
|
|
|
|
hasErrorsOnSave = false;
|
2016-12-09 13:50:28 +00:00
|
|
|
OO.ui.confirm( mw.msg( 'codeeditor-save-with-errors' ) ).done( function ( confirmed ) {
|
|
|
|
if ( confirmed ) {
|
|
|
|
// Programmatic submit doesn't retrigger this event listener
|
|
|
|
form.submit();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return false;
|
2015-03-29 14:20:40 +00:00
|
|
|
}
|
2016-12-09 13:50:28 +00:00
|
|
|
return true;
|
2015-03-29 14:20:40 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
codeEditorSave: function () {
|
2021-10-06 17:00:20 +00:00
|
|
|
if ( context.codeEditor.getSession().getAnnotations().some( function ( ann ) {
|
|
|
|
return ann.type === 'error';
|
|
|
|
} ) ) {
|
|
|
|
hasErrorsOnSave = true;
|
2014-03-28 10:47:21 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
codeEditorSync: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
context.$textarea.val( context.$textarea.textSelection( 'getContents' ) );
|
2014-03-28 10:47:21 +00:00
|
|
|
|
2011-06-16 22:30:01 +00:00
|
|
|
}
|
|
|
|
} );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2017-03-31 12:57:28 +00:00
|
|
|
// Make sure to cast '0' to false
|
|
|
|
context.codeEditorActive = !!Number( mw.user.options.get( 'usecodeeditor' ) );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internally used functions
|
|
|
|
*/
|
|
|
|
context.fn = $.extend( context.fn, {
|
2016-02-27 22:39:07 +00:00
|
|
|
isCodeEditorActive: function () {
|
|
|
|
return context.codeEditorActive;
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2016-02-27 22:39:07 +00:00
|
|
|
isShowInvisibleChars: function () {
|
|
|
|
return context.showInvisibleChars;
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2016-02-27 22:39:07 +00:00
|
|
|
isLineWrappingActive: function () {
|
|
|
|
return context.lineWrappingActive;
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
changeCookieValue: function ( cookieName, value ) {
|
2013-12-30 15:57:47 +00:00
|
|
|
mw.cookie.set(
|
|
|
|
'codeEditor-' + context.instance + '-' + cookieName,
|
|
|
|
value
|
|
|
|
);
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
aceGotoLineColumn: function () {
|
2016-12-09 14:55:17 +00:00
|
|
|
OO.ui.prompt( mw.msg( 'codeeditor-gotoline-prompt' ), {
|
|
|
|
textInput: { placeholder: mw.msg( 'codeeditor-gotoline-placeholder' ) }
|
|
|
|
} ).done( function ( result ) {
|
|
|
|
if ( !result ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var matches = result.split( ':' );
|
|
|
|
var line = 0;
|
|
|
|
var column = 0;
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2016-12-09 14:55:17 +00:00
|
|
|
if ( matches.length > 0 ) {
|
|
|
|
line = +matches[ 0 ];
|
|
|
|
if ( isNaN( line ) ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Lines are zero-indexed
|
|
|
|
line--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( matches.length > 1 ) {
|
|
|
|
column = +matches[ 1 ];
|
|
|
|
if ( isNaN( column ) ) {
|
|
|
|
column = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.codeEditor.navigateTo( line, column );
|
|
|
|
// Scroll up a bit to give some context
|
|
|
|
context.codeEditor.scrollToRow( line - 4 );
|
|
|
|
} );
|
2013-12-31 12:03:11 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
setupCodeEditorToolbar: function () {
|
2021-10-06 17:00:20 +00:00
|
|
|
var toggleEditor = function ( ctx ) {
|
|
|
|
ctx.codeEditorActive = !ctx.codeEditorActive;
|
2014-04-28 13:31:57 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.fn.setCodeEditorPreference( ctx.codeEditorActive );
|
|
|
|
ctx.fn.updateCodeEditorToolbarButton();
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
if ( ctx.codeEditorActive ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
// set it back up!
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.fn.setupCodeEditor();
|
2013-12-31 12:03:11 +00:00
|
|
|
} else {
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.fn.disableCodeEditor();
|
2013-12-31 12:03:11 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var toggleInvisibleChars = function ( ctx ) {
|
|
|
|
ctx.showInvisibleChars = !ctx.showInvisibleChars;
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.fn.changeCookieValue( 'showInvisibleChars', ctx.showInvisibleChars ? 1 : 0 );
|
|
|
|
ctx.fn.updateInvisibleCharsButton();
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.codeEditor.setShowInvisibles( ctx.showInvisibleChars );
|
2013-12-30 15:57:47 +00:00
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var toggleSearchReplace = function ( ctx ) {
|
|
|
|
var searchBox = ctx.codeEditor.searchBox;
|
2021-03-25 20:11:52 +00:00
|
|
|
if ( searchBox && $( searchBox.element ).css( 'display' ) !== 'none' ) {
|
|
|
|
searchBox.hide();
|
2013-12-30 15:57:47 +00:00
|
|
|
} else {
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.codeEditor.execCommand(
|
|
|
|
ctx.codeEditor.getReadOnly() ? 'find' : 'replace'
|
2021-03-25 20:11:52 +00:00
|
|
|
);
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var toggleLineWrapping = function ( ctx ) {
|
|
|
|
ctx.lineWrappingActive = !ctx.lineWrappingActive;
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.fn.changeCookieValue( 'lineWrappingActive', ctx.lineWrappingActive ? 1 : 0 );
|
|
|
|
ctx.fn.updateLineWrappingButton();
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
ctx.codeEditor.getSession().setUseWrapMode( ctx.lineWrappingActive );
|
2013-12-30 15:57:47 +00:00
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var indent = function ( ctx ) {
|
|
|
|
ctx.codeEditor.execCommand( 'indent' );
|
2013-12-30 15:57:47 +00:00
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var outdent = function ( ctx ) {
|
|
|
|
ctx.codeEditor.execCommand( 'outdent' );
|
2013-12-30 15:57:47 +00:00
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var gotoLine = function ( ctx ) {
|
|
|
|
ctx.codeEditor.execCommand( 'gotolinecolumn' );
|
2013-12-30 15:57:47 +00:00
|
|
|
};
|
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
context.api.addToToolbar( context, {
|
2015-06-27 20:22:54 +00:00
|
|
|
section: 'main',
|
|
|
|
groups: {
|
2015-01-29 08:39:43 +00:00
|
|
|
'codeeditor-main': {
|
2015-06-27 20:22:54 +00:00
|
|
|
tools: {
|
|
|
|
codeEditor: {
|
|
|
|
labelMsg: 'codeeditor-toolbar-toggle',
|
2018-07-24 23:13:06 +00:00
|
|
|
type: 'toggle',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'markup',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: toggleEditor
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'codeeditor-format': {
|
2015-06-27 20:22:54 +00:00
|
|
|
tools: {
|
|
|
|
indent: {
|
|
|
|
labelMsg: 'codeeditor-indent',
|
|
|
|
type: 'button',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'indent',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: indent
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
outdent: {
|
|
|
|
labelMsg: 'codeeditor-outdent',
|
|
|
|
type: 'button',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'outdent',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: outdent
|
2014-03-16 22:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2014-03-16 22:43:37 +00:00
|
|
|
}
|
|
|
|
},
|
2013-12-30 15:57:47 +00:00
|
|
|
'codeeditor-style': {
|
2015-06-27 20:22:54 +00:00
|
|
|
tools: {
|
|
|
|
invisibleChars: {
|
|
|
|
labelMsg: 'codeeditor-invisibleChars-toggle',
|
2018-07-24 23:13:06 +00:00
|
|
|
type: 'toggle',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'pilcrow',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: toggleInvisibleChars
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
lineWrapping: {
|
|
|
|
labelMsg: 'codeeditor-lineWrapping-toggle',
|
2018-07-24 23:13:06 +00:00
|
|
|
type: 'toggle',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'wrapping',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: toggleLineWrapping
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
gotoLine: {
|
|
|
|
labelMsg: 'codeeditor-gotoline',
|
|
|
|
type: 'button',
|
2018-03-21 18:35:13 +00:00
|
|
|
oouiIcon: 'gotoLine',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: gotoLine
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
toggleSearchReplace: {
|
|
|
|
labelMsg: 'codeeditor-searchReplace-toggle',
|
2021-03-25 20:11:52 +00:00
|
|
|
type: 'button',
|
2018-04-02 21:25:43 +00:00
|
|
|
oouiIcon: 'articleSearch',
|
2015-06-27 20:22:54 +00:00
|
|
|
action: {
|
|
|
|
type: 'callback',
|
|
|
|
execute: toggleSearchReplace
|
2013-12-30 15:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
}
|
2011-06-10 01:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
} );
|
2016-02-27 22:39:07 +00:00
|
|
|
context.fn.updateCodeEditorToolbarButton();
|
|
|
|
context.fn.updateInvisibleCharsButton();
|
|
|
|
context.fn.updateLineWrappingButton();
|
2013-12-30 15:57:47 +00:00
|
|
|
$( '.group-codeeditor-style' ).prependTo( '.section-main' );
|
|
|
|
$( '.group-codeeditor-format' ).prependTo( '.section-main' );
|
2014-03-16 22:43:37 +00:00
|
|
|
$( '.group-codeeditor-main' ).prependTo( '.section-main' );
|
2013-12-31 12:03:11 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
updateButtonIcon: function ( targetName, iconFn ) {
|
2016-01-29 11:07:47 +00:00
|
|
|
var target = '.tool[rel=' + targetName + ']',
|
2018-03-21 18:35:13 +00:00
|
|
|
$button = context.modules.toolbar.$toolbar.find( target );
|
|
|
|
|
|
|
|
$button.data( 'setActive' )( iconFn() );
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
updateCodeEditorToolbarButton: function () {
|
2016-02-27 22:39:07 +00:00
|
|
|
context.fn.updateButtonIcon( 'codeEditor', context.fn.isCodeEditorActive );
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
updateInvisibleCharsButton: function () {
|
2016-02-27 22:39:07 +00:00
|
|
|
context.fn.updateButtonIcon( 'invisibleChars', context.fn.isShowInvisibleChars );
|
2013-12-30 15:57:47 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
updateLineWrappingButton: function () {
|
2016-02-27 22:39:07 +00:00
|
|
|
context.fn.updateButtonIcon( 'lineWrapping', context.fn.isLineWrappingActive );
|
2013-12-31 12:03:11 +00:00
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
setCodeEditorPreference: function ( prefValue ) {
|
2015-11-28 23:10:40 +00:00
|
|
|
// Do not try to save options for anonymous user
|
|
|
|
if ( mw.user.isAnon() ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-27 11:07:38 +00:00
|
|
|
|
|
|
|
// Abort any previous request
|
|
|
|
api.abort();
|
|
|
|
|
2015-11-29 16:00:13 +00:00
|
|
|
api.saveOption( 'usecodeeditor', prefValue ? 1 : 0 )
|
2017-10-25 17:54:00 +00:00
|
|
|
.fail( function ( code, result ) {
|
|
|
|
if ( code === 'http' && result.textStatus === 'abort' ) {
|
|
|
|
// Request was aborted. Ignore error
|
|
|
|
return;
|
|
|
|
}
|
2016-01-27 11:07:38 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var message = 'Failed to set code editor preference: ' + code;
|
2017-10-25 17:54:00 +00:00
|
|
|
if ( result.error && result.error.info ) {
|
|
|
|
message += '\n' + result.error.info;
|
|
|
|
}
|
|
|
|
mw.log.warn( message );
|
|
|
|
} );
|
2014-04-28 13:31:57 +00:00
|
|
|
},
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Sets up the iframe in place of the textarea to allow more advanced operations
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
setupCodeEditor: function () {
|
2021-10-06 17:00:20 +00:00
|
|
|
var $box = context.$textarea;
|
|
|
|
var lang = mw.config.get( 'wgCodeEditorCurrentLanguage' );
|
|
|
|
var basePath = mw.config.get( 'wgExtensionAssetsPath', '' );
|
2016-09-26 19:36:53 +00:00
|
|
|
if ( basePath.slice( 0, 2 ) === '//' ) {
|
2014-03-16 18:51:40 +00:00
|
|
|
// ACE uses web workers, which have importScripts, which don't like relative links.
|
|
|
|
// This is a problem only when the assets are on another server, so this rewrite should suffice
|
|
|
|
// Protocol relative
|
|
|
|
basePath = window.location.protocol + basePath;
|
|
|
|
}
|
|
|
|
ace.config.set( 'basePath', basePath + '/CodeEditor/modules/ace' );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
if ( lang ) {
|
|
|
|
// Ace doesn't like replacing a textarea directly.
|
|
|
|
// We'll stub this out to sit on top of it...
|
|
|
|
// line-height is needed to compensate for oddity in WikiEditor extension, which zeroes the line-height on a parent container
|
2019-02-06 01:34:32 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-parse-html-literal
|
2021-10-06 17:00:20 +00:00
|
|
|
var container = context.$codeEditorContainer = $( '<div style="position: relative"><div class="editor" style="line-height: 1.5em; top: 0; left: 0; right: 0; bottom: 0; position: absolute;"></div></div>' ).insertAfter( $box );
|
|
|
|
var editdiv = container.find( '.editor' );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2019-11-15 16:40:39 +00:00
|
|
|
$box.css( 'display', 'none' );
|
|
|
|
container.height( $box.height() );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2014-01-10 14:06:22 +00:00
|
|
|
// Non-lazy loaded dependencies: Enable code completion
|
|
|
|
ace.require( 'ace/ext/language_tools' );
|
|
|
|
|
|
|
|
// Load the editor now
|
2015-10-25 12:09:56 +00:00
|
|
|
context.codeEditor = ace.edit( editdiv[ 0 ] );
|
2019-11-15 16:40:39 +00:00
|
|
|
context.codeEditor.getSession().setValue( $box.val() );
|
|
|
|
$box.textSelection( 'register', textSelectionFn );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2021-03-25 20:11:52 +00:00
|
|
|
// Disable some annoying keybindings
|
|
|
|
context.codeEditor.commands.bindKeys( {
|
|
|
|
'Ctrl-T': null,
|
|
|
|
'Ctrl-L': null,
|
|
|
|
'Command-L': null
|
|
|
|
} );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2019-11-15 16:40:39 +00:00
|
|
|
context.codeEditor.setReadOnly( $box.prop( 'readonly' ) );
|
2013-12-30 15:57:47 +00:00
|
|
|
context.codeEditor.setShowInvisibles( context.showInvisibleChars );
|
2014-03-23 20:13:50 +00:00
|
|
|
|
2014-01-10 14:06:22 +00:00
|
|
|
// The options to enable
|
|
|
|
context.codeEditor.setOptions( {
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
enableSnippets: true
|
|
|
|
} );
|
|
|
|
|
2013-12-30 15:57:47 +00:00
|
|
|
context.codeEditor.commands.addCommand( {
|
|
|
|
name: 'gotolinecolumn',
|
|
|
|
bindKey: { mac: 'Command-Shift-L', windows: 'Ctrl-Alt-L' },
|
2015-11-30 09:09:53 +00:00
|
|
|
exec: context.fn.aceGotoLineColumn,
|
|
|
|
readOnly: true
|
2013-12-30 15:57:47 +00:00
|
|
|
} );
|
|
|
|
|
2019-11-15 16:40:39 +00:00
|
|
|
$box.closest( 'form' )
|
|
|
|
.on( 'submit', context.evt.codeEditorSubmit )
|
|
|
|
.find( '#wpSave' ).on( 'click', context.evt.codeEditorSave );
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var session = context.codeEditor.getSession();
|
2013-02-15 03:01:03 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
// Use proper tabs
|
|
|
|
session.setUseSoftTabs( false );
|
2013-12-30 15:57:47 +00:00
|
|
|
session.setUseWrapMode( context.lineWrappingActive );
|
2013-08-25 14:43:55 +00:00
|
|
|
|
2022-05-30 20:38:35 +00:00
|
|
|
// Configure any workers
|
|
|
|
session.on( 'changeMode', function ( e, session2 ) {
|
|
|
|
// eslint-disable-next-line no-jquery/variable-pattern
|
|
|
|
var mode = session2.getMode().$id;
|
|
|
|
if ( mode === 'ace/mode/javascript' ) {
|
|
|
|
session2.$worker.send( 'setOptions', [ {
|
|
|
|
maxerr: 1000
|
|
|
|
} ] );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2016-01-29 12:32:14 +00:00
|
|
|
mw.hook( 'codeEditor.configure' ).fire( session );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2016-10-20 10:06:28 +00:00
|
|
|
ace.config.loadModule( 'ace/ext/modelist', function ( modelist ) {
|
|
|
|
if ( !modelist || !modelist.modesByName[ lang ] ) {
|
|
|
|
lang = 'text';
|
|
|
|
}
|
|
|
|
session.setMode( 'ace/mode/' + lang );
|
2014-10-10 11:58:58 +00:00
|
|
|
} );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2019-11-26 20:06:12 +00:00
|
|
|
// Use jQuery UI resizable() so that users can make the box taller
|
2013-12-31 12:03:11 +00:00
|
|
|
container.resizable( {
|
|
|
|
handles: 's',
|
2019-11-15 16:40:39 +00:00
|
|
|
minHeight: $box.height(),
|
2013-12-31 12:03:11 +00:00
|
|
|
resize: function () {
|
|
|
|
context.codeEditor.resize();
|
|
|
|
}
|
|
|
|
} );
|
2014-03-16 22:43:37 +00:00
|
|
|
$( '.wikiEditor-ui-toolbar' ).addClass( 'codeEditor-ui-toolbar' );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2015-08-27 19:22:09 +00:00
|
|
|
if ( selectedLine > 0 ) {
|
|
|
|
// Line numbers in CodeEditor are zero-based
|
|
|
|
context.codeEditor.navigateTo( selectedLine - 1, 0 );
|
|
|
|
// Scroll up a bit to give some context
|
|
|
|
context.codeEditor.scrollToRow( selectedLine - 4 );
|
|
|
|
}
|
|
|
|
|
2014-03-31 22:47:48 +00:00
|
|
|
context.fn.setupStatusBar();
|
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
// Let modules know we're ready to start working with the content
|
|
|
|
context.fn.trigger( 'ready' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-01-29 08:39:43 +00:00
|
|
|
* Turn off the code editor view and return to the plain textarea.
|
2013-12-31 12:03:11 +00:00
|
|
|
* May be needed by some folks with funky browsers, or just to compare.
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
disableCodeEditor: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
// Kills it!
|
2015-03-29 14:20:40 +00:00
|
|
|
context.$textarea.closest( 'form' )
|
|
|
|
.off( 'submit', context.evt.codeEditorSubmit )
|
|
|
|
.find( '#wpSave' ).off( 'click', context.evt.codeEditorSave );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
// Save contents
|
2014-07-28 20:50:19 +00:00
|
|
|
context.$textarea.textSelection( 'unregister' );
|
|
|
|
context.$textarea.val( textSelectionFn.getContents() );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
// @todo fetch cursor, scroll position
|
|
|
|
|
|
|
|
// Drop the fancy editor widget...
|
2014-03-31 22:47:48 +00:00
|
|
|
context.fn.removeStatusBar();
|
2013-12-31 12:03:11 +00:00
|
|
|
context.$codeEditorContainer.remove();
|
|
|
|
context.$codeEditorContainer = undefined;
|
|
|
|
context.codeEditor = undefined;
|
|
|
|
|
|
|
|
// Restore textarea
|
|
|
|
context.$textarea.show();
|
2014-03-16 22:43:37 +00:00
|
|
|
// Restore toolbar
|
|
|
|
$( '.wikiEditor-ui-toolbar' ).removeClass( 'codeEditor-ui-toolbar' );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
// @todo restore cursor, scroll position
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start monitoring the fragment of the current window for hash change
|
|
|
|
* events. If the hash is already set, handle it as a new event.
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
codeEditorMonitorFragment: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
function onHashChange() {
|
2021-10-06 17:00:20 +00:00
|
|
|
var regexp = /#mw-ce-l(\d+)/;
|
|
|
|
var result = regexp.exec( window.location.hash );
|
2013-12-31 12:23:48 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
if ( result === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:09:56 +00:00
|
|
|
selectedLine = parseInt( result[ 1 ], 10 );
|
2015-08-27 19:22:09 +00:00
|
|
|
if ( context.codeEditor && selectedLine > 0 ) {
|
|
|
|
// Line numbers in CodeEditor are zero-based
|
|
|
|
context.codeEditor.navigateTo( selectedLine - 1, 0 );
|
|
|
|
// Scroll up a bit to give some context
|
|
|
|
context.codeEditor.scrollToRow( selectedLine - 4 );
|
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onHashChange();
|
2014-03-31 22:47:48 +00:00
|
|
|
$( window ).on( 'hashchange', onHashChange );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* This creates a Statusbar, that allows you to see a count of the
|
|
|
|
* errors, warnings and the warning of the current line, as well as
|
|
|
|
* the position of the cursor.
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
setupStatusBar: function () {
|
2014-03-31 22:47:48 +00:00
|
|
|
var shouldUpdateAnnotations,
|
|
|
|
shouldUpdateSelection,
|
|
|
|
shouldUpdateLineInfo,
|
|
|
|
nextAnnotation,
|
|
|
|
delayedUpdate,
|
|
|
|
editor = context.codeEditor,
|
2016-03-13 23:26:27 +00:00
|
|
|
lang = ace.require( 'ace/lib/lang' ),
|
2019-02-06 01:34:32 +00:00
|
|
|
$errors = $( '<span>' ).addClass( 'codeEditor-status-worker-cell ace_gutter-cell ace_error' ).text( '0' ),
|
|
|
|
$warnings = $( '<span>' ).addClass( 'codeEditor-status-worker-cell ace_gutter-cell ace_warning' ).text( '0' ),
|
|
|
|
$infos = $( '<span>' ).addClass( 'codeEditor-status-worker-cell ace_gutter-cell ace_info' ).text( '0' ),
|
2014-03-31 22:47:48 +00:00
|
|
|
$message = $( '<div>' ).addClass( 'codeEditor-status-message' ),
|
|
|
|
$lineAndMode = $( '<div>' ).addClass( 'codeEditor-status-line' ),
|
|
|
|
$workerStatus = $( '<div>' )
|
|
|
|
.addClass( 'codeEditor-status-worker' )
|
2015-05-25 09:07:41 +00:00
|
|
|
.attr( 'title', mw.msg( 'codeeditor-next-annotation' ) )
|
2014-03-31 22:47:48 +00:00
|
|
|
.append( $errors )
|
|
|
|
.append( $warnings )
|
|
|
|
.append( $infos );
|
|
|
|
|
|
|
|
context.$statusBar = $( '<div>' )
|
|
|
|
.addClass( 'codeEditor-status' )
|
|
|
|
.append( $workerStatus )
|
|
|
|
.append( $message )
|
|
|
|
.append( $lineAndMode );
|
|
|
|
|
|
|
|
/* Help function to concatenate strings with different separators */
|
|
|
|
function addToStatus( status, str, separator ) {
|
|
|
|
if ( str ) {
|
|
|
|
status.push( str, separator || '|' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all the information in the status bar
|
|
|
|
*/
|
|
|
|
function updateStatusBar() {
|
2021-10-06 17:00:20 +00:00
|
|
|
var errors = 0,
|
2014-03-31 22:47:48 +00:00
|
|
|
warnings = 0,
|
|
|
|
infos = 0,
|
|
|
|
distance,
|
|
|
|
shortestDistance = Infinity,
|
|
|
|
closestAnnotation,
|
|
|
|
currentLine = editor.selection.lead.row,
|
2015-11-30 12:49:30 +00:00
|
|
|
annotations = editor.getSession().getAnnotations(),
|
2015-06-24 03:39:06 +00:00
|
|
|
closestType;
|
2014-03-31 22:47:48 +00:00
|
|
|
|
|
|
|
// Reset the next annotation
|
|
|
|
nextAnnotation = null;
|
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
for ( var i = 0; i < annotations.length; i++ ) {
|
|
|
|
var annotation = annotations[ i ];
|
2014-03-31 22:47:48 +00:00
|
|
|
distance = Math.abs( currentLine - annotation.row );
|
|
|
|
|
|
|
|
if ( distance < shortestDistance ) {
|
|
|
|
shortestDistance = distance;
|
|
|
|
closestAnnotation = annotation;
|
|
|
|
}
|
|
|
|
if ( nextAnnotation === null && annotation.row > currentLine ) {
|
|
|
|
nextAnnotation = annotation;
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:09:56 +00:00
|
|
|
switch ( annotations[ i ].type ) {
|
2014-03-31 22:47:48 +00:00
|
|
|
case 'error':
|
|
|
|
errors++;
|
|
|
|
break;
|
|
|
|
case 'warning':
|
|
|
|
warnings++;
|
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
infos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wrap around to the beginning for nextAnnotation
|
|
|
|
if ( nextAnnotation === null && annotations.length > 0 ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
nextAnnotation = annotations[ 0 ];
|
2014-03-31 22:47:48 +00:00
|
|
|
}
|
|
|
|
// Update the annotation counts
|
|
|
|
if ( shouldUpdateAnnotations ) {
|
|
|
|
$errors.text( errors );
|
|
|
|
$warnings.text( warnings );
|
|
|
|
$infos.text( infos );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the message of the current line, if we have not already done so
|
|
|
|
if ( closestAnnotation &&
|
|
|
|
currentLine === closestAnnotation.row &&
|
2014-10-10 11:58:58 +00:00
|
|
|
closestAnnotation !== $message.data( 'annotation' ) ) {
|
2014-03-31 22:47:48 +00:00
|
|
|
$message.data( 'annotation', closestAnnotation );
|
2015-06-24 03:39:06 +00:00
|
|
|
closestType =
|
|
|
|
closestAnnotation.type.charAt( 0 ).toUpperCase() +
|
|
|
|
closestAnnotation.type.slice( 1 );
|
|
|
|
|
|
|
|
$message.text( closestType + ': ' + closestAnnotation.text );
|
2014-05-06 23:27:44 +00:00
|
|
|
} else if ( $message.data( 'annotation' ) !== null &&
|
|
|
|
( !closestAnnotation || currentLine !== closestAnnotation.row ) ) {
|
2014-03-31 22:47:48 +00:00
|
|
|
// If we are on a different line without an annotation, then blank the message
|
|
|
|
$message.data( 'annotation', null );
|
|
|
|
$message.text( '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// The cursor position has changed
|
|
|
|
if ( shouldUpdateSelection || shouldUpdateLineInfo ) {
|
|
|
|
// Adapted from Ajax.org's ace/ext/statusbar module
|
2021-10-06 17:00:20 +00:00
|
|
|
var status = [];
|
2014-03-31 22:47:48 +00:00
|
|
|
|
|
|
|
if ( editor.$vimModeHandler ) {
|
|
|
|
addToStatus( status, editor.$vimModeHandler.getStatusText() );
|
|
|
|
} else if ( editor.commands.recording ) {
|
|
|
|
addToStatus( status, 'REC' );
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var c = editor.selection.lead;
|
2014-10-10 11:58:58 +00:00
|
|
|
addToStatus( status, ( c.row + 1 ) + ':' + c.column, '' );
|
2014-03-31 22:47:48 +00:00
|
|
|
if ( !editor.selection.isEmpty() ) {
|
2021-10-06 17:00:20 +00:00
|
|
|
var r = editor.getSelectionRange();
|
2015-01-29 08:39:43 +00:00
|
|
|
addToStatus( status, '(' + ( r.end.row - r.start.row ) + ':' + ( r.end.column - r.start.column ) + ')' );
|
2014-03-31 22:47:48 +00:00
|
|
|
}
|
|
|
|
status.pop();
|
2014-10-10 11:58:58 +00:00
|
|
|
$lineAndMode.text( status.join( '' ) );
|
2014-03-31 22:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldUpdateLineInfo = shouldUpdateSelection = shouldUpdateAnnotations = false;
|
|
|
|
}
|
|
|
|
|
2014-10-10 11:58:58 +00:00
|
|
|
// Function to delay/debounce updates for the StatusBar
|
|
|
|
delayedUpdate = lang.delayedCall( function () {
|
|
|
|
updateStatusBar( editor );
|
2017-05-04 16:10:18 +00:00
|
|
|
} );
|
2014-10-10 11:58:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler that allows you to skip to the next annotation
|
|
|
|
*/
|
|
|
|
$workerStatus.on( 'click', function ( e ) {
|
|
|
|
if ( nextAnnotation ) {
|
|
|
|
context.codeEditor.navigateTo( nextAnnotation.row, nextAnnotation.column );
|
|
|
|
// Scroll up a bit to give some context
|
|
|
|
context.codeEditor.scrollToRow( nextAnnotation.row - 3 );
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.getSession().on( 'changeAnnotation', function () {
|
2014-03-31 22:47:48 +00:00
|
|
|
shouldUpdateAnnotations = true;
|
|
|
|
delayedUpdate.schedule( 100 );
|
|
|
|
} );
|
2014-10-10 11:58:58 +00:00
|
|
|
editor.on( 'changeStatus', function () {
|
2014-03-31 22:47:48 +00:00
|
|
|
shouldUpdateLineInfo = true;
|
|
|
|
delayedUpdate.schedule( 100 );
|
|
|
|
} );
|
2014-10-10 11:58:58 +00:00
|
|
|
editor.on( 'changeSelection', function () {
|
2014-03-31 22:47:48 +00:00
|
|
|
shouldUpdateSelection = true;
|
|
|
|
delayedUpdate.schedule( 100 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Force update
|
|
|
|
shouldUpdateLineInfo = shouldUpdateSelection = shouldUpdateAnnotations = true;
|
|
|
|
updateStatusBar( editor );
|
|
|
|
|
|
|
|
context.$statusBar.insertAfter( $( '.wikiEditor-ui-view-wikitext .wikiEditor-ui-bottom' ) );
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
removeStatusBar: function () {
|
2014-03-31 22:47:48 +00:00
|
|
|
context.codeEditor.getSession().removeListener( 'changeAnnotation' );
|
|
|
|
context.codeEditor.removeListener( 'changeSelection' );
|
|
|
|
context.codeEditor.removeListener( 'changeStatus' );
|
|
|
|
context.nextAnnotation = null;
|
|
|
|
context.$statusBar = null;
|
|
|
|
|
|
|
|
$( '.codeEditor-status' ).remove();
|
2013-05-07 15:08:27 +00:00
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the base functions in a way that lets
|
|
|
|
* us fall back to the originals when we turn off.
|
2016-12-09 13:21:02 +00:00
|
|
|
*
|
|
|
|
* @param {Object} base
|
|
|
|
* @param {Object} extended
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2013-12-31 12:23:48 +00:00
|
|
|
saveAndExtend = function ( base, extended ) {
|
2019-02-06 01:34:32 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-map-util
|
2015-11-29 20:16:03 +00:00
|
|
|
$.map( extended, function ( func, name ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
if ( name in base ) {
|
2021-10-06 17:00:20 +00:00
|
|
|
var orig = base[ name ];
|
2015-10-25 12:09:56 +00:00
|
|
|
base[ name ] = function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
if ( context.codeEditorActive ) {
|
|
|
|
return func.apply( this, arguments );
|
2014-10-10 11:58:58 +00:00
|
|
|
}
|
|
|
|
if ( orig ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
return orig.apply( this, arguments );
|
|
|
|
}
|
2014-10-10 11:58:58 +00:00
|
|
|
throw new Error( 'CodeEditor: no original function to call for ' + name );
|
2013-12-31 12:03:11 +00:00
|
|
|
};
|
|
|
|
} else {
|
2015-10-25 12:09:56 +00:00
|
|
|
base[ name ] = func;
|
2011-06-10 01:11:18 +00:00
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
} );
|
|
|
|
};
|
2011-06-10 01:11:18 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
saveAndExtend( context.fn, {
|
2015-06-27 20:22:54 +00:00
|
|
|
saveSelection: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
mw.log( 'codeEditor stub function saveSelection called' );
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
restoreSelection: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
mw.log( 'codeEditor stub function restoreSelection called' );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-07-28 20:50:19 +00:00
|
|
|
* Scroll an element to the top of the iframe
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
scrollToTop: function () {
|
2014-07-28 20:50:19 +00:00
|
|
|
mw.log( 'codeEditor stub function scrollToTop called' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compatibility with the $.textSelection jQuery plug-in. When the iframe is in use, these functions provide
|
|
|
|
* equivalant functionality to the otherwise textarea-based functionality.
|
|
|
|
*/
|
|
|
|
textSelectionFn = {
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2014-07-28 20:50:19 +00:00
|
|
|
/* Needed for search/replace */
|
2015-06-27 20:22:54 +00:00
|
|
|
getContents: function () {
|
2014-07-28 20:50:19 +00:00
|
|
|
return context.codeEditor.getSession().getValue();
|
2013-12-31 12:03:11 +00:00
|
|
|
},
|
|
|
|
|
2015-06-27 20:22:54 +00:00
|
|
|
setContents: function ( newContents ) {
|
2014-11-29 19:10:18 +00:00
|
|
|
context.codeEditor.getSession().setValue( newContents );
|
|
|
|
},
|
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Gets the currently selected text in the content
|
|
|
|
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
|
2016-12-09 13:21:02 +00:00
|
|
|
*
|
|
|
|
* @return {string}
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
getSelection: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
return context.codeEditor.getCopyText();
|
|
|
|
},
|
2015-10-25 12:09:56 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Inserts text at the begining and end of a text selection, optionally inserting text at the caret when
|
|
|
|
* selection is empty.
|
|
|
|
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
|
2016-12-09 13:21:02 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {jQuery}
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
encapsulateSelection: function ( options ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
// Does not yet handle 'ownline', 'splitlines' option
|
2021-10-06 17:00:20 +00:00
|
|
|
var sel = context.codeEditor.getSelection();
|
|
|
|
var range = sel.getRange();
|
|
|
|
var selText = textSelectionFn.getSelection();
|
|
|
|
var isSample = false;
|
2013-12-31 12:23:48 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
if ( !selText ) {
|
|
|
|
selText = options.peri;
|
|
|
|
isSample = true;
|
|
|
|
} else if ( options.replace ) {
|
|
|
|
selText = options.peri;
|
|
|
|
}
|
2013-12-31 12:23:48 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var text = options.pre;
|
2013-12-31 12:03:11 +00:00
|
|
|
text += selText;
|
|
|
|
text += options.post;
|
|
|
|
context.codeEditor.insert( text );
|
|
|
|
if ( isSample && options.selectPeri && !options.splitlines ) {
|
|
|
|
// May esplode if anything has newlines, be warned. :)
|
|
|
|
range.setStart( range.start.row, range.start.column + options.pre.length );
|
|
|
|
range.setEnd( range.start.row, range.start.column + selText.length );
|
|
|
|
sel.setSelectionRange( range );
|
|
|
|
}
|
|
|
|
return context.$textarea;
|
|
|
|
},
|
2015-10-25 12:09:56 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Gets the position (in resolution of bytes not nessecarily characters) in a textarea
|
|
|
|
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
getCaretPosition: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
mw.log( 'codeEditor stub function getCaretPosition called' );
|
|
|
|
},
|
2015-10-25 12:09:56 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Sets the selection of the content
|
|
|
|
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
|
|
|
|
*
|
2015-10-25 12:09:56 +00:00
|
|
|
* @param {Object} options
|
2016-12-09 13:21:02 +00:00
|
|
|
* @return {jQuery}
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
setSelection: function ( options ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
// Ace stores positions for ranges as row/column pairs.
|
|
|
|
// To convert from character offsets, we'll need to iterate through the document
|
2021-10-06 17:00:20 +00:00
|
|
|
var doc = context.codeEditor.getSession().getDocument();
|
|
|
|
var lines = doc.getAllLines();
|
2013-12-31 12:23:48 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var offsetToPos = function ( offset ) {
|
2013-12-31 12:23:48 +00:00
|
|
|
var row, col, pos;
|
|
|
|
|
|
|
|
row = 0;
|
|
|
|
col = 0;
|
|
|
|
pos = 0;
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2015-10-25 12:09:56 +00:00
|
|
|
while ( row < lines.length && pos + lines[ row ].length < offset ) {
|
|
|
|
pos += lines[ row ].length;
|
2013-12-31 12:03:11 +00:00
|
|
|
pos++; // for the newline
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
col = offset - pos;
|
2015-01-29 08:39:43 +00:00
|
|
|
return { row: row, column: col };
|
2013-12-31 12:03:11 +00:00
|
|
|
};
|
2021-10-06 17:00:20 +00:00
|
|
|
var start = offsetToPos( options.start );
|
|
|
|
var end = offsetToPos( options.end );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2021-10-06 17:00:20 +00:00
|
|
|
var sel = context.codeEditor.getSelection();
|
|
|
|
var range = sel.getRange();
|
2013-12-31 12:03:11 +00:00
|
|
|
range.setStart( start.row, start.column );
|
|
|
|
range.setEnd( end.row, end.column );
|
|
|
|
sel.setSelectionRange( range );
|
|
|
|
return context.$textarea;
|
|
|
|
},
|
2014-07-28 20:50:19 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/**
|
|
|
|
* Scroll a textarea to the current cursor position. You can set the cursor position with setSelection()
|
|
|
|
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
|
2016-12-09 13:21:02 +00:00
|
|
|
*
|
|
|
|
* @return {jQuery}
|
2013-12-31 12:03:11 +00:00
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
scrollToCaretPosition: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
mw.log( 'codeEditor stub function scrollToCaretPosition called' );
|
|
|
|
return context.$textarea;
|
2012-04-23 04:09:47 +00:00
|
|
|
}
|
2014-07-28 20:50:19 +00:00
|
|
|
};
|
2012-04-23 04:09:47 +00:00
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
/* Setup the editor */
|
|
|
|
context.fn.setupCodeEditorToolbar();
|
|
|
|
if ( context.codeEditorActive ) {
|
|
|
|
context.fn.setupCodeEditor();
|
2011-06-16 23:24:32 +00:00
|
|
|
}
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2011-06-16 23:24:32 +00:00
|
|
|
};
|
2018-11-12 13:16:11 +00:00
|
|
|
}() );
|