@flowgram-vue/node-variable-plugin 0.2.0
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/LICENSE +22 -0
- package/dist/index.cjs +139 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
- package/src/components/private-scope-provider.ts +28 -0
- package/src/components/public-scope-provider.ts +22 -0
- package/src/create-node-variable-plugin.ts +19 -0
- package/src/env.d.ts +10 -0
- package/src/form-v2/create-provider-effect.ts +82 -0
- package/src/form-v2/create-variable-provider-plugin.ts +31 -0
- package/src/index.ts +15 -0
- package/src/types.ts +54 -0
- package/src/with-node-variables.ts +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
4
|
+
Copyright (c) 2026 Crayon
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var formCore = require('@flowgram-vue/form-core');
|
|
4
|
+
var core = require('@flowgram-vue/core');
|
|
5
|
+
var vue = require('vue');
|
|
6
|
+
var variablePlugin = require('@flowgram-vue/variable-plugin');
|
|
7
|
+
var node = require('@flowgram-vue/node');
|
|
8
|
+
|
|
9
|
+
// src/create-node-variable-plugin.ts
|
|
10
|
+
var PublicScopeProvider = vue.defineComponent({
|
|
11
|
+
name: "PublicScopeProvider",
|
|
12
|
+
setup(_props, { slots }) {
|
|
13
|
+
const node = core.useEntityFromContext();
|
|
14
|
+
const publicScope = vue.computed(() => node.getData(variablePlugin.FlowNodeVariableData).public);
|
|
15
|
+
return () => vue.h(variablePlugin.ScopeProvider, { scope: publicScope.value }, () => slots.default?.());
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// src/with-node-variables.ts
|
|
20
|
+
var withNodeVariables = (Component) => vue.defineComponent({
|
|
21
|
+
name: "WithNodeVariables",
|
|
22
|
+
setup(_props, { attrs }) {
|
|
23
|
+
return () => vue.h(PublicScopeProvider, null, {
|
|
24
|
+
default: () => {
|
|
25
|
+
if (typeof Component === "function") {
|
|
26
|
+
const result = Component(attrs);
|
|
27
|
+
if (vue.isVNode(result) || result == null) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
return vue.h(result, attrs);
|
|
31
|
+
}
|
|
32
|
+
return vue.h(Component, attrs);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// src/create-node-variable-plugin.ts
|
|
39
|
+
var createNodeVariablePlugin = core.definePluginCreator({
|
|
40
|
+
onInit(ctx) {
|
|
41
|
+
const nodeManager = ctx.get(formCore.NodeManager);
|
|
42
|
+
nodeManager.registerNodeRenderHoc(withNodeVariables);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
var PrivateScopeProvider = vue.defineComponent({
|
|
46
|
+
name: "PrivateScopeProvider",
|
|
47
|
+
setup(_props, { slots }) {
|
|
48
|
+
const node = core.useEntityFromContext();
|
|
49
|
+
const privateScope = vue.computed(() => {
|
|
50
|
+
const variableData = node.getData(variablePlugin.FlowNodeVariableData);
|
|
51
|
+
if (!variableData.private) {
|
|
52
|
+
variableData.initPrivate();
|
|
53
|
+
}
|
|
54
|
+
return variableData.private;
|
|
55
|
+
});
|
|
56
|
+
return () => vue.h(variablePlugin.ScopeProvider, { scope: privateScope.value }, () => slots.default?.());
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
function createEffectFromVariableProvider(options) {
|
|
60
|
+
const getScope = (node) => {
|
|
61
|
+
const variableData = node.getData(variablePlugin.FlowNodeVariableData);
|
|
62
|
+
if (options.private || options.scope === "private") {
|
|
63
|
+
return variableData.initPrivate();
|
|
64
|
+
}
|
|
65
|
+
return variableData.public;
|
|
66
|
+
};
|
|
67
|
+
const transformValueToAST = ({ value, name, context, formValues, form }) => {
|
|
68
|
+
if (!context) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const { node } = context;
|
|
72
|
+
const scope = getScope(node);
|
|
73
|
+
const parsedValue = options.parse(value, {
|
|
74
|
+
node,
|
|
75
|
+
scope,
|
|
76
|
+
options,
|
|
77
|
+
name,
|
|
78
|
+
formValues,
|
|
79
|
+
form
|
|
80
|
+
});
|
|
81
|
+
scope.ast.set(options.namespace || name || "", {
|
|
82
|
+
kind: variablePlugin.ASTKind.VariableDeclarationList,
|
|
83
|
+
declarations: Array.isArray(parsedValue) ? parsedValue : [parsedValue]
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
return [
|
|
87
|
+
{
|
|
88
|
+
event: node.DataEvent.onValueInit,
|
|
89
|
+
effect: ((params) => {
|
|
90
|
+
const { context } = params;
|
|
91
|
+
const scope = getScope(context.node);
|
|
92
|
+
const disposable = options.onInit?.({
|
|
93
|
+
node: context.node,
|
|
94
|
+
scope,
|
|
95
|
+
options,
|
|
96
|
+
name: params.name,
|
|
97
|
+
formValues: params.formValues,
|
|
98
|
+
form: params.form
|
|
99
|
+
});
|
|
100
|
+
transformValueToAST(params);
|
|
101
|
+
return () => {
|
|
102
|
+
disposable?.dispose();
|
|
103
|
+
};
|
|
104
|
+
})
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
event: node.DataEvent.onValueChange,
|
|
108
|
+
effect: ((params) => {
|
|
109
|
+
transformValueToAST(params);
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
var createVariableProviderPlugin = node.defineFormPluginCreator({
|
|
115
|
+
name: "VariableProviderPlugin",
|
|
116
|
+
onInit: (ctx, opts) => {
|
|
117
|
+
},
|
|
118
|
+
onSetupFormMeta({ mergeEffect }) {
|
|
119
|
+
mergeEffect({
|
|
120
|
+
arr: [
|
|
121
|
+
{
|
|
122
|
+
event: node.DataEvent.onValueInitOrChange,
|
|
123
|
+
effect: () => {
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
onDispose: (ctx, opts) => {
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
exports.PrivateScopeProvider = PrivateScopeProvider;
|
|
134
|
+
exports.PublicScopeProvider = PublicScopeProvider;
|
|
135
|
+
exports.createEffectFromVariableProvider = createEffectFromVariableProvider;
|
|
136
|
+
exports.createNodeVariablePlugin = createNodeVariablePlugin;
|
|
137
|
+
exports.createVariableProviderPlugin = createVariableProviderPlugin;
|
|
138
|
+
//# sourceMappingURL=index.cjs.map
|
|
139
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/public-scope-provider.ts","../src/with-node-variables.ts","../src/create-node-variable-plugin.ts","../src/components/private-scope-provider.ts","../src/form-v2/create-provider-effect.ts","../src/form-v2/create-variable-provider-plugin.ts"],"names":["defineComponent","useEntityFromContext","computed","FlowNodeVariableData","h","ScopeProvider","isVNode","definePluginCreator","NodeManager","ASTKind","DataEvent","defineFormPluginCreator"],"mappings":";;;;;;;;;AAaO,IAAM,sBAAsBA,mBAAA,CAAgB;AAAA,EACjD,IAAA,EAAM,qBAAA;AAAA,EACN,KAAA,CAAM,MAAA,EAAQ,EAAE,KAAA,EAAM,EAAG;AACvB,IAAA,MAAM,OAAOC,yBAAA,EAAqB;AAClC,IAAA,MAAM,cAAcC,YAAA,CAAS,MAAM,KAAK,OAAA,CAAQC,mCAAoB,EAAE,MAAM,CAAA;AAE5E,IAAA,OAAO,MAAMC,KAAA,CAAEC,4BAAA,EAAe,EAAE,KAAA,EAAO,WAAA,CAAY,KAAA,EAAM,EAAG,MAAM,KAAA,CAAM,OAAA,IAAW,CAAA;AAAA,EACrF;AACF,CAAC;;;ACVM,IAAM,iBAAA,GAAmC,CAAC,SAAA,KAC/CL,mBAAAA,CAAgB;AAAA,EACd,IAAA,EAAM,mBAAA;AAAA,EACN,KAAA,CAAM,MAAA,EAAQ,EAAE,KAAA,EAAM,EAAG;AACvB,IAAA,OAAO,MACLI,KAAAA,CAAE,mBAAA,EAAqB,IAAA,EAAM;AAAA,MAC3B,SAAS,MAAM;AACb,QAAA,IAAI,OAAO,cAAc,UAAA,EAAY;AACnC,UAAA,MAAM,MAAA,GAAU,UAA2C,KAAK,CAAA;AAChE,UAAA,IAAIE,WAAA,CAAQ,MAAM,CAAA,IAAK,MAAA,IAAU,IAAA,EAAM;AACrC,YAAA,OAAO,MAAA;AAAA,UACT;AACA,UAAA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AAM,CAAA;AAE1B,QAAA,OAAO,MAAM;AACX,UAAA,UAAA,EAAY,OAAA,EAAQ;AAAA,QACtB,CAAA;AAAA,MACF,CAAA;AAAA,KACF;AAAA,IACA;AAAA,MACE,OAAOA,cAAA,CAAU,aAAA;AAAA,MACjB,MAAA,GAAS,CAAC,MAAA,KAAW;AACnB,QAAA,mBAAA,CAAoB,MAAM,CAAA;AAAA,MAC5B,CAAA;AAAA;AACF,GACF;AACF;AC1EO,IAAM,+BAA+BC,4BAAA,CAAwB;AAAA,EAClE,IAAA,EAAM,wBAAA;AAAA,EACN,MAAA,EAAQ,CAAC,GAAA,EAAK,IAAA,KAAS;AAAA,EAGvB,CAAA;AAAA,EACA,eAAA,CAAgB,EAAE,WAAA,EAAY,EAAG;AAC/B,IAAA,WAAA,CAAY;AAAA,MACV,GAAA,EAAK;AAAA,QACH;AAAA,UACE,OAAOD,cAAAA,CAAU,mBAAA;AAAA,UACjB,QAAQ,MAAM;AAAA,UAGd;AAAA;AACF;AACF,KACD,CAAA;AAAA,EACH,CAAA;AAAA,EACA,SAAA,EAAW,CAAC,GAAA,EAAK,IAAA,KAAS;AAAA,EAG1B;AACF,CAAC","file":"index.cjs","sourcesContent":["/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { computed, defineComponent, h } from 'vue';\n\nimport { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';\nimport { useEntityFromContext } from '@flowgram-vue/core';\n\n/**\n * PublicScopeProvider provides the public scope to its children via provide/inject.\n */\nexport const PublicScopeProvider = defineComponent({\n name: 'PublicScopeProvider',\n setup(_props, { slots }) {\n const node = useEntityFromContext();\n const publicScope = computed(() => node.getData(FlowNodeVariableData).public);\n\n return () => h(ScopeProvider, { scope: publicScope.value }, () => slots.default?.());\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { defineComponent, h, isVNode, type Component } from 'vue';\n\nimport { NodeRenderHoc } from '@flowgram-vue/form-core';\n\nimport { PublicScopeProvider } from './components/public-scope-provider';\n\nexport const withNodeVariables: NodeRenderHoc = (Component) =>\n defineComponent({\n name: 'WithNodeVariables',\n setup(_props, { attrs }) {\n return () =>\n h(PublicScopeProvider, null, {\n default: () => {\n if (typeof Component === 'function') {\n const result = (Component as (props?: unknown) => unknown)(attrs);\n if (isVNode(result) || result == null) {\n return result;\n }\n return h(result as Component, attrs);\n }\n return h(Component as Component, attrs);\n },\n });\n },\n });\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\n// import { FormManager } from '@flowgram-vue/form-core';\nimport { NodeManager } from '@flowgram-vue/form-core';\nimport { definePluginCreator } from '@flowgram-vue/core';\n\nimport { withNodeVariables } from './with-node-variables';\n\n// import { withNodeVariables } from './with-node-variables';\n\nexport const createNodeVariablePlugin = definePluginCreator({\n onInit(ctx) {\n const nodeManager = ctx.get<NodeManager>(NodeManager);\n nodeManager.registerNodeRenderHoc(withNodeVariables);\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { computed, defineComponent, h } from 'vue';\n\nimport { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';\nimport { useEntityFromContext } from '@flowgram-vue/core';\n\n/**\n * PrivateScopeProvider provides the private scope to its children via provide/inject.\n */\nexport const PrivateScopeProvider = defineComponent({\n name: 'PrivateScopeProvider',\n setup(_props, { slots }) {\n const node = useEntityFromContext();\n const privateScope = computed(() => {\n const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);\n if (!variableData.private) {\n variableData.initPrivate();\n }\n return variableData.private!;\n });\n\n return () => h(ScopeProvider, { scope: privateScope.value }, () => slots.default?.());\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { FlowNodeVariableData, type Scope, ASTKind } from '@flowgram-vue/variable-plugin';\nimport { DataEvent, type Effect, type EffectOptions } from '@flowgram-vue/node';\nimport { FlowNodeEntity } from '@flowgram-vue/document';\n\nimport { type VariableProviderAbilityOptions } from '../types';\n\n/**\n * 根据 VariableProvider 生成 FormV2 的 Effect\n * @param options\n * @returns\n */\nexport function createEffectFromVariableProvider(\n options: VariableProviderAbilityOptions\n): EffectOptions[] {\n const getScope = (node: FlowNodeEntity): Scope => {\n const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);\n\n if (options.private || options.scope === 'private') {\n return variableData.initPrivate();\n }\n return variableData.public;\n };\n\n const transformValueToAST: Effect = ({ value, name, context, formValues, form }) => {\n if (!context) {\n return;\n }\n const { node } = context;\n const scope = getScope(node);\n\n const parsedValue = options.parse(value, {\n node,\n scope,\n options,\n name,\n formValues,\n form,\n });\n\n // Fix: When parsedValue is not an array, transform it to array\n scope.ast.set(options.namespace || name || '', {\n kind: ASTKind.VariableDeclarationList,\n declarations: Array.isArray(parsedValue) ? parsedValue : [parsedValue],\n });\n };\n\n return [\n {\n event: DataEvent.onValueInit,\n effect: ((params) => {\n const { context } = params;\n\n const scope = getScope(context.node);\n const disposable = options.onInit?.({\n node: context.node,\n scope,\n options,\n name: params.name,\n formValues: params.formValues,\n form: params.form,\n });\n\n transformValueToAST(params);\n\n return () => {\n disposable?.dispose();\n };\n }) as Effect,\n },\n {\n event: DataEvent.onValueChange,\n effect: ((params) => {\n transformValueToAST(params);\n }) as Effect,\n },\n ];\n}\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { DataEvent, defineFormPluginCreator } from '@flowgram-vue/node';\n\nexport const createVariableProviderPlugin = defineFormPluginCreator({\n name: 'VariableProviderPlugin',\n onInit: (ctx, opts) => {\n // todo\n // console.log('>>> VariableProviderPlugin init', ctx, opts);\n },\n onSetupFormMeta({ mergeEffect }) {\n mergeEffect({\n arr: [\n {\n event: DataEvent.onValueInitOrChange,\n effect: () => {\n // todo\n // console.log('>>> VariableProviderPlugin effect triggered');\n },\n },\n ],\n });\n },\n onDispose: (ctx, opts) => {\n // todo\n // console.log('>>> VariableProviderPlugin dispose', ctx, opts);\n },\n});\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as _flowgram_vue_core from '@flowgram-vue/core';
|
|
2
|
+
import { Scope, ASTNodeJSON, VariableDeclarationJSON } from '@flowgram-vue/variable-plugin';
|
|
3
|
+
import { Disposable } from '@flowgram-vue/utils';
|
|
4
|
+
import * as _flowgram_vue_node from '@flowgram-vue/node';
|
|
5
|
+
import { EffectFuncProps, EffectOptions } from '@flowgram-vue/node';
|
|
6
|
+
import { FlowNodeEntity } from '@flowgram-vue/document';
|
|
7
|
+
import * as vue from 'vue';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
11
|
+
* SPDX-License-Identifier: MIT
|
|
12
|
+
*/
|
|
13
|
+
declare const createNodeVariablePlugin: _flowgram_vue_core.PluginCreator<unknown>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
17
|
+
* SPDX-License-Identifier: MIT
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface VariableAbilityCommonContext {
|
|
21
|
+
node: FlowNodeEntity;
|
|
22
|
+
scope: Scope;
|
|
23
|
+
options: VariableAbilityOptions;
|
|
24
|
+
name: string;
|
|
25
|
+
formValues: EffectFuncProps['formValues'];
|
|
26
|
+
form: EffectFuncProps['form'];
|
|
27
|
+
}
|
|
28
|
+
interface VariableAbilityInitCtx extends VariableAbilityCommonContext {
|
|
29
|
+
}
|
|
30
|
+
interface VariableAbilityOptions {
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated use scope: 'private'
|
|
33
|
+
*/
|
|
34
|
+
private?: boolean;
|
|
35
|
+
namespace?: string;
|
|
36
|
+
scope?: 'private' | 'public';
|
|
37
|
+
onInit?: (ctx: VariableAbilityInitCtx) => Disposable | undefined;
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}
|
|
40
|
+
interface VariableAbilityParseContext extends VariableAbilityCommonContext {
|
|
41
|
+
}
|
|
42
|
+
interface VariableProviderAbilityOptions<V = any> extends VariableAbilityOptions {
|
|
43
|
+
parse: (v: V, ctx: VariableAbilityParseContext) => VariableDeclarationJSON | VariableDeclarationJSON[];
|
|
44
|
+
}
|
|
45
|
+
interface VariableConsumerAbilityOptions<V = any> extends VariableAbilityOptions {
|
|
46
|
+
parse: (v: V, ctx: VariableAbilityParseContext) => ASTNodeJSON | undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
51
|
+
* SPDX-License-Identifier: MIT
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* PrivateScopeProvider provides the private scope to its children via provide/inject.
|
|
55
|
+
*/
|
|
56
|
+
declare const PrivateScopeProvider: vue.DefineComponent<{}, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
57
|
+
[key: string]: any;
|
|
58
|
+
}>, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
62
|
+
* SPDX-License-Identifier: MIT
|
|
63
|
+
*/
|
|
64
|
+
/**
|
|
65
|
+
* PublicScopeProvider provides the public scope to its children via provide/inject.
|
|
66
|
+
*/
|
|
67
|
+
declare const PublicScopeProvider: vue.DefineComponent<{}, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
}>, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
73
|
+
* SPDX-License-Identifier: MIT
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 根据 VariableProvider 生成 FormV2 的 Effect
|
|
78
|
+
* @param options
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
declare function createEffectFromVariableProvider(options: VariableProviderAbilityOptions): EffectOptions[];
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
85
|
+
* SPDX-License-Identifier: MIT
|
|
86
|
+
*/
|
|
87
|
+
declare const createVariableProviderPlugin: _flowgram_vue_node.FormPluginCreator<unknown>;
|
|
88
|
+
|
|
89
|
+
export { PrivateScopeProvider, PublicScopeProvider, type VariableAbilityParseContext, type VariableConsumerAbilityOptions, type VariableProviderAbilityOptions, createEffectFromVariableProvider, createNodeVariablePlugin, createVariableProviderPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { NodeManager } from '@flowgram-vue/form-core';
|
|
2
|
+
import { useEntityFromContext, definePluginCreator } from '@flowgram-vue/core';
|
|
3
|
+
import { defineComponent, computed, h, isVNode } from 'vue';
|
|
4
|
+
import { FlowNodeVariableData, ScopeProvider, ASTKind } from '@flowgram-vue/variable-plugin';
|
|
5
|
+
import { defineFormPluginCreator, DataEvent } from '@flowgram-vue/node';
|
|
6
|
+
|
|
7
|
+
// src/create-node-variable-plugin.ts
|
|
8
|
+
var PublicScopeProvider = defineComponent({
|
|
9
|
+
name: "PublicScopeProvider",
|
|
10
|
+
setup(_props, { slots }) {
|
|
11
|
+
const node = useEntityFromContext();
|
|
12
|
+
const publicScope = computed(() => node.getData(FlowNodeVariableData).public);
|
|
13
|
+
return () => h(ScopeProvider, { scope: publicScope.value }, () => slots.default?.());
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// src/with-node-variables.ts
|
|
18
|
+
var withNodeVariables = (Component) => defineComponent({
|
|
19
|
+
name: "WithNodeVariables",
|
|
20
|
+
setup(_props, { attrs }) {
|
|
21
|
+
return () => h(PublicScopeProvider, null, {
|
|
22
|
+
default: () => {
|
|
23
|
+
if (typeof Component === "function") {
|
|
24
|
+
const result = Component(attrs);
|
|
25
|
+
if (isVNode(result) || result == null) {
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
return h(result, attrs);
|
|
29
|
+
}
|
|
30
|
+
return h(Component, attrs);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// src/create-node-variable-plugin.ts
|
|
37
|
+
var createNodeVariablePlugin = definePluginCreator({
|
|
38
|
+
onInit(ctx) {
|
|
39
|
+
const nodeManager = ctx.get(NodeManager);
|
|
40
|
+
nodeManager.registerNodeRenderHoc(withNodeVariables);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
var PrivateScopeProvider = defineComponent({
|
|
44
|
+
name: "PrivateScopeProvider",
|
|
45
|
+
setup(_props, { slots }) {
|
|
46
|
+
const node = useEntityFromContext();
|
|
47
|
+
const privateScope = computed(() => {
|
|
48
|
+
const variableData = node.getData(FlowNodeVariableData);
|
|
49
|
+
if (!variableData.private) {
|
|
50
|
+
variableData.initPrivate();
|
|
51
|
+
}
|
|
52
|
+
return variableData.private;
|
|
53
|
+
});
|
|
54
|
+
return () => h(ScopeProvider, { scope: privateScope.value }, () => slots.default?.());
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
function createEffectFromVariableProvider(options) {
|
|
58
|
+
const getScope = (node) => {
|
|
59
|
+
const variableData = node.getData(FlowNodeVariableData);
|
|
60
|
+
if (options.private || options.scope === "private") {
|
|
61
|
+
return variableData.initPrivate();
|
|
62
|
+
}
|
|
63
|
+
return variableData.public;
|
|
64
|
+
};
|
|
65
|
+
const transformValueToAST = ({ value, name, context, formValues, form }) => {
|
|
66
|
+
if (!context) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const { node } = context;
|
|
70
|
+
const scope = getScope(node);
|
|
71
|
+
const parsedValue = options.parse(value, {
|
|
72
|
+
node,
|
|
73
|
+
scope,
|
|
74
|
+
options,
|
|
75
|
+
name,
|
|
76
|
+
formValues,
|
|
77
|
+
form
|
|
78
|
+
});
|
|
79
|
+
scope.ast.set(options.namespace || name || "", {
|
|
80
|
+
kind: ASTKind.VariableDeclarationList,
|
|
81
|
+
declarations: Array.isArray(parsedValue) ? parsedValue : [parsedValue]
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return [
|
|
85
|
+
{
|
|
86
|
+
event: DataEvent.onValueInit,
|
|
87
|
+
effect: ((params) => {
|
|
88
|
+
const { context } = params;
|
|
89
|
+
const scope = getScope(context.node);
|
|
90
|
+
const disposable = options.onInit?.({
|
|
91
|
+
node: context.node,
|
|
92
|
+
scope,
|
|
93
|
+
options,
|
|
94
|
+
name: params.name,
|
|
95
|
+
formValues: params.formValues,
|
|
96
|
+
form: params.form
|
|
97
|
+
});
|
|
98
|
+
transformValueToAST(params);
|
|
99
|
+
return () => {
|
|
100
|
+
disposable?.dispose();
|
|
101
|
+
};
|
|
102
|
+
})
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
event: DataEvent.onValueChange,
|
|
106
|
+
effect: ((params) => {
|
|
107
|
+
transformValueToAST(params);
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
var createVariableProviderPlugin = defineFormPluginCreator({
|
|
113
|
+
name: "VariableProviderPlugin",
|
|
114
|
+
onInit: (ctx, opts) => {
|
|
115
|
+
},
|
|
116
|
+
onSetupFormMeta({ mergeEffect }) {
|
|
117
|
+
mergeEffect({
|
|
118
|
+
arr: [
|
|
119
|
+
{
|
|
120
|
+
event: DataEvent.onValueInitOrChange,
|
|
121
|
+
effect: () => {
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
onDispose: (ctx, opts) => {
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
export { PrivateScopeProvider, PublicScopeProvider, createEffectFromVariableProvider, createNodeVariablePlugin, createVariableProviderPlugin };
|
|
132
|
+
//# sourceMappingURL=index.js.map
|
|
133
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/public-scope-provider.ts","../src/with-node-variables.ts","../src/create-node-variable-plugin.ts","../src/components/private-scope-provider.ts","../src/form-v2/create-provider-effect.ts","../src/form-v2/create-variable-provider-plugin.ts"],"names":["defineComponent","h","useEntityFromContext","computed","FlowNodeVariableData","ScopeProvider","DataEvent"],"mappings":";;;;;;;AAaO,IAAM,sBAAsB,eAAA,CAAgB;AAAA,EACjD,IAAA,EAAM,qBAAA;AAAA,EACN,KAAA,CAAM,MAAA,EAAQ,EAAE,KAAA,EAAM,EAAG;AACvB,IAAA,MAAM,OAAO,oBAAA,EAAqB;AAClC,IAAA,MAAM,cAAc,QAAA,CAAS,MAAM,KAAK,OAAA,CAAQ,oBAAoB,EAAE,MAAM,CAAA;AAE5E,IAAA,OAAO,MAAM,CAAA,CAAE,aAAA,EAAe,EAAE,KAAA,EAAO,WAAA,CAAY,KAAA,EAAM,EAAG,MAAM,KAAA,CAAM,OAAA,IAAW,CAAA;AAAA,EACrF;AACF,CAAC;;;ACVM,IAAM,iBAAA,GAAmC,CAAC,SAAA,KAC/CA,eAAAA,CAAgB;AAAA,EACd,IAAA,EAAM,mBAAA;AAAA,EACN,KAAA,CAA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,QAAA,MAAM,KAAA,GAAQ,QAAA,CAAS,OAAA,CAAQ,IAAI,CAAA;AACnC,QAAA,MAAM,UAAA,GAAa,QAAQ,MAAA,GAAS;AAAA,UAClC,MAAM,OAAA,CAAQ,IAAA;AAAA,UACd,KAAA;AAAA,UACA,OAAA;AAAA,UACA,MAAM,MAAA,CAAO,IAAA;AAAA,UACb,YAAY,MAAA,CAAO,UAAA;AAAA,UACnB,MAAM,MAAA,CAAO;AAAA,SACd,CAAA;AAED,QAAA,mBAAA,CAAoB,MAAM,CAAA;AAE1B,QAAA,OAAO,MAAM;AACX,UAAA,UAAA,EAAY,OAAA,EAAQ;AAAA,QACtB,CAAA;AAAA,MACF,CAAA;AAAA,KACF;AAAA,IACA;AAAA,MACE,OAAO,SAAA,CAAU,aAAA;AAAA,MACjB,MAAA,GAAS,CAAC,MAAA,KAAW;AACnB,QAAA,mBAAA,CAAoB,MAAM,CAAA;AAAA,MAC5B,CAAA;AAAA;AACF,GACF;AACF;AC1EO,IAAM,+BAA+B,uBAAA,CAAwB;AAAA,EAClE,IAAA,EAAM,wBAAA;AAAA,EACN,MAAA,EAAQ,CAAC,GAAA,EAAK,IAAA,KAAS;AAAA,EAGvB,CAAA;AAAA,EACA,eAAA,CAAgB,EAAE,WAAA,EAAY,EAAG;AAC/B,IAAA,WAAA,CAAY;AAAA,MACV,GAAA,EAAK;AAAA,QACH;AAAA,UACE,OAAOE,SAAAA,CAAU,mBAAA;AAAA,UACjB,QAAQ,MAAM;AAAA,UAGd;AAAA;AACF;AACF,KACD,CAAA;AAAA,EACH,CAAA;AAAA,EACA,SAAA,EAAW,CAAC,GAAA,EAAK,IAAA,KAAS;AAAA,EAG1B;AACF,CAAC","file":"index.js","sourcesContent":["/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { computed, defineComponent, h } from 'vue';\n\nimport { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';\nimport { useEntityFromContext } from '@flowgram-vue/core';\n\n/**\n * PublicScopeProvider provides the public scope to its children via provide/inject.\n */\nexport const PublicScopeProvider = defineComponent({\n name: 'PublicScopeProvider',\n setup(_props, { slots }) {\n const node = useEntityFromContext();\n const publicScope = computed(() => node.getData(FlowNodeVariableData).public);\n\n return () => h(ScopeProvider, { scope: publicScope.value }, () => slots.default?.());\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { defineComponent, h, isVNode, type Component } from 'vue';\n\nimport { NodeRenderHoc } from '@flowgram-vue/form-core';\n\nimport { PublicScopeProvider } from './components/public-scope-provider';\n\nexport const withNodeVariables: NodeRenderHoc = (Component) =>\n defineComponent({\n name: 'WithNodeVariables',\n setup(_props, { attrs }) {\n return () =>\n h(PublicScopeProvider, null, {\n default: () => {\n if (typeof Component === 'function') {\n const result = (Component as (props?: unknown) => unknown)(attrs);\n if (isVNode(result) || result == null) {\n return result;\n }\n return h(result as Component, attrs);\n }\n return h(Component as Component, attrs);\n },\n });\n },\n });\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\n// import { FormManager } from '@flowgram-vue/form-core';\nimport { NodeManager } from '@flowgram-vue/form-core';\nimport { definePluginCreator } from '@flowgram-vue/core';\n\nimport { withNodeVariables } from './with-node-variables';\n\n// import { withNodeVariables } from './with-node-variables';\n\nexport const createNodeVariablePlugin = definePluginCreator({\n onInit(ctx) {\n const nodeManager = ctx.get<NodeManager>(NodeManager);\n nodeManager.registerNodeRenderHoc(withNodeVariables);\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { computed, defineComponent, h } from 'vue';\n\nimport { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';\nimport { useEntityFromContext } from '@flowgram-vue/core';\n\n/**\n * PrivateScopeProvider provides the private scope to its children via provide/inject.\n */\nexport const PrivateScopeProvider = defineComponent({\n name: 'PrivateScopeProvider',\n setup(_props, { slots }) {\n const node = useEntityFromContext();\n const privateScope = computed(() => {\n const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);\n if (!variableData.private) {\n variableData.initPrivate();\n }\n return variableData.private!;\n });\n\n return () => h(ScopeProvider, { scope: privateScope.value }, () => slots.default?.());\n },\n});\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { FlowNodeVariableData, type Scope, ASTKind } from '@flowgram-vue/variable-plugin';\nimport { DataEvent, type Effect, type EffectOptions } from '@flowgram-vue/node';\nimport { FlowNodeEntity } from '@flowgram-vue/document';\n\nimport { type VariableProviderAbilityOptions } from '../types';\n\n/**\n * 根据 VariableProvider 生成 FormV2 的 Effect\n * @param options\n * @returns\n */\nexport function createEffectFromVariableProvider(\n options: VariableProviderAbilityOptions\n): EffectOptions[] {\n const getScope = (node: FlowNodeEntity): Scope => {\n const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);\n\n if (options.private || options.scope === 'private') {\n return variableData.initPrivate();\n }\n return variableData.public;\n };\n\n const transformValueToAST: Effect = ({ value, name, context, formValues, form }) => {\n if (!context) {\n return;\n }\n const { node } = context;\n const scope = getScope(node);\n\n const parsedValue = options.parse(value, {\n node,\n scope,\n options,\n name,\n formValues,\n form,\n });\n\n // Fix: When parsedValue is not an array, transform it to array\n scope.ast.set(options.namespace || name || '', {\n kind: ASTKind.VariableDeclarationList,\n declarations: Array.isArray(parsedValue) ? parsedValue : [parsedValue],\n });\n };\n\n return [\n {\n event: DataEvent.onValueInit,\n effect: ((params) => {\n const { context } = params;\n\n const scope = getScope(context.node);\n const disposable = options.onInit?.({\n node: context.node,\n scope,\n options,\n name: params.name,\n formValues: params.formValues,\n form: params.form,\n });\n\n transformValueToAST(params);\n\n return () => {\n disposable?.dispose();\n };\n }) as Effect,\n },\n {\n event: DataEvent.onValueChange,\n effect: ((params) => {\n transformValueToAST(params);\n }) as Effect,\n },\n ];\n}\n","/**\n * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n * SPDX-License-Identifier: MIT\n */\n\nimport { DataEvent, defineFormPluginCreator } from '@flowgram-vue/node';\n\nexport const createVariableProviderPlugin = defineFormPluginCreator({\n name: 'VariableProviderPlugin',\n onInit: (ctx, opts) => {\n // todo\n // console.log('>>> VariableProviderPlugin init', ctx, opts);\n },\n onSetupFormMeta({ mergeEffect }) {\n mergeEffect({\n arr: [\n {\n event: DataEvent.onValueInitOrChange,\n effect: () => {\n // todo\n // console.log('>>> VariableProviderPlugin effect triggered');\n },\n },\n ],\n });\n },\n onDispose: (ctx, opts) => {\n // todo\n // console.log('>>> VariableProviderPlugin dispose', ctx, opts);\n },\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flowgram-vue/node-variable-plugin",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"homepage": "https://flowgram.ai/",
|
|
6
|
+
"repository": "https://github.com/Crayon-hua/flowgram-vue",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"inversify": "^6.0.1",
|
|
24
|
+
"reflect-metadata": "~0.2.2",
|
|
25
|
+
"@flowgram-vue/form-core": "0.2.0",
|
|
26
|
+
"@flowgram-vue/core": "0.2.0",
|
|
27
|
+
"@flowgram-vue/variable-plugin": "0.2.0",
|
|
28
|
+
"@flowgram-vue/utils": "0.2.0",
|
|
29
|
+
"@flowgram-vue/document": "0.2.0",
|
|
30
|
+
"@flowgram-vue/node": "0.2.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"vue": "^3.5.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^20",
|
|
37
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
38
|
+
"eslint": "^9.0.0",
|
|
39
|
+
"jsdom": "^26.1.0",
|
|
40
|
+
"typescript": "^5.8.3",
|
|
41
|
+
"vitest": "^3.2.4",
|
|
42
|
+
"vue": "^3.5.18",
|
|
43
|
+
"@flowgram-vue/ts-config": "0.2.0",
|
|
44
|
+
"@flowgram-vue/build-config": "0.2.0",
|
|
45
|
+
"@flowgram-vue/eslint-config": "0.2.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public",
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "flowgram-build",
|
|
53
|
+
"test": "vitest run --passWithNoTests",
|
|
54
|
+
"lint": "eslint .",
|
|
55
|
+
"ts-check": "tsc --noEmit",
|
|
56
|
+
"type-check": "tsc --noEmit",
|
|
57
|
+
"build:fast": "flowgram-build --fast",
|
|
58
|
+
"build:watch": "flowgram-build --watch"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { computed, defineComponent, h } from 'vue';
|
|
7
|
+
|
|
8
|
+
import { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';
|
|
9
|
+
import { useEntityFromContext } from '@flowgram-vue/core';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* PrivateScopeProvider provides the private scope to its children via provide/inject.
|
|
13
|
+
*/
|
|
14
|
+
export const PrivateScopeProvider = defineComponent({
|
|
15
|
+
name: 'PrivateScopeProvider',
|
|
16
|
+
setup(_props, { slots }) {
|
|
17
|
+
const node = useEntityFromContext();
|
|
18
|
+
const privateScope = computed(() => {
|
|
19
|
+
const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);
|
|
20
|
+
if (!variableData.private) {
|
|
21
|
+
variableData.initPrivate();
|
|
22
|
+
}
|
|
23
|
+
return variableData.private!;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return () => h(ScopeProvider, { scope: privateScope.value }, () => slots.default?.());
|
|
27
|
+
},
|
|
28
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { computed, defineComponent, h } from 'vue';
|
|
7
|
+
|
|
8
|
+
import { FlowNodeVariableData, ScopeProvider } from '@flowgram-vue/variable-plugin';
|
|
9
|
+
import { useEntityFromContext } from '@flowgram-vue/core';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* PublicScopeProvider provides the public scope to its children via provide/inject.
|
|
13
|
+
*/
|
|
14
|
+
export const PublicScopeProvider = defineComponent({
|
|
15
|
+
name: 'PublicScopeProvider',
|
|
16
|
+
setup(_props, { slots }) {
|
|
17
|
+
const node = useEntityFromContext();
|
|
18
|
+
const publicScope = computed(() => node.getData(FlowNodeVariableData).public);
|
|
19
|
+
|
|
20
|
+
return () => h(ScopeProvider, { scope: publicScope.value }, () => slots.default?.());
|
|
21
|
+
},
|
|
22
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// import { FormManager } from '@flowgram-vue/form-core';
|
|
7
|
+
import { NodeManager } from '@flowgram-vue/form-core';
|
|
8
|
+
import { definePluginCreator } from '@flowgram-vue/core';
|
|
9
|
+
|
|
10
|
+
import { withNodeVariables } from './with-node-variables';
|
|
11
|
+
|
|
12
|
+
// import { withNodeVariables } from './with-node-variables';
|
|
13
|
+
|
|
14
|
+
export const createNodeVariablePlugin = definePluginCreator({
|
|
15
|
+
onInit(ctx) {
|
|
16
|
+
const nodeManager = ctx.get<NodeManager>(NodeManager);
|
|
17
|
+
nodeManager.registerNodeRenderHoc(withNodeVariables);
|
|
18
|
+
},
|
|
19
|
+
});
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare module '*.vue' {
|
|
7
|
+
import type { DefineComponent } from 'vue';
|
|
8
|
+
const component: DefineComponent<object, object, unknown>;
|
|
9
|
+
export default component;
|
|
10
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { FlowNodeVariableData, type Scope, ASTKind } from '@flowgram-vue/variable-plugin';
|
|
7
|
+
import { DataEvent, type Effect, type EffectOptions } from '@flowgram-vue/node';
|
|
8
|
+
import { FlowNodeEntity } from '@flowgram-vue/document';
|
|
9
|
+
|
|
10
|
+
import { type VariableProviderAbilityOptions } from '../types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 根据 VariableProvider 生成 FormV2 的 Effect
|
|
14
|
+
* @param options
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export function createEffectFromVariableProvider(
|
|
18
|
+
options: VariableProviderAbilityOptions
|
|
19
|
+
): EffectOptions[] {
|
|
20
|
+
const getScope = (node: FlowNodeEntity): Scope => {
|
|
21
|
+
const variableData: FlowNodeVariableData = node.getData(FlowNodeVariableData);
|
|
22
|
+
|
|
23
|
+
if (options.private || options.scope === 'private') {
|
|
24
|
+
return variableData.initPrivate();
|
|
25
|
+
}
|
|
26
|
+
return variableData.public;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const transformValueToAST: Effect = ({ value, name, context, formValues, form }) => {
|
|
30
|
+
if (!context) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const { node } = context;
|
|
34
|
+
const scope = getScope(node);
|
|
35
|
+
|
|
36
|
+
const parsedValue = options.parse(value, {
|
|
37
|
+
node,
|
|
38
|
+
scope,
|
|
39
|
+
options,
|
|
40
|
+
name,
|
|
41
|
+
formValues,
|
|
42
|
+
form,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Fix: When parsedValue is not an array, transform it to array
|
|
46
|
+
scope.ast.set(options.namespace || name || '', {
|
|
47
|
+
kind: ASTKind.VariableDeclarationList,
|
|
48
|
+
declarations: Array.isArray(parsedValue) ? parsedValue : [parsedValue],
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return [
|
|
53
|
+
{
|
|
54
|
+
event: DataEvent.onValueInit,
|
|
55
|
+
effect: ((params) => {
|
|
56
|
+
const { context } = params;
|
|
57
|
+
|
|
58
|
+
const scope = getScope(context.node);
|
|
59
|
+
const disposable = options.onInit?.({
|
|
60
|
+
node: context.node,
|
|
61
|
+
scope,
|
|
62
|
+
options,
|
|
63
|
+
name: params.name,
|
|
64
|
+
formValues: params.formValues,
|
|
65
|
+
form: params.form,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
transformValueToAST(params);
|
|
69
|
+
|
|
70
|
+
return () => {
|
|
71
|
+
disposable?.dispose();
|
|
72
|
+
};
|
|
73
|
+
}) as Effect,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
event: DataEvent.onValueChange,
|
|
77
|
+
effect: ((params) => {
|
|
78
|
+
transformValueToAST(params);
|
|
79
|
+
}) as Effect,
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { DataEvent, defineFormPluginCreator } from '@flowgram-vue/node';
|
|
7
|
+
|
|
8
|
+
export const createVariableProviderPlugin = defineFormPluginCreator({
|
|
9
|
+
name: 'VariableProviderPlugin',
|
|
10
|
+
onInit: (ctx, opts) => {
|
|
11
|
+
// todo
|
|
12
|
+
// console.log('>>> VariableProviderPlugin init', ctx, opts);
|
|
13
|
+
},
|
|
14
|
+
onSetupFormMeta({ mergeEffect }) {
|
|
15
|
+
mergeEffect({
|
|
16
|
+
arr: [
|
|
17
|
+
{
|
|
18
|
+
event: DataEvent.onValueInitOrChange,
|
|
19
|
+
effect: () => {
|
|
20
|
+
// todo
|
|
21
|
+
// console.log('>>> VariableProviderPlugin effect triggered');
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
onDispose: (ctx, opts) => {
|
|
28
|
+
// todo
|
|
29
|
+
// console.log('>>> VariableProviderPlugin dispose', ctx, opts);
|
|
30
|
+
},
|
|
31
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export * from './create-node-variable-plugin';
|
|
7
|
+
export type {
|
|
8
|
+
VariableProviderAbilityOptions,
|
|
9
|
+
VariableConsumerAbilityOptions,
|
|
10
|
+
VariableAbilityParseContext,
|
|
11
|
+
} from './types';
|
|
12
|
+
export { PrivateScopeProvider } from './components/private-scope-provider';
|
|
13
|
+
export { PublicScopeProvider } from './components/public-scope-provider';
|
|
14
|
+
export { createEffectFromVariableProvider } from './form-v2/create-provider-effect';
|
|
15
|
+
export { createVariableProviderPlugin } from './form-v2/create-variable-provider-plugin';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Scope,
|
|
8
|
+
type ASTNodeJSON,
|
|
9
|
+
type VariableDeclarationJSON,
|
|
10
|
+
} from '@flowgram-vue/variable-plugin';
|
|
11
|
+
import { Disposable } from '@flowgram-vue/utils';
|
|
12
|
+
import { EffectFuncProps } from '@flowgram-vue/node';
|
|
13
|
+
import { FlowNodeEntity } from '@flowgram-vue/document';
|
|
14
|
+
|
|
15
|
+
export interface VariableAbilityCommonContext {
|
|
16
|
+
node: FlowNodeEntity; // 节点
|
|
17
|
+
scope: Scope; // 作用域
|
|
18
|
+
options: VariableAbilityOptions;
|
|
19
|
+
name: string; // 表单字段名
|
|
20
|
+
formValues: EffectFuncProps['formValues']; // 表单值
|
|
21
|
+
form: EffectFuncProps['form'];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface VariableAbilityInitCtx extends VariableAbilityCommonContext {}
|
|
25
|
+
|
|
26
|
+
export interface VariableAbilityOptions {
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated use scope: 'private'
|
|
29
|
+
*/
|
|
30
|
+
private?: boolean;
|
|
31
|
+
// 生成 AST 在抽象语法树中的索引,默认用 formItem 的 Path 作为 namespace
|
|
32
|
+
namespace?: string;
|
|
33
|
+
// 输出变量的作用域类型,默认为 public
|
|
34
|
+
scope?: 'private' | 'public';
|
|
35
|
+
// 初始化,可以进行额外的操作监听
|
|
36
|
+
onInit?: (ctx: VariableAbilityInitCtx) => Disposable | undefined;
|
|
37
|
+
// 扩展字段,可以在 ability onInit 时进行一些额外操作
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface VariableAbilityParseContext extends VariableAbilityCommonContext {}
|
|
42
|
+
|
|
43
|
+
export interface VariableProviderAbilityOptions<V = any> extends VariableAbilityOptions {
|
|
44
|
+
// 解析变量协议
|
|
45
|
+
parse: (
|
|
46
|
+
v: V,
|
|
47
|
+
ctx: VariableAbilityParseContext
|
|
48
|
+
) => VariableDeclarationJSON | VariableDeclarationJSON[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface VariableConsumerAbilityOptions<V = any> extends VariableAbilityOptions {
|
|
52
|
+
// 解析变量协议
|
|
53
|
+
parse: (v: V, ctx: VariableAbilityParseContext) => ASTNodeJSON | undefined;
|
|
54
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { defineComponent, h, isVNode, type Component } from 'vue';
|
|
7
|
+
|
|
8
|
+
import { NodeRenderHoc } from '@flowgram-vue/form-core';
|
|
9
|
+
|
|
10
|
+
import { PublicScopeProvider } from './components/public-scope-provider';
|
|
11
|
+
|
|
12
|
+
export const withNodeVariables: NodeRenderHoc = (Component) =>
|
|
13
|
+
defineComponent({
|
|
14
|
+
name: 'WithNodeVariables',
|
|
15
|
+
setup(_props, { attrs }) {
|
|
16
|
+
return () =>
|
|
17
|
+
h(PublicScopeProvider, null, {
|
|
18
|
+
default: () => {
|
|
19
|
+
if (typeof Component === 'function') {
|
|
20
|
+
const result = (Component as (props?: unknown) => unknown)(attrs);
|
|
21
|
+
if (isVNode(result) || result == null) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return h(result as Component, attrs);
|
|
25
|
+
}
|
|
26
|
+
return h(Component as Component, attrs);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
});
|