Skip to content

Commit

Permalink
fix: properly code config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 11, 2024
1 parent a4d3be7 commit b537f97
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 73 deletions.
57 changes: 37 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ _React Bindings_
- [Important Notes](#important-notes-1)
- [Live Demo](#live-demo-5)
- [Notes](#notes)
- [TypeScript module augments](#typescript-module-augments)
- [Avatar shortcuts](#avatar-shortcuts)
- [Profile shortcuts](#profile-shortcuts)
- [Theming](#theming)
Expand Down Expand Up @@ -347,23 +346,6 @@ that the browser can support. The live demo below uses Vite.

## Notes

### TypeScript module augments

This module uses a custom object on the browser `window` for configuration.
Under normal circumstances by simply importing the package (with
`import @skyra/discord-components-core`) the module augmentations should also be
loaded. If for whatever reason this does not happen, then you can define them
manually yourself. You can do so with the following code snippet:

```ts
import type { DiscordMessageOptions } from '@skyra/discord-components-core';

declare global {
// eslint-disable-next-line no-var
var $discordMessage: DiscordMessageOptions | undefined;
}
```

### Avatar shortcuts

The current avatar shortcut strings available are "blue" (default), "gray",
Expand All @@ -380,7 +362,8 @@ The current avatar shortcut strings available are "blue" (default), "gray",
```

If you want to add to or override the shortcuts, you can set them via
`window.$discordMessage.avatars`.
`window.$discordMessage.avatars` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -392,11 +375,24 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
avatars: {
default: 'blue',
skyra: 'https://github.com/NM-EEA-Y.png',
djs: require('./assets/discord-avatar-djs.png') // You can use require syntax as well
}
});
```

### Profile shortcuts

Sometimes you'll want to use the same message data across multiple messages. You
can do so by providing an object of profiles in
`window.$discordMessage.profiles`.
`window.$discordMessage.profiles` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -417,6 +413,27 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
profiles: {
skyra: {
author: 'Skyra',
avatar: 'https://github.com/NM-EEA-Y.png',
bot: true,
verified: true,
roleColor: '#1e88e5'
},
favna: {
author: 'Favna',
avatar: 'https://github.com/favna.png',
roleColor: '#ff0000'
}
}
});
```

And then in your code:

```tsx
Expand Down
41 changes: 38 additions & 3 deletions assets/readme-templates/CORE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### TypeScript module augments

This module uses a custom object on the browser `window` for configuration.
This library uses a custom object on the browser `window` for configuration.
Under normal circumstances by simply importing the package (with
`import @skyra/discord-components-core`) the module augmentations should also be
loaded. If for whatever reason this does not happen, then you can define them
Expand Down Expand Up @@ -35,7 +35,8 @@ The current avatar shortcut strings available are "blue" (default), "gray",
```

If you want to add to or override the shortcuts, you can set them via
`window.$discordMessage.avatars`.
`window.$discordMessage.avatars` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -47,11 +48,24 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
avatars: {
default: 'blue',
skyra: 'https://github.com/NM-EEA-Y.png',
djs: require('./assets/discord-avatar-djs.png') // You can use require syntax as well
}
});
```

### Profile shortcuts

Sometimes you'll want to use the same message data across multiple messages. You
can do so by providing an object of profiles in
`window.$discordMessage.profiles`.
`window.$discordMessage.profiles` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -72,6 +86,27 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
profiles: {
skyra: {
author: 'Skyra',
avatar: 'https://github.com/NM-EEA-Y.png',
bot: true,
verified: true,
roleColor: '#1e88e5'
},
favna: {
author: 'Favna',
avatar: 'https://github.com/favna.png',
roleColor: '#ff0000'
}
}
});
```

And then in your code:

```tsx
Expand Down
49 changes: 43 additions & 6 deletions assets/readme-templates/REACT_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

### TypeScript module augments

This module uses a custom object on the browser `window` for configuration. In
order to this you will need to include the following snippet in your source code
when working in TypeScript:
This library uses a custom object on the browser `window` for configuration.
Under normal circumstances by simply importing the package (with
`import @skyra/discord-components-react`) the module augmentations should also
be loaded. If for whatever reason this does not happen, then you can define them
manually yourself. You can do so with the following code snippet:

```ts
import type { DiscordMessageOptions } from '@skyra/discord-components-core/dist/types/options';
import type { DiscordMessageOptions } from '@skyra/discord-components-react';

declare global {
// eslint-disable-next-line no-var
Expand All @@ -33,7 +35,8 @@ The current avatar shortcut strings available are "blue" (default), "gray",
```

If you want to add to or override the shortcuts, you can set them via
`window.$discordMessage.avatars`.
`window.$discordMessage.avatars` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-react'`).

```ts
window.$discordMessage = {
Expand All @@ -45,11 +48,24 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-react';

setConfig({
avatars: {
default: 'blue',
skyra: 'https://github.com/NM-EEA-Y.png',
djs: require('./assets/discord-avatar-djs.png') // You can use require syntax as well
}
});
```

### Profile shortcuts

Sometimes you'll want to use the same message data across multiple messages. You
can do so by providing an object of profiles in
`window.$discordMessage.profiles`.
`window.$discordMessage.profiles` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-react'`).

```ts
window.$discordMessage = {
Expand All @@ -70,6 +86,27 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-react';

setConfig({
profiles: {
skyra: {
author: 'Skyra',
avatar: 'https://github.com/NM-EEA-Y.png',
bot: true,
verified: true,
roleColor: '#1e88e5'
},
favna: {
author: 'Favna',
avatar: 'https://github.com/favna.png',
roleColor: '#ff0000'
}
}
});
```

And then in your React code:

```tsx
Expand Down
57 changes: 37 additions & 20 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ _React Bindings_
- [Important Notes](#important-notes-1)
- [Live Demo](#live-demo-5)
- [Notes](#notes)
- [TypeScript module augments](#typescript-module-augments)
- [Avatar shortcuts](#avatar-shortcuts)
- [Profile shortcuts](#profile-shortcuts)
- [Theming](#theming)
Expand Down Expand Up @@ -355,23 +354,6 @@ that the browser can support. The live demo below uses Vite.

## Notes

### TypeScript module augments

This module uses a custom object on the browser `window` for configuration.
Under normal circumstances by simply importing the package (with
`import @skyra/discord-components-core`) the module augmentations should also be
loaded. If for whatever reason this does not happen, then you can define them
manually yourself. You can do so with the following code snippet:

```ts
import type { DiscordMessageOptions } from '@skyra/discord-components-core';

declare global {
// eslint-disable-next-line no-var
var $discordMessage: DiscordMessageOptions | undefined;
}
```

### Avatar shortcuts

The current avatar shortcut strings available are "blue" (default), "gray",
Expand All @@ -388,7 +370,8 @@ The current avatar shortcut strings available are "blue" (default), "gray",
```

If you want to add to or override the shortcuts, you can set them via
`window.$discordMessage.avatars`.
`window.$discordMessage.avatars` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -400,11 +383,24 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
avatars: {
default: 'blue',
skyra: 'https://github.com/NM-EEA-Y.png',
djs: require('./assets/discord-avatar-djs.png') // You can use require syntax as well
}
});
```

### Profile shortcuts

Sometimes you'll want to use the same message data across multiple messages. You
can do so by providing an object of profiles in
`window.$discordMessage.profiles`.
`window.$discordMessage.profiles` or by using the `setConfig` function
(`import { setConfig } from '@skyra/discord-components-core'`).

```ts
window.$discordMessage = {
Expand All @@ -425,6 +421,27 @@ window.$discordMessage = {
};
```

```ts
import { setConfig } from '@skyra/discord-components-core';

setConfig({
profiles: {
skyra: {
author: 'Skyra',
avatar: 'https://github.com/NM-EEA-Y.png',
bot: true,
verified: true,
roleColor: '#1e88e5'
},
favna: {
author: 'Favna',
avatar: 'https://github.com/favna.png',
roleColor: '#ff0000'
}
}
});
```

And then in your code:

```tsx
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ export const defaultDiscordAvatars: Omit<Avatars, 'default'> = {
pink: 'https://cdn.discordapp.com/embed/avatars/5.png'
};

const globalAvatars: Avatars = globalThis.$discordMessage?.avatars ?? ({} as Avatars);
const globalAvatars: Avatars = getConfig().avatars ?? ({} as Avatars);

export const avatars: Avatars = Object.assign(defaultDiscordAvatars, globalAvatars, {
default: defaultDiscordAvatars[globalAvatars.default] ?? globalAvatars.default ?? defaultDiscordAvatars.blue
});

export const profiles: { [key: string]: Profile } = globalThis.$discordMessage?.profiles ?? {};
export const profiles: { [key: string]: Profile } = getConfig().profiles ?? {};

export const defaultTheme: string = globalThis.$discordMessage?.defaultTheme === 'light' ? 'light' : 'dark';
export const defaultTheme: string = getConfig().defaultTheme === 'light' ? 'light' : 'dark';

export const defaultMode: string = globalThis.$discordMessage?.defaultMode === 'compact' ? 'compact' : 'cozy';
export const defaultMode: string = getConfig().defaultMode === 'compact' ? 'compact' : 'cozy';

export const defaultBackground: string = globalThis.$discordMessage?.defaultBackground === 'none' ? 'none' : 'discord';
export const defaultBackground: string = getConfig().defaultBackground === 'none' ? 'none' : 'discord';
3 changes: 2 additions & 1 deletion packages/core/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getConfig } from './config.js';
import type { Emoji, DiscordTimestamp } from './types.js';

const intlDateFormat = new Intl.DateTimeFormat('en-US', { day: '2-digit', month: '2-digit', year: 'numeric' });
Expand Down Expand Up @@ -29,4 +30,4 @@ export const validateImageExtension = (url: string) => {
if (!IMAGE_EXTENSION.test(url)) throw new Error(`The url of an image for discord-attachment should match the regex ${IMAGE_EXTENSION}`);
};

export const getGlobalEmojiUrl = (emojiName: string): Emoji | undefined => window.$discordMessage?.emojis?.[emojiName];
export const getGlobalEmojiUrl = (emojiName: string): Emoji | undefined => getConfig().emojis?.[emojiName];
Loading

0 comments on commit b537f97

Please sign in to comment.