Skip to content

Commit

Permalink
fix: remove and key update handler undo fixed
Browse files Browse the repository at this point in the history
Related to #767
  • Loading branch information
Skaiir committed Aug 30, 2023
1 parent f638305 commit 43d0a3d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
updatePath
} from './Util';

import {
runRecursively
} from '@bpmn-io/form-js-viewer';

export default class RemoveFormFieldHandler {

/**
Expand Down Expand Up @@ -36,8 +40,8 @@ export default class RemoveFormFieldHandler {
// (2) Update internal paths of its siblings (and their children)
get(schema, sourcePath).forEach((formField, index) => updatePath(this._formFieldRegistry, formField, index));

// (3) Remove form field from form field registry
this._formFieldRegistry.remove(formField);
// (3) Remove form field and children from form field registry
runRecursively(formField, (formField) => this._formFieldRegistry.remove(formField));

// TODO: Create updater/change support that automatically updates paths and schema on command execution
this._formEditor._setState({ schema });
Expand All @@ -60,8 +64,8 @@ export default class RemoveFormFieldHandler {
// (2) Update internal paths of its siblings (and their children)
get(schema, sourcePath).forEach((formField, index) => updatePath(this._formFieldRegistry, formField, index));

// (3) Add form field to form field registry
this._formFieldRegistry.add(formField);
// (3) Add form field and children to form field registry
runRecursively(formField, (formField) => this._formFieldRegistry.add(formField));

// TODO: Create updater/change support that automatically updates paths and schema on command execution
this._formEditor._setState({ schema });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ export default class UpdateKeyClaimHandler {
revert(context) {
const {
claiming,
formField,
valuePath
} = context;

if (claiming) {
this._pathRegistry.unclaimPath(valuePath);
} else {
this._pathRegistry.claimPath(valuePath, formField);
this._pathRegistry.claimPath(valuePath, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,34 @@ export default class UpdatePathClaimHandler {
if (claiming) {
this._pathRegistry.executeRecursivelyOnFields(formField, ({ field, isClosed }) => {
const valuePath = this._pathRegistry.getValuePath(field, options);
valuePaths.push(valuePath);
valuePaths.push({ valuePath, isClosed });
this._pathRegistry.claimPath(valuePath, isClosed);
});
} else {
this._pathRegistry.executeRecursivelyOnFields(formField, ({ field, isClosed }) => {
const valuePath = this._pathRegistry.getValuePath(field, options);
valuePaths.push(valuePath);
valuePaths.push({ valuePath, isClosed });
this._pathRegistry.unclaimPath(valuePath, isClosed);
});
}

// cache path for revert
// cache path info for revert
context.valuePaths = valuePaths;
}

revert(context) {
const {
claiming,
formField,
valuePaths
} = context;

if (claiming) {
valuePaths.forEach(valuePath => {
valuePaths.forEach(({ valuePath })=> {
this._pathRegistry.unclaimPath(valuePath);
});
} else {
valuePaths.forEach(valuePath => {
this._pathRegistry.claimPath(valuePath, formField);
valuePaths.forEach(({ valuePath, isClosed }) => {
this._pathRegistry.claimPath(valuePath, isClosed);
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/form-js-viewer/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ export function getSchemaVariables(schema, options = {}) {
// remove duplicates
return Array.from(new Set(variables));
}

export function runRecursively(formField, fn) {
const components = formField.components || [];

components.forEach((component, index) => {
runRecursively(component, fn);
});

fn(formField);
}

0 comments on commit 43d0a3d

Please sign in to comment.