This commit is contained in:
Jan Prochazka
2021-03-11 10:52:51 +01:00
parent d5ebea3764
commit 8a4275fb09
4 changed files with 177 additions and 1 deletions

View File

@@ -0,0 +1,75 @@
import { derived, get } from 'svelte/store';
import { showModal } from '../modals/modalTools';
import { openedTabs } from '../stores';
import axiosInstance from '../utility/axiosInstance';
import { changeTab } from './common';
import SaveFileModal from '../modals/SaveFileModal.svelte';
import registerCommand from '../commands/registerCommand';
export function saveTabEnabledStore(editorStore) {
return derived(editorStore, editor => editor != null);
}
export default function saveTabFile(editorStore, saveAs, folder, format, fileExtension) {
const editor: any = get(editorStore);
const tabs = get(openedTabs);
const tabid = editor.getTabId();
const data = editor.getData();
const { savedFile, savedFilePath } = tabs.find(x => x.tabid == tabid).props || {};
const handleSave = async () => {
if (savedFile) {
await axiosInstance.post('files/save', { folder, file: savedFile, data, format });
}
if (savedFilePath) {
await axiosInstance.post('files/save-as', { filePath: savedFilePath, data, format });
}
};
const onSave = (title, newProps) => {
changeTab(tabid, tab => ({
...tab,
title,
props: {
...tab.props,
savedFormat: format,
...newProps,
},
}));
};
if ((savedFile || savedFilePath) && !saveAs) {
handleSave();
} else {
showModal(SaveFileModal, {
data,
folder,
format,
fileExtension,
name: savedFile || 'newFile',
filePath: savedFilePath,
onSave,
});
}
}
export function registerSaveCommands({ idPrefix, category, editorStore, folder, format, fileExtension }) {
registerCommand({
id: idPrefix + '.save',
category,
name: 'Save',
keyText: 'Ctrl+S',
icon: 'icon save',
toolbar: true,
enabledStore: saveTabEnabledStore(editorStore),
onClick: () => saveTabFile(editorStore, false, folder, format, fileExtension),
});
registerCommand({
id: idPrefix + '.saveAs',
category,
name: 'Save As',
keyText: 'Ctrl+Shift+S',
enabledStore: saveTabEnabledStore(editorStore),
onClick: () => saveTabFile(editorStore, true, folder, format, fileExtension),
});
}