Skip to content

Commit

Permalink
0.11.0. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz authored Oct 29, 2023
1 parent 0124d4e commit 6c316c7
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 130 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.11.0

This version normalizes names of functions in `ValueContext` and `PropertyValidatorContext` classes.

The `CustomValidatorContext` class is deleted now, please use the `PropertyValidatorContext` class instead.

The `PropertyModelBuilder` class has deleted the `customValidator` function, please use the `validator` function instead.

## 0.10.0

This version deletes all deprecated `*ValueModel` functions. From now, use only `create*ValueModel` functions.
Expand Down
4 changes: 2 additions & 2 deletions demos/webpack-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"sequential-workflow-model": "^0.2.0",
"sequential-workflow-designer": "^0.16.1",
"sequential-workflow-machine": "^0.4.0",
"sequential-workflow-editor-model": "^0.10.0",
"sequential-workflow-editor": "^0.10.0"
"sequential-workflow-editor-model": "^0.11.0",
"sequential-workflow-editor": "^0.11.0"
},
"devDependencies": {
"ts-loader": "^9.4.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export const setStringValueStepModel = createStepModel<SetStringValueStep>('setS
validate(context) {
const value = context.getValue();
if (value.modelId === 'string') {
const variables = TextVariableParser.parse(value.value as string);
const notFoundIndex = context.hasVariables(variables).findIndex(v => !v);
if (notFoundIndex >= 0) {
return `Variable $${variables[notFoundIndex]} is not defined`;
const variableNames = TextVariableParser.parse(value.value as string);
const undefinedVariableName = context.findFirstUndefinedVariable(variableNames);
if (undefinedVariableName) {
return `Variable $${undefinedVariableName} is not defined`;
}
}
return null;
Expand Down
6 changes: 3 additions & 3 deletions editor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequential-workflow-editor",
"version": "0.10.0",
"version": "0.11.0",
"type": "module",
"main": "./lib/esm/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -46,11 +46,11 @@
"prettier:fix": "prettier --write ./src ./css"
},
"dependencies": {
"sequential-workflow-editor-model": "^0.10.0",
"sequential-workflow-editor-model": "^0.11.0",
"sequential-workflow-model": "^0.2.0"
},
"peerDependencies": {
"sequential-workflow-editor-model": "^0.10.0",
"sequential-workflow-editor-model": "^0.11.0",
"sequential-workflow-model": "^0.2.0"
},
"devDependencies": {
Expand Down
19 changes: 13 additions & 6 deletions editor/src/components/dynamic-list-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Html } from '../core/html';
import { Component } from './component';
import { validationErrorComponent } from './validation-error-component';

export interface DynamicListComponent<TItem> extends Component {
export interface DynamicListComponent<TItem, TItemComponent extends DynamicListItemComponent<TItem> = DynamicListItemComponent<TItem>>
extends Component {
onChanged: SimpleEvent<TItem[]>;
add(item: TItem): void;
forEach(callback: (component: TItemComponent) => void): void;
}

export interface DynamicListComponentConfiguration<TItem> {
Expand All @@ -19,12 +21,12 @@ export interface DynamicListItemComponent<TItem> extends Component {
validate(error: string | null): void;
}

export function dynamicListComponent<TItem>(
export function dynamicListComponent<TItem, TItemComponent extends DynamicListItemComponent<TItem> = DynamicListItemComponent<TItem>>(
initialItems: TItem[],
itemComponentFactory: (item: TItem) => DynamicListItemComponent<TItem>,
itemComponentFactory: (item: TItem) => TItemComponent,
context: ValueContext,
configuration?: DynamicListComponentConfiguration<TItem>
): DynamicListComponent<TItem> {
): DynamicListComponent<TItem, TItemComponent> {
const onChanged = new SimpleEvent<TItem[]>();
const items = [...initialItems];

Expand Down Expand Up @@ -58,6 +60,10 @@ export function dynamicListComponent<TItem>(
reloadList();
}

function forEach(callback: (component: TItemComponent) => void) {
components.forEach(callback);
}

function reloadList() {
if (emptyRow) {
view.removeChild(emptyRow);
Expand Down Expand Up @@ -99,13 +105,14 @@ export function dynamicListComponent<TItem>(
const validation = validationErrorComponent();
view.appendChild(validation.view);

const components: DynamicListItemComponent<TItem>[] = [];
const components: TItemComponent[] = [];

reloadList();

return {
onChanged,
view,
add
add,
forEach
};
}
5 changes: 3 additions & 2 deletions editor/src/property-editor/property-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PropertyEditor implements Component {
definitionContext: DefinitionContext,
editorServices: EditorServices
): PropertyEditor {
const valueContext = ValueContext.create(propertyModel.value, propertyModel, definitionContext);
const valueContext = ValueContext.createFromDefinitionContext(propertyModel.value, propertyModel, definitionContext);
const valueEditorFactory = editorServices.valueEditorFactoryResolver.resolve(propertyModel.value.id, propertyModel.value.editorId);
const valueEditor = valueEditorFactory(valueContext, editorServices);
let hint: PropertyHintComponent | null = null;
Expand Down Expand Up @@ -64,7 +64,8 @@ export class PropertyEditor implements Component {

let validationError: PropertyValidationErrorComponent | null = null;
if (propertyModel.validator) {
const validatorContext = PropertyValidatorContext.create(propertyModel, definitionContext);
const valueContext = ValueContext.createFromDefinitionContext(propertyModel.value, propertyModel, definitionContext);
const validatorContext = PropertyValidatorContext.create(valueContext);
validationError = propertyValidationErrorComponent(propertyModel.validator, validatorContext);
view.appendChild(validationError.view);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { formatVariableNameWithType } from '../../core/variable-name-formatter';
import { DynamicListItemComponent } from '../../components/dynamic-list-component';
import { Icons } from '../../core/icons';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AnyVariableItemComponent extends DynamicListItemComponent<AnyVariable> {}
export type AnyVariableItemComponent = DynamicListItemComponent<AnyVariable>;

export function anyVariableItemComponent(variable: AnyVariable): AnyVariableItemComponent {
function validate(error: string | null) {
Expand Down
2 changes: 1 addition & 1 deletion editor/src/value-editors/dynamic/dynamic-value-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function dynamicValueEditor(context: ValueContext<DynamicValueModel>, ser

function onTypeChanged() {
const newModel = subModels[subModelSelect.getSelectedIndex()];
const defaultValueContext = DefaultValueContext.createFromValueContext(services.activator, context);
const defaultValueContext = DefaultValueContext.create(services.activator, context.scopedPropertyContext.propertyContext);
const defaultValue = {
modelId: newModel.id,
value: newModel.getDefaultValue(defaultValueContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { buttonComponent } from '../../components/button-component';
import { DynamicListItemComponent } from '../../components/dynamic-list-component';
import { Icons } from '../../core/icons';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface StringDictionaryItemComponent extends DynamicListItemComponent<StringDictionaryItem> {}
export type StringDictionaryItemComponent = DynamicListItemComponent<StringDictionaryItem>;

export function stringDictionaryItemComponent(item: StringDictionaryItem): StringDictionaryItemComponent {
function validate(error: string | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { Icons } from '../../core/icons';
import { prependedInputComponent } from '../../components/prepended-input-component';
import { inputComponent } from '../../components/input-component';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface VariableDefinitionItemComponent extends DynamicListItemComponent<VariableDefinition> {}
export type VariableDefinitionItemComponent = DynamicListItemComponent<VariableDefinition>;

export function variableDefinitionItemComponent(
variable: VariableDefinition,
Expand Down
2 changes: 1 addition & 1 deletion model/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequential-workflow-editor-model",
"version": "0.10.0",
"version": "0.11.0",
"homepage": "https://nocode-js.com/",
"author": {
"name": "NoCode JS",
Expand Down
14 changes: 9 additions & 5 deletions model/src/activator/model-activator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Definition, Step } from 'sequential-workflow-model';
import { DefinitionModel, PropertyModel, PropertyModels } from '../model';
import { UidGenerator } from '../external-types';
import { DefaultValueContext } from '../context/default-value-context';
import { PropertyContext } from '../context/property-context';

export class ModelActivator<TDefinition extends Definition = Definition> {
public static create<TDef extends Definition>(
Expand All @@ -19,8 +20,9 @@ export class ModelActivator<TDefinition extends Definition = Definition> {
};
this.activatePropertiesInOrder(definition, this.definitionModel.root.properties);

const sequenceValueContext = DefaultValueContext.create(this, definition, this.definitionModel.root.sequence);
const sequence = this.definitionModel.root.sequence.value.getDefaultValue(sequenceValueContext);
const propertyContext = PropertyContext.create(definition, this.definitionModel.root.sequence, this.definitionModel);
const defaultValueContext = DefaultValueContext.create(this, propertyContext);
const sequence = this.definitionModel.root.sequence.value.getDefaultValue(defaultValueContext);
return {
...definition,
sequence
Expand All @@ -40,8 +42,9 @@ export class ModelActivator<TDefinition extends Definition = Definition> {
};
this.activatePropertiesInOrder(step, model.properties);

const nameValueContext = DefaultValueContext.create(this, step, model.name);
const name = model.name.value.getDefaultValue(nameValueContext);
const propertyContext = PropertyContext.create(step, model.name, this.definitionModel);
const defaultValueContext = DefaultValueContext.create(this, propertyContext);
const name = model.name.value.getDefaultValue(defaultValueContext);
return {
...step,
name
Expand All @@ -65,7 +68,8 @@ export class ModelActivator<TDefinition extends Definition = Definition> {
}

private activateProperty(object: object, model: PropertyModel) {
const defaultValueContext = DefaultValueContext.create(this, object, model);
const propertyContext = PropertyContext.create(object, model, this.definitionModel);
const defaultValueContext = DefaultValueContext.create(this, propertyContext);
const defaultValue = model.value.getDefaultValue(defaultValueContext);
model.value.path.write(object, defaultValue);
}
Expand Down
5 changes: 0 additions & 5 deletions model/src/builders/property-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ export class PropertyModelBuilder<TValue extends PropertyValue = PropertyValue,
return this;
}

/**
* @deprecated Use `validator` instead.
*/
public readonly customValidator = this.validator.bind(this);

public build(): PropertyModel<TValue> {
if (!this._value) {
throw new Error(`Model is not set for ${this.path.toString()}`);
Expand Down
22 changes: 5 additions & 17 deletions model/src/context/default-value-context.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { Properties } from 'sequential-workflow-model';
import { ModelActivator } from '../activator';
import { PropertyModel, ValueModel } from '../model';
import { ValueContext } from './value-context';
import { readPropertyValue } from './read-property-value';
import { PropertyContext } from './property-context';

export class DefaultValueContext<TProperties extends Properties = Properties> {
public static create<TProps extends Properties = Properties>(
activator: ModelActivator,
object: object,
model: PropertyModel
propertyContext: PropertyContext<TProps>
): DefaultValueContext<TProps> {
return new DefaultValueContext<TProps>(activator, name => readPropertyValue(name, model, object));
return new DefaultValueContext<TProps>(activator, propertyContext);
}

public static createFromValueContext<TProps extends Properties = Properties>(
activator: ModelActivator,
valueContext: ValueContext<ValueModel, TProps>
): DefaultValueContext<TProps> {
return new DefaultValueContext(activator, valueContext.getPropertyValue);
}

private constructor(
private readonly activator: ModelActivator,
public readonly getPropertyValue: <Key extends keyof TProperties>(name: Key) => TProperties[Key]
) {}
private constructor(private readonly activator: ModelActivator, public readonly propertyContext: PropertyContext<TProperties>) {}

public readonly getPropertyValue = this.propertyContext.getPropertyValue;
public readonly activateStep = this.activator.activateStep;
}
2 changes: 2 additions & 0 deletions model/src/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './default-value-context';
export * from './property-context';
export * from './scoped-property-context';
export * from './value-context';
export * from './definition-context';
28 changes: 28 additions & 0 deletions model/src/context/property-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Properties } from 'sequential-workflow-model';
import { DefinitionModel, PropertyModel } from '../model';
import { ValueType } from '../types';
import { readPropertyValue } from './read-property-value';

export class PropertyContext<TProperties extends Properties = Properties> {
public static create<TProps extends Properties = Properties>(
object: object,
propertyModel: PropertyModel,
definitionModel: DefinitionModel
): PropertyContext<TProps> {
return new PropertyContext<TProps>(object, propertyModel, definitionModel);
}

private constructor(
public readonly object: object,
private readonly propertyModel: PropertyModel,
private readonly definitionModel: DefinitionModel
) {}

public readonly getPropertyValue = <Key extends keyof TProperties>(name: Key): TProperties[Key] => {
return readPropertyValue(name, this.propertyModel, this.object);
};

public readonly getValueTypes = (): ValueType[] => {
return this.definitionModel.valueTypes;
};
}
35 changes: 35 additions & 0 deletions model/src/context/scoped-property-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Properties } from 'sequential-workflow-model';
import { ContextVariable } from '../model';
import { ParentsProvider } from './variables-provider';
import { PropertyContext } from './property-context';

export class ScopedPropertyContext<TProperties extends Properties> {
public static create<TProps extends Properties>(
propertyContext: PropertyContext<TProps>,
parentsProvider: ParentsProvider
): ScopedPropertyContext<TProps> {
return new ScopedPropertyContext<TProps>(propertyContext, parentsProvider);
}

private constructor(public readonly propertyContext: PropertyContext<TProperties>, private readonly parentsProvider: ParentsProvider) {}

public readonly getPropertyValue = this.propertyContext.getPropertyValue;
public readonly getValueTypes = this.propertyContext.getValueTypes;

public readonly hasVariable = (variableName: string, valueType: string | null): boolean => {
return this.getVariables().some(v => v.name === variableName && (valueType === null || v.type === valueType));
};

public readonly findFirstUndefinedVariable = (variableNames: string[]): string | undefined => {
const variables = new Set(this.getVariables().map(v => v.name));
return variableNames.find(name => !variables.has(name));
};

public readonly isVariableDuplicated = (variableName: string): boolean => {
return this.getVariables().filter(v => v.name === variableName).length > 1;
};

public readonly getVariables = (): ContextVariable[] => {
return this.parentsProvider.getVariables();
};
}
Loading

0 comments on commit 6c316c7

Please sign in to comment.