@udixio/theme 1.0.0-beta.25 → 1.0.0-beta.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@udixio/theme",
3
- "version": "1.0.0-beta.25",
3
+ "version": "1.0.0-beta.26",
4
4
  "license": "MIT",
5
5
  "author": "vigreux-joël",
6
6
  "exports": {
@@ -1,6 +1,6 @@
1
1
  import { AddColorsOptions } from '../color';
2
2
  import { VariantEntity } from '../theme';
3
- import { PluginConstructor } from '../plugin';
3
+ import { PluginAbstract } from '../plugin';
4
4
 
5
5
  export interface ConfigInterface {
6
6
  sourceColor: string;
@@ -10,5 +10,5 @@ export interface ConfigInterface {
10
10
  colors?: AddColorsOptions | AddColorsOptions[];
11
11
  useDefaultColors?: boolean;
12
12
  palettes?: Record<string, string>;
13
- plugins?: (PluginConstructor | [PluginConstructor, object])[];
13
+ plugins?: PluginAbstract<any, any>[];
14
14
  }
@@ -56,11 +56,7 @@ export class ConfigService {
56
56
  }
57
57
  if (plugins) {
58
58
  plugins.forEach((plugin) => {
59
- if (Array.isArray(plugin)) {
60
- pluginService.addPlugin(plugin[0], plugin[1]);
61
- } else {
62
- pluginService.addPlugin(plugin, {});
63
- }
59
+ pluginService.addPlugin(plugin);
64
60
  });
65
61
  pluginService.loadPlugins(this.appService);
66
62
  }
@@ -1,3 +1,3 @@
1
- export * from './plugin.abstract';
1
+ export * from './pluginAbstract';
2
2
  export * from './plugin.module';
3
3
  export * from './plugin.service';
@@ -1,47 +1,41 @@
1
- import { PluginAbstract } from './plugin.abstract';
1
+ import { PluginAbstract } from './pluginAbstract';
2
2
  import { AppService } from '../app.service';
3
3
 
4
- export type PluginConstructor = (new (
5
- appService: AppService,
6
- options: any
7
- ) => PluginAbstract) & { dependencies: PluginConstructor[] };
8
-
9
4
  export class PluginService {
10
- private pluginInstances = new Map<string, PluginAbstract>();
11
- private pluginConstructors = new Map<string, [PluginConstructor, object]>();
5
+ private plugins = new Map<string, PluginAbstract<any, any>>();
12
6
 
13
- public addPlugin(plugin: PluginConstructor, config: object) {
14
- this.pluginConstructors.set(plugin.name, [plugin, config]);
7
+ public addPlugin(plugin: PluginAbstract<any, any>) {
8
+ this.plugins.set(plugin.name, plugin);
15
9
  }
16
10
 
17
11
  public loadPlugins(appService: AppService) {
18
- const plugins = new Map(this.pluginConstructors);
12
+ const plugins = new Map(this.plugins);
19
13
 
20
14
  let size = 0;
21
15
  do {
22
16
  size = plugins.size;
23
- plugins.forEach(([plugin, option], key) => {
17
+ plugins.forEach((plugin, key) => {
24
18
  const deps = plugin.dependencies.filter(
25
- (dep) => !this.pluginInstances.has(dep.name)
19
+ (dep) => !this.plugins.has(new dep().name)
26
20
  );
27
21
  if (deps.length === 0) {
28
- this.pluginInstances.set(plugin.name, new plugin(appService, option));
22
+ this.plugins.set(plugin.name, plugin.init(appService));
29
23
  plugins.delete(key);
30
24
  }
31
25
  });
32
26
  } while (plugins.size != 0 && plugins.size < size);
33
27
 
34
28
  if (plugins.size > 0)
35
- console.log(
36
- "Some plugins couldn't be loaded due to missing dependencies: ",
37
- Array.from(plugins.keys())
29
+ throw new Error(
30
+ "Some plugins couldn't be loaded due to missing dependencies: " +
31
+ Array.from(plugins.keys())
38
32
  );
39
33
  }
40
34
 
41
- public getPlugin<T extends PluginAbstract>(
42
- plugin: new (appService: AppService, options: any) => T
35
+ public getPlugin<T extends PluginAbstract<any, any>>(
36
+ plugin: new (...args: any) => T
43
37
  ): T {
44
- const pluginInstance = this.pluginInstances.get(plugin.name);
38
+ const pluginInstance = this.plugins.get(new plugin().name);
45
39
  if (!pluginInstance) throw new Error(`Plugin ${plugin.name} not found`);
46
40
  return pluginInstance as T;
47
41
  }
@@ -0,0 +1,43 @@
1
+ import { AppService } from '../app.service';
2
+
3
+ export type PluginConstructor<Plugin extends PluginImplAbstract<any>> = new (
4
+ ...args: any
5
+ ) => Plugin;
6
+
7
+ export abstract class PluginAbstract<
8
+ Plugin extends PluginImplAbstract<Options>,
9
+ Options extends object
10
+ > {
11
+ public abstract readonly dependencies: (new (...args: any) => PluginAbstract<
12
+ any,
13
+ any
14
+ >)[];
15
+ public abstract readonly name: string;
16
+ public options: Options;
17
+ public abstract readonly pluginClass: PluginConstructor<Plugin>;
18
+ protected pluginInstance: Plugin | undefined;
19
+
20
+ constructor(options: Options) {
21
+ this.options = options;
22
+ }
23
+
24
+ public init(appService: AppService) {
25
+ this.pluginInstance = new this.pluginClass(appService, this.options);
26
+ return this;
27
+ }
28
+
29
+ public getInstance(): Plugin {
30
+ if (!this.pluginInstance) {
31
+ throw new Error(`Plugin ${this.name} is not initialized`);
32
+ }
33
+ return this.pluginInstance;
34
+ }
35
+ }
36
+
37
+ export abstract class PluginImplAbstract<Options extends object> {
38
+ constructor(protected appService: AppService, protected options: Options) {
39
+ this.onInit();
40
+ }
41
+
42
+ abstract onInit(): void;
43
+ }
@@ -1,5 +1,4 @@
1
- import { AppService } from '../../app.service';
2
- import { PluginAbstract } from '../../plugin';
1
+ import { PluginAbstract, PluginImplAbstract } from '../../plugin';
3
2
 
4
3
  export enum FontFamily {
5
4
  Expressive = 'expressive',
@@ -22,18 +21,58 @@ interface FontPluginOptions {
22
21
  fontStyles?: Partial<Record<FontRole, Record<FontSize, Partial<FontStyle>>>>;
23
22
  }
24
23
 
25
- export class FontPlugin extends PluginAbstract {
26
- private readonly fontFamily: { expressive: string[]; neutral: string[] };
27
- private readonly fontStyles: Record<FontRole, Record<FontSize, FontStyle>>;
24
+ export class FontPlugin extends PluginAbstract<
25
+ FontPluginImpl,
26
+ FontPluginOptions
27
+ > {
28
+ dependencies = [];
29
+ name = 'font';
30
+ pluginClass = FontPluginImpl;
31
+ }
32
+
33
+ class FontPluginImpl extends PluginImplAbstract<FontPluginOptions> {
34
+ private _fontFamily: { expressive: string[]; neutral: string[] } | undefined;
35
+
36
+ get fontFamily(): { expressive: string[]; neutral: string[] } {
37
+ if (!this._fontFamily) throw new Error('Font family not initialized');
38
+ return this._fontFamily;
39
+ }
40
+
41
+ set fontFamily(
42
+ value: { expressive: string[]; neutral: string[] } | undefined
43
+ ) {
44
+ this._fontFamily = value;
45
+ }
28
46
 
29
- constructor(
30
- protected appService: AppService,
31
- protected options: FontPluginOptions
47
+ private _fontStyles:
48
+ | Record<FontRole, Record<FontSize, FontStyle>>
49
+ | undefined;
50
+
51
+ get fontStyles(): Record<FontRole, Record<FontSize, FontStyle>> {
52
+ if (!this._fontStyles) throw new Error('Font styles not initialized');
53
+ return this._fontStyles;
54
+ }
55
+
56
+ set fontStyles(
57
+ value: Record<FontRole, Record<FontSize, FontStyle>> | undefined
32
58
  ) {
33
- super();
59
+ this._fontStyles = value;
60
+ }
61
+
62
+ getFonts() {
63
+ return {
64
+ fontStyles: this.fontStyles,
65
+ fontFamily: this.fontFamily,
66
+ };
67
+ }
68
+
69
+ onInit(): void {
34
70
  this.fontFamily = {
35
- expressive: options?.fontFamily?.expressive ?? ['Roboto', 'sans-serif'],
36
- neutral: options?.fontFamily?.neutral ?? ['Roboto', 'sans-serif'],
71
+ expressive: this.options?.fontFamily?.expressive ?? [
72
+ 'Roboto',
73
+ 'sans-serif',
74
+ ],
75
+ neutral: this.options?.fontFamily?.neutral ?? ['Roboto', 'sans-serif'],
37
76
  };
38
77
  this.fontStyles = {
39
78
  display: {
@@ -146,8 +185,9 @@ export class FontPlugin extends PluginAbstract {
146
185
  },
147
186
  },
148
187
  };
149
- if (options && options.fontStyles)
150
- Object.entries(options.fontStyles).forEach(([key, fontParam]) => {
188
+
189
+ if (this.options && this.options.fontStyles)
190
+ Object.entries(this.options.fontStyles).forEach(([key, fontParam]) => {
151
191
  const fontRole: FontRole = key as FontRole;
152
192
  Object.entries(fontParam).forEach(([size, fontStyle]) => {
153
193
  const fontSize: FontSize = size as FontSize;
@@ -160,13 +200,4 @@ export class FontPlugin extends PluginAbstract {
160
200
  });
161
201
  });
162
202
  }
163
- static config(options: FontPluginOptions): FontPluginOptions {
164
- return options;
165
- }
166
- getFonts() {
167
- return {
168
- fontStyles: this.fontStyles,
169
- fontFamily: this.fontFamily,
170
- };
171
- }
172
203
  }
@@ -1,7 +1,7 @@
1
1
  import { PluginsConfig } from 'tailwindcss/types/config';
2
2
  import { bootstrapFromConfig } from '../../main';
3
- import { TailwindPlugin } from './tailwind.plugin';
4
3
  import { AppService } from '../../app.service';
4
+ import { TailwindPlugin } from './tailwind.plugin';
5
5
 
6
6
  export type Theme = {
7
7
  colors: Record<string, string>;
@@ -11,9 +11,6 @@ export type Theme = {
11
11
 
12
12
  export const createTheme = (): Theme & { appService: AppService } => {
13
13
  const app = bootstrapFromConfig();
14
- const plugin = app.pluginService.getPlugin(TailwindPlugin);
15
- if (!plugin) {
16
- throw new Error('Tailwind plugin not found');
17
- }
14
+ const plugin = app.pluginService.getPlugin(TailwindPlugin).getInstance();
18
15
  return { ...plugin.getTheme(), appService: app };
19
16
  };
@@ -1,9 +1,9 @@
1
- import { PluginAbstract } from '../../plugin';
2
- import { AppService } from '../../app.service';
1
+ import { PluginAbstract, PluginImplAbstract } from '../../plugin';
3
2
  import { Theme } from './main';
4
3
  import { state, themer } from './plugins-tailwind';
5
- import { FontPlugin } from '../font';
4
+
6
5
  import { font } from './plugins-tailwind/font';
6
+ import { FontPlugin } from '../font';
7
7
 
8
8
  interface TailwindPluginOptions {
9
9
  darkMode?: 'class' | 'media';
@@ -11,21 +11,23 @@ interface TailwindPluginOptions {
11
11
  subThemes?: Record<string, string>;
12
12
  }
13
13
 
14
- export class TailwindPlugin extends PluginAbstract {
15
- static dependencies = [FontPlugin];
16
- constructor(
17
- protected appService: AppService,
18
- protected options: TailwindPluginOptions
19
- ) {
20
- options.darkMode ??= 'class';
21
- options.responsiveBreakPoints ??= {
14
+ export class TailwindPlugin extends PluginAbstract<
15
+ TailwindImplPlugin,
16
+ TailwindPluginOptions
17
+ > {
18
+ public dependencies = [FontPlugin];
19
+ public name = 'tailwind';
20
+ pluginClass = TailwindImplPlugin;
21
+ }
22
+
23
+ class TailwindImplPlugin extends PluginImplAbstract<TailwindPluginOptions> {
24
+ onInit() {
25
+ this.options.darkMode ??= 'class';
26
+ this.options.responsiveBreakPoints ??= {
22
27
  lg: 1.125,
23
28
  };
24
- super();
25
- }
26
- static config(options: TailwindPluginOptions): TailwindPluginOptions {
27
- return options;
28
29
  }
30
+
29
31
  getTheme(): Theme {
30
32
  const colors: Record<
31
33
  string,
@@ -50,6 +52,7 @@ export class TailwindPlugin extends PluginAbstract {
50
52
 
51
53
  const { fontStyles, fontFamily } = this.appService.pluginService
52
54
  .getPlugin(FontPlugin)
55
+ .getInstance()
53
56
  .getFonts();
54
57
  return {
55
58
  colors: {},
@@ -1,7 +0,0 @@
1
- import { AppService } from '../app.service';
2
- import { PluginConstructor } from './plugin.service';
3
- export declare abstract class PluginAbstract {
4
- static dependencies: PluginConstructor[];
5
- protected abstract appService: AppService;
6
- protected abstract options: object;
7
- }
@@ -1,9 +0,0 @@
1
- import { AppService } from '../app.service';
2
- import { PluginConstructor } from './plugin.service';
3
-
4
- export abstract class PluginAbstract {
5
- static dependencies: PluginConstructor[] = [];
6
-
7
- protected abstract appService: AppService;
8
- protected abstract options: object;
9
- }