|
1 | 1 | import * as vscode from 'vscode' |
2 | 2 | import { PanelProvider } from '@/views/panel/backend/panel-provider' |
3 | | -import { dictionary } from '@shared/constants/dictionary' |
4 | 3 | import { DuplicateWebConfigurationMessage } from '@/views/panel/types/messages' |
5 | | -import { ConfigWebConfigurationFormat } from '@/views/utils/web-configuration-format-converters' |
6 | | - |
7 | | -const generate_unique_name = ( |
8 | | - base_name: string | undefined, |
9 | | - all_web_configurations: ConfigWebConfigurationFormat[] |
10 | | -): string => { |
11 | | - const web_configuration_name = base_name ?? '' |
12 | | - |
13 | | - const parenthetical_match = web_configuration_name.match(/^(.*?)(?:\s*\((\d+)\))?$/) |
14 | | - const original_base_name = parenthetical_match?.[1]?.trim() |
15 | | - const existing_number_str = parenthetical_match?.[2] |
16 | | - |
17 | | - const base_for_duplication = |
18 | | - existing_number_str !== undefined && original_base_name !== undefined |
19 | | - ? original_base_name |
20 | | - : web_configuration_name.trim() |
21 | | - |
22 | | - let copy_number = |
23 | | - existing_number_str !== undefined |
24 | | - ? parseInt(existing_number_str, 10) + 1 |
25 | | - : 1 |
26 | | - |
27 | | - let new_name = base_for_duplication |
28 | | - ? `${base_for_duplication} (${copy_number})` |
29 | | - : `(${copy_number})` |
30 | | - |
31 | | - while (all_web_configurations.some((p) => p.name == new_name)) { |
32 | | - copy_number++ |
33 | | - new_name = base_for_duplication |
34 | | - ? `${base_for_duplication} (${copy_number})` |
35 | | - : `(${copy_number})` |
36 | | - } |
37 | | - return new_name |
38 | | -} |
| 4 | +import { duplicate_web_configuration } from '@/views/utils/duplicate-web-configuration' |
39 | 5 | export const handle_duplicate_web_configuration = async ( |
40 | 6 | panel_provider: PanelProvider, |
41 | 7 | message: DuplicateWebConfigurationMessage, |
42 | 8 | webview_view: vscode.WebviewView |
43 | 9 | ): Promise<void> => { |
44 | | - const original_index = message.index |
45 | | - const config = vscode.workspace.getConfiguration('codeWebChat') |
46 | | - const current_web_configurations = |
47 | | - config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || [] |
48 | | - |
49 | | - if (original_index < 0 || original_index >= current_web_configurations.length) { |
50 | | - vscode.window.showErrorMessage( |
51 | | - dictionary.error_message.PRESET_NOT_FOUND(`at index ${original_index}`) |
52 | | - ) |
53 | | - return |
54 | | - } |
55 | | - |
56 | | - const web_configuration_to_duplicate = current_web_configurations[original_index] |
57 | | - |
58 | | - const new_name = generate_unique_name( |
59 | | - web_configuration_to_duplicate.name, |
60 | | - current_web_configurations |
61 | | - ) |
62 | | - |
63 | | - const duplicated_web_configuration = { |
64 | | - ...web_configuration_to_duplicate, |
65 | | - name: new_name |
66 | | - } |
67 | | - |
68 | | - const updated_web_configurations = [...current_web_configurations] |
69 | | - updated_web_configurations.splice(original_index + 1, 0, duplicated_web_configuration) |
70 | | - |
71 | | - try { |
72 | | - await config.update('webConfigurations', updated_web_configurations, true) |
73 | | - panel_provider.send_web_configurations_to_webview(webview_view.webview) |
74 | | - } catch (error) { |
75 | | - vscode.window.showErrorMessage( |
76 | | - dictionary.error_message.FAILED_TO_DUPLICATE_PRESET(error) |
77 | | - ) |
78 | | - } |
| 10 | + await duplicate_web_configuration({ index: message.index }) |
| 11 | + panel_provider.send_web_configurations_to_webview(webview_view.webview) |
79 | 12 | } |
0 commit comments