Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(TS) Remove ts-nocheck from filters ( resize filter will have its own pr ) #8609

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/filters/2d_backend.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Canvas 2D filter backend.
*/
import type { BaseFilter } from './base_filter.class';
import { T2DPipelineState } from './typedefs';
import { T2DPipelineState, TPipelineResources } from './typedefs';

export class Canvas2dFilterBackend {
/**
Expand All @@ -12,7 +12,7 @@ export class Canvas2dFilterBackend {
* in this object there will be appended some canvases, created once, resized sometimes
* cleared never. Clearing is left to the developer.
**/
resources = {};
resources: TPipelineResources = {};

/**
* Apply a set of filters against a source image and draw the filtered output
Expand Down
13 changes: 4 additions & 9 deletions src/filters/base_filter.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export type AbstractBaseFilterOptions<T> = {

export type BaseFilterOptions = AbstractBaseFilterOptions<string>;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AnyFilter extends AbstractBaseFilter<string | Record<string, string>> {};

export abstract class AbstractBaseFilter<T> {
/**
* Filter type
Expand Down Expand Up @@ -47,15 +50,7 @@ export abstract class AbstractBaseFilter<T> {
* Constructor
* @param {Object} [options] Options object
*/
constructor(options: Partial<AbstractBaseFilterOptions<T>> = {}) {
this.setOptions(options);
}

/**
* Sets filter's properties from options
* @param {Object} [options] Options object
*/
setOptions(options: Record<string, any>) {
constructor(options: Partial<AbstractBaseFilterOptions<T>> & Record<string, any> = {}) {
Object.assign(this, options);
}

Expand Down
5 changes: 2 additions & 3 deletions src/filters/blendimage_filter.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { Image } from '../shapes/image.class';
import type { TClassProperties } from '../typedefs';
import { createCanvasElement } from '../util/misc/dom';
Expand Down Expand Up @@ -100,7 +99,7 @@ export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
resources.blendImage = createCanvasElement();
}
const canvas1 = resources.blendImage;
const context = canvas1.getContext('2d');
const context = canvas1.getContext('2d')!;
if (canvas1.width !== width || canvas1.height !== height) {
canvas1.width = width;
canvas1.height = height;
Expand Down Expand Up @@ -194,7 +193,7 @@ export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
* @param {AbortSignal} [options.signal] handle aborting image loading, see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal
* @returns {Promise<BlendImage>}
*/
static fromObject(object, options) {
static fromObject(object: Record<string, any>, options: { signal: AbortSignal }) {
return Image.fromObject(object.image, options).then(
(image) => new BlendImage({ ...object, image })
);
Expand Down
9 changes: 4 additions & 5 deletions src/filters/blur_filter.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import type { TClassProperties } from '../typedefs';
import { createCanvasElement } from '../util/misc/dom';
import { BaseFilter } from './base_filter.class';
Expand Down Expand Up @@ -65,14 +64,14 @@ export class Blur extends BaseFilter {
resources.blurLayer1 = createCanvasElement();
resources.blurLayer2 = createCanvasElement();
}
const canvas1 = resources.blurLayer1;
const canvas2 = resources.blurLayer2;
const canvas1 = resources.blurLayer1!;
const canvas2 = resources.blurLayer2!;
if (canvas1.width !== width || canvas1.height !== height) {
canvas2.width = canvas1.width = width;
canvas2.height = canvas1.height = height;
}
const ctx1 = canvas1.getContext('2d'),
ctx2 = canvas2.getContext('2d'),
const ctx1 = canvas1.getContext('2d')!,
ctx2 = canvas2.getContext('2d')!,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably need a type that is HTMLCanvasElementThatWorks that never return null on getContext('2d').
We just need to agree that if your machine has no memory to create canvases anymore fabricJS is not going to work anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can modify the global type of the window/dom

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better off,
we use AssertKeys in createCanvasElement

nSamples = 15,
blur = this.blur * 0.06 * 0.5;
let random, percent, j, i;
Expand Down
14 changes: 7 additions & 7 deletions src/filters/composed_filter.class.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// @ts-nocheck
import type { TClassProperties } from '../typedefs';
import {
AbstractBaseFilter,
type AnyFilter,
BaseFilter,
BaseFilterOptions,
type BaseFilterOptions,
} from './base_filter.class';
import type { T2DPipelineState, TWebGLPipelineState } from './typedefs';
import { isWebGLPipelineState } from './typedefs';
Expand All @@ -12,16 +11,17 @@ import { classRegistry } from '../util/class_registry';
/**
* A container class that knows how to apply a sequence of filters to an input image.
*/
// @ts-expect-error
export class Composed extends BaseFilter {
/**
* A non sparse array of filters to apply
*/
declare subFilters: AbstractBaseFilter[];
declare subFilters: AnyFilter[];

constructor({
subFilters = [],
...options
}: Partial<BaseFilterOptions & { subFilters: AbstractBaseFilter[] }> = {}) {
}: Partial<BaseFilterOptions & { subFilters: AnyFilter[] }> = {}) {
super(options);
this.subFilters = subFilters;
}
Expand Down Expand Up @@ -65,9 +65,9 @@ export class Composed extends BaseFilter {
* @param {AbortSignal} [options.signal] handle aborting `BlendImage` filter loading, see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal
* @returns {Promise<Composed>}
*/
static fromObject(object, options) {
static fromObject(object: Record<string, any>, options: { signal: AbortSignal }) {
return Promise.all(
((object.subFilters || []) as AbstractBaseFilter[]).map((filter) =>
((object.subFilters || []) as AnyFilter[]).map((filter) =>
classRegistry.getClass(filter.type).fromObject(filter, options)
)
).then((enlivedFilters) => new Composed({ subFilters: enlivedFilters }));
Expand Down
6 changes: 6 additions & 0 deletions src/filters/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export type TProgramCache = any;

export type TTextureCache = any;

export type TPipelineResources = {
blendImage?: HTMLCanvasElement;
blurLayer1?: HTMLCanvasElement;
blurLayer2?: HTMLCanvasElement;
} & Record<string, unknown>;

export type TWebGLPipelineState = {
filterBackend: WebGLFilterBackend;
originalWidth: number;
Expand Down
4 changes: 2 additions & 2 deletions src/filters/webgl_backend.class.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getEnv } from '../env';
import { config } from '../config';
import { createCanvasElement } from '../util/misc/dom';
import { TWebGLPipelineState, TProgramCache, TTextureCache } from './typedefs';
import { TWebGLPipelineState, TProgramCache, TTextureCache, TPipelineResources } from './typedefs';

export class WebGLFilterBackend {
declare tileSize: number;
Expand Down Expand Up @@ -46,7 +46,7 @@ export class WebGLFilterBackend {
* in this object there will be appended some canvases, created once, resized sometimes
* cleared never. Clearing is left to the developer.
**/
resources = {};
resources: TPipelineResources = {};

constructor({ tileSize = config.textureSize } = {}) {
this.tileSize = tileSize;
Expand Down