fix: "Remove accents" command now will remove custom color conf (#289)

This commit is contained in:
Alessio Occhipinti 2018-11-25 15:05:10 +01:00 committed by Mattia Astorino
parent c8a23ef3d0
commit 85238745fe
5 changed files with 41 additions and 42 deletions

View file

@ -0,0 +1,3 @@
export default {
PURGE_KEY: 'Remove accents'
};

View file

@ -1,29 +1,27 @@
import * as vscode from 'vscode';
import {updateAccent} from './../../helpers/settings';
import {getDefaultValues, getAccentsProperties} from './../../helpers/fs';
import consts from './consts';
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
/**
* Assigns colours
*/
const assignColorCustomizations = (colour: string, config: any): void => {
const assignColorCustomizations = (colour: string): Object => {
const accentsProperties = getAccentsProperties();
const newColour = isValidColour(colour) ? colour : undefined;
Object.keys(accentsProperties).forEach(propertyName => {
const accent = accentsProperties[propertyName];
return Object.keys(accentsProperties).reduce((acc: any, propName) => {
const accent = accentsProperties[propName];
let colorProp = newColour;
if (colour && accent.alpha < 100) {
colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
}
if (accent) {
config[propertyName] = colorProp;
}
});
acc[propName] = colorProp;
return acc;
}, {});
};
/**
@ -38,43 +36,32 @@ const isValidColour = (colour: string | null | undefined): boolean =>
const setWorkbenchOptions = (config: any): Thenable<boolean> =>
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true)
.then(() => true, reason => vscode.window.showErrorMessage(reason));
/**
* VSCode command
*/
export default async (accent?: string): Promise<boolean> => {
const themeConfigCommon = getDefaultValues();
const purgeColourKey: string = 'Remove accents';
const shouldUpdateAccent = Boolean(!accent);
let accentToSelect = accent;
// If called without accent shows the quick pick dropdown and wait response
if (!accentToSelect) {
const options: string[] = Object.keys(themeConfigCommon.accents).concat(purgeColourKey);
const accentSelected = await vscode.window.showQuickPick(options);
if (accentSelected === null || accentSelected === undefined) {
return Promise.resolve(null);
}
accentToSelect = accentSelected;
}
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
switch (accentToSelect) {
case purgeColourKey:
assignColorCustomizations(undefined, config);
return setWorkbenchOptions(config)
.then(() => updateAccent(undefined))
.then(() => Promise.resolve(true));
default:
assignColorCustomizations(themeConfigCommon.accents[accentToSelect], config);
return setWorkbenchOptions(config)
.then(() =>
shouldUpdateAccent ? updateAccent(accentToSelect) : Promise.resolve(accentToSelect)
)
switch (accent) {
case consts.PURGE_KEY: {
const newConfig = {
...config,
...assignColorCustomizations(undefined)
};
return setWorkbenchOptions(newConfig)
.then(() => Promise.resolve(true));
}
default: {
const newConfig = {
...config,
...assignColorCustomizations(themeConfigCommon.accents[accent])
};
return setWorkbenchOptions(newConfig)
.then(() => Boolean(accent));
}
}
};

View file

@ -0,0 +1,9 @@
import * as vscode from 'vscode';
import {getDefaultValues} from '../../helpers/fs';
import consts from './consts';
export default async () => {
const themeConfigCommon = getDefaultValues();
const options: string[] = Object.keys(themeConfigCommon.accents).concat(consts.PURGE_KEY);
return vscode.window.showQuickPick(options);
};

View file

@ -1,3 +1,4 @@
export {default as accentsSetter} from './accents-setter';
export {default as accentsQuickPick} from './accents-setter/quick-pick';
export {default as fixIcons} from './theme-icons';
export {default as toggleApplyIcons} from './toggle-apply-icons';

View file

@ -5,13 +5,12 @@ import {
} from 'vscode';
import * as ThemeCommands from './commands';
import {setCustomSetting} from './helpers/settings';
import {setCustomSetting, updateAccent} from './helpers/settings';
import {onChangeConfiguration} from './helpers/configuration-change';
import {changelogMessage, installationMessage} from './helpers/messages';
import checkInstallation from './helpers/check-installation';
import writeChangelog from './helpers/write-changelog';
import {ReleaseNotesWebview} from './webviews/ReleaseNotes';
import handleAutoapply from './helpers/handle-autoapply';
export async function activate(context: ExtensionContext) {
const config = Workspace.getConfiguration();
@ -42,8 +41,8 @@ export async function activate(context: ExtensionContext) {
// Registering commands
Commands.registerCommand('materialTheme.setAccent', async () => {
const wasSet = await ThemeCommands.accentsSetter();
handleAutoapply(wasSet);
const accentPicked = await ThemeCommands.accentsQuickPick();
await updateAccent(accentPicked);
});
Commands.registerCommand('materialTheme.fixIcons', () => ThemeCommands.fixIcons());
Commands.registerCommand('materialTheme.toggleApplyIcons', () => ThemeCommands.toggleApplyIcons());