Site Notice

hello, world

MediaWiki:Js-InPageEdit-v2.js

From Project-EPB Commons
Revision as of 22:31, 16 October 2019 by 机智的小鱼君 (talk | contribs)

Invoke this: https://common.wjghj.cn/js/{{#replace:{{#replace:Js-InPageEdit-v2.js|Js-|}}|.js|}}

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
 *『Wjghj Project Static』
 * This _JavaScript_ code is from https://common.wjghj.cn
 * CC BY-NC-SA
 **/
/** Modal plugin **/
mw.loader.load('https://common.wjghj.cn/js/SsiModal');

/** Main **/
function InPageEdit(option) {

  // Variables
  var editPage = option.page,
      editSection = option.section,
      editReversion = option.reversion,
      editText,
      editSummary = option.summary,
      jsonPreview = {},
      jsonPost = {};

  // Show modal
  ssi_modal.show({
    title: '正在编辑:<span id="editPage" style="font-style:normal;">ipe-editPage-placeholder</span>',
    content: '<textarea id="editArea" style="max-width:100%;min-width:100%;min-height:350px">正在加载……</textarea><label>摘要:<input id="editSummary"/></label><br/><label><input id="editMinor" type="checkbox" style="margin-left:0;margin-right:4px"/>小编辑</label>',
    keepContent: false,
    beforeClose: function(modal) {
      ssi_modal.confirm({
        position: 'top center',
        content: '确定要关闭窗口吗',
        okBtn: {
          className: 'btn btn-primary',
          label: '确定'
        },
        cancelBtn: {
          className: 'btn btn-danger',
          label: '取消'
        }
      },
      function(result) {
        if (result === true) {
          modal.options.keepContent = false;
          modal.options.beforeClose = '';
          modal.close();
          ssi_modal.notify('error', {
            position: 'right top',
            content: "编辑被取消。"
          })
        }
      });
      return false;
    },
    buttons: [{
      label: '预览',
      method: function (){
        var text = $('#editArea').val();
        requestPreview(text)
      }
    },
    {
      label: '发布',
      method: function() {
        ssi_modal.confirm({
          content: '是否发布',
          okBtn: {
            label: '确定'
          },
          cancelBtn: {
            label: '取消'
          },
        },
        function(result) {
          if (result) {
            var text = $('#editArea').val();
            postArticle(text,editPage);
          }
        })
      }
    }]
  });
  new mw.Api().get({
    action: 'parse',
    page: editPage,
    prop: 'wikitext',
    format: 'json'
  }).then(function(data){
    if (data.error === undefined) {
      editText = data.parse.wikitext['*']
    } else {
      editText = '<!-- 警告:无法获取页面内容 -->';
      console.error('[InPageEdit]警告:无法获取页面内容/兼容');
    }
    $('#editArea').val(editText);
  }).fail(function(data){
    editText = '<!-- 警告:无法获取页面内容 -->';
    console.error('[InPageEdit]警告:无法获取页面内容/常规');
    $('#editArea').val(editText);
  });

  function requestPreview(text){
    new mw.Api().post({
      action: "parse",
      text: text,
      prop: "text",
      preview: true,
      format: "json"
    }).then(function(data){
      var content = data.parse.text['*'];
      ssi_modal.show({content:content});
    });
  }
  
  function postArticle(text,page){
    new mw.Api().post({
      action: "edit",
      title: page,
      text: text,
      token: mw.user.tokens.get('editToken')
    }).then(function(data){
      ssi_modal.show({content:'正在刷新页面。'});
      window.location.reload();
    }).fail(function(data){
      ssi_modal.show({content:'发生未知错误。'});
    });
  }
  
}

/** 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({page:mw.config.get('wgPageName'), summary:' //InPageEdit'})
  })));
});
/** 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({page:target, summary:' //InPageEdit'});
          } else {
            InPageEdit({page:target, summary:' //InPageEdit - Section'+section});
          }
        }
      ));
    }
  });
  $('.mw-editsection .in-page-edit-article-link').before(' | ');
  $('.in-page-edit-article-link:not(.mw-editsection)').before('[').after(']');
});