-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): add values source modeling behavior
Closes #859
- Loading branch information
Niklas Kiefer
committed
Nov 2, 2023
1 parent
bfbcd58
commit 3be4ad3
Showing
6 changed files
with
237 additions
and
32 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/form-js-editor/src/features/modeling/behavior/ValuesSourceBehavior.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; | ||
|
||
import { get } from 'min-dash'; | ||
|
||
import { | ||
VALUES_SOURCES, | ||
VALUES_SOURCES_PATHS | ||
} from '@bpmn-io/form-js-viewer'; | ||
|
||
export default class ValuesSourceBehavior extends CommandInterceptor { | ||
constructor(eventBus) { | ||
super(eventBus); | ||
|
||
/** | ||
* Cleanup properties on changing the values source. | ||
* | ||
* 1) Remove other sources, e.g. set `values` => remove `valuesKey` and `valuesExpression` | ||
* 2) Remove default values for all other values sources | ||
*/ | ||
this.preExecute('formField.edit', function(context) { | ||
const { properties } = context; | ||
|
||
const newProperties = {}; | ||
|
||
if (!isValuesSourceUpdate(properties)) { | ||
return; | ||
} | ||
|
||
// clean up value sources that are not to going to be set | ||
Object.values(VALUES_SOURCES).forEach(source => { | ||
const path = VALUES_SOURCES_PATHS[source]; | ||
if (get(properties, path) == undefined) { | ||
newProperties[VALUES_SOURCES_PATHS[source]] = undefined; | ||
} | ||
}); | ||
|
||
// clean up default value | ||
if ( | ||
get(properties, VALUES_SOURCES_PATHS[VALUES_SOURCES.EXPRESSION]) !== undefined || | ||
get(properties, VALUES_SOURCES_PATHS[VALUES_SOURCES.INPUT]) !== undefined | ||
) { | ||
newProperties['defaultValue'] = undefined; | ||
} | ||
|
||
context.properties = { | ||
...properties, | ||
...newProperties | ||
}; | ||
}, true); | ||
} | ||
} | ||
|
||
ValuesSourceBehavior.$inject = [ 'eventBus' ]; | ||
|
||
// helper /////////////////// | ||
|
||
function isValuesSourceUpdate(properties) { | ||
return Object.values(VALUES_SOURCES_PATHS).some(path => { | ||
return get(properties, path) !== undefined; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ | |
] | ||
}, | ||
{ | ||
"id": "language", | ||
"defaultValue": "english", | ||
"key": "language", | ||
"label": "Language", | ||
|
159 changes: 159 additions & 0 deletions
159
packages/form-js-editor/test/spec/features/modeling/behavior/ValuesSourceBehavior.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { | ||
bootstrapFormEditor, | ||
inject | ||
} from '../../../../TestHelper'; | ||
|
||
import modelingModule from 'src/features/modeling'; | ||
|
||
import schema from '../../../defaultValues.json'; | ||
|
||
describe('features/modeling - ValuesSourceBehavior', function() { | ||
|
||
beforeEach(bootstrapFormEditor(schema, { | ||
additionalModules: [ | ||
modelingModule | ||
] | ||
})); | ||
|
||
|
||
it('should NOT remove values source properties', inject(function(formFieldRegistry, modeling) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'label', | ||
'foo' | ||
); | ||
|
||
// then | ||
expect(formField.values).to.have.length(2); | ||
})); | ||
|
||
|
||
describe('should remove other values source properties', function() { | ||
|
||
it('execute', inject(function(formFieldRegistry, modeling) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesKey', | ||
'' | ||
); | ||
|
||
// then | ||
expect(formField.values).to.not.exist; | ||
expect(formField.valuesExpression).to.not.exist; | ||
})); | ||
|
||
|
||
it('undo', inject(function(formFieldRegistry, modeling, commandStack) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesKey', | ||
'' | ||
); | ||
|
||
// when | ||
commandStack.undo(); | ||
|
||
// then | ||
expect(formField.values).to.have.length(2); | ||
})); | ||
|
||
|
||
it('redo', inject(function(formFieldRegistry, modeling, commandStack) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesKey', | ||
'' | ||
); | ||
|
||
// when | ||
commandStack.undo(); | ||
commandStack.redo(); | ||
|
||
// then | ||
expect(formField.values).to.not.exist; | ||
expect(formField.valuesExpression).to.not.exist; | ||
})); | ||
}); | ||
|
||
|
||
describe('should remove default value', function() { | ||
|
||
it('execute', inject(function(formFieldRegistry, modeling) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesExpression', | ||
'=' | ||
); | ||
|
||
// then | ||
expect(formField.defaultValue).to.not.exist; | ||
})); | ||
|
||
|
||
it('undo', inject(function(formFieldRegistry, modeling, commandStack) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesExpression', | ||
'=' | ||
); | ||
|
||
// when | ||
commandStack.undo(); | ||
|
||
// then | ||
expect(formField.defaultValue).to.eql('english'); | ||
})); | ||
|
||
|
||
it('redo', inject(function(formFieldRegistry, modeling, commandStack) { | ||
|
||
// given | ||
const formField = formFieldRegistry.get('language'); | ||
|
||
// when | ||
modeling.editFormField( | ||
formField, | ||
'valuesExpression', | ||
'=' | ||
); | ||
|
||
// when | ||
commandStack.undo(); | ||
commandStack.redo(); | ||
|
||
// then | ||
expect(formField.defaultValue).to.not.exist; | ||
})); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters