@flowgram.ai/form-materials 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/bin/materials.ts +1 -0
  2. package/dist/esm/index.js +271 -11
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/index.d.mts +81 -5
  5. package/dist/index.d.ts +81 -5
  6. package/dist/index.js +304 -49
  7. package/dist/index.js.map +1 -1
  8. package/package.json +5 -5
  9. package/src/components/assign-row/components/blur-input.tsx +27 -0
  10. package/src/components/assign-row/config.json +11 -0
  11. package/src/components/assign-row/index.tsx +84 -0
  12. package/src/components/assign-row/types.ts +25 -0
  13. package/src/components/assign-rows/config.json +11 -0
  14. package/src/components/assign-rows/index.tsx +59 -0
  15. package/src/components/constant-input/config.json +1 -1
  16. package/src/components/display-outputs/index.tsx +7 -1
  17. package/src/components/display-schema-tree/config.json +1 -1
  18. package/src/components/index.ts +2 -0
  19. package/src/components/variable-selector/index.tsx +1 -1
  20. package/src/effects/index.ts +1 -0
  21. package/src/effects/validate-when-variable-sync/config.json +5 -0
  22. package/src/effects/validate-when-variable-sync/index.ts +35 -0
  23. package/src/form-plugins/index.ts +1 -0
  24. package/src/form-plugins/infer-assign-plugin/config.json +7 -0
  25. package/src/form-plugins/infer-assign-plugin/index.ts +90 -0
  26. package/src/index.ts +1 -0
  27. package/src/plugins/json-schema-preset/create-type-preset-plugin.tsx +1 -1
  28. package/src/plugins/json-schema-preset/type-definition/array.tsx +2 -1
  29. package/src/plugins/json-schema-preset/type-definition/object.tsx +2 -1
  30. package/src/validate/index.tsx +6 -0
  31. package/src/validate/validate-flow-value/config.json +7 -0
  32. package/src/validate/validate-flow-value/index.tsx +73 -0
package/bin/materials.ts CHANGED
@@ -28,6 +28,7 @@ const _types: string[] = [
28
28
  'plugins',
29
29
  'shared',
30
30
  'typings',
31
+ 'validate',
31
32
  'form-plugins',
32
33
  'hooks',
33
34
  ];
package/dist/esm/index.js CHANGED
@@ -735,7 +735,7 @@ var VariableSelector = ({
735
735
  showClear: false,
736
736
  arrowIcon: /* @__PURE__ */ React11.createElement(IconChevronDownStroked, { size: "small" }),
737
737
  triggerRender,
738
- placeholder: config?.placeholder ?? "Select Variable..."
738
+ placeholder: config?.placeholder ?? "Select Variable"
739
739
  }
740
740
  ));
741
741
  };
@@ -3126,7 +3126,16 @@ function DisplayOutputs({ value, showIconInTree, displayFromScope }) {
3126
3126
  return acm;
3127
3127
  }, {}) : value?.properties || {};
3128
3128
  const childEntries = Object.entries(properties || {});
3129
- return /* @__PURE__ */ React37.createElement(DisplayOutputsWrapper, null, childEntries.map(([key, schema]) => /* @__PURE__ */ React37.createElement(DisplaySchemaTag, { key, title: key, value: schema, showIconInTree })));
3129
+ return /* @__PURE__ */ React37.createElement(DisplayOutputsWrapper, null, childEntries.map(([key, schema]) => /* @__PURE__ */ React37.createElement(
3130
+ DisplaySchemaTag,
3131
+ {
3132
+ key,
3133
+ title: key,
3134
+ value: schema,
3135
+ showIconInTree,
3136
+ warning: !schema
3137
+ }
3138
+ )));
3130
3139
  }
3131
3140
 
3132
3141
  // src/components/display-flow-value/index.tsx
