//META{"name":"ShortcutCommands","displayName":"micelle","website":"https://github.com/micelle","source":"https://github.com/micelle/dc_BetterDiscordPlugins"}*//
const lang = document.documentElement.lang;
let commandList = [];
let SortableCreated = false; // 挙動が荒ぶるので制御してます… ↷( ó╻ò)
class ShortcutCommands {
getName() {
return 'ShortcutCommands';
}
getDescription() {
return 'コマンド入力を少し楽にしてくれます。';
}
getVersion() {
return '1.0.1';
}
getAuthor() {
return 'micelle';
}
load() {
this.log('load', this.getVersion());
BdApi.linkJS('zeresLibraryScript', 'https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js');
ZeresPluginLibrary.PluginUpdater.checkForUpdate(this.getName(), this.getVersion(), 'https://raw.githubusercontent.com/micelle/BetterDiscordPlugins/master/plugins/ShortcutCommands.plugin.js');
}
start() {
this.log('start', this.getVersion());
// 設定画面で使うライブラリ
if (!global.Sortable || typeof Sortable != 'object') BdApi.linkJS('SortableScript', 'https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js');
if (!global.BDFDB || typeof BDFDB != 'object' || !BDFDB.loaded) BdApi.linkJS('BDFDBScript', 'https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js');
const loadCommandList = BdApi.loadData(this.getName(), 'commandList');
if (loadCommandList) commandList = loadCommandList;
// コマンド表示ボタンを設置
this.setCommandButton();
// コマンド表示
$(document).on('click', '[aria-label="command"]', function() {
const buttonItems = commandList.map(function(val, index) {
if (/^category:.+/.test(val)) {
const text = val.replace(/^category:/, '');
const escape = ShortcutCommands.prototype.escapeHtml(text);
const html = `
${escape}
`;
return html;
} else {
const escape = ShortcutCommands.prototype.escapeHtml(val);
const className = `${BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemedium}`;
const styleName = 'margin:0px 4px 4px 0; display:inline; width:auto; max-width:100%;';
const html = ``;
if (index === 0) return `Default
${html}`;
return html;
}
}).join('');
const buttonItemsHtml =
``;
$('#app-mount > [data-no-focus-lock] > .da-layerContainer').html(buttonItemsHtml);
});
// コマンド入力
$(document).on('click', '#send-command button', function() {
const txt = $(this).text();
const val = $('.da-textArea').val();
const replaceTxt = (val === '') ? txt : `${val} ${txt}`;
let ta = Utils.getTextArea();
Utils.insertText(ta[0], replaceTxt);
ta[0].dispatchEvent(new Event('input', {
bubbles: true
}));
$('#send-command').remove();
});
// コマンド非表示
$(document).click(function(event) {
const svg = $(event.target).parents('[aria-label="command"]').length;
const cmd = $(event.target).parents('#send-command').length;
if (!svg && !cmd) $('#send-command').remove();
});
// コマンド追加
$(document).on('click', '#add-command', function() {
const val = $('#input-command').val();
if (val === '') {
BdApi.alert('コマンドが空欄です', 'コマンドは必ず入力して下さい。');
} else if (!commandList.includes(val)) {
commandList.push(val);
const htmlCommandList = ShortcutCommands.prototype.getHTML_CommandList();
$('#list-command').html(htmlCommandList);
$('#input-command').val('');
BdApi.saveData(ShortcutCommands.prototype.getName(), 'commandList', commandList);
} else {
BdApi.alert('コマンドが既に登録済みです', '同じコマンドは登録できません。');
}
});
// コマンド更新
$(document).on('change', '#list-command input', function() {
const ary = $('#list-command > div').map(function(i, e) {
return $(e).find('input').val();
}).get();
commandList = ary;
BdApi.saveData(ShortcutCommands.prototype.getName(), 'commandList', commandList);
});
// コマンド削除
$(document).on('click', '#delete-command', function() {
const $parent = $(this).parent();
const val = $parent.children('input').val();
const index = $('#list-command > div').index($parent);
BdApi.showConfirmationModal('確認っす',
[`「${val}」を削除してもいいですか?`], {
danger: true,
confirmText: '削除する',
cancelText: 'キャンセル',
onConfirm: function() {
commandList.splice(index, 1);
$parent.remove();
BdApi.saveData(ShortcutCommands.prototype.getName(), 'commandList', commandList);
}
}
);
});
}
stop() {
this.log('stop', this.getVersion());
if ($('[aria-label="command"]').length) $('[aria-label="command"]').remove();
}
onSwitch() {
this.setCommandButton(); // コマンド表示ボタンを設置
}
setCommandButton() {
const html =
``;
if (!$('[aria-label="command"]').length) $('.da-buttons').append(html);
}
escapeHtml(str) {
const rep = str
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
return rep;
}
getSettingsPanel() {
if (!global.BDFDB || typeof BDFDB != 'object' || !BDFDB.loaded) return;
const SettingsPanel = this.getHTML_SettingsPanel();
const CommandList = this.getHTML_CommandList();
const $SettingsPanel = $(SettingsPanel);
$SettingsPanel.find('#list-command').html(CommandList);
return $SettingsPanel.html();
}
getHTML_SettingsPanel() {
const html =
``;
return html;
}
getHTML_CommandList() {
const html = commandList.map(function(val) {
const escape = ShortcutCommands.prototype.escapeHtml(val);
const h =
``;
return h;
}).join('');
return html;
}
observer(changes) {
const id = $(changes.target).attr('id');
if (id === 'plugin-settings-ShortcutCommands') {
if (SortableCreated) return;
SortableCreated = true;
Sortable.create($('#list-command').get(0), {
group: 'list-command',
handle: '.list-command-handle',
animation: 300,
onSort: function() {
const ary = $('#list-command > div').map(function(i, e) {
return $(e).find('input').val();
}).get();
commandList = ary;
BdApi.saveData(ShortcutCommands.prototype.getName(), 'commandList', commandList);
}
});
} else {
SortableCreated = false;
}
}
log(msg, data) {
const name = this.getName();
console.log(`%c[${name}] ${msg}`, 'color:#C9242F', data);
}
}