@flowgram.ai/node 0.1.1
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.
- package/dist/esm/index.js +563 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +321 -0
- package/dist/index.d.ts +321 -0
- package/dist/index.js +606 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
DataEvent: () => DataEvent,
|
|
34
|
+
FormModelV2: () => FormModelV2,
|
|
35
|
+
FormPlugin: () => FormPlugin,
|
|
36
|
+
createEffectOptions: () => createEffectOptions,
|
|
37
|
+
defineFormPluginCreator: () => defineFormPluginCreator,
|
|
38
|
+
getNodeForm: () => getNodeForm,
|
|
39
|
+
isFormMetaV2: () => isFormMetaV2,
|
|
40
|
+
isFormModelV2: () => isFormModelV2,
|
|
41
|
+
isFormV2: () => isFormV2,
|
|
42
|
+
useInitializedFormModel: () => useInitializedFormModel,
|
|
43
|
+
useWatchFormErrors: () => useWatchFormErrors,
|
|
44
|
+
useWatchFormState: () => useWatchFormState,
|
|
45
|
+
useWatchFormValueIn: () => useWatchFormValueIn,
|
|
46
|
+
useWatchFormValues: () => useWatchFormValues,
|
|
47
|
+
useWatchFormWarnings: () => useWatchFormWarnings
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(src_exports);
|
|
50
|
+
|
|
51
|
+
// src/types.ts
|
|
52
|
+
var DataEvent = /* @__PURE__ */ ((DataEvent2) => {
|
|
53
|
+
DataEvent2["onValueChange"] = "onValueChange";
|
|
54
|
+
DataEvent2["onValueInit"] = "onValueInit";
|
|
55
|
+
DataEvent2["onValueInitOrChange"] = "onValueInitOrChange";
|
|
56
|
+
DataEvent2["onArrayAppend"] = "onArrayAppend";
|
|
57
|
+
DataEvent2["onArrayDelete"] = "onArrayDelete";
|
|
58
|
+
return DataEvent2;
|
|
59
|
+
})(DataEvent || {});
|
|
60
|
+
function isFormModelV2(fm) {
|
|
61
|
+
return "onFormValuesChange" in fm;
|
|
62
|
+
}
|
|
63
|
+
function isFormMetaV2(formMeta) {
|
|
64
|
+
return "render" in formMeta;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/form-model-v2.ts
|
|
68
|
+
var import_lodash2 = require("lodash");
|
|
69
|
+
var import_utils = require("@flowgram.ai/utils");
|
|
70
|
+
var import_form_core2 = require("@flowgram.ai/form-core");
|
|
71
|
+
var import_form2 = require("@flowgram.ai/form");
|
|
72
|
+
var import_core = require("@flowgram.ai/core");
|
|
73
|
+
|
|
74
|
+
// src/utils.ts
|
|
75
|
+
var import_lodash = require("lodash");
|
|
76
|
+
var import_form_core = require("@flowgram.ai/form-core");
|
|
77
|
+
function findMatchedInMap(field, validateMap) {
|
|
78
|
+
if (!validateMap) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (validateMap[field.name]) {
|
|
82
|
+
return validateMap[field.name];
|
|
83
|
+
}
|
|
84
|
+
const found = (0, import_lodash.find)(Object.keys(validateMap), (key) => {
|
|
85
|
+
if (key.startsWith("regex:")) {
|
|
86
|
+
const regex = RegExp(key.split(":")[1]);
|
|
87
|
+
return regex.test(field.name);
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
});
|
|
91
|
+
if (found) {
|
|
92
|
+
return validateMap[found];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function formFeedbacksToNodeCoreFormFeedbacks(formFeedbacks) {
|
|
96
|
+
return formFeedbacks.map(
|
|
97
|
+
(f) => ({
|
|
98
|
+
feedbackStatus: f.level,
|
|
99
|
+
feedbackText: f.message,
|
|
100
|
+
path: f.name
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
function convertGlobPath(path) {
|
|
105
|
+
if (path.startsWith("/")) {
|
|
106
|
+
const parts = import_form_core.FormPathService.normalize(path).slice(1).split("/");
|
|
107
|
+
return parts.join(".");
|
|
108
|
+
}
|
|
109
|
+
return path;
|
|
110
|
+
}
|
|
111
|
+
function mergeEffectMap(origin, source) {
|
|
112
|
+
return (0, import_lodash.mergeWith)(origin, source, function(objValue, srcValue) {
|
|
113
|
+
return (objValue || []).concat(srcValue);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/form-render.tsx
|
|
118
|
+
var import_react = __toESM(require("react"));
|
|
119
|
+
var import_form = require("@flowgram.ai/form");
|
|
120
|
+
var FormRender = ({ formModel }) => formModel?.formControl ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_form.Form, { control: formModel?.formControl, keepModelOnUnMount: true }, formModel.formMeta.render)) : null;
|
|
121
|
+
function renderForm(formModel) {
|
|
122
|
+
return /* @__PURE__ */ import_react.default.createElement(FormRender, { formModel });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/form-model-v2.ts
|
|
126
|
+
var DEFAULT = {
|
|
127
|
+
EFFECT_MAP: {},
|
|
128
|
+
EFFECT_RETURN_MAP: /* @__PURE__ */ new Map([
|
|
129
|
+
["onValueInitOrChange" /* onValueInitOrChange */, {}],
|
|
130
|
+
["onValueChange" /* onValueChange */, {}],
|
|
131
|
+
["onValueInit" /* onValueInit */, {}],
|
|
132
|
+
["onArrayAppend" /* onArrayAppend */, {}],
|
|
133
|
+
["onArrayDelete" /* onArrayDelete */, {}]
|
|
134
|
+
]),
|
|
135
|
+
FORM_FEEDBACKS: [],
|
|
136
|
+
VALID: null
|
|
137
|
+
};
|
|
138
|
+
var FormModelV2 = class extends import_form_core2.FormModel {
|
|
139
|
+
constructor(node) {
|
|
140
|
+
super();
|
|
141
|
+
this.effectMap = DEFAULT.EFFECT_MAP;
|
|
142
|
+
this.effectReturnMap = DEFAULT.EFFECT_RETURN_MAP;
|
|
143
|
+
this.plugins = [];
|
|
144
|
+
this.formFeedbacks = DEFAULT.FORM_FEEDBACKS;
|
|
145
|
+
this.onInitializedEmitter = new import_utils.Emitter();
|
|
146
|
+
this.onValidateEmitter = new import_utils.Emitter();
|
|
147
|
+
this.onValidate = this.onValidateEmitter.event;
|
|
148
|
+
this.onInitialized = this.onInitializedEmitter.event;
|
|
149
|
+
this.onDisposeEmitter = new import_utils.Emitter();
|
|
150
|
+
this.onDispose = this.onDisposeEmitter.event;
|
|
151
|
+
this.toDispose = new import_utils.DisposableCollection();
|
|
152
|
+
this.onFormValuesChangeEmitter = new import_utils.Emitter();
|
|
153
|
+
this.onFormValuesChange = this.onFormValuesChangeEmitter.event;
|
|
154
|
+
this.onValidChangeEmitter = new import_utils.Emitter();
|
|
155
|
+
this.onValidChange = this.onValidChangeEmitter.event;
|
|
156
|
+
this.onFeedbacksChangeEmitter = new import_utils.Emitter();
|
|
157
|
+
this.onFeedbacksChange = this.onFeedbacksChangeEmitter.event;
|
|
158
|
+
this._valid = DEFAULT.VALID;
|
|
159
|
+
this._feedbacks = [];
|
|
160
|
+
this._initialized = false;
|
|
161
|
+
this.node = node;
|
|
162
|
+
this.toDispose.pushAll([
|
|
163
|
+
this.onInitializedEmitter,
|
|
164
|
+
this.onValidateEmitter,
|
|
165
|
+
this.onValidChangeEmitter,
|
|
166
|
+
this.onFeedbacksChangeEmitter,
|
|
167
|
+
this.onFormValuesChangeEmitter
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
get valid() {
|
|
171
|
+
return this._valid;
|
|
172
|
+
}
|
|
173
|
+
set valid(valid) {
|
|
174
|
+
this._valid = valid;
|
|
175
|
+
this.onValidChangeEmitter.fire(valid);
|
|
176
|
+
}
|
|
177
|
+
get flowNodeEntity() {
|
|
178
|
+
return this.node;
|
|
179
|
+
}
|
|
180
|
+
get formManager() {
|
|
181
|
+
return this.node.getService(import_form_core2.FormManager);
|
|
182
|
+
}
|
|
183
|
+
get formControl() {
|
|
184
|
+
return this._formControl;
|
|
185
|
+
}
|
|
186
|
+
get formMeta() {
|
|
187
|
+
return this.node.getNodeRegistry().formMeta;
|
|
188
|
+
}
|
|
189
|
+
get feedbacks() {
|
|
190
|
+
return this._feedbacks;
|
|
191
|
+
}
|
|
192
|
+
set feedbacks(feedbacks) {
|
|
193
|
+
this._feedbacks = feedbacks;
|
|
194
|
+
this.onFeedbacksChangeEmitter.fire(feedbacks);
|
|
195
|
+
}
|
|
196
|
+
get formItemPathMap() {
|
|
197
|
+
return /* @__PURE__ */ new Map();
|
|
198
|
+
}
|
|
199
|
+
get initialized() {
|
|
200
|
+
return this._initialized;
|
|
201
|
+
}
|
|
202
|
+
get nodeContext() {
|
|
203
|
+
return {
|
|
204
|
+
node: this.node,
|
|
205
|
+
playgroundContext: this.node.getService(import_core.PlaygroundContext)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
get nativeFormModel() {
|
|
209
|
+
return this._formControl?._formModel;
|
|
210
|
+
}
|
|
211
|
+
render() {
|
|
212
|
+
return renderForm(this);
|
|
213
|
+
}
|
|
214
|
+
initPlugins(plugins) {
|
|
215
|
+
if (!plugins.length) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
this.plugins = plugins;
|
|
219
|
+
plugins.forEach((plugin) => {
|
|
220
|
+
plugin.init(this);
|
|
221
|
+
if (plugin.config?.effect) {
|
|
222
|
+
mergeEffectMap(this.effectMap, plugin.config.effect);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
init(formMeta, rawInitialValues) {
|
|
227
|
+
const formData = this.node.getData(import_form_core2.FlowNodeFormData);
|
|
228
|
+
this.onFormValuesChange(() => {
|
|
229
|
+
this._valid = null;
|
|
230
|
+
formData.fireChange();
|
|
231
|
+
});
|
|
232
|
+
const { validateTrigger, validate, effect } = formMeta;
|
|
233
|
+
if (effect) {
|
|
234
|
+
this.effectMap = effect;
|
|
235
|
+
}
|
|
236
|
+
const defaultValues = typeof formMeta.defaultValues === "function" ? formMeta.defaultValues() : formMeta.defaultValues;
|
|
237
|
+
const initialValues = formMeta.formatOnInit ? formMeta.formatOnInit(rawInitialValues, this.nodeContext) : rawInitialValues;
|
|
238
|
+
const { control } = (0, import_form2.createForm)({
|
|
239
|
+
initialValues: initialValues || defaultValues,
|
|
240
|
+
validateTrigger,
|
|
241
|
+
context: this.nodeContext,
|
|
242
|
+
validate,
|
|
243
|
+
disableAutoInit: true
|
|
244
|
+
});
|
|
245
|
+
this._formControl = control;
|
|
246
|
+
const nativeFormModel = control._formModel;
|
|
247
|
+
this.toDispose.push(nativeFormModel);
|
|
248
|
+
nativeFormModel.onFormValuesChange((props) => {
|
|
249
|
+
this.onFormValuesChangeEmitter.fire(props);
|
|
250
|
+
});
|
|
251
|
+
if (formMeta.plugins) {
|
|
252
|
+
this.initPlugins(formMeta.plugins);
|
|
253
|
+
}
|
|
254
|
+
nativeFormModel.onFormValuesChange(({ values, prevValues, name }) => {
|
|
255
|
+
const effectKeys = Object.keys(this.effectMap).filter(
|
|
256
|
+
(pattern) => import_form2.Glob.isMatchOrParent(pattern, name)
|
|
257
|
+
);
|
|
258
|
+
effectKeys.forEach((effectKey) => {
|
|
259
|
+
const effectOptionsArr = this.effectMap[effectKey];
|
|
260
|
+
effectOptionsArr.forEach(({ effect: effect2, event }) => {
|
|
261
|
+
if (event === "onValueChange" /* onValueChange */ || event === "onValueInitOrChange" /* onValueInitOrChange */) {
|
|
262
|
+
const currentName = import_form2.Glob.getParentPathByPattern(effectKey, name);
|
|
263
|
+
const prevEffectReturn = this.effectReturnMap.get(event)?.[currentName];
|
|
264
|
+
if (prevEffectReturn) {
|
|
265
|
+
prevEffectReturn();
|
|
266
|
+
}
|
|
267
|
+
const effectReturn = effect2({
|
|
268
|
+
name: currentName,
|
|
269
|
+
value: (0, import_lodash2.get)(values, currentName),
|
|
270
|
+
prevValue: (0, import_lodash2.get)(prevValues, currentName),
|
|
271
|
+
formValues: values,
|
|
272
|
+
context: this.nodeContext
|
|
273
|
+
});
|
|
274
|
+
if (effectReturn && typeof effectReturn === "function" && this.effectReturnMap.has(event)) {
|
|
275
|
+
const eventMap = this.effectReturnMap.get(event);
|
|
276
|
+
eventMap[currentName] = effectReturn;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
nativeFormModel.onFormValuesInit(({ values, name, prevValues }) => {
|
|
283
|
+
Object.keys(this.effectMap).forEach((pattern) => {
|
|
284
|
+
const paths = import_form2.Glob.findMatchPaths(values, pattern);
|
|
285
|
+
const effectOptionsArr = this.effectMap[pattern];
|
|
286
|
+
effectOptionsArr.forEach(({ event, effect: effect2 }) => {
|
|
287
|
+
if (event === "onValueInit" /* onValueInit */ || event === "onValueInitOrChange" /* onValueInitOrChange */) {
|
|
288
|
+
paths.forEach((path) => {
|
|
289
|
+
if (import_form2.Glob.isMatchOrParent(name, path) || name === path) {
|
|
290
|
+
const prevEffectReturn = this.effectReturnMap.get(event)?.[path];
|
|
291
|
+
if (prevEffectReturn) {
|
|
292
|
+
prevEffectReturn();
|
|
293
|
+
}
|
|
294
|
+
const effectReturn = effect2({
|
|
295
|
+
name: path,
|
|
296
|
+
value: (0, import_lodash2.get)(values, path),
|
|
297
|
+
formValues: values,
|
|
298
|
+
prevValue: (0, import_lodash2.get)(prevValues, path),
|
|
299
|
+
context: this.nodeContext
|
|
300
|
+
});
|
|
301
|
+
if (effectReturn && typeof effectReturn === "function" && this.effectReturnMap.has(event)) {
|
|
302
|
+
const eventMap = this.effectReturnMap.get(event);
|
|
303
|
+
eventMap[path] = effectReturn;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
nativeFormModel.onFieldModelCreate((field) => {
|
|
312
|
+
const effectOptionsArr = findMatchedInMap(field, this.effectMap);
|
|
313
|
+
if (effectOptionsArr?.length) {
|
|
314
|
+
const eventMap = (0, import_lodash2.groupBy)(effectOptionsArr, "event");
|
|
315
|
+
(0, import_lodash2.mapKeys)(eventMap, (optionsArr, event) => {
|
|
316
|
+
const combinedEffect = (props) => {
|
|
317
|
+
optionsArr.forEach(
|
|
318
|
+
({ effect: effect2 }) => effect2({
|
|
319
|
+
...props,
|
|
320
|
+
formValues: nativeFormModel.values,
|
|
321
|
+
context: this.nodeContext
|
|
322
|
+
})
|
|
323
|
+
);
|
|
324
|
+
};
|
|
325
|
+
switch (event) {
|
|
326
|
+
case "onArrayAppend" /* onArrayAppend */:
|
|
327
|
+
if (field instanceof import_form2.FieldArrayModel) {
|
|
328
|
+
field.onAppend(combinedEffect);
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
case "onArrayDelete" /* onArrayDelete */:
|
|
332
|
+
if (field instanceof import_form2.FieldArrayModel) {
|
|
333
|
+
field.onDelete(combinedEffect);
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
this._formControl.init();
|
|
341
|
+
this._initialized = true;
|
|
342
|
+
this.onInitializedEmitter.fire(this);
|
|
343
|
+
this.onDispose(() => {
|
|
344
|
+
this._initialized = false;
|
|
345
|
+
this.effectMap = {};
|
|
346
|
+
nativeFormModel.dispose();
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
toJSON() {
|
|
350
|
+
if (this.formMeta.formatOnSubmit) {
|
|
351
|
+
return this.formMeta.formatOnSubmit(this.nativeFormModel?.values, this.nodeContext);
|
|
352
|
+
}
|
|
353
|
+
return this.nativeFormModel?.values;
|
|
354
|
+
}
|
|
355
|
+
clearValid() {
|
|
356
|
+
if (this.valid !== null) {
|
|
357
|
+
this.valid = null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async validate() {
|
|
361
|
+
this.formFeedbacks = await this.nativeFormModel?.validate();
|
|
362
|
+
this.valid = (0, import_lodash2.isEmpty)(this.formFeedbacks?.filter((f) => f.level === "error"));
|
|
363
|
+
this.onValidateEmitter.fire(this);
|
|
364
|
+
return this.valid;
|
|
365
|
+
}
|
|
366
|
+
getValues() {
|
|
367
|
+
return this._formControl?._formModel.values;
|
|
368
|
+
}
|
|
369
|
+
getField(name) {
|
|
370
|
+
let finalName = name.includes("/") ? convertGlobPath(name) : name;
|
|
371
|
+
return this.formControl?.getField(finalName);
|
|
372
|
+
}
|
|
373
|
+
getValueIn(name) {
|
|
374
|
+
let finalName = name.includes("/") ? convertGlobPath(name) : name;
|
|
375
|
+
return this.nativeFormModel?.getValueIn(finalName);
|
|
376
|
+
}
|
|
377
|
+
setValueIn(name, value) {
|
|
378
|
+
let finalName = name.includes("/") ? convertGlobPath(name) : name;
|
|
379
|
+
this.nativeFormModel?.setValueIn(finalName, value);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* 监听表单某个路径下的值变化
|
|
383
|
+
* @param name 路径
|
|
384
|
+
* @param callback 回调函数
|
|
385
|
+
*/
|
|
386
|
+
onFormValueChangeIn(name, callback) {
|
|
387
|
+
if (!this._initialized) {
|
|
388
|
+
throw new Error(
|
|
389
|
+
`[NodeEngine] FormModel Error: onFormValueChangeIn can not be called before initialized`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
this.formControl._formModel.onFormValuesChange(({ name: changedName, values, prevValues }) => {
|
|
393
|
+
if (changedName === name) {
|
|
394
|
+
callback({
|
|
395
|
+
value: (0, import_lodash2.get)(values, name),
|
|
396
|
+
prevValue: (0, import_lodash2.get)(prevValues, name),
|
|
397
|
+
formValues: values,
|
|
398
|
+
prevFormValues: prevValues
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* @deprecated 该方法用于兼容 V1 版本 FormModel接口,如果确定是FormModelV2 请使用 FormModel.getValueIn
|
|
405
|
+
* @param path glob path
|
|
406
|
+
*/
|
|
407
|
+
getFormItemValueByPath(globPath) {
|
|
408
|
+
if (!globPath) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (globPath === "/") {
|
|
412
|
+
return this._formControl?._formModel.values;
|
|
413
|
+
}
|
|
414
|
+
const name = convertGlobPath(globPath);
|
|
415
|
+
return this.getValueIn(name);
|
|
416
|
+
}
|
|
417
|
+
async validateWithFeedbacks() {
|
|
418
|
+
await this.validate();
|
|
419
|
+
return formFeedbacksToNodeCoreFormFeedbacks(this.formFeedbacks);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* @deprecated 该方法用于兼容 V1 版本 FormModel接口,如果确定是FormModelV2, 请使用FormModel.getValueIn 和 FormModel.setValueIn
|
|
423
|
+
* @param path glob path
|
|
424
|
+
*/
|
|
425
|
+
getFormItemByPath(path) {
|
|
426
|
+
const name = convertGlobPath(path);
|
|
427
|
+
const that = this;
|
|
428
|
+
const formItemValue = that.getValueIn(name);
|
|
429
|
+
return {
|
|
430
|
+
get value() {
|
|
431
|
+
return formItemValue;
|
|
432
|
+
},
|
|
433
|
+
set value(v) {
|
|
434
|
+
that.setValueIn(name, v);
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
dispose() {
|
|
439
|
+
this.onDisposeEmitter.fire();
|
|
440
|
+
this.effectReturnMap.forEach((eventMap) => {
|
|
441
|
+
Object.values(eventMap).forEach((effectReturn) => {
|
|
442
|
+
effectReturn();
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
this.effectMap = DEFAULT.EFFECT_MAP;
|
|
446
|
+
this.effectReturnMap = DEFAULT.EFFECT_RETURN_MAP;
|
|
447
|
+
this.plugins.forEach((p) => {
|
|
448
|
+
p.dispose();
|
|
449
|
+
});
|
|
450
|
+
this.plugins = [];
|
|
451
|
+
this.formFeedbacks = DEFAULT.FORM_FEEDBACKS;
|
|
452
|
+
this._valid = DEFAULT.VALID;
|
|
453
|
+
this._formControl = void 0;
|
|
454
|
+
this._initialized = false;
|
|
455
|
+
this.toDispose.dispose();
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// src/helpers.ts
|
|
460
|
+
var import_form_core3 = require("@flowgram.ai/form-core");
|
|
461
|
+
function isFormV2(node) {
|
|
462
|
+
return node.getNodeRegistry().formMeta?.render;
|
|
463
|
+
}
|
|
464
|
+
function createEffectOptions(event, effect) {
|
|
465
|
+
return {
|
|
466
|
+
event,
|
|
467
|
+
effect
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// src/hooks.ts
|
|
472
|
+
var import_react2 = require("react");
|
|
473
|
+
var import_utils3 = require("@flowgram.ai/utils");
|
|
474
|
+
var import_form_core4 = require("@flowgram.ai/form-core");
|
|
475
|
+
var import_form3 = require("@flowgram.ai/form");
|
|
476
|
+
function useWatchFormValues(node) {
|
|
477
|
+
const formModel = node.getData(import_form_core4.FlowNodeFormData).getFormModel();
|
|
478
|
+
const refresh = (0, import_utils3.useRefresh)();
|
|
479
|
+
(0, import_react2.useEffect)(() => {
|
|
480
|
+
const disposable = formModel.nativeFormModel?.onFormValuesChange(() => {
|
|
481
|
+
refresh();
|
|
482
|
+
});
|
|
483
|
+
return () => disposable?.dispose();
|
|
484
|
+
}, [formModel.nativeFormModel]);
|
|
485
|
+
return formModel.getValues();
|
|
486
|
+
}
|
|
487
|
+
function useWatchFormValueIn(node, name) {
|
|
488
|
+
const formModel = node.getData(import_form_core4.FlowNodeFormData).getFormModel();
|
|
489
|
+
const refresh = (0, import_utils3.useRefresh)();
|
|
490
|
+
(0, import_react2.useEffect)(() => {
|
|
491
|
+
const disposable = formModel.nativeFormModel?.onFormValuesChange(({ name: changedName }) => {
|
|
492
|
+
if (name === changedName) {
|
|
493
|
+
refresh();
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
return () => disposable?.dispose();
|
|
497
|
+
}, []);
|
|
498
|
+
return formModel.getValueIn(name);
|
|
499
|
+
}
|
|
500
|
+
function useInitializedFormModel(node) {
|
|
501
|
+
const formModel = node.getData(import_form_core4.FlowNodeFormData).getFormModel();
|
|
502
|
+
const refresh = (0, import_utils3.useRefresh)();
|
|
503
|
+
(0, import_react2.useEffect)(() => {
|
|
504
|
+
const disposable = formModel.onInitialized(() => {
|
|
505
|
+
refresh();
|
|
506
|
+
});
|
|
507
|
+
return () => disposable.dispose();
|
|
508
|
+
}, [formModel]);
|
|
509
|
+
return formModel;
|
|
510
|
+
}
|
|
511
|
+
function useWatchFormState(node) {
|
|
512
|
+
const formModel = useInitializedFormModel(node);
|
|
513
|
+
return (0, import_form3.useFormState)(formModel.formControl);
|
|
514
|
+
}
|
|
515
|
+
function useWatchFormErrors(node) {
|
|
516
|
+
const formModel = useInitializedFormModel(node);
|
|
517
|
+
return (0, import_form3.useFormErrors)(formModel.formControl);
|
|
518
|
+
}
|
|
519
|
+
function useWatchFormWarnings(node) {
|
|
520
|
+
const formModel = useInitializedFormModel(node);
|
|
521
|
+
return (0, import_form3.useFormWarnings)(formModel.formControl);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// src/form-plugin.ts
|
|
525
|
+
var import_nanoid = require("nanoid");
|
|
526
|
+
var FormPlugin = class {
|
|
527
|
+
constructor(name, config, opts) {
|
|
528
|
+
this.name = name;
|
|
529
|
+
this.pluginId = `${name}__${(0, import_nanoid.nanoid)()}`;
|
|
530
|
+
this.config = config;
|
|
531
|
+
this.opts = opts;
|
|
532
|
+
}
|
|
533
|
+
get formModel() {
|
|
534
|
+
return this._formModel;
|
|
535
|
+
}
|
|
536
|
+
get ctx() {
|
|
537
|
+
return {
|
|
538
|
+
formModel: this.formModel,
|
|
539
|
+
node: this.formModel.nodeContext.node,
|
|
540
|
+
playgroundContext: this.formModel.nodeContext.playgroundContext
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
init(formModel) {
|
|
544
|
+
this._formModel = formModel;
|
|
545
|
+
this.config?.onInit?.(this.ctx, this.opts);
|
|
546
|
+
}
|
|
547
|
+
dispose() {
|
|
548
|
+
if (this.config?.onDispose) {
|
|
549
|
+
this.config?.onDispose(this.ctx, this.opts);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
function defineFormPluginCreator(name, config) {
|
|
554
|
+
return function(opts) {
|
|
555
|
+
return new FormPlugin(name, config, opts);
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/get-node-form.tsx
|
|
560
|
+
var import_react3 = __toESM(require("react"));
|
|
561
|
+
var import_form_core5 = require("@flowgram.ai/form-core");
|
|
562
|
+
function getNodeForm(node) {
|
|
563
|
+
const formModel = node.getData(import_form_core5.FlowNodeFormData)?.getFormModel();
|
|
564
|
+
const nativeFormModel = formModel?.nativeFormModel;
|
|
565
|
+
if (!formModel || !nativeFormModel) return void 0;
|
|
566
|
+
const result = {
|
|
567
|
+
initialValues: nativeFormModel.initialValues,
|
|
568
|
+
get values() {
|
|
569
|
+
return nativeFormModel.values;
|
|
570
|
+
},
|
|
571
|
+
state: nativeFormModel.state,
|
|
572
|
+
getValueIn: (name) => nativeFormModel.getValueIn(name),
|
|
573
|
+
setValueIn: (name, value) => nativeFormModel.setValueIn(name, value),
|
|
574
|
+
render: () => /* @__PURE__ */ import_react3.default.createElement(import_form_core5.NodeRender, { node }),
|
|
575
|
+
onFormValuesChange: formModel.onFormValuesChange.bind(formModel),
|
|
576
|
+
onFormValueChangeIn: formModel.onFormValueChangeIn.bind(formModel),
|
|
577
|
+
onValidate: formModel.nativeFormModel.onValidate,
|
|
578
|
+
validate: formModel.validate.bind(formModel)
|
|
579
|
+
};
|
|
580
|
+
Object.defineProperty(result, "_formModel", {
|
|
581
|
+
enumerable: false,
|
|
582
|
+
get() {
|
|
583
|
+
return formModel;
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
return result;
|
|
587
|
+
}
|
|
588
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
589
|
+
0 && (module.exports = {
|
|
590
|
+
DataEvent,
|
|
591
|
+
FormModelV2,
|
|
592
|
+
FormPlugin,
|
|
593
|
+
createEffectOptions,
|
|
594
|
+
defineFormPluginCreator,
|
|
595
|
+
getNodeForm,
|
|
596
|
+
isFormMetaV2,
|
|
597
|
+
isFormModelV2,
|
|
598
|
+
isFormV2,
|
|
599
|
+
useInitializedFormModel,
|
|
600
|
+
useWatchFormErrors,
|
|
601
|
+
useWatchFormState,
|
|
602
|
+
useWatchFormValueIn,
|
|
603
|
+
useWatchFormValues,
|
|
604
|
+
useWatchFormWarnings
|
|
605
|
+
});
|
|
606
|
+
//# sourceMappingURL=index.js.map
|