LLWiki正在建设中,欢迎加入我们!
MediaWiki:Gadget-site-lib.codemirror.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
小无编辑摘要 |
小无编辑摘要 |
||
| 第46行: | 第46行: | ||
* @Source: [https://codemirror.net/addon/runmode/runmode.js] |
* @Source: [https://codemirror.net/addon/runmode/runmode.js] |
||
* @EditedBy: [[User:Bhsd]] |
* @EditedBy: [[User:Bhsd]] |
||
* @Param { |
* @Param {jQuery} $pre, 写有Wikitext代码的jQuery对象 |
||
* @Param {Boolean} force, 是否不执行默认的懒加载 |
|||
*/ |
*/ |
||
const render = function( |
const render = function(target) { |
||
observer.unobserve( obj.target ); |
|||
const mode = CodeMirror.getMode({mwConfig: mw.config.get( 'extCodeMirrorConfig' )}, 'mediawiki'), |
const mode = CodeMirror.getMode({mwConfig: mw.config.get( 'extCodeMirrorConfig' )}, 'mediawiki'), |
||
$target = $( |
$target = $(target), |
||
lines = CodeMirror.splitLines( $target.text().trim() ), |
lines = CodeMirror.splitLines( $target.text().trim() ), |
||
state = mode.startState(), |
state = mode.startState(), |
||
| 第86行: | 第84行: | ||
}).css('padding-left', (content.length + start - 1).toString().length + 2.5 + 'ch').appendTo( $target ); |
}).css('padding-left', (content.length + start - 1).toString().length + 2.5 + 'ch').appendTo( $target ); |
||
} else { $target.append( content ); } |
} 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) { |
|||
| ⚫ | |||
if (force) { render( {target: pre} ); } |
|||
else { observer.observe( pre ); } |
|||
}); |
|||
}; |
}; |
||
| ⚫ | |||
//</nowiki> |
//</nowiki> |
||
// [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]] |
// [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]] |
||
2021年9月28日 (二) 14:23的版本
//<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/[email protected]/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/[email protected]/mode/' + name + '/' + name + '.min.js'
});
}
}
return promise[ name ];
};
/**
* @Function: 高亮页内Wikitext代码
* @Source: [https://codemirror.net/addon/runmode/runmode.js]
* @EditedBy: [[User:Bhsd]]
* @Param {jQuery} $pre, 写有Wikitext代码的jQuery对象
*/
const render = function(target) {
const mode = CodeMirror.getMode({mwConfig: mw.config.get( 'extCodeMirrorConfig' )}, 'mediawiki'),
$target = $(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 ); }
};
CodeMirror.runmode = function($pre) { CodeMirror.download( 'wiki' ).then(function() { $pre.each( render ); }); };
//</nowiki>
// [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]]
// {{DEFAULTSORT:site-lib.codemirror.js}}