@flowgram.ai/form-materials 0.3.1 → 0.3.3

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 (37) hide show
  1. package/bin/materials.ts +1 -0
  2. package/dist/esm/index.js +331 -13
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/index.d.mts +125 -6
  5. package/dist/index.d.ts +125 -6
  6. package/dist/index.js +360 -51
  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 +3 -0
  21. package/src/effects/listen-ref-schema-change/config.json +10 -0
  22. package/src/effects/listen-ref-schema-change/index.ts +56 -0
  23. package/src/effects/listen-ref-value-change/config.json +9 -0
  24. package/src/effects/listen-ref-value-change/index.ts +53 -0
  25. package/src/effects/validate-when-variable-sync/config.json +5 -0
  26. package/src/effects/validate-when-variable-sync/index.ts +35 -0
  27. package/src/form-plugins/index.ts +1 -0
  28. package/src/form-plugins/infer-assign-plugin/config.json +7 -0
  29. package/src/form-plugins/infer-assign-plugin/index.ts +90 -0
  30. package/src/index.ts +1 -0
  31. package/src/plugins/json-schema-preset/create-type-preset-plugin.tsx +1 -1
  32. package/src/plugins/json-schema-preset/index.tsx +2 -0
  33. package/src/plugins/json-schema-preset/type-definition/array.tsx +2 -1
  34. package/src/plugins/json-schema-preset/type-definition/object.tsx +2 -1
  35. package/src/validate/index.tsx +6 -0
  36. package/src/validate/validate-flow-value/config.json +7 -0
  37. 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,86 @@ 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
+
3515
+ // src/effects/listen-ref-value-change/index.ts
3516
+ import {
3517
+ DataEvent as DataEvent4,
3518
+ getNodeScope as getNodeScope2
3519
+ } from "@flowgram.ai/editor";
3520
+ var listenRefValueChange = (cb) => [
3521
+ {
3522
+ event: DataEvent4.onValueInitOrChange,
3523
+ effect: (params) => {
3524
+ const { context, value } = params;
3525
+ if (value?.type !== "ref") {
3526
+ return () => null;
3527
+ }
3528
+ const disposable = getNodeScope2(context.node).available.trackByKeyPath(
3529
+ value?.content || [],
3530
+ (v) => {
3531
+ cb({ ...params, variable: v });
3532
+ }
3533
+ );
3534
+ return () => {
3535
+ disposable.dispose();
3536
+ };
3537
+ }
3538
+ }
3539
+ ];
3540
+
3541
+ // src/effects/listen-ref-schema-change/index.ts
3542
+ import { JsonSchemaUtils as JsonSchemaUtils8 } from "@flowgram.ai/json-schema";
3543
+ import {
3544
+ DataEvent as DataEvent5,
3545
+ getNodeScope as getNodeScope3
3546
+ } from "@flowgram.ai/editor";
3547
+ var listenRefSchemaChange = (cb) => [
3548
+ {
3549
+ event: DataEvent5.onValueInitOrChange,
3550
+ effect: (params) => {
3551
+ const { context, value } = params;
3552
+ if (value?.type !== "ref") {
3553
+ return () => null;
3554
+ }
3555
+ const disposable = getNodeScope3(context.node).available.trackByKeyPath(
3556
+ value?.content || [],
3557
+ (_type) => {
3558
+ cb({ ...params, schema: JsonSchemaUtils8.astToSchema(_type) });
3559
+ },
3560
+ {
3561
+ selector: (_v) => _v?.type
3562
+ }
3563
+ );
3564
+ return () => {
3565
+ disposable.dispose();
3566
+ };
3567
+ }
3568
+ }
3569
+ ];
3570
+
3361
3571
  // src/shared/format-legacy-refs/index.ts
3362
3572
  import { isObject as isObject3 } from "lodash";
