LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-inspect.js”的版本间差异
跳转到导航
跳转到搜索
小 |
小 |
||
第9行: | 第9行: | ||
const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + |
const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + |
||
'#mw-content-text p { border:1px dashed; }'), |
'#mw-content-text p { border:1px dashed; }'), |
||
cssHide = mw.util.addCSS( '.inspector { position:fixed; right: |
cssHide = mw.util.addCSS( '.inspector { position:fixed; right:24px; left:unset !important }' + |
||
'.inspector > div:last-child { margin-bottom:0.5em; }' ), |
'.inspector > div:last-child { margin-bottom:0.5em; }' ), |
||
$content = $('#mw-content-text'), |
$content = $('#mw-content-text'), |
2020年12月18日 (五) 06:10的版本
// 由[[special:mypage/vector.js]]调用,可以使用ES6语法 /** * @dependencies: mediawiki.api, mediawiki.util, oojs-ui-core, jquery.ui, ext.CodeMirror.lib, ext.CodeMirror.mode.mediawiki */ "use strict"; /*global mw, $, OO, CodeMirror*/ $.getJSON( '/zh?title=mediawiki:gadget-CodeMirror.json&action=raw&ctype=application/json', config => { let text = ''; const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + '#mw-content-text p { border:1px dashed; }'), cssHide = mw.util.addCSS( '.inspector { position:fixed; right:24px; left:unset !important }' + '.inspector > div:last-child { margin-bottom:0.5em; }' ), $content = $('#mw-content-text'), id = mw.config.get( 'wgArticleId' ), api = new mw.Api(), outer = $('<div>', {class: 'inspector'}).resizable({handles: 'w', minWidth: 350}).prependTo( $content ), editor = new CodeMirror(outer[0], {mode: 'text/mediawiki', mwConfig: config, lineWrapping: true}), wrapper = $( editor.getWrapperElement() ), btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => { console.log('API request: 查询最新编辑时间戳'); const now = mw.now(); api.get({action: 'query', prop: 'revisions', pageids: id, rvlimit: 1, rvprop: 'timestamp', formatversion: 2}) .then(res => { 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'}); }); }), 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 ); mw.hook( 'wikipage.content' ).fire($content); }, reason => { mw.notify(`无法生成预览!错误信息:${reason}`, {type: 'error'}); }); }), new OO.ui.ButtonWidget({label: '还原', flags: 'destructive'}).on('click', () => { editor.setValue( text ); }), new OO.ui.ButtonWidget({label: '隐藏'}).on('click', () => { css.disabled = !css.disabled; cssHide.disabled = !cssHide.disabled; wrapper.toggle(); }), new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/?redirect=no&curid=${id + 1}`})]; cssHide.disabled = true; $('<div>', {html: btns.map(ele => ele.$element)}).appendTo( outer ); 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'}); }); });