@@ -3187,6 +3196,127 @@ function DisplayInputsValues({ value, showIconInTree }) {
3187
3196
  return /* @__PURE__ */ React39.createElement(DisplayInputsWrapper, null, childEntries.map(([key, value2]) => /* @__PURE__ */ React39.createElement(DisplayFlowValue, { key, title: key, value: value2, showIconInTree })));
3188
3197
  }
3189
3198
 
3199
+ // src/components/assign-rows/index.tsx
3200
+ import React42 from "react";
3201
+ import { FieldArray } from "@flowgram.ai/editor";
3202
+ import { Button as Button5 } from "@douyinfe/semi-ui";
3203
+ import { IconPlus as IconPlus4 } from "@douyinfe/semi-icons";
3204
+
3205
+ // src/components/assign-row/index.tsx
3206
+ import React41 from "react";
3207
+ import { IconButton as IconButton6 } from "@douyinfe/semi-ui";
3208
+ import { IconMinus as IconMinus2 } from "@douyinfe/semi-icons";
3209
+
3210
+ // src/components/assign-row/components/blur-input.tsx
3211
+ import React40, { useEffect as useEffect11, useState as useState11 } from "react";
3212
+ import Input7 from "@douyinfe/semi-ui/lib/es/input";
3213
+ function BlurInput3(props) {
3214
+ const [value, setValue] = useState11("");
3215
+ useEffect11(() => {
3216
+ setValue(props.value);
3217
+ }, [props.value]);
3218
+ return /* @__PURE__ */ React40.createElement(
3219
+ Input7,
3220
+ {
3221
+ ...props,
3222
+ value,
3223
+ onChange: (value2) => {
3224
+ setValue(value2);
3225
+ },
3226
+ onBlur: (e) => props.onChange?.(value, e)
3227
+ }
3228
+ );
3229
+ }
3230
+
3231
+ // src/components/assign-row/index.tsx
3232
+ function AssignRow(props) {
3233
+ const {
3234
+ value = {
3235
+ operator: "assign"
3236
+ },
3237
+ onChange,
3238
+ onDelete,
3239
+ readonly
3240
+ } = props;
3241
+ return /* @__PURE__ */ React41.createElement("div", { style: { display: "flex", alignItems: "center", gap: 5 } }, /* @__PURE__ */ React41.createElement("div", { style: { width: 150, minWidth: 150, maxWidth: 150 } }, value?.operator === "assign" ? /* @__PURE__ */ React41.createElement(
3242
+ VariableSelector,
3243
+ {
3244
+ style: { width: "100%", height: 26 },
3245
+ value: value?.left?.content,
3246
+ config: { placeholder: "Select Left" },
3247
+ onChange: (v) => onChange?.({
3248
+ ...value,
3249
+ left: { type: "ref", content: v }
3250
+ })
3251
+ }
3252
+ ) : /* @__PURE__ */ React41.createElement(
3253
+ BlurInput3,
3254
+ {
3255
+ style: { height: 26 },
3256
+ size: "small",
3257
+ placeholder: "Input Name",
3258
+ value: value?.left,
3259
+ onChange: (v) => onChange?.({
3260
+ ...value,
3261
+ left: v
3262
+ })
3263
+ }
3264
+ )), /* @__PURE__ */ React41.createElement("div", { style: { flexGrow: 1 } }, /* @__PURE__ */ React41.createElement(
3265
+ DynamicValueInput,
3266
+ {
3267
+ readonly,
3268
+ value: value?.right,
3269
+ onChange: (v) => onChange?.({
3270
+ ...value,
3271
+ right: v
3272
+ })
3273
+ }
3274
+ )), onDelete && /* @__PURE__ */ React41.createElement("div", null, /* @__PURE__ */ React41.createElement(
3275
+ IconButton6,
3276
+ {
3277
+ size: "small",
3278
+ theme: "borderless",
3279
+ icon: /* @__PURE__ */ React41.createElement(IconMinus2, null),
3280
+ onClick: () => onDelete?.()
3281
+ }
3282
+ )));
3283
+ }
3284
+
3285
+ // src/components/assign-rows/index.tsx
3286
+ function AssignRows(props) {
3287
+ const { name, readonly } = props;
3288
+ return /* @__PURE__ */ React42.createElement(FieldArray, { name }, ({ field }) => /* @__PURE__ */ React42.createElement(React42.Fragment, null, field.map((childField, index) => /* @__PURE__ */ React42.createElement(
3289
+ AssignRow,
3290
+ {
3291
+ key: childField.key,
3292
+ readonly,
3293
+ value: childField.value,
3294
+ onChange: (value) => {
3295
+ childField.onChange(value);
3296
+ },
3297
+ onDelete: () => field.remove(index)
3298
+ }
3299
+ )), /* @__PURE__ */ React42.createElement("div", { style: { display: "flex", gap: 5 } }, /* @__PURE__ */ React42.createElement(
3300
+ Button5,
3301
+ {
3302
+ size: "small",
3303
+ theme: "borderless",
3304
+ icon: /* @__PURE__ */ React42.createElement(IconPlus4, null),
3305
+ onClick: () => field.append({ operator: "assign" })
3306
+ },
3307
+ "Assign"
3308
+ ), /* @__PURE__ */ React42.createElement(
3309
+ Button5,
3310
+ {
3311
+ size: "small",
3312
+ theme: "borderless",
3313
+ icon: /* @__PURE__ */ React42.createElement(IconPlus4, null),
3314
+ onClick: () => field.append({ operator: "declare" })
3315
+ },
3316
+ "Declaration"
3317
+ ))));
3318
+ }
3319
+
3190
3320
  // src/effects/provide-batch-input/index.ts
