dbgate/packages/web/src/appobj/ArchiveFileAppObject.svelte

211 lines
6.2 KiB
Svelte
Raw Normal View History

2021-03-14 07:30:06 +00:00
<script lang="ts" context="module">
function openArchive(fileName, folderName) {
openNewTab({
title: fileName,
icon: 'img archive',
tooltip: `${folderName}\n${fileName}`,
tabComponent: 'ArchiveFileTab',
props: {
archiveFile: fileName,
archiveFolder: folderName,
},
});
}
2021-09-30 09:05:07 +00:00
async function openTextFile(fileName, fileType, folderName, tabComponent, icon) {
2021-09-30 08:30:35 +00:00
const connProps: any = {};
let tooltip = undefined;
2021-12-22 09:16:44 +00:00
const resp = await apiCall('files/load', {
2021-09-30 08:30:35 +00:00
folder: 'archive:' + folderName,
file: fileName + '.' + fileType,
format: 'text',
});
openNewTab(
{
title: fileName,
2021-09-30 08:58:04 +00:00
icon,
tabComponent,
2021-09-30 08:30:35 +00:00
tooltip,
props: {
savedFile: fileName + '.' + fileType,
savedFolder: 'archive:' + folderName,
savedFormat: 'text',
2021-09-30 09:05:07 +00:00
archiveFolder: folderName,
2021-09-30 08:30:35 +00:00
...connProps,
},
},
2021-12-22 09:16:44 +00:00
{ editor: resp }
2021-09-30 08:30:35 +00:00
);
}
2021-03-14 07:30:06 +00:00
export const extractKey = data => data.fileName;
export const createMatcher = ({ fileName }) => filter => filterName(filter, fileName);
2021-09-30 08:30:35 +00:00
const ARCHIVE_ICONS = {
'table.yaml': 'img table',
'view.sql': 'img view',
2021-11-13 17:01:44 +00:00
'proc.sql': 'img procedure',
2021-09-30 08:30:35 +00:00
'func.sql': 'img function',
'trigger.sql': 'img sql-file',
'matview.sql': 'img view',
};
function getArchiveIcon(archiveFilesAsDataSheets, data) {
if (data.fileType == 'jsonl') {
return isArchiveFileMarkedAsDataSheet(archiveFilesAsDataSheets, data.folderName, data.fileName)
? 'img free-table'
: 'img archive';
}
return ARCHIVE_ICONS[data.fileType];
}
2021-03-14 07:30:06 +00:00
</script>
<script lang="ts">
2021-09-30 08:30:35 +00:00
import _ from 'lodash';
2021-06-03 12:26:21 +00:00
import { filterName } from 'dbgate-tools';
2021-06-06 16:23:14 +00:00
import ImportExportModal from '../modals/ImportExportModal.svelte';
import { showModal } from '../modals/modalTools';
2021-03-14 07:30:06 +00:00
import { archiveFilesAsDataSheets, currentArchive, extensions, getCurrentDatabase } from '../stores';
2021-03-14 07:30:06 +00:00
2021-06-06 16:23:14 +00:00
import createQuickExportMenu from '../utility/createQuickExportMenu';
2022-03-10 08:47:28 +00:00
import { exportElectronFile } from '../utility/exportFileTools';
2021-03-14 07:30:06 +00:00
import openNewTab from '../utility/openNewTab';
import AppObjectCore from './AppObjectCore.svelte';
2021-09-30 08:30:35 +00:00
import getConnectionLabel from '../utility/getConnectionLabel';
2021-11-10 17:04:00 +00:00
import InputTextModal from '../modals/InputTextModal.svelte';
2021-11-13 16:57:22 +00:00
import ConfirmModal from '../modals/ConfirmModal.svelte';
import {
isArchiveFileMarkedAsDataSheet,
markArchiveFileAsDataSheet,
markArchiveFileAsReadonly,
} from '../utility/archiveTools';
2022-01-27 13:31:46 +00:00
import { apiCall } from '../utility/api';
2021-03-14 07:30:06 +00:00
export let data;
2021-11-10 17:04:00 +00:00
const handleRename = () => {
showModal(InputTextModal, {
2021-11-13 16:57:22 +00:00
value: data.fileName,
2021-11-10 17:04:00 +00:00
label: 'New file name',
header: 'Rename file',
onConfirm: newFile => {
2021-12-22 09:16:44 +00:00
apiCall('archive/rename-file', {
2021-11-13 16:57:22 +00:00
file: data.fileName,
folder: data.folderName,
fileType: data.fileType,
newFile,
});
2021-11-10 17:04:00 +00:00
},
});
};
2021-03-14 07:30:06 +00:00
const handleDelete = () => {
2021-11-13 16:57:22 +00:00
showModal(ConfirmModal, {
message: `Really delete file ${data.fileName}?`,
onConfirm: () => {
2021-12-22 09:16:44 +00:00
apiCall('archive/delete-file', {
2021-11-13 16:57:22 +00:00
file: data.fileName,
folder: data.folderName,
fileType: data.fileType,
});
},
});
2021-03-14 07:30:06 +00:00
};
const handleOpenRead = () => {
markArchiveFileAsReadonly(data.folderName, data.fileName);
2021-03-14 07:30:06 +00:00
openArchive(data.fileName, data.folderName);
};
const handleOpenWrite = () => {
markArchiveFileAsDataSheet(data.folderName, data.fileName);
2021-03-14 07:30:06 +00:00
openNewTab({
title: data.fileName,
2021-12-09 12:03:08 +00:00
icon: 'img free-table',
2021-03-14 07:30:06 +00:00
tabComponent: 'FreeTableTab',
props: {
initialArgs: {
functionName: 'archiveReader',
props: {
fileName: data.fileName,
folderName: data.folderName,
},
},
archiveFile: data.fileName,
archiveFolder: data.folderName,
},
});
};
const handleClick = () => {
if (data.fileType == 'jsonl') {
if (isArchiveFileMarkedAsDataSheet($archiveFilesAsDataSheets, data.folderName, data.fileName)) {
handleOpenWrite();
} else {
handleOpenRead();
}
}
2021-09-30 08:58:04 +00:00
if (data.fileType.endsWith('.sql')) {
handleOpenSqlFile();
}
if (data.fileType.endsWith('.yaml')) {
handleOpenYamlFile();
}
2021-09-30 08:30:35 +00:00
};
const handleOpenSqlFile = () => {
2021-09-30 09:05:07 +00:00
openTextFile(data.fileName, data.fileType, data.folderName, 'QueryTab', 'img sql-file');
2021-09-30 08:58:04 +00:00
};
const handleOpenYamlFile = () => {
2021-09-30 09:05:07 +00:00
openTextFile(data.fileName, data.fileType, data.folderName, 'YamlEditorTab', 'img yaml');
2021-03-14 07:30:06 +00:00
};
2022-02-13 09:45:20 +00:00
const handleOpenJsonLinesText = () => {
openTextFile(data.fileName, data.fileType, data.folderName, 'JsonLinesEditorTab', 'img json');
2022-01-30 10:26:28 +00:00
};
2021-03-14 07:30:06 +00:00
function createMenu() {
return [
2021-09-30 08:30:35 +00:00
data.fileType == 'jsonl' && { text: 'Open (readonly)', onClick: handleOpenRead },
2021-11-28 18:57:53 +00:00
data.fileType == 'jsonl' && { text: 'Open as data sheet', onClick: handleOpenWrite },
2022-02-13 09:45:20 +00:00
data.fileType == 'jsonl' && { text: 'Open in text editor', onClick: handleOpenJsonLinesText },
2021-03-14 07:30:06 +00:00
{ text: 'Delete', onClick: handleDelete },
2021-11-10 17:04:00 +00:00
{ text: 'Rename', onClick: handleRename },
2021-09-30 08:30:35 +00:00
data.fileType == 'jsonl' &&
2022-03-10 09:15:05 +00:00
createQuickExportMenu(fmt => async () => {
2021-09-30 08:30:35 +00:00
exportElectronFile(
data.fileName,
{
functionName: 'archiveReader',
props: {
fileName: data.fileName,
folderName: data.folderName,
},
2021-06-06 16:23:14 +00:00
},
2021-09-30 08:30:35 +00:00
fmt
);
}),
data.fileType == 'jsonl' && {
2021-06-06 16:23:14 +00:00
text: 'Export',
onClick: () => {
showModal(ImportExportModal, {
initialValues: {
sourceStorageType: 'archive',
sourceArchiveFolder: data.folderName,
sourceList: [data.fileName],
},
});
},
},
2021-09-30 08:30:35 +00:00
data.fileType.endsWith('.sql') && { text: 'Open SQL', onClick: handleOpenSqlFile },
2021-09-30 08:58:04 +00:00
data.fileType.endsWith('.yaml') && { text: 'Open YAML', onClick: handleOpenYamlFile },
2021-03-14 07:30:06 +00:00
];
}
</script>
<AppObjectCore
{...$$restProps}
{data}
2021-11-14 08:36:14 +00:00
title={data.fileLabel}
icon={getArchiveIcon($archiveFilesAsDataSheets, data)}
2021-03-14 07:30:06 +00:00
menu={createMenu}
on:click={handleClick}
/>