Skip to content

Commit

Permalink
feat: Adds Spritesheets for Custom Emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Jan 8, 2019
1 parent 18e4d82 commit fe3b91d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ emojiFallback = (emoji: any, props: any) => emoji ? `:${emoji.shortNames[0]}:` :

## Custom emojis

You can provide custom emojis which will show up in their own category.
You can provide custom emojis which will show up in their own category. You can either use a single image as imageUrl or use a spritesheet as shown in the second object.

```ts
const customEmojis = [
Expand All @@ -230,6 +230,19 @@ const customEmojis = [
keywords: ['github'],
imageUrl: 'https://assets-cdn.github.com/images/icons/emoji/octocat.png?v7'
},
{
name: 'Test Flag',
short_names: ['test'],
text: '',
emoticons: [],
keywords: ['test', 'flag'],
spriteUrl: 'https://unpkg.com/[email protected]/img/twitter/sheets-256/64.png',
sheet_x: 1,
sheet_y: 1,
size: 64,
sheetColumns: 52,
sheetRows: 52,
},
]
```
```html
Expand Down
12 changes: 12 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ const CUSTOM_EMOJIS = [
keywords: ['github'],
imageUrl: 'https://assets-cdn.github.com/images/icons/emoji/shipit.png?v7',
},
{
name: 'Test Flag',
short_names: ['test'],
keywords: ['test', 'flag'],
spriteUrl:
'https://unpkg.com/[email protected]/img/twitter/sheets-256/64.png',
sheet_x: 1,
sheet_y: 1,
size: 64,
sheetColumns: 52,
sheetRows: 52,
},
];

@Component({
Expand Down
2 changes: 2 additions & 0 deletions src/lib/emoji/data/data.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface EmojiData {
imageUrl?: string;
colons?: string;
skin?: Emoji['skin'];
spriteUrl?: string;
sheetRows?: string;
}

export interface EmojiVariation {
Expand Down
19 changes: 17 additions & 2 deletions src/lib/emoji/emoji.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class EmojiComponent implements OnChanges, Emoji {
@Input() fallback?: Emoji['fallback'];
@Input() hideObsolete = false;
@Input() SHEET_COLUMNS = 52;
@Input() sheetRows: number;
@Input() sheetColumns: number;
@Output() emojiOver: Emoji['emojiOver'] = new EventEmitter();
@Output() emojiLeave: Emoji['emojiLeave'] = new EventEmitter();
@Output() emojiClick: Emoji['emojiClick'] = new EventEmitter();
Expand Down Expand Up @@ -122,9 +124,22 @@ export class EmojiComponent implements OnChanges, Emoji {
width: `${this.size}px`,
height: `${this.size}px`,
display: 'inline-block',
backgroundImage: `url(${data.imageUrl})`,
backgroundSize: 'contain',
};
if (data.spriteUrl) {
this.style = {
...this.style,
backgroundImage: `url(${data.spriteUrl})`,
backgroundSize: `${100 * this.sheetColumns}% ${100 *
this.sheetRows}%`,
backgroundPosition: this.emojiService.getSpritePosition(data.sheet, this.sheetColumns),
}
} else {
this.style = {
...this.style,
backgroundImage: `url(${data.imageUrl})`,
backgroundSize: 'contain',
}
}
} else {
if (data.hidden.length && data.hidden.includes(this.set)) {
if (this.fallback) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/picker/emoji-search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class EmojiSearch {
.filter(a => a);

if (allResults.length > 1) {
results = intersect.apply(null, allResults);
results = intersect.apply(null, allResults as any);
} else if (allResults.length) {
results = allResults[0];
} else {
Expand Down

0 comments on commit fe3b91d

Please sign in to comment.