Skip to content

fix: sync the editor changes and cache to store #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/store/tabStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const useTabStore = defineStore('panel', {
if (!panel) return;
try {
if (saveFile) {
await this.saveFile(panel, panel.content || '');
await this.saveContent(panel, panel.content || '', true);
}
const selectedIndex = this.panels.findIndex(({ id }) => id === panel.id);

Expand All @@ -93,6 +93,8 @@ export const useTabStore = defineStore('panel', {
this.activePanel = this.panels[Math.min(selectedIndex, this.panels.length - 1)];
}
} catch (err) {
console.log(err);
console.log('err str', JSON.stringify(err));
throw new CustomError(500, (err as Error).message);
}
},
Expand All @@ -103,12 +105,14 @@ export const useTabStore = defineStore('panel', {
this.activePanel = selectedPanel;
},

async saveFile(panel: Panel | undefined, content: string): Promise<void> {
async saveContent(panel: Panel | undefined, content: string, validateFilePath = false): Promise<void> {
let checkPanel = panel ?? this.activePanel;
if (!checkPanel) return;
checkPanel.content = content;

let filePath = checkPanel.file;
if (!(await sourceFileApi.exists(filePath))) {

if (!(await sourceFileApi.exists(filePath)) && validateFilePath) {
const selectedFolder = await sourceFileApi.selectFolder();
filePath = `${selectedFolder}/${filePath}`;
if (!filePath) {
Expand All @@ -117,10 +121,11 @@ export const useTabStore = defineStore('panel', {
}

checkPanel.file = filePath;
checkPanel.content = content;

await sourceFileApi.saveFile(filePath, content);
if (await sourceFileApi.exists(filePath)) {
await sourceFileApi.saveFile(filePath, content);
}
},

loadDefaultSnippet() {
if (!this.activePanel) return;
this.activePanel.content = defaultCodeSnippet;
Expand Down
13 changes: 7 additions & 6 deletions src/views/connect/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const tabPanelHandler = async ({
connection: Connection;
}) => {
if (action === 'ADD_PANEL') {
establishPanel(connection);
await establishPanel(connection);
}
};

Expand All @@ -77,6 +77,7 @@ const handleTabChange = async (panelName: string, action: 'CHANGE' | 'CLOSE') =>
setActivePanel(panel.id);
} else if (action === 'CLOSE') {
const exists = await checkFileExists(panel);
console.log('close exists check', exists);
if (!exists) {
dialog.warning({
title: lang.t('file.saveFileBeforeClose.title'),
Expand All @@ -85,25 +86,25 @@ const handleTabChange = async (panelName: string, action: 'CHANGE' | 'CLOSE') =>
negativeText: lang.t('file.saveFileBeforeClose.negativeText'),
onPositiveClick: async () => {
try {
closePanel(panel, true);
await closePanel(panel, true);
message.success(lang.t('file.saveFileBeforeClose.success'));
} catch (err) {
message.error(lang.t('file.saveFileBeforeClose.failed') + ': ' + err);
}
},
onNegativeClick: () => {
closePanel(panel, false);
onNegativeClick:async () => {
await closePanel(panel, false);
},
});
} else {
closePanel(panel, true);
await closePanel(panel, true);
}
}
};

onMounted(async () => {
if (route.params.filePath && route.params.filePath !== ':filePath') {
establishPanel(route.params.filePath as string);
await establishPanel(route.params.filePath as string);
}
});
</script>
Expand Down
48 changes: 16 additions & 32 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CustomError } from '../../common';
import { useAppStore, useChatStore, useConnectionStore, useTabStore } from '../../store';
import { useLang } from '../../lang';
import DisplayEditor from './display-editor.vue';
import { register, isRegistered, unregister } from '@tauri-apps/api/globalShortcut';
import { isRegistered, register, unregister } from '@tauri-apps/api/globalShortcut';
import {
buildCodeLens,
buildSearchToken,
Expand All @@ -33,15 +33,15 @@ import {
monaco,
SearchAction,
searchTokens,
transformQDSL,
transformQDSL
} from '../../common/monaco';

const appStore = useAppStore();
const message = useMessage();
const lang = useLang();

const tabStore = useTabStore();
const { saveFile, checkFileExists } = tabStore;
const { saveContent } = tabStore;
const { activePanel } = storeToRefs(tabStore);

const connectionStore = useConnectionStore();
Expand Down Expand Up @@ -247,6 +247,10 @@ const setupQueryEditor = () => {
return;
}

queryEditor.onDidChangeModelContent((_changes) => {
saveModelContent(false);
});

autoIndentCmdId = queryEditor.addCommand(0, (...args) => autoIndentAction(queryEditor!, args[1]));
copyCurlCmdId = queryEditor.addCommand(0, (...args) => copyCurlAction(args[1]));

Expand Down Expand Up @@ -358,21 +362,20 @@ const displayJsonEditor = (content: string) => {
displayEditorRef.value.display(content);
};

const unListenSaveFile = ref<Function>();
let saveInterval: NodeJS.Timeout;
const saveFileListener = ref<Function>();

const saveModelValueToFile = async () => {
const saveModelContent = async (validateFile: boolean) => {
const model = queryEditor?.getModel();
if (!model) {
return;
}
await saveFile(undefined, model.getValue() || '');
await saveContent(undefined, model.getValue() || '', validateFile);
};

const setupFileListener = async () => {
// listen for saveFile event
unListenSaveFile.value = await listen('saveFile', async () => {
await saveModelValueToFile();
saveFileListener.value = await listen('saveFile', async () => {
await saveModelContent(true);
});

/**
Expand All @@ -383,37 +386,19 @@ const setupFileListener = async () => {
const saveShortcutWin = await isRegistered('Control+S');
if (!saveShortcutWin) {
await register('Control+S', async () => {
await saveModelValueToFile();
await saveModelContent(true);
});
}

// Set up autosave interval if the file exists
if (await checkFileExists(undefined)) {
saveInterval = setInterval(async () => {
const model = queryEditor?.getModel();
if (!model) {
return;
}
const position = queryEditor?.getPosition();
const currentContent = model.getValue();

await saveFile(undefined, currentContent);
if (position) {
queryEditor?.setPosition(position);
}
}, 5000);
}
};

const cleanupFileListener = async () => {
if (unListenSaveFile?.value) {
await unListenSaveFile.value();
if (saveFileListener?.value) {
await saveFileListener.value();
}
const saveShortcutWin = await isRegistered('Control+S');
if (saveShortcutWin) {
await unregister('Control+S');
}
clearInterval(saveInterval);
};

onMounted(async () => {
Expand All @@ -422,11 +407,10 @@ onMounted(async () => {
});

onUnmounted(async () => {
await cleanupFileListener();
codeLensProvider?.dispose();
queryEditor?.dispose();
displayEditorRef?.value?.dispose();

await cleanupFileListener();
});
</script>

Expand Down