2023年政策修订增补工作正在进行中,欢迎参与!
User:Bhsd/js/wikieditor-highlight.js
跳转到导航
跳转到搜索
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
- Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
- Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
- Opera:在“工具→首选项”中清除缓存
- 如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码
?_=1
来访问最新页面。 - 添加代码后的本页地址如下:
-{R|https://moegirl.icu/User:Bhsd/js/wikieditor-highlight.js?_=1}-
//<nowiki>
"use strict";
/* jshint bitwise: true, curly: true, nonew: true, singleGroups: true, unused: true */
/*global mw, $, OO, CodeMirror*/
// 本页面大部分内容均直接或间接修改自[[MW:Extension:CodeMirror]]
$.when($.ready, mw.loader.using( 'oojs-ui-core' )).then(function() {
if (!['edit', 'submit'].includes( mw.config.get('wgAction') ) ||
mw.config.get('wgPageContentModel') != 'wikitext') { return; }
var cm, $doc, state = JSON.parse( localStorage.getItem( 'wikieditor-codemirror' ) );
const $textarea = $('#wpTextbox1'),
isAdvanced = ['loading', 'loaded', 'executing', 'ready'].includes( mw.loader.getState( 'ext.wikiEditor' ) ),
ns = mw.config.get( 'wgNamespaceNumber' ),
init = function() {
mw.loader.load('//fastly.jsdelivr.net/npm/[email protected]/lib/codemirror.min.css', 'text/css');
mw.loader.load('//fastly.jsdelivr.net/gh/bhsd-harry/[email protected]/mediawiki.min.css', 'text/css');
mw.loader.addStyleTag( '#wikiEditor-ui-toolbar .menu { position: relative; z-index: 5; }' +
'.CodeMirror pre { font-family: Monaco, Menlo, "Ubuntu Mono", Consolas, "source-code-pro", monospace; }' +
'#wpTextbox1 + .CodeMirror { font-size: 13px; line-height: 1.5; }'
);
const $search = $('.group-search a'),
getExt = $.get({ dataType: 'script', cache: true,
url: '//fastly.jsdelivr.net/combine/npm/[email protected]/lib/codemirror.min.js'
}).then(function() {
return Promise.all([ $.get({ dataType: 'script', cache: true,
url: '//fastly.jsdelivr.net/gh/bhsd-harry/[email protected]/mediawiki.min.js'
}) ].concat(ns == 274 ? [ $.get({ dataType: 'script', cache: true,
url: '//fastly.jsdelivr.net/npm/[email protected]/mode/javascript/javascript.min.js'
}), $.get({ dataType: 'script', cache: true,
url: '//fastly.jsdelivr.net/npm/[email protected]/mode/css/css.min.js'
}) ] : []));
}),
getJSON = $.get({ dataType: 'json', cache: true,
url: '//fastly.jsdelivr.net/gh/bhsd-harry/[email protected]/otherwiki/gadget-CodeMirror.json'
}).then(function(config) {
if (ns == 274) {
$.extend( config.tags, {script: true, style: true} );
$.extend( config.tagModes, {script: 'javascript', style: 'css'} );
}
window.mwConfig = config;
});
return Promise.all([getJSON, getExt]).then(function() {
if (isAdvanced) {
cm = new CodeMirror( $textarea.parent()[0], { mode: 'text/mediawiki', mwConfig: window.mwConfig,
lineWrapping: true, lineNumbers: true, readOnly: $textarea.prop( 'readonly' ),
} );
} else {
cm = CodeMirror.fromTextArea( $textarea[0], { mode: 'text/mediawiki', mwConfig: window.mwConfig,
lineWrapping: true, lineNumbers: true, readOnly: $textarea.prop( 'readonly' ),
} );
cm.setSize( null, $textarea.height() );
}
mw.hook( 'wiki-codemirror' ).fire( cm );
$doc = $(cm.getWrapperElement());
$.valHooks.textarea = {
get: function(ele) { return ele === $textarea[0] && state ? cm.getValue() : ele.value; },
set: function(ele, val) { ele === $textarea[0] && state ? cm.setValue( val ) : ele.value = val; }
};
if (mw.loader.getState( 'jquery.ui.resizable' ) == 'ready') { $doc.resizable( {handles: 's'} ); }
if ($search.length === 0) { return; }
cm.addKeyMap({'Ctrl-F': function() { $search.click(); }, 'Cmd-F': function() { $search.click(); }});
});
};
if (state === null || !isAdvanced) { state = true; }
if (!isAdvanced) {
init();
return;
}
const $form = $(document.editform),
btn = new OO.ui.ButtonWidget({ classes: ['tool'], icon: 'highlight', framed: false, title: '代码高亮开关'})
.on('click', function() {
if(cm) {
$doc.toggle();
update();
} else { initAndUpdate(); }
}),
fn = { getSelection: function() { return cm.getSelection(); },
setSelection: function(options) {
cm.setSelection( cm.posFromIndex( options.start ), cm.posFromIndex( options.end ) );
cm.focus();
return this;
},
getCaretPosition: function(options) {
const caretPos = cm.indexFromPos( cm.getCursor( 'from' ) ),
endPos = cm.indexFromPos( cm.getCursor( 'to' ) );
if (options.startAndEnd) { return [caretPos, endPos]; }
return caretPos;
},
scrollToCaretPosition: function() {
cm.scrollIntoView();
return this;
}
},
submit = function() { $textarea[0].value = cm.getValue(); },
shared = function() {
btn.$element.toggleClass( 'tool-active' );
if (state) {
cm.setValue( $textarea[0].value );
cm.setSize( null, $textarea.height() );
} else { $textarea[0].value = cm.getValue(); }
$textarea.toggle();
$form[ state ? 'on' : 'off' ]('submit', submit);
if ($textarea.textSelection) {
const start = mw.util.getParamValue('start'),
end = mw.util.getParamValue('end') || start;
$textarea.textSelection(state ? 'register' : 'unregister', fn);
if (start) {
$textarea.textSelection( 'setSelection', { start: start, end: end } )
.textSelection( 'scrollToCaretPosition' );
}
}
},
update = function() {
state = !state;
localStorage.setItem( 'wikieditor-codemirror', state );
shared();
},
initAndUpdate = function() { init().then( update ); },
group = $('#wikiEditor-section-main > .group-insert')[0];
$textarea.on( 'wikiEditor-toolbar-doneInitialSections', function() {
btn.$element.appendTo( '#wikiEditor-section-main > .group-insert' );
} );
if (group && !group.contains( btn.$element[0] )) {
$textarea.trigger( 'wikiEditor-toolbar-doneInitialSections' );
}
if (state) { mw.loader.using( 'ext.wikiEditor' ).then( init ).then( shared ); }
});
//</nowiki>