LLWiki正在建设中,欢迎加入我们

“MediaWiki:Gadget-inspect.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
 
(未显示2个用户的39个中间版本)
第4行: 第4行:
* @Function: 在页面内快速编辑和预览
* @Function: 在页面内快速编辑和预览
* @Author: [[User:Bhsd]]
* @Author: [[User:Bhsd]]
* @Translation: [[User:Lakejason0]]
*/
*/
"use strict";
"use strict";
/*global mw, $, OO, CodeMirror, wgULS*/
/*global OO, CodeMirror, wgULS*/
$(() => { // 避免全局变量
$(() => { // 避免全局变量
const id = mw.config.get( 'wgArticleId' ),
let nextid, list = mw.storage.getObject( 'inspect-category' ), flag;
curRevid = mw.config.get( 'wgCurRevisionId' ),
const id = mw.config.get( 'wgArticleId' ),
page = mw.config.get( 'wgPageName' ), // 这里未转义,后面应用时需要注意
page = mw.config.get( 'wgPageName' ), // 这里未转义,后面应用时需要注意
isCode = mw.config.get( 'wgPageContentModel' ) != 'wikitext', // javascript/css/Scribunto/json
isCode = mw.config.get( 'wgPageContentModel' ) != 'wikitext', // javascript/css/Scribunto/json
gadgets = mw.gadgets || {},
gadgets = mw.gadgets || {},
charInsert = gadgets.charinsert || {},
charInsert = gadgets.charinsert || {},
src = charInsert.src || 'special:我的用户页/edittools',
src = charInsert.src || 'special:mypage/edittools',
// 由于resizable,left, top, height必须加!important,width不可加!important
// 由于resizable,left, top, height必须加!important,width不可加!important
cssHide = mw.util.addCSS( `#inspector { position:fixed; bottom:0; right:24px; left:unset !important;
cssHide = mw.util.addCSS( `#inspector { position:fixed; bottom:0; right:24px; left:auto !important;
top:unset !important; height:unset !important; width:calc(50% - 7rem - 0.5px); }` ),
top:auto !important; height:auto !important; width:calc(50% - 7rem - 0.5px); }
div.start-screen { bottom:40px; }` ),
$content = $('#mw-content-text, #mw-imagepage-content').last(),
$content = $('#mw-content-text, #mw-imagepage-content').last(),
// wikitext/Scribunto对应.mw-parser-output,javascript/css对应.mw-code,json对应.mw-json
// wikitext/Scribunto对应.mw-parser-output,javascript/css对应.mw-code,json对应.mw-json
$original = $content.children('.mw-json, .mw-code, .mw-parser-output'),
$original = $content.children('.mw-json, .mw-code, .mw-parser-output'),
$outer = $('<div>', {id: 'inspector', class: 'mw-ajax-loader'}).insertBefore( $original ),
$outer = $('<div>', {id: 'inspector', class: 'mw-ajax-loader'}).insertBefore( $original ),
api = new mw.Api(),
api = new mw.Api();
initList = (pages, ctn) => {
if (!list) { nextid = id + 1; }
flag = pages[0] == id || (ctn && pages.includes(id));
else {
list = [pages, flag ? pages.indexOf(id) + 1 : 0];
flag = list[0][ list[1] ] == id;
if (ctn && !flag) { mw.notify(wgULS('当前页面并不属于该分类或名字空间!', '當前頁面並不屬於該分類或名字空間!'),
if (flag) { list[1]++; }
{type: 'warn'}); }
nextid = list[0][ list[1] ];
mw.storage.setObject( 'inspect-category', [pages, list[1] - flag] );
}
btnGo.setHref( `/zh?redirect=no&curid=${pages[ list[1] ]}` ).setDisabled( false );
const btnGo = new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/zh?redirect=no&curid=${nextid}`,
};
disabled: !nextid}),
let nextid, list = mw.storage.getObject( 'inspect-category' ), flag;
initList = (pages, ctn) => {
if (!list) { nextid = id + 1; }
console.info({pages: pages, ctn: ctn, id: id});
else {
flag = pages[0] == id || ctn && pages.includes(id); // &&的优先级高于||
flag = (list[0][ list[1] ] == id);
list = [pages, flag ? pages.indexOf(id) + 1 : 0];
if (flag) { list[1]++; }
if (ctn && !flag) { mw.notify(wgULS('当前页面并不属于该分类或命名空间!', '目前的頁面並不屬於此分類或命名空間!'),
nextid = list[0][ list[1] ];
{type: 'warn'}); }
}
mw.storage.setObject( 'inspect-category', [pages, list[1] - flag] );
const btnGo = new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/zh?redirect=no&curid=${nextid}`,
btnGo.setHref( `/zh?redirect=no&curid=${pages[ list[1] ]}` ).setDisabled( false );
disabled: !nextid}),
},
dialog = new OO.ui.MessageDialog(),
actionC = new OO.ui.ActionWidget({label: '取消'}),
dialog = new OO.ui.MessageDialog(),
actionG = new OO.ui.ActionWidget({label: wgULS('确定', '確認'), flags: 'progressive'}),
actionC = new OO.ui.ActionWidget({action: 'cancel', label: '取消'}),
actionG = new OO.ui.ActionWidget({action: 'go', label: wgULS('确定', '確認'), flags: 'progressive'}),
textInput = new OO.ui.TextInputWidget(),
fromHere = new OO.ui.CheckboxInputWidget(),
textInput = new OO.ui.TextInputWidget(),
fromHere = new OO.ui.CheckboxInputWidget(),
message = [ $('<p>', {text: wgULS('请输入分类名或名字空间编号:', '請輸入分類名或名字空間編號:')}),
message = [ $('<p>', {text: wgULS('请输入分类名或命名空间编号:', '請輸入分類名或命名空間編號:')}),
textInput.$element,
textInput.$element,
new OO.ui.FieldLayout(fromHere, {label: wgULS('从当前页面继续', '從當前頁面繼續'), align: 'inline'}).$element
new OO.ui.FieldLayout(fromHere, {label: wgULS('从当前页面继续', '從目前的頁面繼續'), align: 'inline'}).$element
],
],
updateCat = function(cat, ctn) {
updateCat = function(cat, ctn) {
// 输入空白则清除分类
// 输入空白则清除分类
if (cat === '') {
if (cat === '') {
mw.storage.remove( 'inspect-category' );
mw.storage.remove( 'inspect-category' );
btnGo.setHref( `/zh?redirect=no&curid=${id + 1}` ).setDisabled( false );
btnGo.setHref( `/zh?redirect=no&curid=${id + 1}` ).setDisabled( false );
return;
return;
}
}
// 输入数字对应名字空间
// 输入数字对应命名空间
if (!isNaN(cat)) {
if (!isNaN(cat)) {
if (!Object.keys( mw.config.get( 'wgFormattedNamespaces' ) ).includes(cat)) {
if (!Object.keys( mw.config.get( 'wgFormattedNamespaces' ) ).includes(cat)) {
mw.notify(wgULS('错误的名字空间编号!', '錯誤的名字空間編號!'), {type: 'error'});
mw.notify(wgULS('错误的命名空间编号!', '錯誤的命名空間編號!'), {type: 'error'});
return;
return;
}
}
mw.timedQuery(api, {list: 'allpages', apnamespace: cat, apfilterredir: 'nonredirects', aplimit: 'max'},
mw.timedQuery(api, {list: 'allpages', apnamespace: cat, apfilterredir: 'nonredirects', aplimit: 'max'},
wgULS('该名字空间的页面列表', '該名字空間的頁面列表')).then(ap => {
wgULS('该命名空间的页面列表', '此命名空間的頁面清單')).then(ap => {
const pages = ap.query.allpages.map(ele => ele.pageid).sort((a, b) => a < b); // 由新到旧排列
const pages = ap.query.allpages.map(ele => ele.pageid).sort((a, b) => a < b); // 由新到旧排列
if (pages.length === 0) {
if (pages.length === 0) {
mw.notify( wgULS('该名字空间沒有非重定向的页面!', '該名字空間沒有非重定向的頁面!'), {type: 'warn'} );
mw.notify( wgULS('该命名空间沒有非重定向的页面!', '此命名空間沒有非重新導向的頁面!'), {type: 'warn'} );
return;
return;
}
}
initList(pages, ctn);
initList(pages, ctn);
}, () => {}); // mw.timedQuery已经通知过错误信息,这里只要一个空函数处理reject,下同
}, () => {}); // mw.timedQuery已经通知过错误信息,这里只要一个空函数处理reject,下同
return;
return;
}
}
if (!/^(category|分[类類]):/i.test( cat )) { cat = `Category:${cat}`; }
if (!/^(category|分[类類]):/i.test( cat )) { cat = `Category:${cat}`; }
// cmtitle参数不可自动转换,所以需要先获得转换后的正确标题
// cmtitle参数不可自动转换,所以需要先获得转换后的正确标题
mw.timedQuery(api, {titles: cat, converttitles: 1}, wgULS('标准页面名称', '標準頁面名稱')).then(r => {
mw.timedQuery(api, {titles: cat, converttitles: 1}, wgULS('标准页面名称', '標準頁面名稱')).then(r => {
const target = r.query.pages[0];
const target = r.query.pages[0];
if (target.missing) {
if (target.missing) {
mw.notify(wgULS('错误的分类名!', '錯誤的分類名!'), {type: 'error'});
mw.notify(wgULS('错误的分类名!', '錯誤的分類名!'), {type: 'error'});
return;
return;
}
}
mw.timedQuery(api, {list: 'categorymembers', cmtitle: target.title, cmprop: 'ids', cmlimit: 'max',
mw.timedQuery(api, {list: 'categorymembers', cmtitle: target.title, cmprop: 'ids', cmlimit: 'max',
cmsort: 'timestamp', cmdir: 'older'}, wgULS('分类下的页面列表', '分類下的頁面列表')).then(cm => {
cmsort: 'timestamp', cmdir: 'older'}, wgULS('分类下的页面列表', '分類下的頁面清單')).then(cm => {
const pages = cm.query.categorymembers.map(ele => ele.pageid);
const pages = cm.query.categorymembers.map(ele => ele.pageid);
if (pages.length === 0) {
if (pages.length === 0) {
mw.notify( wgULS('该分类下无页面!', '該分類下無頁面!'), {type: 'warn'} );
mw.notify( wgULS('该分类下无页面!', '此分類下無頁面!'), {type: 'warn'} );
return;
return;
}
}
initList(pages, ctn);
initList(pages, ctn);
}, () => {});
}, () => {});
}, () => {});
};
}, () => {});
};
// 这个click事件不能绑到btnGo上,否则会禁用href跳转
// 这个click事件不能绑到btnGo上,否则会禁用href跳转
btnGo.$element.click(() => { if (list && flag) { mw.storage.setObject( 'inspect-category', list ); } })
btnGo.$element.click(() => { if (list && flag) { mw.storage.setObject( 'inspect-category', list ); } })
.contextmenu(e => {
.contextmenu(e => {
e.preventDefault();
e.preventDefault();
mw.dialog(dialog, [actionC, actionG], message).then(() => {
mw.dialog(dialog, [actionC, actionG], message).then(action => {
actionG.on('click', () => { updateCat( textInput.getValue(), fromHere.isSelected() ); });
if (action == 'go') { updateCat( textInput.getValue(), fromHere.isSelected() ); }
});
});
});
});
// 模板、JavaScript、CSS、Lua、JSON只需要箭头按钮
// 模板、JavaScript、CSS、Lua、JSON只需要箭头按钮
if ((mw.config.get( 'wgNamespaceNumber' ) == 10 && !page.endsWith( '/doc' )) || isCode) {
if (mw.config.get( 'wgNamespaceNumber' ) == 10 && !page.endsWith( '/doc' ) || isCode) {
$outer.removeClass( 'mw-ajax-loader' ).append( $('<div>', {id: 'inspector-btns', html: btnGo.$element}) );
mw.loader.addStyleTag( '#inspector-btns { width:auto; }' );
$outer.removeClass( 'mw-ajax-loader' ).append( $('<div>', {id: 'inspector-btns', html: btnGo.$element}) );
mw.loader.addStyleTag( '#inspector-btns { width:auto; }' );
return;
return;
}
}
// 先提交Ajax请求,这里手动设置cache: true
// 先提交Ajax请求,这里手动设置cache: true
const getJSON = $.get({ dataType: 'json', cache: true,
const getJSON = mw.loader.using('ext.CodeMirror.data'),
url: '/zh?title=mediawiki:gadget-CodeMirror.json&action=raw&ctype=application/json' }),
getExt = mw.loader.using(['ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki']);
getExt = mw.loader.using(['ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki']);
// 生成通用的API请求
// 生成通用的API请求
mw.standardQuery(api);
mw.standardQuery(api);
let text, editor, $wrapper, $replace, $charinsert, prefix, ns, section, secTitle, summary,
$original.find( ':header:has(.mw-editsection)' ).dblclick(function() {
lang = mw.config.get( 'wgUserVariant' );
section = mw.util.getParamValue( 'section', $(this).find( '.mw-editsection > a' ).attr('href') );
$original.find( ':header:has(.mw-editsection)' ).dblclick(function() {
mw.sectionQuery(api, section).then(r => { editor.setValue( r.parse.wikitext ); }, () => {});
section = mw.util.getParamValue( 'section', $(this).find( '.mw-editsection > a' ).attr('href') );
});
mw.sectionQuery(api, section).then(r => {
// 标注<p>标签
editor.setValue( r.parse.wikitext );
const css = mw.util.addCSS( `.empty { border:1px solid rgba(253,220,154,0.5); box-shadow:0 0 0.5em #fddc9a; }
secTitle = r.parse.sections[0].anchor.replaceAll(' ', '_');
#mw-content-text .mw-parser-output { display:flow-root; overflow:hidden; word-wrap:break-word; }` );
}, () => {});
css.disabled = true;
});
let text, editor, $wrapper, $replace, $charinsert, prefix, ns, section,
// 标注<p>标签
lang = mw.config.get( 'wgUserVariant' );
const css = mw.util.addCSS( `.empty { border:1px solid rgba(253,220,154,0.5); box-shadow:0 0 0.5em #fddc9a; }
// 也可以使用user.options,这里使用其他方法绕过
#mw-content-text .mw-parser-output { display:flow-root; max-height:100vh; overflow:auto;
const isGadget = (name) => ['loaded', 'loading', 'ready'].includes( mw.loader.getState(`ext.gadget.${name}`) ),
word-wrap:break-word; }` );
isBackup = isGadget('contentBackup'),
css.disabled = true;
backupObj = isBackup ? Object.fromEntries( mw.storage.getObject( 'LLWiki-contentBackup' ) || [] ) : {},
// 也可以使用user.options,这里使用其他方法绕过
backup = isBackup ? () => {
const isGadget = (name) => ['loaded', 'loading', 'ready'].includes( mw.loader.getState(`ext.gadget.${name}`) ),
backupObj[id] = [mw.now(), editor.getValue()];
isBackup = isGadget('contentBackup'),
mw.storage.setObject( Object.entries(backupObj) );
backupObj = isBackup ? Object.fromEntries( mw.storage.getObject( 'LLWiki-contentBackup' ) || [] ) : {},
} : () => {},
loadBackup = () => {
backup = isBackup ? () => {
const backupContent = (backupObj[id] || [])[1];
backupObj[id] = [mw.now(), editor.getValue()];
mw.storage.setObject( 'LLWiki-contentBackup', Object.entries(backupObj) );
if (!backupContent) {
} : () => {},
mw.notify(wgULS('当前页面尚未备份!', '當前頁面尚未備份!'), {type: 'warn'});
loadBackup = () => {
return;
const backupContent = (backupObj[id] || [])[1];
}
if (!backupContent) {
mw.confirm( wgULS('要加载备份吗?', '要加載備份嗎?'), 'progressive' ).then(confirm => {
mw.notify(wgULS('当前页面尚未备份!', '目前的頁面尚未備份!'), {type: 'warn'});
if (!confirm) { return; }
return;
editor.setValue( backupContent );
}
mw.notify(wgULS('已还原备份!', '已復原備份!'), {type: 'success'});
mw.confirm( wgULS('要加载备份吗?', '要載入備份嗎?'), 'progressive' ).then(confirm => {
});
if (!confirm) { return; }
},
editor.setValue( backupContent );
pEmpty = ($div) => {
mw.notify(wgULS('已还原备份!', '已還原備份!'), {type: 'success'});
$div.find( 'p:not(:has(script))' ).filter(function() { return !/\S/.test( this.textContent ); })
});
.addClass('empty');
},
},
pEmpty = ($div) => {
$placeholder = $('<div>', {class: "mw-parser-output"}).css('display', 'none'), // 用于替换时保留原始数据
$div.find( 'p:not(:has(script))' ).filter(function() { return !/\S/.test( this.textContent ); })
$warning = $('<div>', {html: [wgULS("确定要还原为未编辑的状态吗?", "確認要復原為未編輯的狀態嗎?"), '<br>',
.addClass('empty');
wgULS("建议您做好编辑内容备份。", "建議您做好編輯內容備份。")]}),
},
btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => {
placeholder = [
backup();
$('<div>', {class: "mw-parser-output"}).css('display', 'none'),
// 改变CSS样式表示提交中
$('<h1>', {id: 'firstHeading', class: 'firstHeading'}),
btns[0].setDisabled( true );
$('<div>', {id: 'catlinks'}).css('display', 'none'),
mw.safeEdit(api, curRevid, {pageid: id, text: editor.getValue(), section: section,
$('.mw-indicators')
summary: wgULS( '使用[[help:小工具/页面文本对比查看器|页面/文本对比查看器]]快速编辑',
], // 用于替换时保留原始数据
'使用[[help:小工具/页面文本对比查看器|頁面/文本對比察看器]]快速編輯')}, isBackup)
$warning = $('<div>', {html: [wgULS("确定要还原为未编辑的状态吗?", "確認要還原為未編輯的狀態嗎?"), '<br>',
.then(() => { location.reload(); },
wgULS("建议您做好编辑内容备份。", "建議您做好編輯內容備份。")]}),
reason => { btns[0].setDisabled( reason == 'editConflict' ); }); // 编辑冲突必须刷新页面,不应重复提交
$heading = $('#firstHeading'),
}), new OO.ui.ButtonWidget({label: wgULS('预览', '預覽')}).on('click', () => {
$cat = $('#catlinks'),
backup();
$indicator = $('.mw-indicator'),
// 改变CSS样式表示预览中
btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => {
btns[1].setDisabled( true );
backup();
console.log('API request: 请求预览');
// 改变CSS样式表示提交中
const now = mw.now();
btns[0].setDisabled( true );
api.parse(editor.getValue(), {title: page, uselang: lang, disablelimitreport: 1, disableeditsection: 1})
mw.safeEdit(api, {pageid: id, text: editor.getValue(), section: section,
.then(html => {
summary: (section === undefined ? '' : `/* ${secTitle} */ `) + (summary ? `${summary} //` : '//') +
console.log(`End API request: 已生成预览,用时 ${mw.now() - now} ms`);
wgULS( '使用[[help:小工具/页面文本对比查看器|页面/文本对比查看器]]快速编辑',
if ($.contains( $content[0], $original[0] )) { $original.after( $placeholder ).detach(); }
'使用[[help:小工具/页面文本对比查看器|頁面/文字對比檢視器]]快速編輯')}, isBackup)
$content.children( '.mw-parser-output' ).replaceWith( html );
.then(() => { location.href = mw.util.getUrl( page ); },
pEmpty( $content.children( '.mw-parser-output' ) );
reason => { btns[0].setDisabled( reason == 'editConflict' ); }); // 编辑冲突必须刷新页面,不应重复提交
if (mw.resizeLyrics) { mw.resizeLyrics(); }
}), new OO.ui.ButtonWidget({label: wgULS('预览', '預覽')}).on('click', () => {
mw.hook( 'wikipage.content' ).fire($content);
backup();
}, reason => { mw.apiFailure(reason, wgULS('预览', '預覽')); }) // mw.apiFailure不会抛出错误
// 改变CSS样式表示预览中
.then(() => { btns[1].setDisabled( false ); });
btns[1].setDisabled( true );
}), new OO.ui.ButtonWidget({label: wgULS('还原', '復原'), flags: 'destructive', disabled: true}).on('click', () => {
mw.timedParse(api, {text: editor.getValue(), uselang: lang,
mw.confirm($warning, ['primary', 'destructive']).then(confirm => {
prop: 'text|categorieshtml|displaytitle|indicators|modules|headitems'
if (!confirm) { return; }
}, wgULS('预览', '預覽')).then(r => {
section = undefined;
mw.loader.load( r.parse.modulestyles );
editor.setValue( text );
const tabsStyles = r.parse.headitems.find(function(ele) { return ele.tag === 'TabsStyles'; });
$content.children( '.mw-parser-output' ).replaceWith( $original );
if (tabsStyles) {
if (mw.resizeLyrics) { mw.resizeLyrics(); }
$('head').append(tabsStyles.content); // 可能重复,但重复也没关系
});
}
}), new OO.ui.ButtonWidget({label: wgULS('显示', '顯示')}).on('click', () => {
if ($.contains( $content[0], $original[0] )) {
css.disabled = !css.disabled;
$original.after( placeholder[0] ).detach();
cssHide.disabled = !cssHide.disabled;
$heading.after( placeholder[1] ).detach();
$wrapper.toggle();
$cat.after( placeholder[2] ).detach();
editor.setSize();
$indicator.detach();
btns[3].setLabel( css.disabled ? wgULS('显示', '顯示') : wgULS('隐藏', '隱藏'));
}
btns[2].setDisabled( css.disabled ); // 折叠时无法更新textarea
$content.children( '.mw-parser-output' ).replaceWith( r.parse.text );
if (mw.resizeLyrics) { mw.resizeLyrics(); }
pEmpty( $content.children( '.mw-parser-output' ) );
}), btnGo],
if (mw.resizeLyrics) { mw.resizeLyrics(); }
options = $('<div>', {text: wgULS('查找替换', '查找替換')}).click(() => {
mw.hook( 'wikipage.content' ).fire($content);
if ($replace) {
$('#catlinks').replaceWith( r.parse.categorieshtml );
$replace.show();
placeholder[1].html( r.parse.displaytitle );
return;
placeholder[3].html( Object.entries( r.parse.indicators ).map(ele =>
}
$('<div>', {class: 'mw-indicator', id: `mw-indicator-${ele[0]}`, html: ele[1]})) );
let replaceBackup;
const enable = () => { replaceBtn.setDisabled( false ); },
}, () => {}).then(() => { btns[1].setDisabled( false ); });
}), new OO.ui.ButtonWidget({label: wgULS('还原', '還原'), flags: 'destructive', disabled: true}).on('click', () => {
ptn = new OO.ui.TextInputWidget().on('change', enable),
mw.confirm($warning, ['primary', 'destructive']).then(confirm => {
val = new OO.ui.MultilineTextInputWidget({autosize: true, maxRows: 3}).on('change', enable),
if (!confirm) { return; }
regex = new OO.ui.CheckboxInputWidget().on('change', enable),
section = undefined;
modifier = new OO.ui.CheckboxInputWidget().on('change', enable),
editor.setValue( text );
undoBtn = new OO.ui.ButtonWidget({label: wgULS('撤销', '撤銷'), disabled: true, flags: ['destructive']})
if ($.contains( $content[0], $original[0] )) { return; } // 无事发生
.on('click', () => {
$content.children( '.mw-parser-output' ).replaceWith( $original );
editor.setValue( replaceBackup );
placeholder[1].replaceWith( $heading );
enable();
$('#catlinks').replaceWith( $cat );
}),
placeholder[3].html( $indicator );
replaceBtn = new OO.ui.ButtonWidget({label: wgULS('替换', '替換'), flags: ['progressive']})
if (mw.resizeLyrics) { mw.resizeLyrics(); }
.on('click', () => {
});
const pattern = ptn.getValue(),
}), new OO.ui.ButtonWidget({label: wgULS('显示', '顯示')}).on('click', () => {
regexp = new RegExp(regex.isSelected() ? pattern : mw.util.escapeRegExp(pattern),
css.disabled = !css.disabled;
'g' + (modifier.isSelected() ? 'i' : ''));
cssHide.disabled = !cssHide.disabled;
replaceBackup = editor.getValue();
$wrapper.toggle();
undoBtn.setDisabled( false );
editor.setSize();
replaceBtn.setDisabled( true ); // 防止连续点击造成无法撤销
btns[3].setLabel( css.disabled ? wgULS('显示', '顯示') : wgULS('隐藏', '隱藏'));
editor.setValue( replaceBackup.replace(regexp, val.getValue()) );
btns[2].setDisabled( css.disabled ); // 折叠时无法更新textarea
}),
if (mw.resizeLyrics) { mw.resizeLyrics(); }
hideBtn = new OO.ui.ButtonWidget({label: wgULS('关闭', '關閉')})
}), btnGo],
.on('click', () => {
options = $('<div>', {text: wgULS('查找替换', '尋找取代')}).click(() => {
$replace.hide();
if ($replace) {
enable();
$replace.show();
});
return;
$replace = $('<div>', {class: 'inspector-field', html: [
}
$('<div>', {html: ['查找:', ptn.$element]}),
let replaceBackup;
$('<div>', {html: [wgULS('替换:', '替換:'), val.$element]}),
const enable = () => { replaceBtn.setDisabled( false ); },
$('<div>', {html: [
ptn = new OO.ui.TextInputWidget().on('change', enable),
new OO.ui.FieldLayout(regex, {label: wgULS('正则', '正則'), align: 'inline'}).$element,
val = new OO.ui.MultilineTextInputWidget({autosize: true, maxRows: 3}).on('change', enable),
new OO.ui.FieldLayout(modifier, {label: $('<i>', {text: 'i'}), align: 'inline'}).$element,
regex = new OO.ui.CheckboxInputWidget().on('change', enable),
replaceBtn.$element, undoBtn.$element, hideBtn.$element
modifier = new OO.ui.CheckboxInputWidget().on('change', enable),
]})
undoBtn = new OO.ui.ButtonWidget({label: wgULS('撤销', '復原'), disabled: true, flags: ['destructive']})
]}).appendTo( 'body' ).draggable();
.on('click', () => {
}).add(isGadget('charinsert') ? $('<div>', {text: '快速插入'}).click(() => {
editor.setValue( replaceBackup );
if ($charinsert) {
enable();
$charinsert.show();
}),
return;
replaceBtn = new OO.ui.ButtonWidget({label: wgULS('替换', '取代'), flags: ['progressive']})
}
.on('click', () => {
$charinsert = $('<div>', {html: $('<div>', {text: '快速插入工具', id: 'inspector-field-title'}),
const pattern = ptn.getValue(),
class: 'inspector-field mw-ajax-loader'}).appendTo('body').draggable().contextmenu(e => {
regexp = new RegExp(regex.isSelected() ? pattern : mw.util.escapeRegExp(pattern),
e.preventDefault();
'g' + (modifier.isSelected() ? 'i' : ''));
$charinsert.hide();
replaceBackup = editor.getValue();
}).on('click', '.mw-charinsert-item', function() {
undoBtn.setDisabled( false );
const $this = $(this),
replaceBtn.setDisabled( true ); // 防止连续点击造成无法撤销
start = $this.data('mw-charinsert-start') || $this.data('start') || '',
editor.setValue( replaceBackup.replace(regexp, val.getValue()) );
end = $this.data('mw-charinsert-end') || $this.data('end') || '';
}),
editor.replaceSelection( start + editor.getSelection() + end );
hideBtn = new OO.ui.ButtonWidget({label: wgULS('关闭', '關閉')})
editor.focus();
.on('click', () => {
});
$replace.hide();
// 为了充分利用浏览器缓存,这里不使用API;注意url转义,这里不需要简化'/mediawiki/index.php'
enable();
$.get({url: mw.util.getUrl(src, {variant: mw.config.get( 'wgUserLanguage' )}),
});
cache: charInsert.cache !== false, dataType: 'text'}).then(doc => {
$replace = $('<div>', {class: 'inspector-field', html: [
$charinsert.append( $( doc.match(/<body[\s\S]+<\/body>/)[0] ).find( '.mw-parser-output' ) );
$('<div>', {html: [wgULS('查找:', '尋找:' ), ptn.$element]}),
}, () => {
$('<div>', {html: [wgULS('替换:', '取代:'), val.$element]}),
mw.notify([
$('<div>', {html: [
wgULS('无法加载', '無法加載'),
new OO.ui.FieldLayout(regex, {label: wgULS('正则', '正規表達式'), align: 'inline'}).$element,
$('<a>', {text: src, href: mw.util.getUrl(src)}),
new OO.ui.FieldLayout(modifier, {label: $('<i>', {text: 'i'}), align: 'inline'}).$element,
wgULS('!请检查该页面是否存在。', '!請檢查該頁面是否存在。')
replaceBtn.$element, undoBtn.$element, hideBtn.$element
], {type: 'error', autoHideSeconds: 'long'});
]})
}).then(() => { $charinsert.removeClass( 'mw-ajax-loader' ); });
]}).appendTo( 'body' ).draggable();
}) : null),
}).add(isGadget('charinsert') ? $('<div>', {text: '快速插入'}).click(() => {
$dropdown = $('<div>', {class: "inspector-menu", html: options}),
if ($charinsert) {
$hints = $('<div>', {class: "inspector-menu"}).on('click', '.inspector-hint', function() {
$charinsert.show();
const cursor = editor.getCursor();
return;
editor.replaceRange(this.textContent + (ns > 0 ? '}}' : ']]'),
}
{line: cursor.line, ch: cursor.ch - prefix.length}, cursor);
$charinsert = $('<div>', {html: $('<div>', {text: '快速插入工具', id: 'inspector-field-title'}),
// 随后再次执行updateHints时会自行stop
class: 'inspector-field mw-ajax-loader'}).appendTo('body').draggable().contextmenu(e => {
editor.focus();
e.preventDefault();
}),
$charinsert.hide();
// 判别模板时可能会误处理解析器函数
}).on('click', '.mw-charinsert-item', function() {
regexps = {0: /\[\[\s*:?([^|[\]{}<>]*)$/i, 10: /{{\s*([^|[\]{}<>#]*)$/i,
const $this = $(this),
828: /{{\s*#invoke:\s*([^|[\]{}<>#:]*)$/i, 274: /{{\s*#widget:\s*([^|[\]{}<>#:]*)$/i},
start = $this.data('mw-charinsert-start') || $this.data('start') || '',
$noHint = $('<span>', {class: 'error'}),
end = $this.data('mw-charinsert-end') || $this.data('end') || '';
nsPrefixes = {0: '', 10: 'Template:', 828: '模块:', 274: 'Widget:'},
editor.replaceSelection( start + editor.getSelection() + end );
autocomplete = isGadget('autocomplete') ? e => {
editor.focus();
if (![9, 27].includes( e.keyCode )) { return; } // tab和esc
});
let $cursor, before, sections, title;
// 为了充分利用浏览器缓存,这里不使用API;注意url转义,这里不需要简化'/mediawiki/index.php'
const cursorActivity = () => {
$.get({url: mw.util.getUrl(src, {variant: mw.config.get( 'wgUserLanguage' )}),
const newCursor = editor.getCursor(),
cache: charInsert.cache !== false, dataType: 'text'}).then(doc => {
newLine = editor.getLine( newCursor.line ),
$charinsert.append( $( doc.match(/<body[\s\S]+<\/body>/)[0] ).find( '.mw-parser-output' ) );
newAfter = newLine.slice( newCursor.ch );
}, () => {
// 如果光标改变过位置
mw.notify([
if (newCursor.line != cursor.line || newAfter != after) {
wgULS('无法加载', '無法載入'),
stop();
$('<a>', {text: src, href: mw.util.getUrl(src)}),
return;
wgULS('!请检查该页面是否存在。', '!請檢查此頁面是否存在。')
}
], {type: 'error', autoHideSeconds: 'long'});
before = newLine.slice(0, newCursor.ch);
}).then(() => { $charinsert.removeClass( 'mw-ajax-loader' ); });
updateHints();
},
}) : null),
$dropdown = $('<div>', {class: "inspector-menu", html: options}),
stop = () => {
$hints = $('<div>', {class: "inspector-menu"}).on('click', '.inspector-hint', function() {
$hints.slideUp('fast');
const cursor = editor.getCursor();
editor.off('cursorActivity', cursorActivity);
editor.replaceRange(this.textContent + (ns > 0 ? '}}' : ']]'),
},
{line: cursor.line, ch: cursor.ch - prefix.length + 1}, cursor);
updateHints = mw.util.debounce(500, () => {
// 随后再次执行updateHints时会自行stop
if (!regexps[ns].test( before )) {
editor.focus();
stop();
}),
return;
// 判别模板时可能会误处理解析器函数
}
regexps = {0: /\[\[\s*:?([^|[\]{}<>]*)$/i, 10: /{{\s*([^|[\]{}<>#]*)$/i,
$cursor = $wrapper.find( '.CodeMirror-cursor' );
828: /{{\s*#invoke:\s*([^|[\]{}<>#:]*)$/i, 274: /{{\s*#widget:\s*([^|[\]{}<>#:]*)$/i},
prefix = before.match( regexps[ns] )[1];
$noHint = $('<span>', {class: 'error'}),
if (prefix === "" || prefix.startsWith('#')) { return; }
nsPrefixes = {0: '', 10: 'Template:', 828: '模块:', 274: 'Widget:'},
if (ns == '0' && prefix.includes('#')) {
autocomplete = isGadget('autocomplete') ? e => {
const capture = prefix.match(/^(.+)#(.*)$/);
if (![9, 27].includes( e.keyCode )) { return; } // tab和esc
prefix = capture[2];
let $cursor, before, sections, title;
(title == capture[1] ? Promise.resolve() : mw.timedQuery(api, {action: 'parse', page: capture[1],
const cursor = editor.getCursor(),
prop: 'sections'}, wgULS('段落标题', '段落標題')).then(r => {
line = editor.getLine( cursor.line ),
title = capture[1];
after = line.slice( cursor.ch );
sections = r.parse.sections.map(ele => $('<div>', {class: 'inspector-hint', text: ele.line}));
before = line.slice(0, cursor.ch);
}, () => { throw null; })).then(() => {
const cursorActivity = () => {
$hints.html( sections.filter(ele => ele.text().startsWith( prefix )) )
const newCursor = editor.getCursor(),
.show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
newLine = editor.getLine( newCursor.line ),
if ($hints.children().length === 0) {
newAfter = newLine.slice( newCursor.ch );
$noHint.text(wgULS('不存在对应段落!', '不存在對應段落!')).appendTo( $hints );
// 如果光标改变过位置
}
if (newCursor.line != cursor.line || newAfter != after) {
}, stop);
stop();
return;
return;
}
}
mw.timedQuery(api, {list: 'prefixsearch', pssearch: prefix, psnamespace: ns},
before = newLine.slice(0, newCursor.ch);
wgULS('前缀相符的页面', '前綴相符的頁面')).then(r => {
updateHints();
$hints.html( r.query.prefixsearch.map(ele =>
},
$('<div>', {class: 'inspector-hint', text: ele.title.slice( nsPrefixes[ns].length )})
stop = () => {
) ).show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
$hints.slideUp('fast');
if ($hints.children().length === 0) {
editor.off('cursorActivity', cursorActivity);
$noHint.text(wgULS('不存在对应页面!', '不存在對應頁面!')).appendTo( $hints );
},
}
updateHints = mw.util.debounce(500, () => {
}, stop);
if (!regexps[ns].test( before )) {
});
stop();
if (e.keyCode == 27) {
return;
stop();
}
return;
$cursor = $wrapper.find( '.CodeMirror-cursor' );
}
prefix = before.match( regexps[ns] )[1];
editor.execCommand( 'delCharBefore' ); // 删掉新增的\t
if (prefix === "" || prefix.startsWith('#')) { return; }
const cursor = editor.getCursor(),
if (ns == '0' && prefix.includes('#')) {
line = editor.getLine( cursor.line ),
const capture = prefix.match(/^(.+)#(.*)$/);
after = line.slice( cursor.ch );
prefix = capture[2];
before = line.slice(0, cursor.ch);
(title == capture[1] ? Promise.resolve() : mw.timedQuery(api, {action: 'parse', page: capture[1],
$.each(regexps, (k, v) => { if (v.test(before)) { ns = k; } });
prop: 'sections'}, wgULS('段落标题', '段落標題')).then(r => {
if (!ns) { return; }
title = capture[1];
mw.notify(wgULS('开启自动补全提示。', '開啟自動補全提示。'), {tag: 'autocomplete'});
sections = r.parse.sections.map(ele => $('<div>', {class: 'inspector-hint', text: ele.line}));
editor.on('cursorActivity', cursorActivity);
}, () => { throw null; })).then(() => {
// 实际上updateHints总是延迟500ms执行,因此下一行代码的位置影响不大
$hints.html( sections.filter(ele => ele.text().startsWith( prefix )) )
updateHints();
.show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
} : () => {};
if ($hints.children().length === 0) {
mw.loader.using('jquery.ui').then(() => {
$noHint.text(wgULS('不存在对应段落!', '不存在對應段落!')).appendTo( $hints );
const actionP = new OO.ui.ActionWidget({label: '是', target: '_blank', flags: 'progressive'}),
}
actionD = new OO.ui.ActionWidget({label: '否'}),
}, stop);
$url = $('<a>'), // 桌面版CSS不必需href
return;
label = [wgULS('要在新标签页打开', '要在新標籤頁打開'), $url, wgULS('吗?', '嗎?')];
}
$outer.on('contextmenu', '.cm-mw-template-name', function() {
mw.timedQuery(api, {list: 'prefixsearch', pssearch: prefix, psnamespace: ns},
const template = '模板:' + $(this).text();
wgULS('前缀相符的页面', '前綴相符的頁面')).then(r => {
// 这里增加一次确认界面是因为部分浏览器不允许JS直接打开新标签页。
$hints.html( r.query.prefixsearch.map(ele =>
$url.text( template );
$('<div>', {class: 'inspector-hint', text: ele.title.slice( nsPrefixes[ns].length )})
actionP.setHref( mw.util.getUrl(template) ); // 注意url转义
) ).show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
mw.dialog(dialog, [actionD, actionP], label);
if ($hints.children().length === 0) {
return false; // 这里需要同时执行stopImmediatePropagation()和preventDefault()
$noHint.text(wgULS('不存在对应页面!', '不存在對應頁面!')).appendTo( $hints );
}).on('contextmenu', '.CodeMirror', e => {
}
e.preventDefault();
}, stop);
// 使用jquery.ui让菜单总是落在CodeMirror内,此时slideDown效果不生效
});
$dropdown.show().position({my: 'left top', of: e, within: $wrapper, collision: 'fit'});
if (e.keyCode == 27) {
}).on('keydown', '.CodeMirror', autocomplete).resizable( {handles: 'w', minWidth: 350} );
stop();
});
return;
$('body').click(() => { $dropdown.slideUp('fast'); });
}
if (isBackup) {
editor.execCommand( 'delCharBefore' ); // 删掉新增的\t
btns[2].$element.contextmenu(e => {
$.each(regexps, (k, v) => { if (v.test(before)) { ns = k; } });
e.preventDefault();
if (!ns) { return; }
loadBackup();
mw.notify(wgULS('开启自动补全提示。', '開啟自動補全提示。'), {tag: 'autocomplete'});
});
editor.on('cursorActivity', cursorActivity);
}
// 实际上updateHints总是延迟500ms执行,因此下一行代码的位置影响不大
if (isGadget( 'PreviewWithVariant' ) ) {
updateHints();
const options = [{label: "大陆简体", data: "zh-cn"}, {label: "香港繁體", data: "zh-hk"},
} : () => {};
{label: "澳門繁體", data: "zh-mo"}, {label: "马来西亚简体", data: "zh-my"},
mw.loader.using(['jquery.ui', 'user.options']).then(() => {
{label: "新加坡简体", data: "zh-sg"}, {label: "臺灣繁體", data: "zh-tw"}],
select = new OO.ui.DropdownInputWidget({classes: ['inspector-variant'], options: options, value: lang})
const actionP = new OO.ui.ActionWidget({label: '', target: '_blank', flags: 'progressive'}),
actionD = new OO.ui.ActionWidget({label: '否'}),
.on('change', () => { lang = select.getValue(); }),
$url = $('<a>'), // 桌面版CSS不必需href
menu = select.$element.find( '.oo-ui-menuSelectWidget' )
label = [wgULS('要在新标签页打开', '要在新分頁打開'), $url, wgULS('吗?', '嗎?')];
.click(() => { menu.addClass( 'oo-ui-element-hidden' ); });
$outer.on('contextmenu', '.cm-mw-template-name', function() {
select.$element.prependTo( btns[1].$element );
const template = '模板:' + $(this).text();
btns[1].$element.contextmenu(e => {
// 这里增加一次确认界面是因为部分浏览器不允许JS直接打开新标签页。
e.preventDefault();
$url.text( template );
menu.removeClass( 'oo-ui-element-hidden' );
actionP.setHref( mw.util.getUrl(template) ); // 注意url转义
}).children('a').blur(() => { menu.addClass( 'oo-ui-element-hidden' ); });
mw.dialog(dialog, [actionD, actionP], label).catch(() => {});
}
return false; // 这里需要同时执行stopImmediatePropagation()和preventDefault()
pEmpty( $original );
}).on('contextmenu', '.CodeMirror', e => {
// $.when很容易出错,这里保险起见用Promise.all
e.preventDefault();
Promise.all([getJSON, mw.request, getExt]).then(data => {
// 使用jquery.ui让菜单总是落在CodeMirror内,此时slideDown效果不生效
mw.config.set('extCodeMirrorConfig', data[0]);
$dropdown.show().position({my: 'left top', of: e, within: $wrapper, collision: 'fit'});
mw.hook( 'codemirror.config' ).fire();
}).on('keydown', '.CodeMirror', autocomplete).resizable( {handles: 'w', minWidth: 350} );
text = data[1].query.pages[0].revisions[0].content;
});
editor = new CodeMirror($outer[0], {value: text, mode: 'text/mediawiki', mwConfig: data[0],
$('body').click(() => { $dropdown.slideUp('fast'); });
lineWrapping: true, lineNumbers: true});
btns[0].$element.contextmenu(e => {
$wrapper = $( editor.getWrapperElement() ).toggle().append( [$dropdown, $hints] );
e.preventDefault();
$('<div>', {id: 'inspector-btns', html: btns.map(ele => ele.$element)}).appendTo( $outer );
OO.ui.prompt( wgULS('请输入编辑摘要:', '請輸入編輯摘要:') , {textInput: {value: summary}} ).then(input => {
// 处理页面上方的差异
if (input !== null) { summary = input; }
$('.diff').click(e => {
});
const row = $( e.target ).closest('tr'),
});
isLineno = row.children( '.diff-lineno' ).length ? 1 : 0,
if (isBackup) {
rowLineno = isLineno ? row : row.prevAll( ':has(.diff-lineno)' ).first(),
btns[2].$element.contextmenu(e => {
n = parseInt( rowLineno.children().last().text().match(/\d+/) ) + row.index() - rowLineno.index()
e.preventDefault();
- 2 + isLineno, // 使用parseInt规避可能的程序错误,注意CodeMirror的行号从0开始
loadBackup();
nLine = editor.lastLine();
});
if (nLine >= n) {
}
editor.scrollIntoView(nLine);
if (isGadget( 'PreviewWithVariant' ) ) {
editor.scrollIntoView(n);
const options = [{label: "大陆简体", data: "zh-cn"}, {label: "香港繁體", data: "zh-hk"},
}
{label: "澳門繁體", data: "zh-mo"}, {label: "大马简体", data: "zh-my"},
else { mw.notify( wgULS('当前不存在该行!', '當前不存在該行!'), {type: 'warn'} ); }
{label: "新加坡简体", data: "zh-sg"}, {label: "臺灣正體", data: "zh-tw"}],
});
select = new OO.ui.DropdownInputWidget({classes: ['inspector-variant'], options: options, value: lang})
}, reason => { mw.apiFailure(reason, `CodeMirror${wgULS('设置或页面', '設置或頁面')}Wikitext`); })
.then(() => { $outer.removeClass( 'mw-ajax-loader' ); });
.on('change', () => { lang = select.getValue(); }),
menu = select.$element.find( '.oo-ui-menuSelectWidget' )
.click(() => { menu.addClass( 'oo-ui-element-hidden' ); });
select.$element.prependTo( btns[1].$element );
btns[1].$element.contextmenu(e => {
e.preventDefault();
menu.removeClass( 'oo-ui-element-hidden' );
}).children('a').blur(() => { menu.addClass( 'oo-ui-element-hidden' ); });
}
pEmpty( $original );
// $.when很容易出错,这里保险起见用Promise.all
Promise.all([getJSON, mw.request, getExt]).then(data => {
mw.hook( 'codemirror.config' ).fire();
text = data[1].query.pages[0].revisions[0].content;
editor = new CodeMirror($outer[0], {value: text, mode: 'text/mediawiki',
mwConfig: mw.config.get('extCodeMirrorConfig'), lineWrapping: true, lineNumbers: true
});
$wrapper = $( editor.getWrapperElement() ).toggle().append( [$dropdown, $hints] );
$('<div>', {id: 'inspector-btns', html: btns.map(ele => ele.$element)}).appendTo( $outer );
// 处理页面上方的差异
$('.diff').click(e => {
const row = $( e.target ).closest('tr');
if (row.hasClass( 'diff-title' )) { return; }
const isLineno = row.children( '.diff-lineno' ).length ? 1 : 0,
rowLineno = isLineno ? row : row.prevAll( ':has(.diff-lineno)' ).first(),
n = parseInt( rowLineno.children().last().text().match(/\d+/) ) + row.index() - rowLineno.index()
- 2 + isLineno, // 使用parseInt规避可能的程序错误,注意CodeMirror的行号从0开始
nLine = editor.lastLine();
if (nLine >= n) {
editor.scrollIntoView(nLine);
editor.scrollIntoView(n);
}
else { mw.notify( wgULS('当前不存在该行!', '目前不存在此行!'), {type: 'warn'} ); }
});
}, reason => { mw.apiFailure(reason, `CodeMirror${wgULS('设置或页面', '設定或頁面')}Wikitext`); })
.then(() => { $outer.removeClass( 'mw-ajax-loader' ); });
});
});
//</nowiki>
//</nowiki>

2023年3月27日 (一) 13:11的最新版本

//<nowiki>
// 由[[mediawiki:gadget-inspect-loader.js]]调用,可以使用ES6语法
/**
 * @Function: 在页面内快速编辑和预览
 * @Author: [[User:Bhsd]]
 * @Translation: [[User:Lakejason0]]
 */
"use strict";
/*global OO, CodeMirror, wgULS*/
$(() => { // 避免全局变量
	let nextid, list = mw.storage.getObject( 'inspect-category' ), flag;
	const id = mw.config.get( 'wgArticleId' ),
		page = mw.config.get( 'wgPageName' ), // 这里未转义,后面应用时需要注意
		isCode = mw.config.get( 'wgPageContentModel' ) != 'wikitext', // javascript/css/Scribunto/json
		gadgets = mw.gadgets || {},
		charInsert = gadgets.charinsert || {},
		src = charInsert.src || 'special:mypage/edittools',
		// 由于resizable,left, top, height必须加!important,width不可加!important
		cssHide = mw.util.addCSS( `#inspector { position:fixed; bottom:0; right:24px; left:auto !important;
		top:auto !important; height:auto !important; width:calc(50% - 7rem - 0.5px); }
		div.start-screen { bottom:40px; }` ),
		$content = $('#mw-content-text, #mw-imagepage-content').last(),
		// wikitext/Scribunto对应.mw-parser-output,javascript/css对应.mw-code,json对应.mw-json
		$original = $content.children('.mw-json, .mw-code, .mw-parser-output'),
		$outer = $('<div>', {id: 'inspector', class: 'mw-ajax-loader'}).insertBefore( $original ),
		api = new mw.Api();
	if (!list) { nextid = id + 1; }
	else {
		flag = list[0][ list[1] ] == id;
		if (flag) { list[1]++; }
		nextid = list[0][ list[1] ];
	}
	const btnGo = new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/zh?redirect=no&curid=${nextid}`,
		disabled: !nextid}),
		initList = (pages, ctn) => {
		console.info({pages: pages, ctn: ctn, id: id});
		flag = pages[0] == id || ctn && pages.includes(id); // &&的优先级高于||
		list = [pages, flag ? pages.indexOf(id) + 1 : 0];
		if (ctn && !flag) { mw.notify(wgULS('当前页面并不属于该分类或命名空间!', '目前的頁面並不屬於此分類或命名空間!'),
			{type: 'warn'}); }
		mw.storage.setObject( 'inspect-category', [pages, list[1] - flag] );
		btnGo.setHref( `/zh?redirect=no&curid=${pages[ list[1] ]}` ).setDisabled( false );
	},
		dialog = new OO.ui.MessageDialog(),
		actionC = new OO.ui.ActionWidget({action: 'cancel', label: '取消'}),
		actionG = new OO.ui.ActionWidget({action: 'go', label: wgULS('确定', '確認'), flags: 'progressive'}),
		textInput = new OO.ui.TextInputWidget(),
		fromHere = new OO.ui.CheckboxInputWidget(),
		message = [ $('<p>', {text: wgULS('请输入分类名或命名空间编号:', '請輸入分類名或命名空間編號:')}),
		textInput.$element,
		new OO.ui.FieldLayout(fromHere, {label: wgULS('从当前页面继续', '從目前的頁面繼續'), align: 'inline'}).$element
	],
		updateCat = function(cat, ctn) {
		// 输入空白则清除分类
		if (cat === '') {
			mw.storage.remove( 'inspect-category' );
			btnGo.setHref( `/zh?redirect=no&curid=${id + 1}` ).setDisabled( false );
			return;
		}
		// 输入数字对应命名空间
		if (!isNaN(cat)) {
			if (!Object.keys( mw.config.get( 'wgFormattedNamespaces' ) ).includes(cat)) {
				mw.notify(wgULS('错误的命名空间编号!', '錯誤的命名空間編號!'), {type: 'error'});
				return;
			}
			mw.timedQuery(api, {list: 'allpages', apnamespace: cat, apfilterredir: 'nonredirects', aplimit: 'max'},
				wgULS('该命名空间的页面列表', '此命名空間的頁面清單')).then(ap => {
				const pages = ap.query.allpages.map(ele => ele.pageid).sort((a, b) => a < b); // 由新到旧排列
				if (pages.length === 0) {
					mw.notify( wgULS('该命名空间沒有非重定向的页面!', '此命名空間沒有非重新導向的頁面!'), {type: 'warn'} );
					return;
				}
				initList(pages, ctn);
			}, () => {}); // mw.timedQuery已经通知过错误信息,这里只要一个空函数处理reject,下同
			return;
		}
		if (!/^(category|分[类類]):/i.test( cat )) { cat = `Category:${cat}`; }
		// cmtitle参数不可自动转换,所以需要先获得转换后的正确标题
		mw.timedQuery(api, {titles: cat, converttitles: 1}, wgULS('标准页面名称', '標準頁面名稱')).then(r => {
			const target = r.query.pages[0];
			if (target.missing) {
				mw.notify(wgULS('错误的分类名!', '錯誤的分類名!'), {type: 'error'});
				return;
			}
			mw.timedQuery(api, {list: 'categorymembers', cmtitle: target.title, cmprop: 'ids', cmlimit: 'max',
				cmsort: 'timestamp', cmdir: 'older'}, wgULS('分类下的页面列表', '分類下的頁面清單')).then(cm => {
				const pages = cm.query.categorymembers.map(ele => ele.pageid);
				if (pages.length === 0) {
					mw.notify( wgULS('该分类下无页面!', '此分類下無頁面!'), {type: 'warn'} );
					return;
				}
				initList(pages, ctn);
			}, () => {});
		}, () => {});
	};
	// 这个click事件不能绑到btnGo上,否则会禁用href跳转
	btnGo.$element.click(() => { if (list && flag) { mw.storage.setObject( 'inspect-category', list ); } })
		.contextmenu(e => {
		e.preventDefault();
		mw.dialog(dialog, [actionC, actionG], message).then(action => {
			if (action == 'go') { updateCat( textInput.getValue(), fromHere.isSelected() ); }
		});
	});
	// 模板、JavaScript、CSS、Lua、JSON只需要箭头按钮
	if (mw.config.get( 'wgNamespaceNumber' ) == 10 && !page.endsWith( '/doc' ) || isCode) {
		$outer.removeClass( 'mw-ajax-loader' ).append( $('<div>', {id: 'inspector-btns', html: btnGo.$element}) );
		mw.loader.addStyleTag( '#inspector-btns { width:auto; }' );
		return;
	}
	// 先提交Ajax请求,这里手动设置cache: true
	const getJSON = mw.loader.using('ext.CodeMirror.data'),
		getExt = mw.loader.using(['ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki']);
	// 生成通用的API请求
	mw.standardQuery(api);
	let text, editor, $wrapper, $replace, $charinsert, prefix, ns, section, secTitle, summary,
		lang = mw.config.get( 'wgUserVariant' );
	$original.find( ':header:has(.mw-editsection)' ).dblclick(function() {
		section = mw.util.getParamValue( 'section', $(this).find( '.mw-editsection > a' ).attr('href') );
		mw.sectionQuery(api, section).then(r => {
			editor.setValue( r.parse.wikitext );
			secTitle = r.parse.sections[0].anchor.replaceAll(' ', '_');
		}, () => {});
	});
	// 标注<p>标签
	const css = mw.util.addCSS( `.empty { border:1px solid rgba(253,220,154,0.5); box-shadow:0 0 0.5em #fddc9a; }
		#mw-content-text .mw-parser-output { display:flow-root; max-height:100vh; overflow:auto;
		word-wrap:break-word; }` );
	css.disabled = true;
	// 也可以使用user.options,这里使用其他方法绕过
	const isGadget = (name) => ['loaded', 'loading', 'ready'].includes( mw.loader.getState(`ext.gadget.${name}`) ),
		isBackup = isGadget('contentBackup'),
		backupObj = isBackup ? Object.fromEntries( mw.storage.getObject( 'LLWiki-contentBackup' ) || [] ) : {},
		backup = isBackup ? () => {
		backupObj[id] = [mw.now(), editor.getValue()];
		mw.storage.setObject( 'LLWiki-contentBackup', Object.entries(backupObj) );
	} : () => {},
		loadBackup = () => {
		const backupContent = (backupObj[id] || [])[1];
		if (!backupContent) {
			mw.notify(wgULS('当前页面尚未备份!', '目前的頁面尚未備份!'), {type: 'warn'});
			return;
		}
		mw.confirm( wgULS('要加载备份吗?', '要載入備份嗎?'), 'progressive' ).then(confirm => {
			if (!confirm) { return; }
			editor.setValue( backupContent );
			mw.notify(wgULS('已还原备份!', '已還原備份!'), {type: 'success'});
		});
	},
		pEmpty = ($div) => {
		$div.find( 'p:not(:has(script))' ).filter(function() { return !/\S/.test( this.textContent ); })
			.addClass('empty');
	},
		placeholder = [
		$('<div>', {class: "mw-parser-output"}).css('display', 'none'),
		$('<h1>', {id: 'firstHeading', class: 'firstHeading'}),
		$('<div>', {id: 'catlinks'}).css('display', 'none'),
		$('.mw-indicators')
	], // 用于替换时保留原始数据
		$warning = $('<div>', {html: [wgULS("确定要还原为未编辑的状态吗?", "確認要還原為未編輯的狀態嗎?"), '<br>',
			wgULS("建议您做好编辑内容备份。", "建議您做好編輯內容備份。")]}),
		$heading = $('#firstHeading'),
		$cat = $('#catlinks'),
		$indicator = $('.mw-indicator'),
		btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => {
		backup();
		// 改变CSS样式表示提交中
		btns[0].setDisabled( true );
		mw.safeEdit(api, {pageid: id, text: editor.getValue(), section: section,
			summary: (section === undefined ? '' : `/* ${secTitle} */ `) + (summary ? `${summary} //` : '//') +
				wgULS( '使用[[help:小工具/页面文本对比查看器|页面/文本对比查看器]]快速编辑',
				'使用[[help:小工具/页面文本对比查看器|頁面/文字對比檢視器]]快速編輯')}, isBackup)
			.then(() => { location.href = mw.util.getUrl( page ); },
			reason => { btns[0].setDisabled( reason == 'editConflict' ); }); // 编辑冲突必须刷新页面,不应重复提交
	}), new OO.ui.ButtonWidget({label: wgULS('预览', '預覽')}).on('click', () => {
		backup();
		// 改变CSS样式表示预览中
		btns[1].setDisabled( true );
		mw.timedParse(api, {text: editor.getValue(), uselang: lang,
			prop: 'text|categorieshtml|displaytitle|indicators|modules|headitems'
		}, wgULS('预览', '預覽')).then(r => {
			mw.loader.load( r.parse.modulestyles );
			const tabsStyles = r.parse.headitems.find(function(ele) { return ele.tag === 'TabsStyles'; });
			if (tabsStyles) {
				$('head').append(tabsStyles.content); // 可能重复,但重复也没关系
			}
			if ($.contains( $content[0], $original[0] )) {
				$original.after( placeholder[0] ).detach();
				$heading.after( placeholder[1] ).detach();
				$cat.after( placeholder[2] ).detach();
				$indicator.detach();
			}
			$content.children( '.mw-parser-output' ).replaceWith( r.parse.text );
			pEmpty( $content.children( '.mw-parser-output' ) );
			if (mw.resizeLyrics) { mw.resizeLyrics(); }
			mw.hook( 'wikipage.content' ).fire($content);
			$('#catlinks').replaceWith( r.parse.categorieshtml );
			placeholder[1].html( r.parse.displaytitle );
			placeholder[3].html( Object.entries( r.parse.indicators ).map(ele =>
				$('<div>', {class: 'mw-indicator', id: `mw-indicator-${ele[0]}`, html: ele[1]})) );
		}, () => {}).then(() => { btns[1].setDisabled( false ); });
	}), new OO.ui.ButtonWidget({label: wgULS('还原', '還原'), flags: 'destructive', disabled: true}).on('click', () => {
		mw.confirm($warning, ['primary', 'destructive']).then(confirm => {
			if (!confirm) { return; }
			section = undefined;
			editor.setValue( text );
			if ($.contains( $content[0], $original[0] )) { return; } // 无事发生
			$content.children( '.mw-parser-output' ).replaceWith( $original );
			placeholder[1].replaceWith( $heading );
			$('#catlinks').replaceWith( $cat );
			placeholder[3].html( $indicator );
			if (mw.resizeLyrics) { mw.resizeLyrics(); }
		});
	}), new OO.ui.ButtonWidget({label: wgULS('显示', '顯示')}).on('click', () => {
		css.disabled = !css.disabled;
		cssHide.disabled = !cssHide.disabled;
		$wrapper.toggle();
		editor.setSize();
		btns[3].setLabel( css.disabled ? wgULS('显示', '顯示') : wgULS('隐藏', '隱藏'));
		btns[2].setDisabled( css.disabled ); // 折叠时无法更新textarea
		if (mw.resizeLyrics) { mw.resizeLyrics(); }
	}), btnGo],
		options = $('<div>', {text: wgULS('查找替换', '尋找取代')}).click(() => {
		if ($replace) {
			$replace.show();
			return;
		}
		let replaceBackup;
		const enable = () => { replaceBtn.setDisabled( false ); },
			ptn = new OO.ui.TextInputWidget().on('change', enable),
			val = new OO.ui.MultilineTextInputWidget({autosize: true, maxRows: 3}).on('change', enable),
			regex = new OO.ui.CheckboxInputWidget().on('change', enable),
			modifier = new OO.ui.CheckboxInputWidget().on('change', enable),
			undoBtn = new OO.ui.ButtonWidget({label: wgULS('撤销', '復原'), disabled: true, flags: ['destructive']})
			.on('click', () => {
			editor.setValue( replaceBackup );
			enable();
		}),
			replaceBtn = new OO.ui.ButtonWidget({label: wgULS('替换', '取代'), flags: ['progressive']})
			.on('click', () => {
			const pattern = ptn.getValue(),
				regexp = new RegExp(regex.isSelected() ? pattern : mw.util.escapeRegExp(pattern),
				'g' + (modifier.isSelected() ? 'i' : ''));
			replaceBackup = editor.getValue();
			undoBtn.setDisabled( false );
			replaceBtn.setDisabled( true ); // 防止连续点击造成无法撤销
			editor.setValue( replaceBackup.replace(regexp, val.getValue()) );
		}),
			hideBtn = new OO.ui.ButtonWidget({label: wgULS('关闭', '關閉')})
			.on('click', () => {
			$replace.hide();
			enable();
		});
		$replace = $('<div>', {class: 'inspector-field', html: [
			$('<div>', {html: [wgULS('查找:', '尋找:' ), ptn.$element]}),
			$('<div>', {html: [wgULS('替换:', '取代:'), val.$element]}),
			$('<div>', {html: [
				new OO.ui.FieldLayout(regex, {label: wgULS('正则', '正規表達式'), align: 'inline'}).$element,
				new OO.ui.FieldLayout(modifier, {label: $('<i>', {text: 'i'}), align: 'inline'}).$element,
				replaceBtn.$element, undoBtn.$element, hideBtn.$element
			]})
		]}).appendTo( 'body' ).draggable();
	}).add(isGadget('charinsert') ? $('<div>', {text: '快速插入'}).click(() => {
		if ($charinsert) {
			$charinsert.show();
			return;
		}
		$charinsert = $('<div>', {html: $('<div>', {text: '快速插入工具', id: 'inspector-field-title'}),
			class: 'inspector-field mw-ajax-loader'}).appendTo('body').draggable().contextmenu(e => {
			e.preventDefault();
			$charinsert.hide();
		}).on('click', '.mw-charinsert-item', function() {
			const $this = $(this),
				start = $this.data('mw-charinsert-start') || $this.data('start') || '',
				end = $this.data('mw-charinsert-end') || $this.data('end') || '';
			editor.replaceSelection( start + editor.getSelection() + end );
			editor.focus();
		});
		// 为了充分利用浏览器缓存,这里不使用API;注意url转义,这里不需要简化'/mediawiki/index.php'
		$.get({url: mw.util.getUrl(src, {variant: mw.config.get( 'wgUserLanguage' )}),
			cache: charInsert.cache !== false, dataType: 'text'}).then(doc => {
			$charinsert.append( $( doc.match(/<body[\s\S]+<\/body>/)[0] ).find( '.mw-parser-output' ) );
		}, () => {
			mw.notify([
				wgULS('无法加载', '無法載入'),
				$('<a>', {text: src, href: mw.util.getUrl(src)}),
				wgULS('!请检查该页面是否存在。', '!請檢查此頁面是否存在。')
			], {type: 'error', autoHideSeconds: 'long'});
		}).then(() => { $charinsert.removeClass( 'mw-ajax-loader' ); });
	}) : null),
		$dropdown = $('<div>', {class: "inspector-menu", html: options}),
		$hints = $('<div>', {class: "inspector-menu"}).on('click', '.inspector-hint', function() {
		const cursor = editor.getCursor();
		editor.replaceRange(this.textContent + (ns > 0 ? '}}' : ']]'),
			{line: cursor.line, ch: cursor.ch - prefix.length + 1}, cursor);
		// 随后再次执行updateHints时会自行stop
		editor.focus();
	}),
		// 判别模板时可能会误处理解析器函数
		regexps = {0: /\[\[\s*:?([^|[\]{}<>]*)$/i, 10: /{{\s*([^|[\]{}<>#]*)$/i,
		828: /{{\s*#invoke:\s*([^|[\]{}<>#:]*)$/i, 274: /{{\s*#widget:\s*([^|[\]{}<>#:]*)$/i},
		$noHint = $('<span>', {class: 'error'}),
		nsPrefixes = {0: '', 10: 'Template:', 828: '模块:', 274: 'Widget:'},
		autocomplete = isGadget('autocomplete') ? e => {
		if (![9, 27].includes( e.keyCode )) { return; } // tab和esc
		let $cursor, before, sections, title;
		const cursor = editor.getCursor(),
			line = editor.getLine( cursor.line ),
			after = line.slice( cursor.ch );
		before = line.slice(0, cursor.ch);
		const cursorActivity = () => {
			const newCursor = editor.getCursor(),
				newLine = editor.getLine( newCursor.line ),
				newAfter = newLine.slice( newCursor.ch );
			// 如果光标改变过位置
			if (newCursor.line != cursor.line || newAfter != after) {
				stop();
				return;
			}
			before = newLine.slice(0, newCursor.ch);
			updateHints();
		},
			stop = () => {
			$hints.slideUp('fast');
			editor.off('cursorActivity', cursorActivity);
		},
			updateHints = mw.util.debounce(500, () => {
			if (!regexps[ns].test( before )) {
				stop();
				return;
			}
			$cursor = $wrapper.find( '.CodeMirror-cursor' );
			prefix = before.match( regexps[ns] )[1];
			if (prefix === "" || prefix.startsWith('#')) { return; }
			if (ns == '0' && prefix.includes('#')) {
				const capture = prefix.match(/^(.+)#(.*)$/);
				prefix = capture[2];
				(title == capture[1] ? Promise.resolve() : mw.timedQuery(api, {action: 'parse', page: capture[1],
					prop: 'sections'}, wgULS('段落标题', '段落標題')).then(r => {
					title = capture[1];
					sections = r.parse.sections.map(ele => $('<div>', {class: 'inspector-hint', text: ele.line}));
				}, () => { throw null; })).then(() => {
					$hints.html( sections.filter(ele => ele.text().startsWith( prefix )) )
						.show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
					if ($hints.children().length === 0) {
						$noHint.text(wgULS('不存在对应段落!', '不存在對應段落!')).appendTo( $hints );
					}
				}, stop);
				return;
			}
			mw.timedQuery(api, {list: 'prefixsearch', pssearch: prefix, psnamespace: ns},
				wgULS('前缀相符的页面', '前綴相符的頁面')).then(r => {
				$hints.html( r.query.prefixsearch.map(ele =>
					$('<div>', {class: 'inspector-hint', text: ele.title.slice( nsPrefixes[ns].length )})
				) ).show().position({my: 'left top', of: $cursor, within: $wrapper, collision: 'fit'});
				if ($hints.children().length === 0) {
					$noHint.text(wgULS('不存在对应页面!', '不存在對應頁面!')).appendTo( $hints );
				}
			}, stop);
		});
		if (e.keyCode == 27) {
			stop();
			return;
		}
		editor.execCommand( 'delCharBefore' ); // 删掉新增的\t
		$.each(regexps, (k, v) => { if (v.test(before)) { ns = k; } });
		if (!ns) { return; }
		mw.notify(wgULS('开启自动补全提示。', '開啟自動補全提示。'), {tag: 'autocomplete'});
		editor.on('cursorActivity', cursorActivity);
		// 实际上updateHints总是延迟500ms执行,因此下一行代码的位置影响不大
		updateHints();
	} : () => {};
	mw.loader.using(['jquery.ui', 'user.options']).then(() => {
		const actionP = new OO.ui.ActionWidget({label: '是', target: '_blank', flags: 'progressive'}),
			actionD = new OO.ui.ActionWidget({label: '否'}),
			$url = $('<a>'), // 桌面版CSS不必需href
			label = [wgULS('要在新标签页打开', '要在新分頁打開'), $url, wgULS('吗?', '嗎?')];
		$outer.on('contextmenu', '.cm-mw-template-name', function() {
			const template = '模板:' + $(this).text();
			// 这里增加一次确认界面是因为部分浏览器不允许JS直接打开新标签页。
			$url.text( template );
			actionP.setHref( mw.util.getUrl(template) ); // 注意url转义
			mw.dialog(dialog, [actionD, actionP], label).catch(() => {});
			return false; // 这里需要同时执行stopImmediatePropagation()和preventDefault()
		}).on('contextmenu', '.CodeMirror', e => {
			e.preventDefault();
			// 使用jquery.ui让菜单总是落在CodeMirror内,此时slideDown效果不生效
			$dropdown.show().position({my: 'left top', of: e, within: $wrapper, collision: 'fit'});
		}).on('keydown', '.CodeMirror', autocomplete).resizable( {handles: 'w', minWidth: 350} );
	});
	$('body').click(() => { $dropdown.slideUp('fast'); });
	btns[0].$element.contextmenu(e => {
		e.preventDefault();
		OO.ui.prompt( wgULS('请输入编辑摘要:', '請輸入編輯摘要:') , {textInput: {value: summary}} ).then(input => {
			if (input !== null) { summary = input; }
		});
	});
	if (isBackup) {
		btns[2].$element.contextmenu(e => {
			e.preventDefault();
			loadBackup();
		});
	}
	if (isGadget( 'PreviewWithVariant' ) ) {
		const options = [{label: "大陆简体", data: "zh-cn"}, {label: "香港繁體", data: "zh-hk"},
			{label: "澳門繁體", data: "zh-mo"}, {label: "大马简体", data: "zh-my"},
			{label: "新加坡简体", data: "zh-sg"}, {label: "臺灣正體", data: "zh-tw"}],
			select = new OO.ui.DropdownInputWidget({classes: ['inspector-variant'], options: options, value: lang})
			.on('change', () => { lang = select.getValue(); }),
			menu = select.$element.find( '.oo-ui-menuSelectWidget' )
			.click(() => { menu.addClass( 'oo-ui-element-hidden' ); });
		select.$element.prependTo( btns[1].$element );
		btns[1].$element.contextmenu(e => {
			e.preventDefault();
			menu.removeClass( 'oo-ui-element-hidden' );
		}).children('a').blur(() => { menu.addClass( 'oo-ui-element-hidden' ); });
	}
	pEmpty( $original );
	// $.when很容易出错,这里保险起见用Promise.all
	Promise.all([getJSON, mw.request, getExt]).then(data => {
		mw.hook( 'codemirror.config' ).fire();
		text = data[1].query.pages[0].revisions[0].content;
		editor = new CodeMirror($outer[0], {value: text, mode: 'text/mediawiki',
			mwConfig: mw.config.get('extCodeMirrorConfig'), lineWrapping: true, lineNumbers: true
		});
		$wrapper = $( editor.getWrapperElement() ).toggle().append( [$dropdown, $hints] );
		$('<div>', {id: 'inspector-btns', html: btns.map(ele => ele.$element)}).appendTo( $outer );
		// 处理页面上方的差异
		$('.diff').click(e => {
			const row = $( e.target ).closest('tr');
			if (row.hasClass( 'diff-title' )) { return; }
			const isLineno = row.children( '.diff-lineno' ).length ? 1 : 0,
				rowLineno = isLineno ? row : row.prevAll( ':has(.diff-lineno)' ).first(),
				n = parseInt( rowLineno.children().last().text().match(/\d+/) ) + row.index() - rowLineno.index()
				- 2 + isLineno, // 使用parseInt规避可能的程序错误,注意CodeMirror的行号从0开始
				nLine = editor.lastLine();
			if (nLine >= n) {
				editor.scrollIntoView(nLine);
				editor.scrollIntoView(n);
			}
			else { mw.notify( wgULS('当前不存在该行!', '目前不存在此行!'), {type: 'warn'} ); }
		});
	}, reason => { mw.apiFailure(reason, `CodeMirror${wgULS('设置或页面', '設定或頁面')}Wikitext`); })
		.then(() => { $outer.removeClass( 'mw-ajax-loader' ); });
});
//</nowiki>
// [[category:维护工具]] [[category:桌面版小工具]] [[category:需要自确用户权限的小工具]] [[category:不作为模块的小工具]]
// {{DEFAULTSORT:inspect.js}}