-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Feather Icons (#959)
- Loading branch information
1 parent
4085102
commit b071baa
Showing
12 changed files
with
211 additions
and
98 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
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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
import feather, { FeatherIcon } from 'feather-icons'; | ||
import IconsService from './IconsService'; | ||
import { normalizeColor } from '../logoColor'; | ||
|
||
class FeatherIconsService extends IconsService { | ||
public static async getIcon(slug: string, color: string|null = null): | ||
Promise<{ slug: string; type: string; data: string } | null> { | ||
const normalized = slug.toLowerCase(); | ||
if (!(normalized in feather.icons)) { | ||
return null; | ||
} | ||
// @ts-ignore - icon is checked above | ||
const icon: FeatherIcon = feather.icons[normalized]; | ||
const normalizedColor = normalizeColor(color ?? 'whitesmoke'); | ||
const svg = icon.toSvg({ color: normalizedColor }); | ||
return { | ||
slug: icon.name, | ||
type: 'svg+xml', | ||
data: Buffer.from(svg, 'utf8').toString('base64'), | ||
}; | ||
} | ||
} | ||
|
||
export default FeatherIconsService; |
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,15 @@ | ||
abstract class IconsService { | ||
/** | ||
* Get an icon by slug | ||
* | ||
* @param slug The slug of the icon to get | ||
* @param color The color to set in the SVG or null to use default | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
public static async getIcon(slug: string, color: string|null = null): | ||
Promise<{ slug: string; type: string; data: string } | null> { | ||
throw new Error('Not implemented'); | ||
} | ||
} | ||
|
||
export default IconsService; |
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,24 @@ | ||
import octicons, { IconName } from '@primer/octicons'; | ||
import IconsService from './IconsService'; | ||
import { normalizeColor } from '../logoColor'; | ||
|
||
class OcticonsService extends IconsService { | ||
public static async getIcon(slug: string, color: string|null = null): | ||
Promise<{ slug: string; type: string; data: string } | null> { | ||
const normalized = slug.toLowerCase(); | ||
if (!(normalized in octicons)) { | ||
return null; | ||
} | ||
const icon = octicons[normalized as IconName]; | ||
const normalizedColor = normalizeColor(color ?? 'whitesmoke'); | ||
// add 'xmlns' and 'fill' attribute to the svg | ||
const svg = icon.toSVG().replace('<svg', `<svg xmlns="http://www.w3.org/2000/svg" fill="${normalizedColor}"`); | ||
return { | ||
slug: icon.symbol, | ||
type: 'svg+xml', | ||
data: Buffer.from(svg, 'utf8').toString('base64'), | ||
}; | ||
} | ||
} | ||
|
||
export default OcticonsService; |
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,59 @@ | ||
import monk, { FindResult } from 'monk'; | ||
import IconsService from './IconsService'; | ||
import { setLogoColor } from '../logoColor'; | ||
|
||
const DB_NAME = 'custom-icon-badges'; | ||
const DB_URL = process.env.DB_URL ?? `mongodb://localhost:27017/${DB_NAME}`; | ||
const db = monk(DB_URL); | ||
const icons = db.get('icons'); | ||
icons.createIndex({ slug: 1 }, { unique: true }); | ||
|
||
class IconDatabaseService extends IconsService { | ||
// eslint-disable-next-line no-unused-vars | ||
public static async getIcon(slug: string, color: string|null = null): | ||
Promise<{ slug: string; type: string; data: string } | null> { | ||
// find slug in database, returns null if not found | ||
const icon = await icons.findOne({ slug: slug.toLowerCase() }); | ||
// return null if not found | ||
if (!icon) { | ||
return null; | ||
} | ||
// set color if it is not null | ||
const data = color ? setLogoColor(icon.data, color) : icon.data; | ||
// return icon | ||
return { | ||
slug: icon.slug, | ||
type: icon.type, | ||
data, | ||
}; | ||
} | ||
|
||
/** | ||
* Insert a new icon into the database | ||
* @param {string} slug The slug to use for the icon | ||
* @param {string} type The type of icon to use (eg. 'png', 'svg+xml') | ||
* @param {string} data The base64 encoded data for the icon | ||
* @returns {Object} The icon data | ||
*/ | ||
public static async insertIcon(slug: string, type: string, data: string): | ||
Promise<{ slug: string, type: string, data: string }> { | ||
// create item | ||
const item = { slug: slug.toLowerCase(), type, data }; | ||
// insert item | ||
await icons.insert(item); | ||
// return inserted item | ||
return item; | ||
} | ||
|
||
/** | ||
* Get all icons from the database | ||
* @returns {FindResult} The icons in the database | ||
*/ | ||
public static async getIcons(): | ||
Promise<FindResult<{ slug: string, type: string, data: string }>> { | ||
// return all items | ||
return icons.find({}, { sort: { _id: -1 } }); | ||
} | ||
} | ||
|
||
export default IconDatabaseService; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.