2018-04-20 20:07:36 +02:00
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
2018-06-28 23:26:07 +02:00
|
|
|
import {getDefaultValues, getAccentsProperties} from './../../helpers/fs';
|
2018-11-25 15:05:10 +01:00
|
|
|
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
|
|
|
|
*/
|
2018-11-25 15:05:10 +01:00
|
|
|
const assignColorCustomizations = (colour: string): Object => {
|
2018-06-28 23:26:07 +02:00
|
|
|
const accentsProperties = getAccentsProperties();
|
2018-05-03 11:39:51 +02:00
|
|
|
const newColour = isValidColour(colour) ? colour : undefined;
|
2018-11-25 15:05:10 +01:00
|
|
|
return Object.keys(accentsProperties).reduce((acc: any, propName) => {
|
|
|
|
const accent = accentsProperties[propName];
|
2018-05-03 11:39:51 +02:00
|
|
|
let colorProp = newColour;
|
2018-04-20 20:07:36 +02:00
|
|
|
|
|
|
|
if (colour && accent.alpha < 100) {
|
2018-05-03 11:39:51 +02:00
|
|
|
colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
|
2018-04-20 20:07:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-25 15:05:10 +01:00
|
|
|
acc[propName] = colorProp;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2018-05-03 11:39:51 +02:00
|
|
|
};
|
2018-04-20 20:07:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a string is a valid colour
|
|
|
|
*/
|
2018-05-03 11:39:51 +02:00
|
|
|
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
|
|
|
|
*/
|
2018-08-20 22:19:10 +02:00
|
|
|
const setWorkbenchOptions = (config: any): Thenable<boolean> =>
|
2018-05-18 13:47:22 +02:00
|
|
|
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true)
|
2018-08-20 22:19:10 +02:00
|
|
|
.then(() => true, reason => vscode.window.showErrorMessage(reason));
|
2018-04-20 20:07:36 +02:00
|
|
|
/**
|
|
|
|
* VSCode command
|
|
|
|
*/
|
2018-08-20 22:19:10 +02:00
|
|
|
export default async (accent?: string): Promise<boolean> => {
|
2018-05-03 11:39:51 +02:00
|
|
|
const themeConfigCommon = getDefaultValues();
|
2018-11-25 15:05:10 +01:00
|
|
|
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-11-25 15:05:10 +01:00
|
|
|
switch (accent) {
|
|
|
|
case consts.PURGE_KEY: {
|
|
|
|
const newConfig = {
|
|
|
|
...config,
|
|
|
|
...assignColorCustomizations(undefined)
|
|
|
|
};
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-11-25 15:05:10 +01:00
|
|
|
return setWorkbenchOptions(newConfig)
|
|
|
|
.then(() => Promise.resolve(true));
|
2018-08-20 22:19:10 +02:00
|
|
|
}
|
2018-11-25 15:05:10 +01:00
|
|
|
default: {
|
|
|
|
const newConfig = {
|
|
|
|
...config,
|
|
|
|
...assignColorCustomizations(themeConfigCommon.accents[accent])
|
|
|
|
};
|
2018-08-20 22:19:10 +02:00
|
|
|
|
2018-11-25 15:05:10 +01:00
|
|
|
return setWorkbenchOptions(newConfig)
|
|
|
|
.then(() => Boolean(accent));
|
|
|
|
}
|
2018-05-18 13:47:22 +02:00
|
|
|
}
|
2018-04-20 20:07:36 +02:00
|
|
|
|
2018-05-03 11:39:51 +02:00
|
|
|
};
|