3191
3321
  import {
3192
3322
  ASTFactory,
@@ -3358,6 +3488,30 @@ var syncVariableTitle = [
3358
3488
  }
3359
3489
  ];
3360
3490
 
3491
+ // src/effects/validate-when-variable-sync/index.ts
3492
+ import { isEmpty } from "lodash";
3493
+ import {
3494
+ DataEvent as DataEvent3,
3495
+ getNodeScope,
3496
+ getNodePrivateScope
3497
+ } from "@flowgram.ai/editor";
3498
+ var validateWhenVariableSync = ({
3499
+ scope
3500
+ } = {}) => [
3501
+ {
3502
+ event: DataEvent3.onValueInit,
3503
+ effect: ({ context, form }) => {
3504
+ const nodeScope = scope === "private" ? getNodePrivateScope(context.node) : getNodeScope(context.node);
3505
+ const disposable = nodeScope.available.onListOrAnyVarChange(() => {
3506
+ if (!isEmpty(form.state.errors)) {
3507
+ form.validate();
3508
+ }
3509
+ });
3510
+ return () => disposable.dispose();
3511
+ }
3512
+ }
3513
+ ];
3514
+
3361
3515
  // src/shared/format-legacy-refs/index.ts
3362
3516
  import { isObject as isObject3 } from "lodash";
