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 */
|
|
|
|
( function ( $, mw, OO ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
$.wikiEditor.modules.codeEditor = {
|
|
|
|
/**
|
|
|
|
* Core Requirements
|
|
|
|
*/
|
2015-06-27 20:22:54 +00:00
|
|
|
req: [ 'codeEditor' ],
|
2014-04-29 12:08:23 +00:00
|
|
|
/**
|
2015-01-29 08:39:43 +00:00
|
|
|
* Compatability map
|
2014-04-29 12:08:23 +00:00
|
|
|
*/
|
|
|
|
browsers: {
|
2015-10-25 12:09:56 +00:00
|
|
|
msie: [ [ '>=', 8 ] ],
|
|
|
|
ipod: [ [ '>=', 6 ] ],
|
|
|
|
iphone: [ [ '>=', 6 ] ],
|
|
|
|
android: [ [ '>=', 4 ] ]
|
2014-04-29 12:08:23 +00:00
|
|
|
},
|
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
|
|
|
cookieEnabled,
|
|
|
|
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
|
|
|
|
cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-showInvisibleChars' ), 10 );
|
|
|
|
context.showInvisibleChars = ( cookieEnabled === 1 );
|
|
|
|
cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-lineWrappingActive' ), 10 );
|
|
|
|
context.lineWrappingActive = ( cookieEnabled === 1 );
|
|
|
|
context.showSearchReplace = 0;
|
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 () {
|
2014-03-28 10:47:21 +00:00
|
|
|
var i,
|
|
|
|
annotations = context.codeEditor.getSession().getAnnotations();
|
2014-10-10 11:58:58 +00:00
|
|
|
for ( i = 0; i < annotations.length; i++ ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
if ( annotations[ i ].type === 'error' ) {
|
2015-03-29 14:20:40 +00:00
|
|
|
hasErrorsOnSave = true;
|
2014-03-28 10:47:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
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
|
|
|
|
2014-04-28 13:31:57 +00:00
|
|
|
context.codeEditorActive = mw.user.options.get( 'usecodeeditor' ) !== '0';
|
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 13:49:46 +00:00
|
|
|
var lineinput = prompt( mw.msg( 'codeeditor-gotoline-prompt' ), mw.msg( 'codeeditor-gotoline-placeholder' ) ),
|
2016-02-27 22:39:39 +00:00
|
|
|
matches = lineinput ? lineinput.split( ':' ) : [],
|
2013-12-30 15:57:47 +00:00
|
|
|
line = 0,
|
|
|
|
column = 0;
|
|
|
|
|
|
|
|
if ( matches.length > 0 ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
line = parseInt( matches[ 0 ], 10 ) || 0;
|
2013-12-30 15:57:47 +00:00
|
|
|
line--;
|
|
|
|
}
|
|
|
|
if ( matches.length > 1 ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
column = parseInt( matches[ 1 ], 10 ) || 0;
|
2013-12-30 15:57:47 +00:00
|
|
|
column--;
|
|
|
|
}
|
|
|
|
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 () {
|
2013-12-30 15:57:47 +00:00
|
|
|
var toggleEditor,
|
|
|
|
toggleInvisibleChars,
|
|
|
|
toggleSearchReplace,
|
|
|
|
toggleLineWrapping,
|
|
|
|
indent, outdent, gotoLine;
|
|
|
|
|
|
|
|
toggleEditor = function ( context ) {
|
2013-12-31 12:03:11 +00:00
|
|
|
context.codeEditorActive = !context.codeEditorActive;
|
2014-04-28 13:31:57 +00:00
|
|
|
|
|
|
|
context.fn.setCodeEditorPreference( context.codeEditorActive );
|
2013-12-30 15:57:47 +00:00
|
|
|
context.fn.updateCodeEditorToolbarButton();
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
if ( context.codeEditorActive ) {
|
|
|
|
// set it back up!
|
|
|
|
context.fn.setupCodeEditor();
|
|
|
|
} else {
|
|
|
|
context.fn.disableCodeEditor();
|
|
|
|
}
|
|
|
|
};
|
2013-12-30 15:57:47 +00:00
|
|
|
toggleInvisibleChars = function ( context ) {
|
|
|
|
context.showInvisibleChars = !context.showInvisibleChars;
|
|
|
|
|
|
|
|
context.fn.changeCookieValue( 'showInvisibleChars', context.showInvisibleChars ? 1 : 0 );
|
|
|
|
context.fn.updateInvisibleCharsButton();
|
|
|
|
|
|
|
|
context.codeEditor.setShowInvisibles( context.showInvisibleChars );
|
|
|
|
};
|
|
|
|
toggleSearchReplace = function ( context ) {
|
|
|
|
context.showSearchReplace = !context.showSearchReplace;
|
|
|
|
|
|
|
|
if ( context.showSearchReplace ) {
|
|
|
|
ace.config.loadModule( 'ace/ext/searchbox', function ( e ) {
|
|
|
|
// ace.editor.searchBox.show();
|
|
|
|
e.Search( context.codeEditor, !0 );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
context.codeEditor.searchBox.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
toggleLineWrapping = function ( context ) {
|
|
|
|
context.lineWrappingActive = !context.lineWrappingActive;
|
|
|
|
|
|
|
|
context.fn.changeCookieValue( 'lineWrappingActive', context.lineWrappingActive ? 1 : 0 );
|
|
|
|
context.fn.updateLineWrappingButton();
|
|
|
|
|
|
|
|
context.codeEditor.getSession().setUseWrapMode( context.lineWrappingActive );
|
|
|
|
};
|
|
|
|
indent = function ( context ) {
|
|
|
|
context.codeEditor.execCommand( 'indent' );
|
|
|
|
};
|
|
|
|
outdent = function ( context ) {
|
|
|
|
context.codeEditor.execCommand( 'outdent' );
|
|
|
|
};
|
|
|
|
gotoLine = function ( context ) {
|
|
|
|
context.codeEditor.execCommand( 'gotolinecolumn' );
|
|
|
|
};
|
|
|
|
|
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',
|
|
|
|
type: 'button',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
|
|
|
type: 'button',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
|
|
|
type: 'button',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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',
|
|
|
|
type: 'button',
|
2016-02-27 22:39:07 +00:00
|
|
|
offset: [ 0, 0 ],
|
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 + ']',
|
|
|
|
$icon = context.modules.toolbar.$toolbar.find( target );
|
2016-02-27 22:39:07 +00:00
|
|
|
$icon.toggleClass( 'icon-active', 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 )
|
|
|
|
.fail( function ( code, result ) {
|
2016-01-27 11:07:38 +00:00
|
|
|
var message;
|
|
|
|
|
|
|
|
if ( code === 'http' && result.textStatus === 'abort' ) {
|
|
|
|
// Request was aborted. Ignore error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = 'Failed to set code editor preference: ' + code;
|
2015-11-28 22:51:20 +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 () {
|
2016-10-20 10:06:28 +00:00
|
|
|
var box, lang, basePath, container, editdiv, session;
|
2013-12-31 12:23:48 +00:00
|
|
|
|
|
|
|
box = context.$textarea;
|
2014-03-16 23:28:35 +00:00
|
|
|
lang = mw.config.get( 'wgCodeEditorCurrentLanguage' );
|
2014-03-16 18:51:40 +00:00
|
|
|
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
|
2013-12-31 10:37:46 +00:00
|
|
|
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 );
|
2013-12-31 12:23:48 +00:00
|
|
|
editdiv = container.find( '.editor' );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
box.css( 'display', 'none' );
|
2015-06-17 04:23:56 +00:00
|
|
|
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 ] );
|
2014-04-30 12:26:53 +00:00
|
|
|
context.codeEditor.getSession().setValue( box.val() );
|
2014-07-28 20:50:19 +00:00
|
|
|
box.textSelection( 'register', textSelectionFn );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
|
|
|
// Disable some annoying commands
|
|
|
|
context.codeEditor.commands.removeCommand( 'replace' ); // ctrl+R
|
|
|
|
context.codeEditor.commands.removeCommand( 'transposeletters' ); // ctrl+T
|
|
|
|
context.codeEditor.commands.removeCommand( 'gotoline' ); // ctrl+L
|
|
|
|
|
2014-03-23 20:13:50 +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
|
|
|
} );
|
|
|
|
|
2015-03-29 14:20:40 +00:00
|
|
|
box.closest( 'form' )
|
|
|
|
.submit( context.evt.codeEditorSubmit )
|
|
|
|
.find( '#wpSave' ).click( context.evt.codeEditorSave );
|
2013-12-30 15:57:47 +00:00
|
|
|
|
2013-12-31 12:23:48 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
// Use jquery.ui.resizable so user can make the box taller too
|
|
|
|
container.resizable( {
|
|
|
|
handles: 's',
|
|
|
|
minHeight: box.height(),
|
|
|
|
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() {
|
2015-08-27 19:22:09 +00:00
|
|
|
var regexp, result;
|
2013-12-31 12:23:48 +00:00
|
|
|
|
|
|
|
regexp = /#mw-ce-l(\d+)/;
|
|
|
|
result = regexp.exec( window.location.hash );
|
|
|
|
|
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' ),
|
2014-03-31 22:47:48 +00:00
|
|
|
$errors = $( '<span class="codeEditor-status-worker-cell ace_gutter-cell ace_error">0</span>' ),
|
|
|
|
$warnings = $( '<span class="codeEditor-status-worker-cell ace_gutter-cell ace_warning">0</span>' ),
|
|
|
|
$infos = $( '<span class="codeEditor-status-worker-cell ace_gutter-cell ace_info">0</span>' ),
|
|
|
|
$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() {
|
2014-10-10 11:58:58 +00:00
|
|
|
var i, c, r,
|
|
|
|
status,
|
|
|
|
annotation,
|
2014-03-31 22:47:48 +00:00
|
|
|
errors = 0,
|
|
|
|
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;
|
|
|
|
|
2014-10-10 11:58:58 +00:00
|
|
|
for ( i = 0; i < annotations.length; i++ ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
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
|
2014-10-10 11:58:58 +00:00
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
2014-10-10 11:58:58 +00:00
|
|
|
c = editor.selection.lead;
|
|
|
|
addToStatus( status, ( c.row + 1 ) + ':' + c.column, '' );
|
2014-03-31 22:47:48 +00:00
|
|
|
if ( !editor.selection.isEmpty() ) {
|
2014-10-10 11:58:58 +00:00
|
|
|
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 );
|
|
|
|
}.bind( this ) );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2015-11-29 20:16:03 +00:00
|
|
|
$.map( extended, function ( func, name ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
var orig;
|
2013-12-31 12:03:11 +00:00
|
|
|
if ( name in base ) {
|
2015-10-25 12:09:56 +00:00
|
|
|
orig = base[ name ];
|
|
|
|
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
|
|
|
saveCursorAndScrollTop: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
// Stub out textarea behavior
|
|
|
|
return;
|
|
|
|
},
|
2015-06-27 20:22:54 +00:00
|
|
|
restoreCursorAndScrollTop: function () {
|
2013-12-31 12:03:11 +00:00
|
|
|
// Stub out textarea behavior
|
2012-04-23 04:09:47 +00:00
|
|
|
return;
|
2013-12-31 12:03:11 +00:00
|
|
|
},
|
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:23:48 +00:00
|
|
|
var sel, range, selText, isSample, text;
|
|
|
|
|
2013-12-31 12:03:11 +00:00
|
|
|
// Does not yet handle 'ownline', 'splitlines' option
|
2013-12-31 12:23:48 +00:00
|
|
|
sel = context.codeEditor.getSelection();
|
|
|
|
range = sel.getRange();
|
2014-07-28 20:50:19 +00:00
|
|
|
selText = textSelectionFn.getSelection();
|
2013-12-31 12:23:48 +00:00
|
|
|
isSample = false;
|
|
|
|
|
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
|
|
|
|
|
|
|
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:23:48 +00:00
|
|
|
var doc, lines, offsetToPos, start, end, sel, range;
|
|
|
|
|
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
|
2013-12-31 12:23:48 +00:00
|
|
|
doc = context.codeEditor.getSession().getDocument();
|
|
|
|
lines = doc.getAllLines();
|
|
|
|
|
|
|
|
offsetToPos = function ( offset ) {
|
|
|
|
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
|
|
|
};
|
2013-12-31 12:23:48 +00:00
|
|
|
start = offsetToPos( options.start );
|
|
|
|
end = offsetToPos( options.end );
|
2013-12-31 12:03:11 +00:00
|
|
|
|
2013-12-31 12:23:48 +00:00
|
|
|
sel = context.codeEditor.getSelection();
|
|
|
|
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
|
|
|
};
|
2016-12-09 13:50:28 +00:00
|
|
|
}( jQuery, mediaWiki, OO ) );
|