Skip to content

Commit

Permalink
Update config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Aug 27, 2022
1 parent 5e15d51 commit c7a5b40
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { WebGLPrecision } from "./typedefs"
import { WebGLPrecision } from "./typedefs";

export type TConfiguration = Partial<BaseConfiguration>;

export class Configuration {
class BaseConfiguration {

/**
* Browser-specific constant to adjust CanvasRenderingContext2D.shadowBlur value,
Expand Down Expand Up @@ -118,16 +119,21 @@ export class Configuration {
fontPaths: Record<string, string> = {}

/**
* Defines the number of fraction digits to use when serializing object values.
* Used in exporting methods (`toObject`, `toJSON`, `toSVG`)
* Controls the percision of exported values
* You can use it to increase/decrease precision of such values like left, top, scaleX, scaleY, etc.
*/
NUM_FRACTION_DIGITS = 4
}

export class Configuration extends BaseConfiguration {

constructor(config?: Partial<Omit<Configuration, 'configure'>>) {
constructor(config?: TConfiguration) {
super();
this.configure(config);
}

configure(config: Partial<Omit<Configuration, 'configure'>> = {}) {
configure(config: TConfiguration = {}) {
Object.assign(this, config);
}

Expand All @@ -141,7 +147,7 @@ export class Configuration {
};
}

removeFonts(...fontFamilys: string[]) {
removeFonts(fontFamilys: string[] = []) {
fontFamilys.forEach(fontFamily => {
delete this.fontPaths[fontFamily];
});
Expand All @@ -150,6 +156,15 @@ export class Configuration {
clearFonts() {
this.fontPaths = {};
}

restoreDefaults<T extends BaseConfiguration>(keys?: (keyof T)[]) {
const defaults = new BaseConfiguration() as T;
const config = keys?.reduce((acc, key) => {
acc[key] = defaults[key];
return acc;
}, {} as T) || defaults;
this.configure(config);
}
}

export const config = new Configuration();

0 comments on commit c7a5b40

Please sign in to comment.