「MediaWiki:Gadget-PrettyDiff.js」の版間の差分
ナビゲーションに移動
検索に移動
(版間の差分をdiff-match-patchで表示する) |
(ライブプレビューの差分で最初の1回しか動作しない問題の修正) |
||
25行目: | 25行目: | ||
function prettyDiff() { | function prettyDiff() { | ||
var $table = $(this); | var $table = $(this); | ||
if ($table. | if ($table.find('> tbody > tr.pretty-diff').length > 0) return; | ||
var dmp = new diff_match_patch(); | var dmp = new diff_match_patch(); | ||
var $lineno = $table.find('> tbody > tr:has(.diff-lineno)'); | var $lineno = $table.find('> tbody > tr:has(.diff-lineno)'); | ||
37行目: | 36行目: | ||
.map(function() { return $(this).text(); }).get().join('\n'); | .map(function() { return $(this).text(); }).get().join('\n'); | ||
$('<tr/>') | $('<tr class="pretty-diff"/>') | ||
.append('<td colspan="4" style="border: 1px solid #e6e6e6;">' + | .append('<td colspan="4" style="border: 1px solid #e6e6e6;">' + | ||
diff_prettyHtml(dmp.diff_main(oldStr, newStr)) + | diff_prettyHtml(dmp.diff_main(oldStr, newStr)) + |
2014年7月5日 (土) 10:30時点における版
var DMP_JS_URL = 'http://google-diff-match-patch.googlecode.com/svn/trunk/javascript/diff_match_patch.js';
function diff_prettyHtml(a) {
var c = /&/g,
d = /</g,
e = />/g,
f = /\n/g;
return a.map(function(b) {
var h = b[0],
j = b[1].replace(c, '&')
.replace(d, '<')
.replace(e, '>')
.replace(f, '¶<br/>');
switch (h) {
case 1:
return '<ins style="background-color:#D8ECFF;">' + j + '</ins>';
case -1:
return '<del style="background-color:#FEEEC8;">' + j + '</del>';
case 0:
return j;
}
}).join('');
}
function prettyDiff() {
var $table = $(this);
if ($table.find('> tbody > tr.pretty-diff').length > 0) return;
var dmp = new diff_match_patch();
var $lineno = $table.find('> tbody > tr:has(.diff-lineno)');
$lineno.each(function() {
var $c = $(this).nextUntil($lineno);
var oldStr = $c.find('> .diff-deletedline, > .diff-context:eq(0)')
.map(function() { return $(this).text(); }).get().join('\n');
var newStr = $c.find('> .diff-addedline, > .diff-context:eq(1)')
.map(function() { return $(this).text(); }).get().join('\n');
$('<tr class="pretty-diff"/>')
.append('<td colspan="4" style="border: 1px solid #e6e6e6;">' +
diff_prettyHtml(dmp.diff_main(oldStr, newStr)) +
'</td>')
.insertAfter(this);
$c.remove();
});
}
if (mw.util.getParamValue('diff') || $.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
mw.hook('wikipage.content').add(function($content) {
$.getScript(DMP_JS_URL, function() {
$content.on('dblclick', 'table.diff', prettyDiff);
});
});
}