LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-site-lib.codemirror.js”的版本间差异
跳转到导航
跳转到搜索
小 |
小 |
||
第72行: | 第72行: | ||
); |
); |
||
} |
} |
||
$('<span>', {text: stream.current() |
$('<span>', { text: stream.current(), class: style.filter(function(ele) { |
||
return !ele.startsWith( 'line-' ); |
return !ele.startsWith( 'line-' ); |
||
}).map(function(ele) { return 'cm-' + ele; }) ).appendTo( $line ); |
}).map(function(ele) { return 'cm-' + ele; }).join( ' ' ) }).appendTo( $line ); |
||
stream.start = stream.pos; |
stream.start = stream.pos; |
||
} |
} |
2021年9月28日 (二) 12:57的版本
//<nowiki> // 由ResourceLoader直接调用,不可使用ES6语法 /** * @Description: LLWiki定义的CodeMirror扩展函数,桌面版、手机版均可用 * @Function: 1. 下载需要的CodeMirror模式(CodeMirror.download) * 2. 高亮显示页内Wikitext代码(CodeMirror.runmode) * @Author: 无特殊说明时均为[[User:Bhsd]] */ "use strict"; /*global CodeMirror*/ /** * @Function: 根据内容模型下载需要的CodeMirror模式 * @Param {String} alias, 内容模型名称或别名 * @Return {Promise} 表示下载是否成功的Promise对象 */ 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', wiki: 'wiki' }; CodeMirror.download = function(alias) { const name = aliases[ alias.toLowerCase() ]; if (!(name in promise)) { console.error( '无法识别的CodeMirror模式' ); throw null; } 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 ]; }; /** * @Function: 高亮页内Wikitext代码 * @Source: [https://codemirror.net/addon/runmode/runmode.js] * @EditedBy: [[User:Bhsd]] * @Param {HTMLElement} pre, 含有Wikitext代码的HTML元素,通常为<pre>或<code> * @Param {Boolean} force, 是否不执行默认的懒加载 */ 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(), class: style.filter(function(ele) { return !ele.startsWith( 'line-' ); }).map(function(ele) { return 'cm-' + ele; }).join( ' ' ) }).appendTo( $line ); stream.start = stream.pos; } return $line[0]; }); $target.addClass( 'runmode' ).empty(); if ($target.hasClass( 'linenums' )) { // 添加行号 const start = $target.data( 'start' ) || 1; $('<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').appendTo( $target ); } else { $target.append( 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, force) { CodeMirror.download( 'wiki' ).then(function() { if (force) { render( {target: pre} ); } else { observer.observe( pre ); } }); }; //</nowiki> // [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]] // {{DEFAULTSORT:site-lib.codemirror.js}}