3363
3517
  function formatLegacyRefOnSubmit(value) {
@@ -3426,8 +3580,8 @@ import {
3426
3580
  createEffectFromVariableProvider as createEffectFromVariableProvider3,
3427
3581
  defineFormPluginCreator,
3428
3582
  getNodeForm as getNodeForm3,
3429
- getNodePrivateScope,
3430
- getNodeScope,
3583
+ getNodePrivateScope as getNodePrivateScope2,
3584
+ getNodeScope as getNodeScope2,
3431
3585
  ScopeChainTransformService,
3432
3586
  FlowNodeScopeType
3433
3587
  } from "@flowgram.ai/editor";
@@ -3472,7 +3626,7 @@ var createBatchOutputsFormPlugin = defineFormPluginCreator({
3472
3626
  transformCovers: (covers, ctx2) => {
3473
3627
  const node = ctx2.scope.meta?.node;
3474
3628
  if (node?.parent?.flowNodeType === batchNodeType) {
3475
- return [...covers, getNodeScope(node.parent)];
3629
+ return [...covers, getNodeScope2(node.parent)];
3476
3630
  }
3477
3631
  return covers;
3478
3632
  },
@@ -3485,8 +3639,8 @@ var createBatchOutputsFormPlugin = defineFormPluginCreator({
3485
3639
  if (node?.flowNodeType === batchNodeType) {
3486
3640
  const childBlocks = node.blocks;
3487
3641
  return [
3488
- getNodePrivateScope(node),
3489
- ...childBlocks.map((_childBlock) => getNodeScope(_childBlock))
3642
+ getNodePrivateScope2(node),
3643
+ ...childBlocks.map((_childBlock) => getNodeScope2(_childBlock))
3490
3644
  ];
3491
3645
  }
3492
3646
  return scopes;
@@ -3500,8 +3654,8 @@ import { get as get2, set as set2 } from "lodash";
3500
3654
  import { JsonSchemaUtils as JsonSchemaUtils8 } from "@flowgram.ai/json-schema";
3501
3655
  import {
3502
3656
  defineFormPluginCreator as defineFormPluginCreator2,
3503
- getNodePrivateScope as getNodePrivateScope2,
3504
- getNodeScope as getNodeScope2
3657
+ getNodePrivateScope as getNodePrivateScope3,
3658
+ getNodeScope as getNodeScope3
3505
3659
  } from "@flowgram.ai/editor";
3506
3660
  var createInferInputsPlugin = defineFormPluginCreator2({
3507
3661
  onSetupFormMeta({ addFormatOnSubmit }, { sourceKey, targetKey, scope }) {
@@ -3514,7 +3668,7 @@ var createInferInputsPlugin = defineFormPluginCreator2({
3514
3668
  targetKey,
3515
3669
  infer(
3516
3670
  get2(formData, sourceKey),
3517
- scope === "private" ? getNodePrivateScope2(ctx.node) : getNodeScope2(ctx.node)
3671
+ scope === "private" ? getNodePrivateScope3(ctx.node) : getNodeScope3(ctx.node)
3518
3672
  )
3519
3673
  );
3520
3674
  return formData;
@@ -3574,7 +3728,110 @@ var infer = (values, scope) => {
3574
3728
  };
3575
3729
  }
3576
3730
  };
3731
+
3732
+ // src/form-plugins/infer-assign-plugin/index.ts
3733
+ import { set as set3, uniqBy } from "lodash";
3734
+ import { JsonSchemaUtils as JsonSchemaUtils9 } from "@flowgram.ai/json-schema";
3735
+ import {
3736
+ ASTFactory as ASTFactory4,
3737
+ createEffectFromVariableProvider as createEffectFromVariableProvider4,
3738
+ defineFormPluginCreator as defineFormPluginCreator3,
3739
+ getNodeForm as getNodeForm4,
3740
+ getNodeScope as getNodeScope4
3741
+ } from "@flowgram.ai/editor";
3742
+ var createInferAssignPlugin = defineFormPluginCreator3({
3743
+ onSetupFormMeta({ addFormatOnSubmit, mergeEffect }, { assignKey, outputKey }) {
3744
+ if (!assignKey || !outputKey) {
3745
+ return;
3746
+ }
3747
+ mergeEffect({
3748
+ [assignKey]: createEffectFromVariableProvider4({
3749
+ parse: (value, ctx) => {
3750
+ const declareRows = uniqBy(
3751
+ value.filter((_v) => _v.operator === "declare" && _v.left && _v.right),
3752
+ "left"
3753
+ );
3754
+ return [
3755
+ ASTFactory4.createVariableDeclaration({
3756
+ key: `${ctx.node.id}`,
3757
+ meta: {
3758
+ title: getNodeForm4(ctx.node)?.getValueIn("title"),
3759
+ icon: ctx.node.getNodeRegistry().info?.icon
3760
+ },
3761
+ type: ASTFactory4.createObject({
3762
+ properties: declareRows.map(
3763
+ (_v) => ASTFactory4.createProperty({
3764
+ key: _v.left,
3765
+ type: _v.right?.type === "constant" ? JsonSchemaUtils9.schemaToAST(_v.right?.schema || {}) : void 0,
3766
+ initializer: _v.right?.type === "ref" ? ASTFactory4.createKeyPathExpression({
3767
+ keyPath: _v.right?.content || []
3768
+ }) : {}
3769
+ })
3770
+ )
3771
+ })
3772
+ })
3773
+ ];
3774
+ }
3775
+ })
3776
+ });
3777
+ addFormatOnSubmit((formData, ctx) => {
3778
+ set3(
3779
+ formData,
3780
+ outputKey,
3781
+ JsonSchemaUtils9.astToSchema(getNodeScope4(ctx.node).output.variables?.[0]?.type)
3782
+ );
3783
+ return formData;
3784
+ });
3785
+ }
3786
+ });
3787
+
3788
+ // src/validate/validate-flow-value/index.tsx
3789
+ import { isNil, uniq as uniq2 } from "lodash";
3790
+ import { FeedbackLevel, getNodeScope as getNodeScope5 } from "@flowgram.ai/editor";
3791
+ function validateFlowValue(value, ctx) {
3792
+ const { node, required, errorMessages } = ctx;
3793
+ const {
3794
+ required: requiredMessage = "Field is required",
3795
+ unknownVariable: unknownVariableMessage = "Unknown Variable"
3796
+ } = errorMessages || {};
3797
+ if (required && (isNil(value) || isNil(value?.content) || value?.content === "")) {
3798
+ return {
3799
+ level: FeedbackLevel.Error,
3800
+ message: requiredMessage
3801
+ };
3802
+ }
3803
+ if (value?.type === "ref") {
3804
+ const variable = getNodeScope5(node).available.getByKeyPath(value?.content || []);
3805
+ if (!variable) {
3806
+ return {
3807
+ level: FeedbackLevel.Error,
3808
+ message: unknownVariableMessage
3809
+ };
3810
+ }
3811
+ }
3812
+ if (value?.type === "template") {
3813
+ const allRefs = getTemplateKeyPaths2(value);
3814
+ for (const ref of allRefs) {
3815
+ const variable = getNodeScope5(node).available.getByKeyPath(ref);
3816
+ if (!variable) {
3817
+ return {
3818
+ level: FeedbackLevel.Error,
3819
+ message: unknownVariableMessage
3820
+ };
3821
+ }
3822
+ }
3823
+ }
3824
+ return void 0;
3825
+ }
3826
+ function getTemplateKeyPaths2(value) {
3827
+ const keyPathReg = /{{(.*?)}}/g;
3828
+ return uniq2(value.content?.match(keyPathReg) || []).map(
3829
+ (_keyPath) => _keyPath.slice(2, -2).split(".")
3830
+ );
3831
+ }
3577
3832
  export {
3833
+ AssignRow,
3834
+ AssignRows,
3578
3835
  BatchOutputs,
3579
3836
  BatchVariableSelector,
3580
3837
  CodeEditor,
@@ -3600,6 +3857,7 @@ export {
3600
3857
  autoRenameRefEffect,
3601
3858
  createBatchOutputsFormPlugin,
3602
3859
  createDisableDeclarationPlugin,
3860
+ createInferAssignPlugin,
3603
3861
  createInferInputsPlugin,
3604
3862
  createTypePresetPlugin,
3605
3863
  formatLegacyRefOnInit,
@@ -3615,6 +3873,8 @@ export {
3615
3873
  provideJsonSchemaOutputs,
3616
3874
  syncVariableTitle,
3617
3875
  useTypeManager,
3618
- useVariableTree
3876
+ useVariableTree,
3877
+ validateFlowValue,
3878
+ validateWhenVariableSync
3619
3879
  };
3620
3880
  //# sourceMappingURL=index.js.map