From 85238745fe61b588b8b3f8f77d9a38a6531647fd Mon Sep 17 00:00:00 2001 From: Alessio Occhipinti Date: Sun, 25 Nov 2018 15:05:10 +0100 Subject: [PATCH] fix: "Remove accents" command now will remove custom color conf (#289) --- extensions/commands/accents-setter/consts.ts | 3 + extensions/commands/accents-setter/index.ts | 63 ++++++++----------- .../commands/accents-setter/quick-pick.ts | 9 +++ extensions/commands/index.ts | 1 + extensions/material.theme.config.ts | 7 +-- 5 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 extensions/commands/accents-setter/consts.ts create mode 100644 extensions/commands/accents-setter/quick-pick.ts diff --git a/extensions/commands/accents-setter/consts.ts b/extensions/commands/accents-setter/consts.ts new file mode 100644 index 0000000..ed3b8fc --- /dev/null +++ b/extensions/commands/accents-setter/consts.ts @@ -0,0 +1,3 @@ +export default { + PURGE_KEY: 'Remove accents' +}; diff --git a/extensions/commands/accents-setter/index.ts b/extensions/commands/accents-setter/index.ts index 8f716c2..272f6bd 100644 --- a/extensions/commands/accents-setter/index.ts +++ b/extensions/commands/accents-setter/index.ts @@ -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 => vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true) .then(() => true, reason => vscode.window.showErrorMessage(reason)); - /** * VSCode command */ export default async (accent?: string): Promise => { 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)); + } } }; diff --git a/extensions/commands/accents-setter/quick-pick.ts b/extensions/commands/accents-setter/quick-pick.ts new file mode 100644 index 0000000..013c0ff --- /dev/null +++ b/extensions/commands/accents-setter/quick-pick.ts @@ -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); +}; diff --git a/extensions/commands/index.ts b/extensions/commands/index.ts index 2c3543a..79ff089 100644 --- a/extensions/commands/index.ts +++ b/extensions/commands/index.ts @@ -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'; diff --git a/extensions/material.theme.config.ts b/extensions/material.theme.config.ts index 24ccd4f..a7568f2 100644 --- a/extensions/material.theme.config.ts +++ b/extensions/material.theme.config.ts @@ -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());