|
1 | 1 | import { window, commands, workspace, Selection, TextEditorRevealType, env } from 'vscode' |
2 | | -import { Global, Commands } from '../core' |
| 2 | +import { Global, Commands, LocaleRecord } from '../core' |
3 | 3 | import { ExtensionModule } from '../modules' |
4 | 4 | import { LocaleTreeItem, Node } from '../views/LocalesTreeView' |
5 | 5 | import * as path from 'path' |
| 6 | +import { flatten } from 'lodash' |
6 | 7 |
|
7 | 8 | async function getRecordFromNode (node: Node, defaultLocale?: string) { |
8 | 9 | if (node.type === 'tree') |
@@ -125,6 +126,72 @@ const m: ExtensionModule = (ctx) => { |
125 | 126 | window.showErrorMessage(err.toString()) |
126 | 127 | } |
127 | 128 | }), |
| 129 | + |
| 130 | + commands.registerCommand(Commands.rename_key, |
| 131 | + async ({ node }: LocaleTreeItem) => { |
| 132 | + let records: LocaleRecord[] = [] |
| 133 | + |
| 134 | + if (node.type === 'tree') |
| 135 | + return |
| 136 | + |
| 137 | + else if (node.type === 'record') |
| 138 | + records = [node] |
| 139 | + |
| 140 | + else |
| 141 | + records = Object.values(node.locales) |
| 142 | + |
| 143 | + try { |
| 144 | + const newkeypath = await window.showInputBox({ |
| 145 | + value: node.keypath, |
| 146 | + prompt: 'Enter the new keypath', |
| 147 | + }) |
| 148 | + |
| 149 | + if (newkeypath !== undefined && newkeypath !== node.keypath) { |
| 150 | + const writes = flatten(records |
| 151 | + .map(record => [{ |
| 152 | + value: undefined, |
| 153 | + keypath: record.keypath, |
| 154 | + filepath: record.filepath, |
| 155 | + locale: record.locale, |
| 156 | + }, { |
| 157 | + value: record.value, |
| 158 | + keypath: newkeypath, |
| 159 | + filepath: record.filepath, |
| 160 | + locale: record.locale, |
| 161 | + }])) |
| 162 | + await Global.loader.writeToFile(writes) |
| 163 | + } |
| 164 | + } |
| 165 | + catch (err) { |
| 166 | + window.showErrorMessage(err.toString()) |
| 167 | + } |
| 168 | + }), |
| 169 | + |
| 170 | + commands.registerCommand(Commands.delete_key, |
| 171 | + async ({ node }: LocaleTreeItem) => { |
| 172 | + let records: LocaleRecord[] = [] |
| 173 | + |
| 174 | + if (node.type === 'tree') |
| 175 | + return |
| 176 | + |
| 177 | + else if (node.type === 'record') |
| 178 | + records = [node] |
| 179 | + |
| 180 | + else |
| 181 | + records = Object.values(node.locales) |
| 182 | + |
| 183 | + try { |
| 184 | + await Global.loader.writeToFile(records.map(record => ({ |
| 185 | + value: undefined, |
| 186 | + keypath: record.keypath, |
| 187 | + filepath: record.filepath, |
| 188 | + locale: record.locale, |
| 189 | + }))) |
| 190 | + } |
| 191 | + catch (err) { |
| 192 | + window.showErrorMessage(err.toString()) |
| 193 | + } |
| 194 | + }), |
128 | 195 | ] |
129 | 196 | } |
130 | 197 |
|
|
0 commit comments