feat: closes #11428, add a copy code button to code blocks

isekai-main
Barış Soner Uşaklı 2 years ago
parent c3854b786e
commit 0a292fc1ae

@ -39,6 +39,7 @@
"scheduled": "Scheduled", "scheduled": "Scheduled",
"moved": "Moved", "moved": "Moved",
"moved-from": "Moved from %1", "moved-from": "Moved from %1",
"copy-code": "Copy Code",
"copy-ip": "Copy IP", "copy-ip": "Copy IP",
"ban-ip": "Ban IP", "ban-ip": "Ban IP",
"view-history": "Edit History", "view-history": "Edit History",

@ -16,11 +16,12 @@ define('forum/topic', [
'api', 'api',
'alerts', 'alerts',
'bootbox', 'bootbox',
'clipboard',
], function ( ], function (
infinitescroll, threadTools, postTools, infinitescroll, threadTools, postTools,
events, posts, navigator, sort, quickreply, events, posts, navigator, sort, quickreply,
components, storage, hooks, api, alerts, components, storage, hooks, api, alerts,
bootbox bootbox, clipboard,
) { ) {
const Topic = {}; const Topic = {};
let tid = 0; let tid = 0;
@ -61,6 +62,7 @@ define('forum/topic', [
} }
addBlockQuoteHandler(); addBlockQuoteHandler();
addCodeBlockHandler();
addParentHandler(); addParentHandler();
addDropupHandler(); addDropupHandler();
addRepliesHandler(); addRepliesHandler();
@ -218,6 +220,29 @@ define('forum/topic', [
}); });
} }
function addCodeBlockHandler() {
new clipboard('[component="copy/code/btn"]', {
text: function (trigger) {
const btn = $(trigger);
btn.find('i').removeClass('fa-copy').addClass('fa-check');
setTimeout(() => btn.find('i').removeClass('fa-check').addClass('fa-copy'), 2000);
return btn.parent().find('code').text();
},
});
function addCopyCodeButton() {
const codeBlocks = $('[component="topic"] [component="post/content"] code:not([data-button-added])');
const container = $('<div class="hover-parent position-relative"></div>');
const buttonDiv = $('<button component="copy/code/btn" class="hover-visible position-absolute end-0 top-0 btn btn-sm btn-outline-secondary"><i class="fa fa-fw fa-copy"></i></button>');
codeBlocks.parent().wrap(container).parent().append(buttonDiv);
codeBlocks.parent().parent().find('[component="copy/code/btn"]').translateAttr('title', '[[topic:copy-code]]');
codeBlocks.attr('data-button-added', 1);
}
hooks.registerPage('action:posts.loaded', addCopyCodeButton);
hooks.registerPage('action:topic.loaded', addCopyCodeButton);
hooks.registerPage('action:posts.edited', addCopyCodeButton);
}
function addParentHandler() { function addParentHandler() {
components.get('topic').on('click', '[component="post/parent"]', function (e) { components.get('topic').on('click', '[component="post/parent"]', function (e) {
const toPid = $(this).attr('data-topid'); const toPid = $(this).attr('data-topid');

Loading…
Cancel
Save