“MediaWiki:Gadget-site-lib.codemirror.js”的版本间差异

// 使用Wikiplus小工具快速编辑
(// 使用Wikiplus小工具快速编辑)
 
(未显示同一用户的9个中间版本)
// 由ResourceLoader直接调用,不可使用ES6语法
/**
* @FunctionDescription: 1. 下载需要LLWiki定义的CodeMirror模式扩展函数,桌面版、手机版均可用
// * @Function: 1. 下载需要的CodeMirror模式(CodeMirror.download)
* 2. 高亮显示页内Wikitext代码(CodeMirror.runmode)
* @Source: [https://codemirror.net/addon/runmode/runmode.js]
* @EditedByAuthor: 无特殊说明时均为[[User:Bhsd]]
*/
"use strict";
/*global CodeMirror*/
 
// 1. 下载需要的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)) { throw new Error( '无法识别的CodeMirror模式' ); }
console.error( '无法识别的CodeMirror模式' );
throw null;
}
if (!promise[ name ]) {
if (name == 'wiki') {
promise[ name ] = mw.config.get( 'extCodeMirrorConfig' ) ? Promise.resolve() : // 兼容CodeMirror扩展
mw.loader.using('ext.CodeMirror.data');
$.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({['clike', dataType:'css', 'scripthtmlmixed', cache:'javascript', true'php', 'xml'].includes(name)
? mw.loader.using('ext.CodeMirror.lib.mode.' + name)
url: '//cdn.jsdelivr.net/npm/codemirror@5.35.0/mode/' + name + '/' + name + '.min.js'
: $.get({ dataType: 'jsonscript', cache: true,
});
url: '//cdn.jsdelivr.net/npm/codemirror@5.35.0/mode/' + name + '/' + name + '.min.js'
});
}
}
};
 
/**
// 2. 高亮显示页面代码
* @Function: 高亮页内Wikitext代码
const render = function(obj) {
* @Source: [https://codemirror.net/addon/runmode/runmode.js]
observer.unobserve( obj.target );
* @EditedBy: [[User:Bhsd]]
* @Param {jQuery} $pre, 写有Wikitext代码的jQuery对象
*/
const render = function(objtarget) {
const mode = CodeMirror.getMode({mwConfig: mw.config.get( 'extCodeMirrorConfig' )}, 'mediawiki'),
$target = $(obj.target),
lines = CodeMirror.splitLines( $target.text().trim() ),
state = mode.startState(),
);
}
$('<span>', { text: stream.current()}).addClass(, 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( 'wikirunmode' ).empty();
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').appendTo( $target );
} else { $target.replaceWithappend( content ); }
},;
CodeMirror.runmode = function($pre) {
callback = function(entries) { entries.filter(function(obj) { return obj.isIntersecting; }).forEach( render ); },
CodeMirror.download( 'wiki' ).then(function() { $pre.toArray().forEach( render ); });
observer = new IntersectionObserver(callback, {threshold: 0.01}); // jshint ignore: line
CodeMirror.runmode = function(pre) {
if (!CodeMirror.modes.mediawiki || !mw.config.get( 'extCodeMirrorConfig' )) {
throw new Error( '未加载CodeMirror的mediawiki模式' );
}
observer.observe( pre );
};
//</nowiki>
16,874

个编辑