forked from oliverschwendener/ueli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added color converter feature (Fixes oliverschwendener#133)
- Loading branch information
1 parent
db64de2
commit c817415
Showing
28 changed files
with
438 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface ColorConverterOptions { | ||
isEnabled: boolean; | ||
hexEnabled: boolean; | ||
rgbEnabled: boolean; | ||
rgbaEnabled: boolean; | ||
hslEnabled: boolean; | ||
showColorPreview: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ColorConverterOptions } from "./color-converter-options"; | ||
|
||
export const defaultColorConverterOptions: ColorConverterOptions = { | ||
hexEnabled: true, | ||
hslEnabled: true, | ||
isEnabled: true, | ||
rgbEnabled: true, | ||
rgbaEnabled: true, | ||
showColorPreview: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum IconType { | ||
URL = "URL", | ||
SVG = "SVG", | ||
Color = "color", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/plugins/color-converter-plugin/color-converter-helpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { isValidColorCode } from "./color-converter-helpers"; | ||
|
||
describe(isValidColorCode.name, () => { | ||
it("should return true if value is a valid hex code", () => { | ||
const validColorCodes = [ | ||
"#fff", | ||
"#ffffff", | ||
"#FFF", | ||
"#FFFFFF", | ||
"rgb(255,255,255)", | ||
"rgba(255,255,255,0)", | ||
"rgb(1,2,3)", | ||
"rgba(0,0,0,1)", | ||
" #fff", | ||
"#fff ", | ||
]; | ||
|
||
validColorCodes.forEach((validColorCode) => { | ||
const actual = isValidColorCode(validColorCode); | ||
expect(actual).toBe(true); | ||
}); | ||
}); | ||
|
||
it("should return false if value is an invalid hex code", () => { | ||
const invalidColorCodes = [ | ||
"ffffff", | ||
"fff", | ||
"some string", | ||
"", | ||
"undefined", | ||
"null", | ||
"#", | ||
"rgb()", | ||
"rgba()", | ||
"blue", | ||
"black", | ||
"yellow", | ||
]; | ||
|
||
invalidColorCodes.forEach((invalidColorCode) => { | ||
const actual = isValidColorCode(invalidColorCode); | ||
expect(actual).toBe(false); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
src/main/plugins/color-converter-plugin/color-converter-helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as color from "color"; | ||
import { StringHelpers } from "../../../common/helpers/string-helpers"; | ||
|
||
export function isValidColorCode(value: string): boolean { | ||
value = StringHelpers.replaceWhitespace(value.trim(), ""); | ||
try { | ||
color(value); | ||
return value.startsWith("#") | ||
|| value.startsWith("rgb(") | ||
|| value.startsWith("rgba("); | ||
} catch { | ||
return false; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/plugins/color-converter-plugin/color-converter-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ExecutionPlugin } from "../../execution-plugin"; | ||
import { PluginType } from "../../plugin-type"; | ||
import { ColorConverterOptions } from "../../../common/config/color-converter-options"; | ||
import { isValidColorCode } from "./color-converter-helpers"; | ||
import { SearchResultItem } from "../../../common/search-result-item"; | ||
import { TranslationSet } from "../../../common/translation/translation-set"; | ||
import { UserConfigOptions } from "../../../common/config/user-config-options"; | ||
import * as color from "color"; | ||
import { defaultColorConverterIcon } from "../../../common/icon/default-icons"; | ||
import { IconType } from "../../../common/icon/icon-type"; | ||
import { StringHelpers } from "../../../common/helpers/string-helpers"; | ||
|
||
export class ColorConverterPlugin implements ExecutionPlugin { | ||
public pluginType: PluginType.ColorConverter; | ||
private config: ColorConverterOptions; | ||
private readonly clipboardCopier: (value: string) => Promise<void>; | ||
|
||
constructor(config: ColorConverterOptions, clipboardCopier: (value: string) => Promise<void>) { | ||
this.config = config; | ||
this.clipboardCopier = clipboardCopier; | ||
} | ||
|
||
public isValidUserInput(userInput: string, fallback?: boolean | undefined): boolean { | ||
return isValidColorCode(userInput); | ||
} | ||
public getSearchResults(userInput: string, fallback?: boolean | undefined): Promise<SearchResultItem[]> { | ||
return new Promise((resolve, reject) => { | ||
resolve(this.buildSearchResult(userInput)); | ||
}); | ||
} | ||
|
||
public isEnabled(): boolean { | ||
return this.config.isEnabled; | ||
} | ||
|
||
public execute(searchResultItem: SearchResultItem, privileged: boolean): Promise<void> { | ||
return this.clipboardCopier(searchResultItem.executionArgument); | ||
} | ||
|
||
public updateConfig(updatedConfig: UserConfigOptions, translationSet: TranslationSet): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.config = updatedConfig.colorConverterOptions; | ||
resolve(); | ||
}); | ||
} | ||
|
||
private buildSearchResult(value: string): SearchResultItem[] { | ||
const converted = color(StringHelpers.replaceWhitespace(value.trim(), "")); | ||
const result: SearchResultItem[] = []; | ||
|
||
if (this.config.hexEnabled) { | ||
result.push(this.buildColorSearchResult("HEX", converted.hex().toString())); | ||
} | ||
|
||
if (this.config.rgbEnabled) { | ||
result.push(this.buildColorSearchResult("RGB", converted.rgb().toString())); | ||
} | ||
|
||
if (this.config.rgbaEnabled) { | ||
const rgbaString = `rgba(${converted.red()}, ${converted.green()}, ${converted.blue()}, ${converted.alpha()})`; | ||
result.push(this.buildColorSearchResult("RGBA", rgbaString)); | ||
} | ||
|
||
if (this.config.hslEnabled) { | ||
result.push(this.buildColorSearchResult("HSL", converted.hsl().toString())); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private buildColorSearchResult(colorName: string, colorValue: string): SearchResultItem { | ||
return { | ||
description: colorName, | ||
executionArgument: colorValue, | ||
hideMainWindowAfterExecution: true, | ||
icon: this.config.showColorPreview | ||
? { | ||
parameter: colorValue, | ||
type: IconType.Color, | ||
} | ||
: defaultColorConverterIcon, | ||
name: colorValue, | ||
originPluginType: this.pluginType, | ||
searchable: [], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.