apply-action.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginCommands } from 'mol-plugin/command';
  7. import { PluginContext } from 'mol-plugin/context';
  8. import { State, StateTransform, StateAction } from 'mol-state';
  9. import { memoizeLatest } from 'mol-util/memoize';
  10. import { StateTransformParameters, TransformContolBase } from './common';
  11. import { ParamDefinition as PD } from 'mol-util/param-definition';
  12. export { ApplyActionContol };
  13. namespace ApplyActionContol {
  14. export interface Props {
  15. plugin: PluginContext,
  16. nodeRef: StateTransform.Ref,
  17. state: State,
  18. action: StateAction
  19. }
  20. export interface ComponentState {
  21. ref: StateTransform.Ref,
  22. version: string,
  23. params: any,
  24. error?: string,
  25. busy: boolean,
  26. isInitial: boolean
  27. }
  28. }
  29. class ApplyActionContol extends TransformContolBase<ApplyActionContol.Props, ApplyActionContol.ComponentState> {
  30. applyAction() {
  31. return PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  32. state: this.props.state,
  33. action: this.props.action.create(this.state.params),
  34. ref: this.props.nodeRef
  35. });
  36. }
  37. getInfo() { return this._getInfo(this.props.nodeRef, this.props.state.transforms.get(this.props.nodeRef).version); }
  38. getTransformerId() { return this.props.state.transforms.get(this.props.nodeRef).transformer.id; }
  39. getHeader() { return this.props.action.definition.display; }
  40. canApply() { return !this.state.error && !this.state.busy; }
  41. canAutoApply() { return false; }
  42. applyText() { return 'Apply'; }
  43. isUpdate() { return false; }
  44. private _getInfo = memoizeLatest((t: StateTransform.Ref, v: string) => StateTransformParameters.infoFromAction(this.plugin, this.props.state, this.props.action, this.props.nodeRef));
  45. state = { ref: this.props.nodeRef, version: this.props.state.transforms.get(this.props.nodeRef).version, error: void 0, isInitial: true, params: this.getInfo().initialValues, busy: false };
  46. static getDerivedStateFromProps(props: ApplyActionContol.Props, state: ApplyActionContol.ComponentState) {
  47. if (props.nodeRef === state.ref) return null;
  48. const version = props.state.transforms.get(props.nodeRef).version;
  49. if (version === state.version) return null;
  50. const source = props.state.cells.get(props.nodeRef)!.obj!;
  51. const params = props.action.definition.params
  52. ? PD.getDefaultValues(props.action.definition.params(source, props.plugin))
  53. : { };
  54. const newState: Partial<ApplyActionContol.ComponentState> = {
  55. ref: props.nodeRef,
  56. version,
  57. params,
  58. isInitial: true,
  59. error: void 0
  60. };
  61. return newState;
  62. }
  63. }