feat(mongo): open and save js files in mongo tabs

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-01 22:53:16 +08:00
1 parent 906f5c271c
commit 51b8342f5d
7 files changed
+152 -36

No files matched your search

+16 -10
View File
@@ -94,7 +94,7 @@ import { isMacOS, isWindows } from "@/lib/backend/platform";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { openQueryResultArchiveFile } from "@/lib/query/queryResultArchiveFile";
import { activeTabExternalSqlFileTarget, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab, unassociatedExternalSqlFileTarget, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
import { externalSqlFileOpenErrorMessage, externalSqlEditorMaxBytes, isSqlFilePath, readBrowserSqlFile, sqlFileTitleFromPath } from "@/lib/sql/sqlFileOpen";
import { defaultSavedQueryFileName, externalSqlFileOpenErrorMessage, externalSqlEditorMaxBytes, isSqlFilePath, queryEditorOpenFileAccept, queryEditorOpenFileFilters, readBrowserSqlFile, sqlFileTitleFromPath } from "@/lib/sql/sqlFileOpen";
import type { ConnectionConfig, DatabaseType, ObjectBrowserFilter, ObjectSourceKind, QueryTab, TabOutputView, TreeNode } from "@/types/database";
import { OPEN_PLUGIN_SETTINGS, type PluginCenterFocus } from "@/lib/plugins/pluginCenterNavigation";
import { parsePluginInstallDeepLink } from "@/lib/plugins/pluginInstallDeepLink";
@@ -2174,11 +2174,11 @@ function savedSqlTargetForSave(tab: QueryTab) {
*/
async function formattedSqlForSave(tab: QueryTab): Promise<string> {
if (!settingsStore.editorSettings.formatSqlOnSqlFileSave) return tab.sql;
if (tab.externalSqlPath && !isSqlFilePath(tab.externalSqlPath)) return tab.sql;
const sqlSnapshot = tab.sql;
if (!sqlSnapshot.trim()) return sqlSnapshot;
const connection = connectionStore.getConfig(tab.connectionId);
const databaseType = effectiveDatabaseTypeForConnection(connection) ?? connection?.db_type;
if (tab.externalSqlPath && !isSqlFilePath(tab.externalSqlPath) && !(databaseType === "mongodb" && /\.js$/i.test(tab.externalSqlPath))) return tab.sql;
const sqlSnapshot = tab.sql;
if (!sqlSnapshot.trim()) return sqlSnapshot;
if (!canFormatSqlForDatabaseType(databaseType)) return sqlSnapshot;
try {
return await formatSqlSnapshotForSave(
@@ -2460,8 +2460,12 @@ async function saveExternalSqlTabAs(tab: QueryTab): Promise<boolean> {
// Non-SQL external tabs (custom-filtered text files) keep their own file
// name and extension when saving a copy instead of being forced to .sql.
const currentFileName = tab.externalSqlPath?.split(/[\\/]/).pop()?.trim() ?? "";
const filterExtension = currentFileName.includes(".") ? currentFileName.split(".").pop()?.toLowerCase() : undefined;
const saved = await api.saveExternalSqlFile(currentFileName || defaultSavedSqlName(tab.title), await formattedSqlForSave(tab), filterExtension);
const connection = connectionStore.getConfig(tab.connectionId);
const databaseType = effectiveDatabaseTypeForConnection(connection) ?? connection?.db_type;
const isMongo = databaseType === "mongodb";
const filterExtension = currentFileName.includes(".") ? currentFileName.split(".").pop()?.toLowerCase() : isMongo ? "js" : undefined;
const defaultName = currentFileName || defaultSavedQueryFileName(tab.title, databaseType);
const saved = await api.saveExternalSqlFile(defaultName, await formattedSqlForSave(tab), filterExtension);
if (!saved) return false;
queryStore.linkExternalSqlPath(tab.id, saved.path, sqlFileTitleFromPath(saved.path), saved.version);
rememberExternalSqlFileTarget(saved.path, { connectionId: tab.connectionId, database: tab.database, catalog: tab.catalog, schema: tab.schema });
@@ -2497,19 +2501,21 @@ function applyExternalSqlTarget(tab: QueryTab, target: ExternalSqlFileTarget) {
function applyExternalSqlFileTarget(tab: QueryTab, path: string) {
applyExternalSqlTarget(
tab,
resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, tab.id, (connectionId) => connectionStore.getConfig(connectionId)),
resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, tab.id, (connectionId) => connectionStore.getConfig(connectionId), { allowMongoScripts: true }),
);
}
async function openSqlFile() {
const tab = activeTab.value;
if (!tab) return;
const connection = connectionStore.getConfig(tab.connectionId);
const databaseType = effectiveDatabaseTypeForConnection(connection) ?? connection?.db_type;
let openedSqlPath: string | undefined;
try {
if (isTauriRuntime()) {
const { open } = await import("@tauri-apps/plugin-dialog");
const path = await open({
filters: [{ name: "SQL", extensions: ["sql"] }],
filters: queryEditorOpenFileFilters(databaseType),
multiple: false,
});
if (path) {
@@ -2523,7 +2529,7 @@ async function openSqlFile() {
} else {
const input = document.createElement("input");
input.type = "file";
input.accept = ".sql";
input.accept = queryEditorOpenFileAccept(databaseType);
input.onchange = async () => {
const file = input.files?.[0];
if (!file) return;
@@ -2531,7 +2537,7 @@ async function openSqlFile() {
queryStore.updateSql(tab.id, await readBrowserSqlFile(file, externalSqlEditorMaxBytes(settingsStore.editorSettings.externalSqlEditorMaxMb)));
applyExternalSqlTarget(
tab,
activeTabExternalSqlFileTarget(queryStore.tabs, tab.id, (connectionId) => connectionStore.getConfig(connectionId)),
activeTabExternalSqlFileTarget(queryStore.tabs, tab.id, (connectionId) => connectionStore.getConfig(connectionId), { allowMongoScripts: true }),
);
} catch (e: any) {
toast(t("toolbar.sqlOpenFailed", { message: externalSqlFileOpenErrorMessage(e, (key, params) => t(key, params)) }), 5000);