vsc-material-theme/extensions/commands/accents-setter/index.ts

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-04-20 20:07:36 +02:00
import * as vscode from 'vscode';
import {getDefaultValues, getAccentsProperties} from './../../helpers/fs';
import consts from './consts';
2018-04-20 20:07:36 +02:00
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
/**
* Assigns colours
*/
const assignColorCustomizations = (colour: string): Object => {
const accentsProperties = getAccentsProperties();
const newColour = isValidColour(colour) ? colour : undefined;
return Object.keys(accentsProperties).reduce((acc: any, propName) => {
const accent = accentsProperties[propName];
let colorProp = newColour;
2018-04-20 20:07:36 +02:00
if (colour && accent.alpha < 100) {
colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
2018-04-20 20:07:36 +02:00
}
acc[propName] = colorProp;
return acc;
}, {});
};
2018-04-20 20:07:36 +02:00
/**
* Determines if a string is a valid colour
*/
const isValidColour = (colour: string | null | undefined): boolean =>
typeof colour === 'string' && REGEXP_HEX.test(colour);
2018-04-20 20:07:36 +02:00
/**
* Sets workbench options
*/
const setWorkbenchOptions = (config: any): Thenable<boolean> =>
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true)
.then(() => true, reason => vscode.window.showErrorMessage(reason));
2018-04-20 20:07:36 +02:00
/**
* VSCode command
*/
export default async (accent?: string): Promise<boolean> => {
const themeConfigCommon = getDefaultValues();
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
2018-04-20 20:07:36 +02:00
switch (accent) {
case consts.PURGE_KEY: {
const newConfig = {
...config,
...assignColorCustomizations(undefined)
};
2018-04-20 20:07:36 +02:00
return setWorkbenchOptions(newConfig)
.then(() => Promise.resolve(true));
}
default: {
const newConfig = {
...config,
...assignColorCustomizations(themeConfigCommon.accents[accent])
};
return setWorkbenchOptions(newConfig)
.then(() => Boolean(accent));
}
}
2018-04-20 20:07:36 +02:00
};