3363
3573
  function formatLegacyRefOnSubmit(value) {
@@ -3426,8 +3636,8 @@ import {
3426
3636
  createEffectFromVariableProvider as createEffectFromVariableProvider3,
3427
3637
  defineFormPluginCreator,
3428
3638
  getNodeForm as getNodeForm3,
3429
- getNodePrivateScope,
3430
- getNodeScope,
3639
+ getNodePrivateScope as getNodePrivateScope2,
3640
+ getNodeScope as getNodeScope4,
3431
3641
  ScopeChainTransformService,
3432
3642
  FlowNodeScopeType
3433
3643
  } from "@flowgram.ai/editor";
@@ -3472,7 +3682,7 @@ var createBatchOutputsFormPlugin = defineFormPluginCreator({
3472
3682
  transformCovers: (covers, ctx2) => {
3473
3683
  const node = ctx2.scope.meta?.node;
3474
3684
  if (node?.parent?.flowNodeType === batchNodeType) {
3475
- return [...covers, getNodeScope(node.parent)];
3685
+ return [...covers, getNodeScope4(node.parent)];
3476
3686
  }
3477
3687
  return covers;
3478
3688
  },
@@ -3485,8 +3695,8 @@ var createBatchOutputsFormPlugin = defineFormPluginCreator({
3485
3695
  if (node?.flowNodeType === batchNodeType) {
3486
3696
  const childBlocks = node.blocks;
3487
3697
  return [
3488
- getNodePrivateScope(node),
3489
- ...childBlocks.map((_childBlock) => getNodeScope(_childBlock))
3698
+ getNodePrivateScope2(node),
3699
+ ...childBlocks.map((_childBlock) => getNodeScope4(_childBlock))
3490
3700
  ];
3491
3701
  }
3492
3702
  return scopes;
@@ -3497,11 +3707,11 @@ var createBatchOutputsFormPlugin = defineFormPluginCreator({
3497
3707
 
3498
3708
  // src/form-plugins/infer-inputs-plugin/index.ts
3499
3709
  import { get as get2, set as set2 } from "lodash";
3500
- import { JsonSchemaUtils as JsonSchemaUtils8 } from "@flowgram.ai/json-schema";
3710
+ import { JsonSchemaUtils as JsonSchemaUtils9 } from "@flowgram.ai/json-schema";
3501
3711
  import {
3502
3712
  defineFormPluginCreator as defineFormPluginCreator2,
3503
- getNodePrivateScope as getNodePrivateScope2,
3504
- getNodeScope as getNodeScope2
3713
+ getNodePrivateScope as getNodePrivateScope3,
3714
+ getNodeScope as getNodeScope5
3505
3715
  } from "@flowgram.ai/editor";
3506
3716
  var createInferInputsPlugin = defineFormPluginCreator2({
3507
3717
  onSetupFormMeta({ addFormatOnSubmit }, { sourceKey, targetKey, scope }) {
@@ -3514,7 +3724,7 @@ var createInferInputsPlugin = defineFormPluginCreator2({
3514
3724
  targetKey,
3515
3725
  infer(
3516
3726
  get2(formData, sourceKey),
3517
- scope === "private" ? getNodePrivateScope2(ctx.node) : getNodeScope2(ctx.node)
3727
+ scope === "private" ? getNodePrivateScope3(ctx.node) : getNodeScope5(ctx.node)
3518
3728
  )
3519
3729
  );
3520
3730
  return formData;
@@ -3554,7 +3764,7 @@ var infer = (values, scope) => {
3554
3764
  }
3555
3765
  if (isRef2(values)) {
3556
3766
  const variable = scope.available.getByKeyPath(values?.content);
3557
- const schema = variable?.type ? JsonSchemaUtils8.astToSchema(variable?.type) : void 0;
3767
+ const schema = variable?.type ? JsonSchemaUtils9.astToSchema(variable?.type) : void 0;
3558
3768
  return schema;
3559
3769
  }
3560
3770
  if (isTemplate2(values)) {
@@ -3574,7 +3784,110 @@ var infer = (values, scope) => {
3574
3784
  };
3575
3785
  }
3576
3786
  };
3787
+
3788
+ // src/form-plugins/infer-assign-plugin/index.ts
3789
+ import { set as set3, uniqBy } from "lodash";
3790
+ import { JsonSchemaUtils as JsonSchemaUtils10 } from "@flowgram.ai/json-schema";
3791
+ import {
3792
+ ASTFactory as ASTFactory4,
3793
+ createEffectFromVariableProvider as createEffectFromVariableProvider4,
3794
+ defineFormPluginCreator as defineFormPluginCreator3,
3795
+ getNodeForm as getNodeForm4,
3796
+ getNodeScope as getNodeScope6
3797
+ } from "@flowgram.ai/editor";
3798
+ var createInferAssignPlugin = defineFormPluginCreator3({
3799
+ onSetupFormMeta({ addFormatOnSubmit, mergeEffect }, { assignKey, outputKey }) {
3800
+ if (!assignKey || !outputKey) {
3801
+ return;
3802
+ }
3803
+ mergeEffect({
3804
+ [assignKey]: createEffectFromVariableProvider4({
3805
+ parse: (value, ctx) => {
3806
+ const declareRows = uniqBy(
3807
+ value.filter((_v) => _v.operator === "declare" && _v.left && _v.right),
3808
+ "left"
3809
+ );
3810
+ return [
3811
+ ASTFactory4.createVariableDeclaration({
3812
+ key: `${ctx.node.id}`,
3813
+ meta: {
3814
+ title: getNodeForm4(ctx.node)?.getValueIn("title"),
3815
+ icon: ctx.node.getNodeRegistry().info?.icon
3816
+ },
3817
+ type: ASTFactory4.createObject({
3818
+ properties: declareRows.map(
3819
+ (_v) => ASTFactory4.createProperty({
3820
+ key: _v.left,
3821
+ type: _v.right?.type === "constant" ? JsonSchemaUtils10.schemaToAST(_v.right?.schema || {}) : void 0,
3822
+ initializer: _v.right?.type === "ref" ? ASTFactory4.createKeyPathExpression({
3823
+ keyPath: _v.right?.content || []
3824
+ }) : {}
3825
+ })
3826
+ )
3827
+ })
3828
+ })
3829
+ ];
3830
+ }
3831
+ })
3832
+ });
3833
+ addFormatOnSubmit((formData, ctx) => {
3834
+ set3(
3835
+ formData,
3836
+ outputKey,
3837
+ JsonSchemaUtils10.astToSchema(getNodeScope6(ctx.node).output.variables?.[0]?.type)
3838
+ );
3839
+ return formData;
3840
+ });
3841
+ }
3842
+ });
3843
+
3844
+ // src/validate/validate-flow-value/index.tsx
3845
+ import { isNil, uniq as uniq2 } from "lodash";
3846
+ import { FeedbackLevel, getNodeScope as getNodeScope7 } from "@flowgram.ai/editor";
3847
+ function validateFlowValue(value, ctx) {
3848
+ const { node, required, errorMessages } = ctx;
3849
+ const {
3850
+ required: requiredMessage = "Field is required",
3851
+ unknownVariable: unknownVariableMessage = "Unknown Variable"
3852
+ } = errorMessages || {};
3853
+ if (required && (isNil(value) || isNil(value?.content) || value?.content === "")) {
3854
+ return {
3855
+ level: FeedbackLevel.Error,
3856
+ message: requiredMessage
3857
+ };
3858
+ }
3859
+ if (value?.type === "ref") {
3860
+ const variable = getNodeScope7(node).available.getByKeyPath(value?.content || []);
3861
+ if (!variable) {
3862
+ return {
3863
+ level: FeedbackLevel.Error,
3864
+ message: unknownVariableMessage
3865
+ };
3866
+ }
3867
+ }
3868
+ if (value?.type === "template") {
3869
+ const allRefs = getTemplateKeyPaths2(value);
3870
+ for (const ref of allRefs) {
3871
+ const variable = getNodeScope7(node).available.getByKeyPath(ref);
3872
+ if (!variable) {
3873
+ return {
3874
+ level: FeedbackLevel.Error,
3875
+ message: unknownVariableMessage
3876
+ };
3877
+ }
3878
+ }
3879
+ }
3880
+ return void 0;
3881
+ }
3882
+ function getTemplateKeyPaths2(value) {
3883
+ const keyPathReg = /{{(.*?)}}/g;
3884
+ return uniq2(value.content?.match(keyPathReg) || []).map(
3885
+ (_keyPath) => _keyPath.slice(2, -2).split(".")
3886
+ );
3887
+ }
3577
3888
  export {
3889
+ AssignRow,
3890
+ AssignRows,
3578
3891
  BatchOutputs,
3579
3892
  BatchVariableSelector,
3580
3893
  CodeEditor,
@@ -3600,6 +3913,7 @@ export {
3600
3913
  autoRenameRefEffect,
3601
3914
  createBatchOutputsFormPlugin,
3602
3915
  createDisableDeclarationPlugin,
3916
+ createInferAssignPlugin,
3603
3917
  createInferInputsPlugin,
3604
3918
  createTypePresetPlugin,
3605
3919
  formatLegacyRefOnInit,
@@ -3609,12 +3923,16 @@ export {
3609
3923
  getTypeSelectValue,
3610
3924
  isLegacyFlowRefValueSchema,
3611
3925
  isNewFlowRefValueSchema,
3926
+ listenRefSchemaChange,
3927
+ listenRefValueChange,
3612
3928
  parseTypeSelectValue,
3613
3929
  provideBatchInputEffect,
3614
3930
  provideBatchOutputsEffect,
3615
3931
  provideJsonSchemaOutputs,
3616
3932
  syncVariableTitle,
3617
3933
  useTypeManager,
3618
- useVariableTree
3934
+ useVariableTree,
3935
+ validateFlowValue,
3936
+ validateWhenVariableSync
3619
3937
  };
3620
3938
  //# sourceMappingURL=index.js.map