/**
* Copyright (c) 2020-present, 安忆.
*
* @author 安忆 [[zh:U:安忆]]
* @file Difflink.js
* @license GPL v3
*/
/**
* 如想自定义复制结果中的文本可通过向自己的common.js中加入以下内容:
* window.difflink = ['版本差异', '固定版本'];
* 如想使用默认值1,但自定值2,请将值1留空,如下例:
* window.difflink = ['', '固定版本'];
*/
(function diffLink() {
var $body = $(document).find('body');
var isRevisionPage = $body.find('#contentSub #mw-revision-nav').length === 1 || $body.find('main#content>.pre-content #mw-revision-nav').length === 1;
if (!(isRevisionPage || $body.find('table').hasClass('diff'))) {
return;
}
mw.loader.using(['ext.gadget.site-lib', 'mediawiki.api', 'mediawiki.util', 'mediawiki.widgets', 'oojs-ui-windows']).then(function () {
var defaultTextArray = [wgULS('版本差异', '版本差異'), '固定版本'];
if (typeof window.difflink !== 'undefined' && Object.prototype.toString.call(window.difflink) === '[object Array]') {
if (window.difflink[0] !== '' && Object.prototype.toString.call(window.difflink[0]) === '[object String]') {
defaultTextArray[0] = window.difflink[0];
}
if (window.difflink[1] !== '' && Object.prototype.toString.call(window.difflink[1]) === '[object String]') {
defaultTextArray[1] = window.difflink[1];
}
}
var isMinerva = mw.config.get('skin') === 'minerva';
var portletId = 'p-cactions';
if ($body.hasClass('mw-special-MobileDiff')) {
portletId = 'mw-mf-diffarea';
} else if (isMinerva) {
portletId = 'p-tb';
}
var ins = function ins(_ref) {
var text = _ref.text,
tooltip = _ref.tooltip,
link = _ref.link,
defaultTextArrayIndex = _ref.defaultTextArrayIndex,
isPermaLink = _ref.isPermaLink;
var _element = document.getElementById('t-difflink');
if (!_element) {
_element = mw.util.addPortletLink(portletId, '#', text, 't-difflink', tooltip);
if (!_element) {
return;
}
}
var element = _element.querySelector('a');
if (!element) {
return;
}
if (isMinerva) {
var style = document.createElement('style');
style.id = 'css-difflink';
style.appendChild(document.createTextNode('.mw-ui-icon-portletlink-t-difflink{background-image:url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2220%22%20height%3D%2220%22%20viewBox%3D%220%200%2020%2020%22%3E%3Cg%20fill%3D%22%2354595d%22%3E%3Cpath%20d%3D%22M4.83%2015h2.91a4.88%204.88%200%2001-1.55-2H5a3%203%200%20110-6h3a3%203%200%20012.82%204h2.1a4.82%204.82%200%2000.08-.83v-.34A4.83%204.83%200%20008.17%205H4.83A4.83%204.83%200%20000%209.83v.34A4.83%204.83%200%20004.83%2015z%22%2F%3E%3Cpath%20d%3D%22M15.17%205h-2.91a4.88%204.88%200%20011.55%202H15a3%203%200%20110%206h-3a3%203%200%2001-2.82-4h-2.1a4.82%204.82%200%2000-.08.83v.34A4.83%204.83%200%200011.83%2015h3.34A4.83%204.83%200%200020%2010.17v-.34A4.83%204.83%200%200015.17%205z%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)}'));
document.head.appendChild(style);
}
if (portletId === 'mw-mf-diffarea') {
$('#css-difflink').html($('#css-difflink').html() + '#t-difflink{float:right}#t-difflink>a>span:first-child{vertical-align:text-bottom;padding-right:.5em}');
}
element.onclick = function (event) {
event.preventDefault();
var $element = $('<div>'),
hash = isPermaLink ? decodeURIComponent(location.hash) : '';
[link, '[[' + link + hash + ']]', '[[' + link + hash + '|' + defaultTextArray[defaultTextArrayIndex] + ']]'].forEach(function (value) {
$element.append(new mw.widgets.CopyTextLayout({
align: 'top',
copyText: value
}).$element);
});
/android|iphone|mobile/i.test(navigator.userAgent) ? OO.ui.alert($element) : OO.ui.alert($element, {
size: 'medium'
});
};
};
var init = function init(_ref2) {
var diffId = _ref2.diffId,
oldId = _ref2.oldId,
revisionId = _ref2.revisionId;
if (diffId) {
var buildLink = function buildLink(_oldId) {
var link = 'Special:Diff/';
if (_oldId) {
link += _oldId + '/';
}
link += diffId;
ins({
text: wgULS('当前差异链接', '當前差異連結'),
tooltip: wgULS('复制链接到当前差异版本的维基语法', '複製連結到當前差異版本的維基語法'),
link: link,
defaultTextArrayIndex: 0
});
};
buildLink(oldId);
if (oldId) {
new mw.Api().get({
action: 'compare',
format: 'json',
formatversion: '2',
prop: 'ids',
fromrev: diffId,
torelative: 'prev'
}).then(function (response) {
if (diffId === mw.config.get('wgDiffNewId') && response.compare && response.compare.fromrevid === mw.config.get('wgDiffOldId')) {
buildLink(false);
}
});
}
} else if (revisionId && isRevisionPage) {
ins({
text: wgULS('当前修订链接', '當前修訂連結'),
tooltip: wgULS('复制链接到当前修订版本的维基语法', '複製連結到當前修訂版本的維基語法'),
link: 'Special:PermaLink/' + revisionId,
defaultTextArrayIndex: 1,
isPermaLink: true
});
}
};
mw.hook('wikipage.content').add(function ($content) {
if ($content.attr('id') !== 'mw-content-text') {
return;
}
init({
diffId: mw.config.get('wgDiffNewId'),
oldId: mw.config.get('wgDiffOldId'),
revisionId: mw.config.get('wgRevisionId')
});
});
});
})();