|
|
(未显示同一用户的8个中间版本) |
第1行: |
第1行: |
− | /**
| + | mw.loader.load('https://cdn.jsdelivr.net/npm/mediawiki-inpageedit@latest/dist/InPageEdit.min.js'); |
− | * MediaWiki JS Plugin: In Page Edit
| + | mw.hook('InPageEdit').add(function (ctx) { |
− | * Author: 机智的小鱼君
| + | var InPageEdit = ctx.InPageEdit, |
− | * Url: https://github.com/Dragon-Fish/wjghj-wiki/edit/master/Gadgets/in-page-edit
| + | _msg = ctx._msg, |
− | * Description: Let you edit page without open new tab. And edit Navebox via navbar, edit section via section edit link etc.
| + | wgPageName = mw.config.get('wgPageName'), |
− | **/
| + | wgRevisionId = mw.config.get('wgRevisionId'); |
− | | + | $('#ca-edit').after( |
− | function InPageEdit(option) {
| + | $('<li>', { |
− | // 只能存在一个窗口
| + | id: 'ca-quick-edit', |
− | if ($('#InPageEdit').length > 0) {
| + | 'class': 'collapsible' |
− | Modal('已存在一个编辑器<br/><button data-action="closeModal">好的</button>','<span class="error">InPageEdit发生错误</span>');
| + | }).append( |
− | return;
| + | $('<span>').append( |
− | }
| + | $('<a>', { |
− | | + | href: 'javascript:void(0)', |
− | // Variables
| + | text: _msg('quick-edit') |
− | var origintext, | + | }).on('click', function () { |
− | inPageEditTarget = option.target,
| + | InPageEdit.quickEdit({ |
− | inPageEditSection = option.section,
| + | page: wgPageName, |
− | inPageEditReason = option.reason,
| + | revision: wgRevisionId |
− | inPageEditTags = option.tags,
| + | }); |
− | inPageEditRefresh = option.refresh;
| |
− | if (inPageEditTarget === undefined || inPageEditTarget === ''){inPageEditTarget = mw.config.get('wgPageName')}else{inPageEditTarget = decodeURIComponent(inPageEditTarget)}
| |
− | if (inPageEditSection === undefined || inPageEditSection === ''){inPageEditSection = 'none'}
| |
− | if (inPageEditReason === undefined || inPageEditReason === ''){inPageEditReason = ''}
| |
− | if (inPageEditRefresh === undefined || inPageEditRefresh == 'true' || inPageEditRefresh == '1'){inPageEditRefresh = true;}
| |
− | if (inPageEditTags === undefined || inPageEditTags === ''){inPageEditTags = ''}
| |
− | | |
− | // Debug
| |
− | console.info('[InPageEdit]\n'+'ninPageEditTarget = ' + inPageEditTarget + '\n' + 'inPageEditSection = ' + inPageEditSection + '\n' + 'inPageEditReason = ' + inPageEditReason + '\n' + 'inPageEditTags = ' + inPageEditTags + '\n' + 'inPageEditRefresh = ' + inPageEditRefresh);
| |
− | | |
− | // 开始执行任务
| |
− | $('body').addClass('action-in-page-edit');
| |
− | // Create area
| |
− | Modal(
| |
− | '<div id="InPageEdit">' +
| |
− | '<textarea id="newcontent" style="font-size:18px; width:100%; min-height:380px; max-height:1200px" disabled="">正在读取页面数据,请稍候…</textarea>' +
| |
− | | |
− | '<div id="button-area">' + '<span id="normal"><button id="cancle-btn">取消</button> <button id="preview-btn">预览</button> <label><input type="checkbox" id="is-minor"/> 小编辑</label></span> <div style="float:right"><input id="reason" placeholder="编辑摘要"> <button id="submit-btn">提交</button></div>' +
| |
− | '<div id="preview-area" class="" style="margin-top:18px; padding:8px; border:1px dotted #aaa; display: none"></div>' +
| |
− | | |
− | '</div>','<span id="inPageEdit-edit-title">加载中…</span>',{closeBtn:false,disableBg:true,addClass:'inPageEditModal'});
| |
− | | |
− | if (inPageEditSection === 'none') {
| |
− | varGet = {
| |
− | action: "parse",
| |
− | page: inPageEditTarget,
| |
− | prop: "wikitext",
| |
− | format: "json"
| |
− | }
| |
− | Section = ''
| |
− | } else {
| |
− | varGet = {
| |
− | action: "parse",
| |
− | page: inPageEditTarget,
| |
− | section: inPageEditSection,
| |
− | prop: "wikitext",
| |
− | format: "json"
| |
− | } | |
− | Section = '#'+inPageEditSection
| |
− | }
| |
− |
| |
− | new mw.Api().get(varGet).then(function(data) {
| |
− | origintext = data.parse.wikitext['*'];
| |
− | ajaxArea()
| |
− | }).fail(function() {
| |
− | origintext = '<!-- ⚠ 警告:无法获取页面内容,新建页面请删除此行。 -->\n';
| |
− | console.error('[InPageEdit] Can’t get page content.');
| |
− | ajaxArea()
| |
− | });
| |
− | function ajaxArea() { | |
− | | |
− | $('#InPageEdit #reason').val(inPageEditReason);
| |
− | $('#InPageEdit #newcontent').val(origintext).attr('disabled',false);
| |
− | $('#inPageEdit-edit-title').html('正在编辑:' + decodeURIComponent(inPageEditTarget)+Section);
| |
− |
| |
− | // Cancle
| |
− | $('.inPageEditModal').each(function(){ | |
− | var $this = $(this); | |
− | $this.find('#cancle-btn').attr('data-ipe-id',$this.attr('data-modalid'));
| |
− | });
| |
− | $('#InPageEdit #cancle-btn').click(function() {
| |
− | var confirmCancel = confirm('确定要取消吗?');
| |
− | if(confirmCancel) {
| |
− | $('body').removeClass('action-in-page-edit');
| |
− | closeModal($(this).attr('data-ipe-id'));
| |
− | } | |
− | });
| |
− |
| |
− | // Preview
| |
− | $('#InPageEdit #preview-btn').click(function() {
| |
− | new mw.Api().post({
| |
− | action: "parse",
| |
− | text: $('#InPageEdit #newcontent').val(),
| |
− | prop: "text",
| |
− | preview: true,
| |
− | format: "json"
| |
− | }).then(function(data) {
| |
− | var previewcontent = data.parse.text['*'];
| |
− |
| |
− | $('#InPageEdit #preview-area').show(400).html(previewcontent);
| |
− | });
| |
− | }); | |
− |
| |
− | // Submit
| |
− | $('#InPageEdit #submit-btn').click(function() {
| |
− | var confirmSubmit = confirm('确定发布编辑吗?'); | |
− | if (confirmSubmit) {
| |
− | // Hide elements
| |
− | $('#InPageEdit #newcontent').attr('readonly','readonly');
| |
− | $('#InPageEdit #button-area #normal').hide();
| |
− | $('#InPageEdit #submit-btn').hide().after('<button disabled="" id="fake-button">正在提交 <span id="spinner"></span></button>'); | |
− |
| |
− | // Do post request
| |
− | var isMinor = $('#InPageEdit #is-minor').prop('checked');
| |
− | if (inPageEditSection === 'none') {
| |
− | varSubmit = { | |
− | action: 'edit',
| |
− | text: $('#InPageEdit #newcontent').val(),
| |
− | title: inPageEditTarget,
| |
− | minor: isMinor,
| |
− | tags: inPageEditTags,
| |
− | summary: $('#InPageEdit #reason').val(),
| |
− | token: mw.user.tokens.get('editToken')
| |
− | }
| |
− | } else {
| |
− | varSubmit = { | |
− | action: 'edit',
| |
− | text: $('#InPageEdit #newcontent').val(),
| |
− | title: inPageEditTarget,
| |
− | section: inPageEditSection,
| |
− | minor: isMinor,
| |
− | tags: inPageEditTags,
| |
− | summary: $('#InPageEdit #reason').val(),
| |
− | token: mw.user.tokens.get('editToken')
| |
− | }
| |
− | } | |
− | new mw.Api().post(varSubmit).done(function() {
| |
− | $(''#InPageEdit #fake-button').html('发布成功,正在刷新页面……');
| |
− | window.location.reload();
| |
− | }).fail(function(){
| |
− | // Show elements | |
− | $('#InPageEdit #submit-btn').html('重试').show();
| |
− | $('#InPageEdit #newcontent').attr('readonly',false);
| |
− | $('#InPageEdit #fake-button').remove();
| |
− | $('#InPageEdit #button-area, #InPageEdit #button-area #normal').show();
| |
− | Modal('发布编辑时出现错误,建议复制内容后使用常规编辑器保存您的编辑。<br/><button data-action="closeModal">好的</button>','<span class="error">InPageEdit发生错误</span>');
| |
− | });
| |
− | }
| |
− | });
| |
− | }
| |
− | }
| |
− |
| |
− | /** Add button **/
| |
− | $(function() {
| |
− | if (wgIsArticle === false) {
| |
− | console.info('[InPageEdit] Not article page, plugin shut down.');
| |
− | return;
| |
− | }
| |
− | $('.action-view #p-userpagetools ul, #p-views .mw-portlet-body ul').append($('<li>').append($('<a>').addClass('in-page-edit-btn-link').attr('href', 'javascript:void(0)').text('快速编辑').click(function() {
| |
− | InPageEdit({target:mw.config.get('wgPageName'), reason:' //InPageEdit', tags:'in-page-edit'})
| |
− | })));
| |
− | });
| |
− | /** Get links in ariticle **/
| |
− | $(function() {
| |
− | $('#mw-content-text a:not(.new)').each(function(i) {
| |
− | if ($(this).attr('href') === undefined) return;
| |
− | var url = $(this).attr('href');
| |
− | params = {};
| |
− | var vars = url.split('?').pop().split("&");
| |
− | for (var i=0;i<vars.length;i++) {
| |
− | var pair = vars[i].split("=");
| |
− | params[pair[0]] = pair[1];
| |
− | }
| |
− |
| |
− | // Not edit link of this wiki
| |
− | if (url.split('/')['2'] !== location.href.split('/')['2'] && url.substr(0, 1)!=='/') return;
| |
− | // Not url start with 'index.php?title=FOO'
| |
− | if (params.title === undefined) params.title = url.split('com/').pop().split('?')['0'];
| |
− | if (params.section === undefined) params.section = 'none';
| |
− | | |
− | var target = params.title,
| |
− | section = params.section;
| |
− |
| |
− | if (params.action === 'edit' && target !== undefined && section !== 'new') {
| |
− | $(this).after(
| |
− | $('<a>',{
| |
− | 'href': 'javascript:void(0)',
| |
− | 'class': 'in-page-edit-article-link'
| |
| }) | | }) |
− | .text('快速编辑')
| + | ) |
− | .click(function (){
| + | ) |
− | if (section === 'none') {
| + | ); |
− | InPageEdit({target:target, reason:' //InPageEdit', tags:'in-page-edit|in-page-edit-outer'});
| |
− | } else {
| |
− | InPageEdit({target:target, reason:' //InPageEdit - Section'+section, section: section, tags:'in-page-edit|in-page-edit-outer'});
| |
− | }
| |
− | }
| |
− | )); | |
− | } | |
− | });
| |
− | $('.mw-editsection .in-page-edit-article-link').before(' | '); | |
− | $('.in-page-edit-article-link:not(.mw-editsection)').before('[').after(']');
| |
| }); | | }); |