Site Notice

hello, world

InPageEdit-v2/SDK

From Project-EPB Commons
< InPageEdit-v2
Revision as of 09:47, 10 April 2020 by 机智的小鱼君 (talk | contribs) ([InPageEdit] 没有编辑摘要)

快速编辑

InPageEdit.edit(options)

  • options: {object}
    • page: 必须;编辑的页面名
    • revision: 修订版本号,如果与当前页面版本号不同则取代
    • section: 编辑的段落编号

最基础的调用

InPageEdit.edit({
  page: mw.config.get('wgPageName')
});

<html><button onclick="InPageEdit.edit({

 page: mw.config.get('wgPageName')

});">测试</button></html>

快速重定向

InPageEdit.redirect(type)

  • type: "string"(to/from)
    • from 重定向页面至此
    • to 将此页面重定向到

最基础的调用

InPageEdit.redirect('from');
InPageEdit.redirect('to');

<html><button onclick="InPageEdit.redirect('from')">测试from</button> <button onclick="InPageEdit.redirect('to')">测试to</button></html>

快速删除

InPageEdit.deletepage(page)

  • page "string"
    • 要删除的页面,预设为当前页面

快速重命名

InPageEdit.renamepage()

  • 打开快速重命名窗口

偏好设置

InPageEdit.preference()

  • 打开InPageEdit偏好设置窗口

<html><button onclick="InPageEdit.preference()">测试</button></html>

比较差异

InPageEdit.quickDiff(options)

  • options: {object}
    • fromtitle: First title to compare.
    • fromid: First page ID to compare. Type: integer
    • fromrev: First revision to compare. Type: integer
    • fromtext: Use this text instead of the content specified by fromtitle, fromid or fromrev.
    • frompst: Do a pre-save transform on fromtext. Type: boolean
    • totitle: Second title to compare.
    • toid: Second page ID to compare. Type: integer
    • torev: Second revision to compare. Type: integer
    • torelative: Compare to a relative revision of fromtitle, fromid or fromrev. All the other "to" options will be ignored. Possible values: prev, next, cur
    • totext: Use this text instead of the content specified by totitle, toid, torev, or torelative.
    • topst: Do a pre-save transform on totext. Type: boolean

最简单的示例,比较本页面最后一次修改

InPageEdit.quickDiff({
  fromtitle: mw.config.get('wgPageName'),
  torelative: 'prev'
});

<html><button onclick="InPageEdit.quickDiff({

 fromtitle: mw.config.get('wgPageName'),
 torelative: 'prev'

});">测试</button></html>

添加编辑链接

该模块在页面加载时将运行一次[1]

InPageEdit.articleLink(element)

  • element: <element>
    • 缺省值:$('#mw-content-text a:not(.new)')
    • 将该链接作为编辑链接,识别并添加快速编辑链接

无示例

加载框

不建议使用[2]

InPageEdit.progress(title)

  • title: "string",√boolean×
    • true: 将顶层加载框状态切换为已完成
    • false: 关闭顶层的加载框
    • string: 以字符串为标题创建加载框
function testProgress() {
  InPageEdit.progress('正在测试加载框……');
  setTimeout(function(){
    InPageEdit.progress(true);
    setTimeout(function(){
      InPageEdit.progress(false);
    },3000);
  },5000);
}

<html><button onclick="testProgress()">测试</button> <script> function testProgress() {

 InPageEdit.progress('正在测试加载框……');
 setTimeout(function(){
   InPageEdit.progress(true);
   setTimeout(function(){
     InPageEdit.progress(false);
   },3000);
 },5000);

} </script></html>

更新日志

Wjghj Project 的服务器不是高带宽高并发服务器,请不要过于频繁调用此模块[3]

InPageEdit.versionInfo()

  • 调出更新日志的弹窗,可以在不打开Wjghj Common Wiki的情况下查看更新日志
    • <html><button onclick="InPageEdit.versionInfo()">测试</button></html>

版本号

请勿改写此变量;这是一个变量,不是一个函数

InPageEdit.version

  • 获取版本号
    • Response: 字符串 → <html><span onclick="$(this).text(InPageEdit.version)">获取</span></html>

快速编辑工具条自定义按钮

可以自定义需要的按钮,按以下提示在个人js页面写入全局变量window.InPageEdit.buttons即可,类型为Array。

window.InPageEdit = window.InPageEdit || {}; // 务必在之前添加这一行否则会报错
InPageEdit.buttons =[{
  open: '<-- ', // 添加到光标前的内容
  middle: '注释文字', // 选区内容占位符,选填
  close: ' -->', // 添加到光标后的内容
  text: '<span class="material-icons">description</span>' // 按钮文字,被解析为html
}, { // 再举个栗子
  open: '<s>',
  middle: '删除线',
  close: '</s>',
  text: '<span class="material-icons">strikethrough_s</span>'
}];

实例演示

通过IPE的扩展接口,您可以制作很多自定义插件,这里提供一些简单、实用的代码实例

IPE原版工具盒

这里是IPE原版右下角工具盒的源代码,只要您明白了其中的原理,便完全可以自由自定义一个工具盒

