-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstate.ts
146 lines (123 loc) · 5.95 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as vscode from 'vscode';
import * as deepl from './deepl';
import * as debug from './debug';
import { TranslationMode, type ExtensionState } from './types';
import { reactive, effect, ref } from '@vue/reactivity';
import { getDefaultSourceLanguage, getDefaultTargetLanguage } from './helper';
import {
DEEPL_CONFIGURATION_SECTION,
CONFIG_API_KEY,
CONFIG_FORMALITY,
CONFIG_IGNORE_TAGS,
CONFIG_TAG_HANDLING,
CONFIG_SPLITTING_TAGS,
CONFIG_SPLIT_SENTENCES,
CONFIG_NON_SPLITTING_TAGS,
CONFIG_PRESERVE_FORMATTING,
CONFIG_GLOSSARY_ID,
WORKSPACE_TARGET_LANGUAGE,
WORKSPACE_SOURCE_LANGUAGE,
CONFIG_TRANSLATION_MODE
} from './constants';
import { SourceLanguageCode, TargetLanguageCode } from 'deepl-node';
const initialized = ref(false);
export const state = reactive<ExtensionState>({
apiKey: undefined,
targetLanguage: undefined,
sourceLanguage: undefined,
tagHandling: undefined,
ignoreTags: undefined,
nonSplittingTags: undefined,
splittingTags: undefined,
preserveFormatting: undefined,
formality: undefined,
splitSentences: undefined,
glossaryId: undefined,
translationMode: TranslationMode.Replace
});
const loadExtensionState = async (context: vscode.ExtensionContext) => {
const config = vscode.workspace.getConfiguration();
debug.write('Loading extension state...');
state.apiKey = await context.secrets.get(CONFIG_API_KEY);
state.formality = config.get(CONFIG_FORMALITY) || undefined;
state.glossaryId = config.get(CONFIG_GLOSSARY_ID) || undefined;
state.ignoreTags = config.get(CONFIG_IGNORE_TAGS) || undefined;
state.tagHandling = config.get(CONFIG_TAG_HANDLING) || undefined;
state.splittingTags = config.get(CONFIG_SPLITTING_TAGS) || undefined;
state.splitSentences = config.get(CONFIG_SPLIT_SENTENCES) || undefined;
state.translationMode = config.get(CONFIG_TRANSLATION_MODE) || TranslationMode.Replace;
state.nonSplittingTags = config.get(CONFIG_NON_SPLITTING_TAGS) || undefined;
state.preserveFormatting = config.get(CONFIG_PRESERVE_FORMATTING) || undefined;
state.targetLanguage = context.workspaceState.get<TargetLanguageCode>(WORKSPACE_TARGET_LANGUAGE) || getDefaultTargetLanguage(config);
state.sourceLanguage = context.workspaceState.get<SourceLanguageCode>(WORKSPACE_SOURCE_LANGUAGE) || getDefaultSourceLanguage(config);
debug.write(`Loaded extension state:`);
debug.write(JSON.stringify(state, null, 2));
};
const migrateApiKeyFromConfigToSecrets = async (config: vscode.WorkspaceConfiguration, context: vscode.ExtensionContext) => {
const apiKeyFromConfiguration = config.get<string>(CONFIG_API_KEY);
if (!apiKeyFromConfiguration) {
return;
}
await context.secrets.store(CONFIG_API_KEY, apiKeyFromConfiguration);
config.update(CONFIG_API_KEY, apiKeyFromConfiguration, vscode.ConfigurationTarget.Global);
debug.write('Moved api key from configuration to secret store.');
};
const migrateWorkspaceStates = async (context: vscode.ExtensionContext) => {
const sourceLanguageToMigrate = context.workspaceState.get<string>('deepl:sourceLanguage');
if (sourceLanguageToMigrate) {
context.workspaceState.update(WORKSPACE_SOURCE_LANGUAGE, sourceLanguageToMigrate);
context.workspaceState.update('deepl:sourceLanguage', undefined);
debug.write('Moved source language to new workspace state key');
}
const targetLanguageToMigrate = context.workspaceState.get<string>('deepl:targetLanguage');
if (targetLanguageToMigrate) {
context.workspaceState.update(WORKSPACE_TARGET_LANGUAGE, sourceLanguageToMigrate);
context.workspaceState.update('deepl:targetLanguage', undefined);
debug.write('Moved target language to new workspace state key');
}
};
export async function setup(context: vscode.ExtensionContext) {
if (initialized.value) {
return;
}
initialized.value = true;
const config = vscode.workspace.getConfiguration();
await migrateWorkspaceStates(context);
await migrateApiKeyFromConfigToSecrets(config, context);
effect(async () => {
if (state.apiKey) {
await Promise.all([
deepl.getSourceLanguages(),
deepl.getTargetLanguages()
]);
}
});
await loadExtensionState(context);
effect(() => state.apiKey ? context.secrets.store(CONFIG_API_KEY, state.apiKey) : context.secrets.delete(CONFIG_API_KEY));
effect(() => config.update(CONFIG_FORMALITY, state.formality, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_IGNORE_TAGS, state.ignoreTags, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_TAG_HANDLING, state.tagHandling, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_SPLITTING_TAGS, state.splittingTags, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_NON_SPLITTING_TAGS, state.nonSplittingTags, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_SPLIT_SENTENCES, state.splitSentences, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_PRESERVE_FORMATTING, state.preserveFormatting, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_GLOSSARY_ID, state.glossaryId, vscode.ConfigurationTarget.Global));
effect(() => config.update(CONFIG_TRANSLATION_MODE, state.translationMode, vscode.ConfigurationTarget.Global));
effect(() => context.workspaceState.update(WORKSPACE_TARGET_LANGUAGE, state.targetLanguage));
effect(() => context.workspaceState.update(WORKSPACE_SOURCE_LANGUAGE, state.sourceLanguage));
const secretChangeListener = context.secrets.onDidChange((e) => {
if (e.key !== CONFIG_API_KEY) {
return;
}
debug.write(`Secret (${CONFIG_API_KEY}) has been changed!`);
loadExtensionState(context);
});
const configurationChangeListener = vscode.workspace.onDidChangeConfiguration(e => {
if (!e.affectsConfiguration(DEEPL_CONFIGURATION_SECTION)) {
return;
}
debug.write(`Extension configuration has been changed!`);
loadExtensionState(context);
});
context.subscriptions.push(secretChangeListener, configurationChangeListener);
}