LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-site-lib.codemirror.js”的版本间差异
跳转到导航
跳转到搜索
小 |
|||
第65行: | 第65行: | ||
return $line[0]; |
return $line[0]; |
||
}); |
}); |
||
$target |
$target.addClass( 'wiki' ); |
||
if ($target.hasClass( 'linenums' )) { // 添加行号 |
|||
const start = $target.data( 'start' ) || 1; |
|||
$target.replaceWith( $('<ol>', { start: start, |
|||
html: content.map(function(ele, i) { return $('<li>', {html: ele, id: 'L' + (i + start)}); }) |
|||
}).css('padding-left', (content.length + start - 1).toString().length + 2.5 + 'ch') ); |
|||
} else { $target.replaceWith( content ); } |
|||
}, |
}, |
||
callback = function(entries) { entries.filter(function(obj) { return obj.isIntersecting; }).forEach( render ); }, |
callback = function(entries) { entries.filter(function(obj) { return obj.isIntersecting; }).forEach( render ); }, |
2021年9月28日 (二) 10:29的版本
//<nowiki> // 由ResourceLoader直接调用,不可使用ES6语法 /** * @Function: 1. 下载需要的CodeMirror模式 * 2. 高亮显示页面代码 * @Source: [https://codemirror.net/addon/runmode/runmode.js] * @EditedBy: [[User:Bhsd]] */ "use strict"; /*global $, CodeMirror*/ // 1. 下载需要的CodeMirror模式 const promise = {javascript: null, css: null, lua: null, wiki: null}, aliases = { js: 'javascript', javascript: 'javascript', json: 'javascript', css: 'css', lua: 'lua', scribunto: 'lua', wikitext: 'wiki', mediawiki: 'wiki' }; CodeMirror.download = function(alias) { const name = aliases[ alias.toLowerCase() ]; if (!(name in promise)) { throw new Error( '无法识别的CodeMirror模式' ); } if (!promise[ name ]) { if (name == 'wiki') { promise[ name ] = mw.config.get( 'extCodeMirrorConfig' ) ? Promise.resolve() : // 兼容CodeMirror扩展 $.get({ dataType: 'json', cache: true, url: '//cdn.jsdelivr.net/gh/bhsd-harry/LLWiki@1.6/json/gadget-CodeMirror.json' }).then(function(config) { mw.config.set( 'extCodeMirrorConfig', config ); }, function(reason) { throw reason; } ); } else { promise[ name ] = $.get({ dataType: 'script', cache: true, url: '//cdn.jsdelivr.net/npm/codemirror@5.35.0/mode/' + name + '/' + name + '.min.js' }); } } return promise[ name ]; }; // 2. 高亮显示页面代码 const render = function(obj) { observer.unobserve( obj.target ); const mode = CodeMirror.getMode({mwConfig: mw.config.get( 'extCodeMirrorConfig' )}, 'mediawiki'), $target = $(obj.target), lines = CodeMirror.splitLines( $target.text().trim() ), state = mode.startState(), content = lines.map(function(line) { const stream = new CodeMirror.StringStream( line ), $line = $('<pre>'); var token, style; if (!stream.string) { token = mode.blankLine( state ); style = token ? token.trim().split( /\s+/ ) : []; return $line.addClass( style.map(function(ele) { return ele.slice(5); }) )[0]; } while (!stream.eol()) { token = mode.token(stream, state); style = token ? token.trim().split( /\s+/ ) : []; if (stream.start === 0) { $line.addClass( style.filter(function(ele) { return ele.startsWith( 'line-' ); }) .map(function(ele) { return ele.slice(5); }) ); } $('<span>', {text: stream.current()}).addClass( style.filter(function(ele) { return !ele.startsWith( 'line-' ); }).map(function(ele) { return 'cm-' + ele; }) ).appendTo( $line ); stream.start = stream.pos; } return $line[0]; }); $target.addClass( 'wiki' ); if ($target.hasClass( 'linenums' )) { // 添加行号 const start = $target.data( 'start' ) || 1; $target.replaceWith( $('<ol>', { start: start, html: content.map(function(ele, i) { return $('<li>', {html: ele, id: 'L' + (i + start)}); }) }).css('padding-left', (content.length + start - 1).toString().length + 2.5 + 'ch') ); } else { $target.replaceWith( content ); } }, callback = function(entries) { entries.filter(function(obj) { return obj.isIntersecting; }).forEach( render ); }, observer = new IntersectionObserver(callback, {threshold: 0.01}); // jshint ignore: line CodeMirror.runmode = function(pre) { if (!CodeMirror.modes.includes( 'mediawiki' ) || !mw.config.get( 'extCodeMirrorConfig' )) { throw new Error( '未加载CodeMirror的mediawiki模式' ); } observer.observe( pre ); }; //</nowiki> // [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]] // {{DEFAULTSORT:site-lib.codemirror.js}}