-
Notifications
You must be signed in to change notification settings - Fork 395
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
CB-5832 Add SQL editor shortcut to save script #3167
base: devel
Are you sure you want to change the base?
Changes from 3 commits
37d3638
8923763
ba5ba3f
7fb4f1a
b80ec9b
f52c30a
e48d9e2
bcb308e
828b6dd
5e3e523
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { createKeyBinding } from '@cloudbeaver/core-view'; | ||
|
||
export const KEY_BINDING_SQL_EDITOR_SAVE_AS_SCRIPT = createKeyBinding({ | ||
id: 'save-as-script', | ||
keys: 'shift+mod+s', | ||
preventDefault: true, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import { isResourceOfType, ProjectInfoResource, ProjectsService } from '@cloudbe | |
import { CachedMapAllKey, CachedTreeChildrenKey } from '@cloudbeaver/core-resource'; | ||
import { getRmResourcePath, NAV_NODE_TYPE_RM_RESOURCE, ResourceManagerResource, RESOURCES_NODE_PATH } from '@cloudbeaver/core-resource-manager'; | ||
import { createPath, getPathName } from '@cloudbeaver/core-utils'; | ||
import { ActionService, MenuService } from '@cloudbeaver/core-view'; | ||
import { ActionService, KeyBindingService, MenuService } from '@cloudbeaver/core-view'; | ||
import { NavigationTabsService } from '@cloudbeaver/plugin-navigation-tabs'; | ||
import { getResourceKeyFromNodeId } from '@cloudbeaver/plugin-navigation-tree-rm'; | ||
import { RESOURCE_NAME_REGEX, ResourceManagerService } from '@cloudbeaver/plugin-resource-manager'; | ||
|
@@ -33,6 +33,7 @@ import { | |
import { isSQLEditorTab, SqlEditorNavigatorService } from '@cloudbeaver/plugin-sql-editor-navigation-tab'; | ||
|
||
import { ACTION_SAVE_AS_SCRIPT } from './ACTION_SAVE_AS_SCRIPT.js'; | ||
import { KEY_BINDING_SQL_EDITOR_SAVE_AS_SCRIPT } from './KEY_BINDING_SQL_EDITOR_SAVE_AS_SCRIPT.js'; | ||
import { ResourceSqlDataSource } from './ResourceSqlDataSource.js'; | ||
import { SqlEditorTabResourceService } from './SqlEditorTabResourceService.js'; | ||
|
||
|
@@ -55,6 +56,7 @@ export class PluginBootstrap extends Bootstrap { | |
private readonly sqlEditorSettingsService: SqlEditorSettingsService, | ||
private readonly resourceManagerResource: ResourceManagerResource, | ||
private readonly resourceManagerScriptsService: ResourceManagerScriptsService, | ||
private readonly keyBindingService: KeyBindingService, | ||
) { | ||
super(); | ||
} | ||
|
@@ -78,105 +80,7 @@ export class PluginBootstrap extends Bootstrap { | |
|
||
return dataSource instanceof MemorySqlDataSource || dataSource instanceof LocalStorageSqlDataSource; | ||
}, | ||
handler: async (context, action) => { | ||
const state = context.get(DATA_CONTEXT_SQL_EDITOR_STATE)!; | ||
|
||
let dataSource: ISqlDataSource | ResourceSqlDataSource | undefined = this.sqlDataSourceService.get(state.editorId); | ||
|
||
if (!dataSource) { | ||
return; | ||
} | ||
|
||
if (action === ACTION_SAVE_AS_SCRIPT) { | ||
let projectId = dataSource.executionContext?.projectId ?? null; | ||
await this.projectInfoResource.load(CachedMapAllKey); | ||
const name = getSqlEditorName(state, dataSource); | ||
|
||
if (projectId) { | ||
const project = this.projectInfoResource.get(projectId); | ||
|
||
if (!project?.canEditResources) { | ||
projectId = null; | ||
} | ||
} | ||
|
||
const result = await this.commonDialogService.open(SaveScriptDialog, { | ||
defaultScriptName: name, | ||
projectId, | ||
validation: async ({ name, projectId }, setMessage) => { | ||
const trimmedName = name.trim(); | ||
|
||
if (!projectId || !trimmedName.length) { | ||
return false; | ||
} | ||
|
||
if (!RESOURCE_NAME_REGEX.test(trimmedName)) { | ||
setMessage('plugin_resource_manager_scripts_script_name_invalid_characters_message'); | ||
return false; | ||
} | ||
|
||
const project = this.projectInfoResource.get(projectId); | ||
const nameWithExtension = this.projectInfoResource.getNameWithExtension(projectId, SCRIPTS_TYPE_ID, trimmedName); | ||
const rootFolder = project ? this.resourceManagerScriptsService.getRootFolder(project) : undefined; | ||
const key = getRmResourcePath(projectId, rootFolder); | ||
|
||
try { | ||
await this.resourceManagerResource.load(CachedTreeChildrenKey(key)); | ||
return !this.resourceManagerResource.has(createPath(key, nameWithExtension)); | ||
} catch (exception: any) { | ||
return false; | ||
} | ||
}, | ||
}); | ||
|
||
if (result !== DialogueStateResult.Rejected && result !== DialogueStateResult.Resolved) { | ||
try { | ||
projectId = result.projectId; | ||
|
||
if (!projectId) { | ||
throw new Error('Project not selected'); | ||
} | ||
|
||
const project = this.projectInfoResource.get(projectId); | ||
if (!project) { | ||
throw new Error('Project not found'); | ||
} | ||
|
||
const nameWithoutExtension = result.name.trim(); | ||
const scriptName = this.projectInfoResource.getNameWithExtension(projectId, SCRIPTS_TYPE_ID, nameWithoutExtension); | ||
const scriptsRootFolder = this.resourceManagerScriptsService.getRootFolder(project); | ||
const folderResourceKey = getResourceKeyFromNodeId(createPath(RESOURCES_NODE_PATH, projectId, scriptsRootFolder)); | ||
|
||
if (!folderResourceKey) { | ||
this.notificationService.logError({ title: 'ui_error', message: 'plugin_sql_editor_navigation_tab_resource_save_script_error' }); | ||
return; | ||
} | ||
|
||
const resourceKey = createPath(folderResourceKey, scriptName); | ||
|
||
await this.resourceManagerScriptsService.createScript(resourceKey, dataSource.executionContext, dataSource.script); | ||
|
||
dataSource = this.sqlDataSourceService.create(state, ResourceSqlDataSource.key, { | ||
script: dataSource.script, | ||
executionContext: dataSource.executionContext, | ||
}); | ||
|
||
(dataSource as ResourceSqlDataSource).setResourceKey(resourceKey); | ||
|
||
this.notificationService.logSuccess({ | ||
title: 'plugin_sql_editor_navigation_tab_resource_save_script_success', | ||
message: nameWithoutExtension, | ||
}); | ||
|
||
if (!this.resourceManagerScriptsService.active) { | ||
this.resourceManagerScriptsService.togglePanel(); | ||
} | ||
} catch (exception) { | ||
this.notificationService.logException(exception as any, 'plugin_sql_editor_navigation_tab_resource_save_script_error'); | ||
} | ||
} | ||
} | ||
}, | ||
handler: this.saveAsScriptHandler.bind(this), | ||
getActionInfo: (context, action) => { | ||
if (action === ACTION_SAVE_AS_SCRIPT) { | ||
return { | ||
|
@@ -189,6 +93,8 @@ export class PluginBootstrap extends Bootstrap { | |
}, | ||
}); | ||
|
||
this.navigationTabsService.registerAction(ACTION_SAVE_AS_SCRIPT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use SqlEditorView instead |
||
|
||
this.menuService.addCreator({ | ||
menus: [SQL_EDITOR_TOOLS_MENU], | ||
contexts: [DATA_CONTEXT_SQL_EDITOR_STATE], | ||
|
@@ -201,6 +107,115 @@ export class PluginBootstrap extends Bootstrap { | |
}, | ||
getItems: (context, items) => [...items, ACTION_SAVE_AS_SCRIPT], | ||
}); | ||
|
||
this.keyBindingService.addKeyBindingHandler({ | ||
id: 'save-as-script', | ||
binding: KEY_BINDING_SQL_EDITOR_SAVE_AS_SCRIPT, | ||
actions: [ACTION_SAVE_AS_SCRIPT], | ||
contexts: [DATA_CONTEXT_SQL_EDITOR_STATE], | ||
isBindingApplicable: (_, action) => action === ACTION_SAVE_AS_SCRIPT, | ||
handler: this.saveAsScriptHandler.bind(this), | ||
}); | ||
} | ||
|
||
private async saveAsScriptHandler(context: any, action: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add types to context arg. Also no need to check and pass action type as we already know it from a method name. Check it only in handler above |
||
const state = context.get(DATA_CONTEXT_SQL_EDITOR_STATE)!; | ||
|
||
let dataSource: ISqlDataSource | ResourceSqlDataSource | undefined = this.sqlDataSourceService.get(state.editorId); | ||
|
||
if (!dataSource) { | ||
return; | ||
} | ||
|
||
if (action === ACTION_SAVE_AS_SCRIPT) { | ||
let projectId = dataSource.executionContext?.projectId ?? null; | ||
await this.projectInfoResource.load(CachedMapAllKey); | ||
const name = getSqlEditorName(state, dataSource); | ||
|
||
if (projectId) { | ||
const project = this.projectInfoResource.get(projectId); | ||
|
||
if (!project?.canEditResources) { | ||
projectId = null; | ||
} | ||
} | ||
|
||
const result = await this.commonDialogService.open(SaveScriptDialog, { | ||
defaultScriptName: name, | ||
projectId, | ||
validation: async ({ name, projectId }, setMessage) => { | ||
const trimmedName = name.trim(); | ||
|
||
if (!projectId || !trimmedName.length) { | ||
return false; | ||
} | ||
|
||
if (!RESOURCE_NAME_REGEX.test(trimmedName)) { | ||
setMessage('plugin_resource_manager_scripts_script_name_invalid_characters_message'); | ||
return false; | ||
} | ||
|
||
const project = this.projectInfoResource.get(projectId); | ||
const nameWithExtension = this.projectInfoResource.getNameWithExtension(projectId, SCRIPTS_TYPE_ID, trimmedName); | ||
const rootFolder = project ? this.resourceManagerScriptsService.getRootFolder(project) : undefined; | ||
const key = getRmResourcePath(projectId, rootFolder); | ||
|
||
try { | ||
await this.resourceManagerResource.load(CachedTreeChildrenKey(key)); | ||
return !this.resourceManagerResource.has(createPath(key, nameWithExtension)); | ||
} catch (exception: any) { | ||
return false; | ||
} | ||
}, | ||
}); | ||
|
||
if (result !== DialogueStateResult.Rejected && result !== DialogueStateResult.Resolved) { | ||
try { | ||
projectId = result.projectId; | ||
|
||
if (!projectId) { | ||
throw new Error('Project not selected'); | ||
} | ||
|
||
const project = this.projectInfoResource.get(projectId); | ||
if (!project) { | ||
throw new Error('Project not found'); | ||
} | ||
|
||
const nameWithoutExtension = result.name.trim(); | ||
const scriptName = this.projectInfoResource.getNameWithExtension(projectId, SCRIPTS_TYPE_ID, nameWithoutExtension); | ||
const scriptsRootFolder = this.resourceManagerScriptsService.getRootFolder(project); | ||
const folderResourceKey = getResourceKeyFromNodeId(createPath(RESOURCES_NODE_PATH, projectId, scriptsRootFolder)); | ||
|
||
if (!folderResourceKey) { | ||
this.notificationService.logError({ title: 'ui_error', message: 'plugin_sql_editor_navigation_tab_resource_save_script_error' }); | ||
return; | ||
} | ||
|
||
const resourceKey = createPath(folderResourceKey, scriptName); | ||
|
||
await this.resourceManagerScriptsService.createScript(resourceKey, dataSource.executionContext, dataSource.script); | ||
|
||
dataSource = this.sqlDataSourceService.create(state, ResourceSqlDataSource.key, { | ||
script: dataSource.script, | ||
executionContext: dataSource.executionContext, | ||
}); | ||
|
||
(dataSource as ResourceSqlDataSource).setResourceKey(resourceKey); | ||
|
||
this.notificationService.logSuccess({ | ||
title: 'plugin_sql_editor_navigation_tab_resource_save_script_success', | ||
message: nameWithoutExtension, | ||
}); | ||
|
||
if (!this.resourceManagerScriptsService.active) { | ||
this.resourceManagerScriptsService.togglePanel(); | ||
} | ||
} catch (exception) { | ||
this.notificationService.logException(exception as any, 'plugin_sql_editor_navigation_tab_resource_save_script_error'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private canOpenHandler(data: INodeNavigationData, contexts: IExecutionContextProvider<INodeNavigationData>): void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сохранить как скрипт