LLWiki正在建设中,欢迎加入我们!
“User:Bhsd/tool.js”的版本间差异
跳转到导航
跳转到搜索
小 |
小 |
||
第36行: | 第36行: | ||
var tableProperty = table.match(/^([\s\S]*?)\n/)[1], |
var tableProperty = table.match(/^([\s\S]*?)\n/)[1], |
||
tbody = table.substring( tableProperty.length ).trim(), |
tbody = table.substring( tableProperty.length ).trim(), |
||
tr = tbody.split('|-').map(e => e.trim()) |
tr = tbody.split('|-').map(e => e.trim()).filter(e => /[\|!]/.test(e)); |
||
trProperty = tr.map(e => e.match(/^([\s\S]*?)[\|!]/)[1]), |
trProperty = tr.map(e => e.match(/^([\s\S]*?)[\|!]/)[1]), |
||
td = tr.map( (e, i) => [e.charAt(trProperty[i].length), ...e.substring( trProperty[i].length + 1 ).split(/(\|\||\n[\s]*\||!!|\n[\s]*!)/)]); |
td = tr.map( (e, i) => [e.charAt(trProperty[i].length), ...e.substring( trProperty[i].length + 1 ).split(/(\|\||\n[\s]*\||!!|\n[\s]*!)/)]); |
||
第89行: | 第89行: | ||
action: { |
action: { |
||
type: 'callback', |
type: 'callback', |
||
execute: |
execute: function() { |
||
var parts = getSelection(editBox), |
|||
⚫ | |||
if(!table) { |
|||
mw.notify( wgULS("表格不完整或存在错误", "表格不完整或存在錯誤") ); |
|||
return; |
|||
} |
|||
handler(); |
|||
} |
|||
} |
} |
||
} |
} |
||
第96行: | 第104行: | ||
} |
} |
||
addButton(wgULS("<tr>分行书写", "<tr>分行書寫"), "//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png", function() { |
addButton(wgULS("<tr>分行书写", "<tr>分行書寫"), "//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png", function() { |
||
var |
var newTable = parseTable(table); |
||
⚫ | |||
editBox.val( parts[0] + newTable + parts[2] ); |
editBox.val( parts[0] + newTable + parts[2] ); |
||
}); |
}); |
||
addButton(wgULS("<tr>一行书写", "<tr>一行書寫"), "//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png", function() { |
addButton(wgULS("<tr>一行书写", "<tr>一行書寫"), "//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png", function() { |
||
var |
var newTable = parseCompactTable(table); |
||
newTable = parseCompactTable( readTable( parts[1] ) ); |
|||
editBox.val( parts[0] + newTable + parts[2] ); |
editBox.val( parts[0] + newTable + parts[2] ); |
||
}); |
}); |
2020年9月11日 (五) 11:00的版本
/* 生成JS文件大小 var api = new mw.Api(), table = $('table').first(), list = table.find('td:first-child a'); table.find('th').first().after( '<th>大小</th>' ); list.each(function() { var ele = $(this); var title = ele.attr('href').substring(4); api.get({ action:'query', formatversion:2, prop:'info', titles:title }).then(function(data) { var length = data.query.pages[0].length; ele.parent().after('<td>' + length + '</td>'); }); }); */ // 引自[[wikipedia:User:Kephir/gadgets/table-editor.js]] $(function() { if(!['edit', 'submit'].includes( mw.config.get('wgAction') ) || mw.config.get('wgPageContentModel') != 'wikitext') return; /* 表格列操作 */ var editBox = $('#wpTextbox1'); function getSelection(textarea) { var start = textarea.prop('selectionStart'), end = textarea.prop('selectionEnd'); return [textarea.val().substring(0, start), textarea.val().substring(start, end), textarea.val().substring(end) ]; } function readTable(table) { table = table.trim(); if( !/^{\|[\s\S]*\|}$/.test(table) ) { return false; } table = table.substring(2, table.length - 2).trim(); var tableProperty = table.match(/^([\s\S]*?)\n/)[1], tbody = table.substring( tableProperty.length ).trim(), tr = tbody.split('|-').map(e => e.trim()).filter(e => /[\|!]/.test(e)); trProperty = tr.map(e => e.match(/^([\s\S]*?)[\|!]/)[1]), td = tr.map( (e, i) => [e.charAt(trProperty[i].length), ...e.substring( trProperty[i].length + 1 ).split(/(\|\||\n[\s]*\||!!|\n[\s]*!)/)]); return {tableProperty: tableProperty, trProperty: trProperty, td: td}; } function parseTable(tableStruct) { var result = "{|" + tableStruct.tableProperty.trim() + "\n", td = tableStruct.td; tableStruct.trProperty.forEach(function(e, i) { if(i || e.trim()) { result += "|-" + e.trim() + "\n"; } for(var j = 0; j < td[i].length / 2; j++) { result += td[i][j * 2].slice(-1) + td[i][j * 2 + 1] + "\n"; } }); return result + "|}"; } function parseCompactTable(tableStruct) { var result = "{|" + tableStruct.tableProperty.trim() + "\n", td = tableStruct.td; tableStruct.trProperty.forEach(function(e, i) { if(i || e.trim()) { result += "|-" + e.trim() + "\n"; } for(var j = 0; j < td[i].length / 2; j++) { result += td[i][j * 2].slice(-1) + td[i][j * 2 + 1] + (td[i][j * 2 + 2] || "\n").slice(-1); } }); return result + "|}"; } editBox.wikiEditor('addToToolbar', { sections: { 'tables': { type: 'toolbar', label: '表格' } } }); editBox.wikiEditor('addToToolbar', { section: 'tables', groups: { 'tables': {} } }); function addButton(text, icon, handler) { editBox.wikiEditor('addToToolbar', { section: 'tables', group: 'tables', tools: { ident: { label: text, type: 'button', icon: icon, action: { type: 'callback', execute: function() { var parts = getSelection(editBox), table = readTable( parts[1] ); if(!table) { mw.notify( wgULS("表格不完整或存在错误", "表格不完整或存在錯誤") ); return; } handler(); } } } } }); } addButton(wgULS("<tr>分行书写", "<tr>分行書寫"), "//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png", function() { var newTable = parseTable(table); editBox.val( parts[0] + newTable + parts[2] ); }); addButton(wgULS("<tr>一行书写", "<tr>一行書寫"), "//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png", function() { var newTable = parseCompactTable(table); editBox.val( parts[0] + newTable + parts[2] ); }); });