mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-06 03:26:51 +03:00
feat: add copy & download actions for modal
This commit is contained in:
12
fe-app-podkop/src/helpers/copyToClipboard.ts
Normal file
12
fe-app-podkop/src/helpers/copyToClipboard.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function copyToClipboard(text: string) {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = text;
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
} catch (_err) {
|
||||
console.error('copyToClipboard - e', _err);
|
||||
}
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
15
fe-app-podkop/src/helpers/downloadAsTxt.ts
Normal file
15
fe-app-podkop/src/helpers/downloadAsTxt.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function downloadAsTxt(text: string, filename: string) {
|
||||
const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.href = URL.createObjectURL(blob);
|
||||
|
||||
const safeName = filename.endsWith('.txt') ? filename : `${filename}.txt`;
|
||||
link.download = safeName;
|
||||
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(link.href);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { renderButton } from '../button/renderButton';
|
||||
import { copyToClipboard } from '../../helpers/copyToClipboard';
|
||||
import { downloadAsTxt } from '../../helpers/downloadAsTxt';
|
||||
|
||||
export function renderModal(text: string) {
|
||||
export function renderModal(text: string, name: string) {
|
||||
return E(
|
||||
'div',
|
||||
{ class: 'pdk-partial-modal__body' },
|
||||
@@ -8,10 +10,16 @@ export function renderModal(text: string) {
|
||||
E('pre', { class: 'pdk-partial-modal__content' }, E('code', {}, text)),
|
||||
|
||||
E('div', { class: 'pdk-partial-modal__footer' }, [
|
||||
renderButton({
|
||||
classNames: ['cbi-button-apply'],
|
||||
text: _('Download'),
|
||||
onClick: () => downloadAsTxt(text, name),
|
||||
}),
|
||||
renderButton({
|
||||
classNames: ['cbi-button-apply'],
|
||||
text: _('Copy'),
|
||||
onClick: () => {},
|
||||
onClick: () =>
|
||||
copyToClipboard(` \`\`\`${name} \n ${text} \n \`\`\``),
|
||||
}),
|
||||
renderButton({
|
||||
classNames: ['cbi-button-remove'],
|
||||
|
||||
@@ -214,7 +214,10 @@ async function handleShowGlobalCheck() {
|
||||
const globalCheck = await PodkopShellMethods.globalCheck();
|
||||
|
||||
if (globalCheck.success) {
|
||||
ui.showModal(_('Global check'), renderModal(globalCheck.data as string));
|
||||
ui.showModal(
|
||||
_('Global check'),
|
||||
renderModal(globalCheck.data as string, 'global_check'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('handleShowGlobalCheck - e', e);
|
||||
@@ -241,7 +244,10 @@ async function handleViewLogs() {
|
||||
const viewLogs = await PodkopShellMethods.checkLogs();
|
||||
|
||||
if (viewLogs.success) {
|
||||
ui.showModal(_('View logs'), renderModal(viewLogs.data as string));
|
||||
ui.showModal(
|
||||
_('View logs'),
|
||||
renderModal(viewLogs.data as string, 'view_logs'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('handleViewLogs - e', e);
|
||||
@@ -270,7 +276,7 @@ async function handleShowSingBoxConfig() {
|
||||
if (showSingBoxConfig.success) {
|
||||
ui.showModal(
|
||||
_('Show sing-box config'),
|
||||
renderModal(showSingBoxConfig.data as string),
|
||||
renderModal(showSingBoxConfig.data as string, 'show_sing_box_config'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -2954,19 +2954,52 @@ function renderButton({
|
||||
);
|
||||
}
|
||||
|
||||
// src/helpers/copyToClipboard.ts
|
||||
function copyToClipboard(text) {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
try {
|
||||
document.execCommand("copy");
|
||||
} catch (_err) {
|
||||
console.error("copyToClipboard - e", _err);
|
||||
}
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
|
||||
// src/helpers/downloadAsTxt.ts
|
||||
function downloadAsTxt(text, filename) {
|
||||
const blob = new Blob([text], { type: "text/plain;charset=utf-8" });
|
||||
const link = document.createElement("a");
|
||||
link.href = URL.createObjectURL(blob);
|
||||
const safeName = filename.endsWith(".txt") ? filename : `${filename}.txt`;
|
||||
link.download = safeName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(link.href);
|
||||
}
|
||||
|
||||
// src/partials/modal/renderModal.ts
|
||||
function renderModal(text) {
|
||||
function renderModal(text, name) {
|
||||
return E(
|
||||
"div",
|
||||
{ class: "pdk-partial-modal__body" },
|
||||
E("div", {}, [
|
||||
E("pre", { class: "pdk-partial-modal__content" }, E("code", {}, text)),
|
||||
E("div", { class: "pdk-partial-modal__footer" }, [
|
||||
renderButton({
|
||||
classNames: ["cbi-button-apply"],
|
||||
text: _("Download"),
|
||||
onClick: () => downloadAsTxt(text, name)
|
||||
}),
|
||||
renderButton({
|
||||
classNames: ["cbi-button-apply"],
|
||||
text: _("Copy"),
|
||||
onClick: () => {
|
||||
}
|
||||
onClick: () => copyToClipboard(` \`\`\`${name}
|
||||
${text}
|
||||
\`\`\``)
|
||||
}),
|
||||
renderButton({
|
||||
classNames: ["cbi-button-remove"],
|
||||
@@ -3464,7 +3497,10 @@ async function handleShowGlobalCheck() {
|
||||
try {
|
||||
const globalCheck = await PodkopShellMethods.globalCheck();
|
||||
if (globalCheck.success) {
|
||||
ui.showModal(_("Global check"), renderModal(globalCheck.data));
|
||||
ui.showModal(
|
||||
_("Global check"),
|
||||
renderModal(globalCheck.data, "global_check")
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("handleShowGlobalCheck - e", e);
|
||||
@@ -3488,7 +3524,10 @@ async function handleViewLogs() {
|
||||
try {
|
||||
const viewLogs = await PodkopShellMethods.checkLogs();
|
||||
if (viewLogs.success) {
|
||||
ui.showModal(_("View logs"), renderModal(viewLogs.data));
|
||||
ui.showModal(
|
||||
_("View logs"),
|
||||
renderModal(viewLogs.data, "view_logs")
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("handleViewLogs - e", e);
|
||||
@@ -3514,7 +3553,7 @@ async function handleShowSingBoxConfig() {
|
||||
if (showSingBoxConfig.success) {
|
||||
ui.showModal(
|
||||
_("Show sing-box config"),
|
||||
renderModal(showSingBoxConfig.data)
|
||||
renderModal(showSingBoxConfig.data, "show_sing_box_config")
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user