Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
beforeEach,
describe, expect, it, jest,
} from '@jest/globals';
import type { Component } from '@js/core/component';

import { createComponentMock } from './component.mock';
import { OptionsController } from './options_controller_base';

const setup = <TOptions extends Record<string, any>>(
const setup = <TOptions extends object>(
options: TOptions,
defaultOptions: TOptions,
) => {
): {
component: Component<TOptions>;
optionsController: OptionsController<TOptions>;
onOptionChangedMock: jest.Mock;
} => {
const onOptionChangedMock = jest.fn();

const component = createComponentMock(options, defaultOptions);
Expand Down Expand Up @@ -90,6 +94,7 @@ describe('oneWay', () => {
const mixedComplexOptions: MixedOptions = {
value: { valueA: 'A', valueB: 'B' },
};
const mixedComplexValue = mixedComplexOptions.value as { valueA?: string; valueB?: string };

describe('initial values', () => {
describe('plain options', () => {
Expand Down Expand Up @@ -133,7 +138,7 @@ describe('oneWay', () => {

const value = optionsController.oneWay('nested.value');

expect(value.peek()).toBe(nestedPlainOptions.nested!.value);
expect(value.peek()).toBe(nestedPlainOptions.nested?.value);
});

it('should have initial default value if parent config option is undefined', () => {
Expand Down Expand Up @@ -277,7 +282,7 @@ describe('oneWay', () => {
});

it('should have initial value same as passed in config if default not specified', () => {
const optionalOptions = { optionalValue: function optionalFnValue() {} };
const optionalOptions = { optionalValue: function optionalFnValue(): void {} };
const { optionsController } = setup<FunctionOptions>(
optionalOptions,
defaultFunctionOptions,
Expand Down Expand Up @@ -306,7 +311,7 @@ describe('oneWay', () => {

const value = optionsController.oneWay('value.valueA');

expect(value.peek()).toStrictEqual((mixedComplexOptions.value as any).valueA);
expect(value.peek()).toStrictEqual(mixedComplexValue.valueA);
});

it('should have initial value undefined if parent is plain value', () => {
Expand All @@ -322,7 +327,7 @@ describe('oneWay', () => {

const value = optionsController.oneWay('value.valueA');

expect(value.peek()).toBe((mixedComplexOptions.value as any).valueA);
expect(value.peek()).toBe(mixedComplexValue.valueA);
});
});

Expand Down Expand Up @@ -600,7 +605,7 @@ describe('oneWay', () => {
const value = optionsController.oneWay('value.valueA');
component.option('value.valueA', undefined);

expect(value.peek()).toBe((mixedComplexOptions.value as any)?.valueA);
expect(value.peek()).toBe(mixedComplexValue.valueA);
});

it('should update to undefined if parent option update to plain one', () => {
Expand All @@ -624,7 +629,7 @@ describe('oneWay', () => {
const value = optionsController.oneWay('value.valueA');
component.option('value', undefined);

expect(value.peek()).toBe((mixedComplexOptions.value as any)?.valueA);
expect(value.peek()).toBe(mixedComplexValue.valueA);
});

it('should update to undefined if default value is plain', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-return */

import { Component } from '@js/core/component';
import { getPathParts } from '@js/core/utils/data';
import type { ChangedOptionInfo } from '@js/events';
Expand All @@ -20,9 +17,9 @@ import type {
TemplateProperty,
} from './types';

function getOr<T>(cache: Record<string, T>, key: string, orElse: () => T): T {
function getOr<T>(cache: Record<string, unknown>, key: string, orElse: () => T): T {
if (cache[key]) {
return cache[key];
return cache[key] as T;
}

const value = orElse();
Expand All @@ -32,10 +29,13 @@ function getOr<T>(cache: Record<string, T>, key: string, orElse: () => T): T {
}

export class OptionsController<
TProps extends Record<string, any>,
TProps extends object,
TDefaultProps extends TProps = TProps,
> {
private readonly cache = {
private readonly cache: Record<
'oneWay' | 'oneWayWithChanges' | 'twoWay' | 'action' | 'template',
Record<string, unknown>
> = {
oneWay: {},
oneWayWithChanges: {},
twoWay: {},
Expand Down Expand Up @@ -115,7 +115,7 @@ export class OptionsController<
() => getTreeNodeByPath(
this.internalOptions.value.options,
pathArray,
),
) as PropertyWithDefaults<TProps, TDefaultProps, TProp>,
);
});
}
Expand All @@ -131,7 +131,10 @@ export class OptionsController<
const { options, changes } = this.internalOptions.value;

return {
value: getTreeNodeByPath(options, pathArray),
value: getTreeNodeByPath(
options,
pathArray,
) as PropertyWithDefaults<TProps, TDefaultProps, TProp>,
changes,
};
},
Expand All @@ -142,28 +145,32 @@ export class OptionsController<
public twoWay<TProp extends string>(
name: TProp,
): Signal<PropertyWithDefaults<TProps, TDefaultProps, TProp>> {
return getOr(this.cache.twoWay, name, () => {
type TValue = PropertyWithDefaults<TProps, TDefaultProps, TProp>;

return getOr(this.cache.twoWay, name, (): Signal<TValue> => {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const obs = signal(this.component.option(name));
const obs = signal(this.component.option(name) as TValue);
effect(() => {
obs.value = this.oneWay(name).value as any;
obs.value = this.oneWay(name).value;
});
return {
get value(): any {
get value(): TValue {
return obs.value;
},
set value(value: any) {
set value(value: TValue) {
const isInitialized = that.initialized.peek();
const callbackName = `on${name}Change`;
const callback = that.component.option(callbackName) as any;
const callback = that.component.option(callbackName) as
((value: TValue) => void) | undefined;
const isControlled = that.isControlledMode && that.component.option(name) !== undefined;

if (isControlled) {
callback?.(value);
return;
}

// @ts-expect-error Component.option() doesn't resolve nested option paths
that.component.option(name, value);

// 🚨🚨🚨 Hotfix for filterSync
Expand All @@ -177,29 +184,20 @@ export class OptionsController<

callback?.(value);
},
peek(): any {
peek(): TValue {
return obs.peek();
},
subscribe(...params: any): any {
// @ts-expect-error
return obs.subscribe(...params);
},
toJSON(...params: any[]): any {
// @ts-expect-error
return obs.toJSON(...params);
},
valueOf(...params: any[]): any {
// @ts-expect-error
return obs.valueOf(...params);
},
subscribe: obs.subscribe.bind(obs),
toJSON: obs.toJSON.bind(obs),
valueOf: obs.valueOf.bind(obs),
brand: obs.brand,
};
});
}

public normalizeTemplate<T>(template: Template<T>): ComponentType<T> {
// @ts-expect-error
return TemplateWrapper(this.component._getTemplate(template)) as any;
return TemplateWrapper(this.component._getTemplate(template)) as ComponentType<T>;
}

public template<TProp extends string>(
Expand All @@ -208,8 +206,10 @@ export class OptionsController<
return getOr(this.cache.template, name, () => {
const templateOption = this.oneWay(name);
return computed(
// @ts-expect-error
() => templateOption.value && this.normalizeTemplate(templateOption.value) as any,
() => (
templateOption.value
&& this.normalizeTemplate(templateOption.value as Template<unknown>)
) as TemplateProperty<TProps, TProp>,
);
});
}
Expand All @@ -223,7 +223,7 @@ export class OptionsController<
() => {
track(actionOption.value);
// @ts-expect-error
return this.component._createActionByOption(name) as any;
return this.component._createActionByOption(name) as ActionProperty<TProps, TProp>;
},
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const getFileUploader = (editor: HtmlEditor): FileUploader => FileUploader.getIn

const openFileTab = ({ imageUpload, fileTabIndex }: DialogConfig): HtmlEditor => {
const $element = $('<div>').appendTo(document.body);
// @ts-expect-error direct instantiation is not typed
const editor: HtmlEditor = new HtmlEditor($element, {
value: VALUE,
toolbar: { items: ['image'] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const editors: HtmlEditor[] = [];

const createEditor = (options: Properties): HtmlEditor => {
const $element = $('<div>').appendTo(document.body);
// @ts-expect-error direct instantiation is not typed
const editor: HtmlEditor = new HtmlEditor($element, { value: TABLE_MARKUP, ...options });

editors.push(editor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const editors: HtmlEditor[] = [];

const createEditor = ({ options, caretIndex }: EditorState): HtmlEditor => {
const $element = $('<div>').appendTo(document.body);
// @ts-expect-error direct instantiation is not typed
const editor: HtmlEditor = new HtmlEditor($element, {
tableContextMenu: { enabled: true },
toolbar: {
Expand Down
Loading