原版代码
/** 
 * InPageEdit Toolbox
 * Example extension for InPageEdit-v2,
 * Default loaded for everyone.
 **/
mw.hook('InPageEdit').add(function () {
  // Not article
  if (mw.config.get('wgIsArticle') === false) {
    return;
  }

  /** 
   * @param{msg} type: function - this is the function for load i18n-js strings
   ** You can use normally strings insteed.
   **/
  $('<div>', { id: 'ipe-edit-toolbox' }).append(
    $('<ul>', { class: 'btn-group group1' }).append(
      $('<li>', { class: 'btn-tip-group' }).append(
        $('<div>', { class: 'btn-tip', text: msg('quick-edit') }),
        $('<button>', { id: 'edit-btn', class: 'ipe-toolbox-btn material-icons', text: 'edit' })
      ),
      $('<li>', { class: 'btn-tip-group' }).append(
        $('<div>', { class: 'btn-tip', text: msg('redirect-from') }),
        $('<button>', { id: 'redirectfrom-btn', class: 'ipe-toolbox-btn material-icons', text: 'flight_land' })
      ),
      $('<li>', { class: 'btn-tip-group' }).append(
        $('<div>', { class: 'btn-tip', text: msg('redirect-to') }),
        $('<button>', { id: 'redirectto-btn', class: 'ipe-toolbox-btn material-icons', text: 'flight_takeoff' })
      )
    ),
    $('<ul>', { class: 'btn-group group2' }).append(
      $('<div>', { style: 'display: flex;' }).append(
        $('<li>', { class: 'btn-tip-group' }).append(
          $('<div>', { class: 'btn-tip', text: msg('quick-delete') }),
          $('<button>', { id: 'deletepage-btn', class: 'ipe-toolbox-btn material-icons', text: 'delete_forever' })
        ),
        $('<li>', { class: 'btn-tip-group' }).append(
          $('<div>', { class: 'btn-tip', text: msg('quick-rename') }),
          $('<button>', { id: 'renamepage-btn', class: 'ipe-toolbox-btn material-icons', text: 'format_italic' })
        ),
        $('<li>', { class: 'btn-tip-group' }).append(
          $('<div>', { class: 'btn-tip', text: msg('ipe-preference') }),
          $('<button>', { id: 'preference-btn', class: 'ipe-toolbox-btn material-icons', text: 'settings' })
        )
      )
    ),
    $('<button>', { class: 'ipe-toolbox-btn material-icons', id: 'toolbox-toggle', text: 'add' })
  ).appendTo('body');
  $('#ipe-edit-toolbox #toolbox-toggle').click(function () {
    $('#ipe-edit-toolbox #toolbox-toggle, #ipe-edit-toolbox .btn-group').toggleClass('opened');
  });
  $('body > *:not(#ipe-edit-toolbox)').click(function () {
    $('#ipe-edit-toolbox #toolbox-toggle, #ipe-edit-toolbox .btn-group').removeClass('opened');
  });
  $('#ipe-edit-toolbox .btn-group .ipe-toolbox-btn').click(function () {
    InPageEdit.analysis({ type: 'functionCount', function: '工具盒' });
    switch ($(this).attr('id')) {
      case 'edit-btn':
        InPageEdit.quickEdit({
          page: mw.config.get('wgPageName'),
          revision: mw.config.get('wgRevisionId')
        });
        break;
      case 'redirectfrom-btn':
        InPageEdit.quickRedirect('from');
        break;
      case 'redirectto-btn':
        InPageEdit.quickRedirect('to');
        break;
      case 'preference-btn':
        InPageEdit.preference();
        break;
      case 'deletepage-btn':
        InPageEdit.quickDelete();
        break;
      case 'renamepage-btn':
        InPageEdit.quickRename();
        break;
    }
  });
  mw.hook('InPageEdit.toolbox').fire();
});

在编辑链接后添加快速编辑链接

此示例代码已在Vector、Timeless以及Hydra皮肤上测试可用

源代码
mw.hook('InPageEdit').add(function(){
  $('#ca-view').after(
    $('<li>',{
      id:'ca-quick-edit',
      class:'collapsible'
    }).append(
      $('<span>').append(
        $('<a>',{
          href: 'javascript:void(0)'
        })
        .text('快速编辑')
        .click(function(){
          InPageEdit.edit({
            page: mw.config.get('wgPageName'),
            revision: mw.config.get('wgRevisionId')
          });
        })
      )
    )
  );
});

注释

  1. 当页面为文章页时,会以以下参数自动运行一次: InPageEdit.articleLink($('#mw-content-text a:not(.new)'))
  2. 这是一个内部模块,用于在特定步骤遮挡屏幕阻止用户的其他操作,不应该单独使用。
    如果您喜欢IPE风格的progress-bar,请使用该部件:
    <div class="ipe-progress" style="width:可规定一个长度"><div class="ipe-progress-bar"></div></div>
  3. 每调用一次该模块,就会向Wjghj_Project的服务器发送一次需要处理的get请求,这会给服务器带来一定的负荷