16,874
个编辑
LLWiki正在建设中,欢迎加入我们!
(创建页面,内容为“// 由special:mypage/vector.js调用,可以使用ES6语法 /** * @dependencies: mediawiki.api, mediawiki.util, oojs-ui-core, ext.CodeMirror.lib, ext.CodeMirro…”) |
|||
/*global mw, $, OO, CodeMirror*/
mw.util.addCSS( '#mw-content-text > .mw-parser-output, .CodeMirror { display:inline-block; width:49%; }' +
'.CodeMirror { float:right; position:sticky; top:7px; border:1px solid; height:90vh; }'
'.oo-ui-buttonWidget { margin:0.5em; }');
$.getJSON( '/zh?title=mediawiki:gadget-CodeMirror.json&action=raw&ctype=application/json', config => {
let text = '';
const $content = $('#mw-content-text'),
api = new mw.Api(),
api.get({action: 'parse', page: mw.config.get('wgPageName'), prop: 'wikitext', formatversion: 2})▼
editor = new CodeMirror($content[0], {
.then(data => {▼
btn1 = new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => {
const text = data.parse.wikitext,▼
console.log('API request: 查询最新编辑时间戳');
▲ {value: text, mode: 'text/mediawiki', mwConfig: config, lineWrapping: true});
▲ api.get({action: '
console.log(`End API request: 已获得最新编辑时间戳,用时 ${mw.now() - now} ms`);
if (new Date(res.query.pages[0].revisions[0].timestamp) > new Date(mw.request.curtimestamp)) {
mw.notify('编辑冲突!', {type: 'error'});
return;
}
api.postWithEditToken({action: 'edit', pageid: id, text: editor.getValue()}).then(() => {
window.location.reload();
}, reason => { mw.notify(`编辑失败!错误信息:${reason}`, {type: 'error'}); });
}, reason => { mw.notify(`无法获得最新编辑时间戳!错误信息:${reason}`, {type: 'error'}); });
}),
btn2 = new OO.ui.ButtonWidget({label: '预览'}).on('click', () => {
console.log('API request: 请求预览');
const now = mw.now();
api.parse( editor.getValue(), { disablelimitreport: 1, disableeditsection: 1} ).then(html => {
console.log(`End API request: 已生成预览,用时 ${mw.now() - now} ms`);
$content.children( '.mw-parser-output' ).replaceWith( html );
}, reason => { mw.notify(`无法生成预览!错误信息:${reason}`, {type: 'error'}); });
}),
btn3 = new OO.ui.ButtonWidget({label: '还原', flags: ['destructive']}).on('click', () => {
editor.setValue( text );
});
$('<div>', {html: [btn1.$element, btn2.$element, btn3.$element]}).appendTo( $content ).css('float', 'right');
mw.request = mw.request || api.get({action: 'query', revids: mw.config.get('wgRevisionId'), prop: 'revisions',
rvprop: 'content', curtimestamp: 1, formatversion: 2});
mw.request.then(data => {
console.log('End API request: 已获得页面Wikitext');
text = data.query.pages[0].revisions[0].content;
editor.setValue( text );
}, reason => { mw.notify(`无法获得页面Wikitext!错误原因${reason}`, {type: 'error'}); });
});
|