LLWiki正在建设中,欢迎加入我们!
User:Bhsd/table-editor.js
跳转到导航
跳转到搜索
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
// 引自[[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\S]*\n[\s]*\|}$/.test(table) ) { return false; } table = table.slice(2, -2); var tableProperty = table.match(/^([\s\S]*?)\n/)[1], tbody = table.substring( tableProperty.length ).trim(), tr = tbody.split('|-').filter(e => /[\|!]/.test(e)).map(e => e.trim()); trProperty = tr.map(e => { if( /^[\|!]/.test(e) ) { return ""; } return e.match(/^([\s\S]*?\n[\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(table) { var result = "{|" + table.tableProperty.trim() + "\n", td = table.td; table.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].trim() + "\n"; } }); return result + "|}"; } function parseCompactTable(table) { var result = "{|" + table.tableProperty.trim() + "\n", td = table.td; table.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].trim() + ' ' + (td[i][j * 2 + 2] || "\n").slice(-1); } }); return result + "|}"; } function insertCol(table) { var init = prompt( wgULS("请输入插入列的初始内容:", "請輸入插入列的初始內容:") ); table.td = table.td.map(e => [...e, '|', init]); return table; } function permuteCol(table) { var order = prompt( wgULS("请输入新的列序。原第一列记为0,依次类推。以半角符号(,)分隔,省略想要删除的列的序号", "請輸入新的列序。原第一列記為0,依此類推。以半形符號(,)分隔,省略想要刪除的列的序號") ).split(','); if( order.find(e => isNaN(e) || e < 0) ) { mw.notify("列序输入格式不正确", "列序輸入格式不正確"); return table; } table.td = table.td.map(e => { var tdArray = []; order.forEach(function(i) { tdArray.push(e[2 * i] || '|'); tdArray.push(e[2 * i + 1] || ''); }); return tdArray; }); return table; } 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; } editBox.val( parts[0] + handler(table) + parts[2] ); } } } } }); } addButton(wgULS("<tr>分行书写", "<tr>分行書寫"), "//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png", parseTable ); addButton(wgULS("<tr>一行书写", "<tr>一行書寫"), "//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png", parseCompactTable ); addButton(wgULS("在最右侧插入一列", "在最右側插入一列"), "//upload.wikimedia.org/wikipedia/commons/6/60/Button_support.png", function(table) { return parseCompactTable( insertCol(table) ); } ); addButton(wgULS("改变列序和/或删除部分列", "改變列序和/或刪除部分列"), "//upload.wikimedia.org/wikipedia/commons/9/98/Button_oppose.png", function(table) { return parseCompactTable( permuteCol(table) ); } ); });