Skip to content

Commit 9af0b13

Browse files
committed
backup
1 parent 6ff990b commit 9af0b13

File tree

6 files changed

+88
-15
lines changed

6 files changed

+88
-15
lines changed

src/example/materialUis.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const materialUis: IMaterialUIs = {
2525
settingsPanel: AuditPanel,
2626
validate: (node: IWorkFlowNode<IApproverSettings>, { t }) => {
2727
if (!node.config) {
28-
return (t("noSelectedDealer"))
28+
return t("noSelectedDealer")
2929
}
3030
return true
3131
}

src/workflow-editor/actions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IConditionNode, IWorkFlowNode } from "./interfaces";
2-
import { ISnapshot } from "./interfaces/state";
2+
import { IErrors, ISnapshot } from "./interfaces/state";
33

44
export enum ActionType {
55
SET_CHANGE_FLAG = "workflow/SET_CHANGE_FLAG",
@@ -13,6 +13,7 @@ export enum ActionType {
1313
CHANGE_NODE = 'workflow/CHANGE_NODE',
1414
SELECT_NODE = 'workflow/SELECTED_NODE',
1515
SET_VALIDATED = 'workflow/SET_VALIDATED',
16+
SET_ERRORS = 'workflow/SET_ERRORS',
1617
}
1718

1819
export interface Action {
@@ -96,4 +97,12 @@ export interface SetValidatedPayload {
9697

9798
export interface SetValidatedAction extends Action {
9899
payload: SetValidatedPayload
100+
}
101+
102+
export interface SetErrorsPayload {
103+
errors: IErrors,
104+
}
105+
106+
export interface SetErrorsAction extends Action {
107+
payload: SetErrorsPayload
99108
}

src/workflow-editor/classes/EditorStore.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,49 @@ import { configureStore } from "@reduxjs/toolkit"
44
import { mainReducer } from "../reducers"
55
import { RedoListChangeListener, SelectedListener, StartNodeListener, UndoListChangeListener } from "../interfaces/listeners"
66
import { IConditionNode, IRouteNode, IWorkFlowNode, NodeType } from "../interfaces"
7-
import { Action, ActionType, AddNodeAction, ChangeNodeAction, DeleteNodeAction, SelectNodeAction, SetStartNodeAction, UnRedoListAction } from "../actions"
8-
import { IMaterialUIs, INodeMaterial } from "../interfaces/material"
7+
import { Action, ActionType, AddNodeAction, ChangeNodeAction, DeleteNodeAction, SelectNodeAction, SetErrorsAction, SetStartNodeAction, SetValidatedAction, UnRedoListAction } from "../actions"
8+
import { IMaterialUIs, INodeMaterial, Translate } from "../interfaces/material"
99
import { createUuid } from "../utils/create-uuid"
1010

11-
export type Translate = (msg: string) => string | undefined
12-
1311
export class EditorStore {
1412
store: Store<IState>
15-
t?: Translate
13+
t: Translate = (msg: string) => msg
1614
materials: INodeMaterial[] = []
1715
materialUis: IMaterialUIs = {}
1816
constructor(debugMode?: boolean,) {
1917
this.store = makeStoreInstance(debugMode || false)
2018
}
2119

2220
validate = (): IErrors | true => {
21+
const setValidatedAction: SetValidatedAction = {
22+
type: ActionType.SET_VALIDATED,
23+
payload: {
24+
validated: true
25+
}
26+
}
27+
this.dispatch(setValidatedAction)
28+
29+
const errors: IErrors = {}
30+
this.setErrors({})
31+
this.doValidateNode(this.store.getState().startNode, errors)
32+
if (Object.keys(errors).length > 0) {
33+
this.setErrors(errors)
34+
return errors
35+
}
2336
return true;
2437
}
2538

39+
40+
setErrors(errors: IErrors) {
41+
const setErrorsAction: SetErrorsAction = {
42+
type: ActionType.SET_ERRORS,
43+
payload: {
44+
errors
45+
}
46+
}
47+
this.store.dispatch(setErrorsAction)
48+
}
49+
2650
dispatch = (action: Action) => {
2751
this.store.dispatch(action)
2852
}
@@ -124,6 +148,7 @@ export class EditorStore {
124148
}
125149

126150
this.dispatch(setStartNodeAction)
151+
this.revalidate()
127152
}
128153

129154
changeNode(node: IWorkFlowNode) {
@@ -136,6 +161,7 @@ export class EditorStore {
136161
}
137162

138163
this.dispatch(changeNodeAction)
164+
this.revalidate()
139165
}
140166

141167
addCondition(node: IRouteNode, condition: IConditionNode) {
@@ -198,6 +224,7 @@ export class EditorStore {
198224
this.backup()
199225
const addAction: AddNodeAction = { type: ActionType.ADD_NODE, payload: { parentId, node } }
200226
this.store.dispatch(addAction)
227+
this.revalidate()
201228
}
202229

203230
selectNode(id: string | undefined) {
@@ -210,6 +237,7 @@ export class EditorStore {
210237
this.backup()
211238
const removeAction: DeleteNodeAction = { type: ActionType.DELETE_NODE, payload: { id } }
212239
this.store.dispatch(removeAction)
240+
this.revalidate()
213241
}
214242
}
215243

@@ -272,6 +300,32 @@ export class EditorStore {
272300

273301
return this.store.subscribe(handleChange)
274302
}
303+
304+
305+
//审批流节点不多,节点变化全部重新校验一遍,无需担心性能问题,以后有需求再优化
306+
private revalidate = () => {
307+
if (this.store.getState().validated) {
308+
this.validate()
309+
}
310+
}
311+
312+
private doValidateNode = (node: IWorkFlowNode, errors: IErrors) => {
313+
const materialUi = this.materialUis[node.nodeType]
314+
if (materialUi?.validate) {
315+
const result = materialUi.validate(node, { t: this.t })
316+
if (result !== true && result !== undefined) {
317+
errors[node.id] = result
318+
}
319+
}
320+
if (node.childNode) {
321+
this.doValidateNode(node.childNode, errors)
322+
}
323+
if (node.nodeType === NodeType.route) {
324+
for (const condition of (node as IRouteNode).conditionNodeList) {
325+
this.doValidateNode(condition, errors)
326+
}
327+
}
328+
}
275329
}
276330

277331
function resetId(node: IWorkFlowNode) {

src/workflow-editor/interfaces/material.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import { IWorkFlowNode } from "./workflow"
1+
import { IWorkFlowNode, IWorkflowNodeContent } from "./workflow"
2+
3+
export type Translate = (msg: string) => string | undefined
24

35
export interface IContext {
46
//翻译
5-
t: (msg: string) => string | undefined
7+
t: Translate
68
}
79

810
export interface INodeMaterial<Context extends IContext = IContext> {
911
color: string
1012
label: string
1113
icon?: React.ReactElement
1214
//默认配置
13-
defaultConfig?: IWorkFlowNode
15+
defaultConfig?: IWorkflowNodeContent
1416
//创建一个默认节点,跟defaultCofig只选一个
1517
createDefault?: (context: Context) => IWorkFlowNode
1618
//从物料面板隐藏,比如发起人节点
1719
hidden?: boolean
1820
}
1921

2022

21-
export interface IMaterialUI<FlowNode extends IWorkFlowNode, Settings = any, Context = any> {
23+
export interface IMaterialUI<FlowNode extends IWorkFlowNode, Settings = any, Context extends IContext = IContext> {
2224
viewContent?: (node: FlowNode, context: Context) => React.ReactNode
2325
//属性面板设置组件
2426
settingsPanel?: React.FC<{ value: Settings, onChange: (value: Settings) => void }>
2527
//校验失败返回错误消息,成功返回ture
26-
validate?: (node: FlowNode, context: Context) => string | true
28+
validate?: (node: FlowNode, context: Context) => string | true | undefined
2729
}
2830

2931
export interface IMaterialUIs {

src/workflow-editor/interfaces/workflow.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ export enum NodeType {
1313
condition = "condition",
1414
}
1515

16-
export interface IWorkFlowNode<Config = unknown> {
17-
id?: string
16+
export interface IWorkflowNodeContent{
1817
nodeType: NodeType | string //string可以用于自定义节点,暂时用不上
18+
}
19+
20+
export interface IWorkFlowNode<Config = unknown> extends IWorkflowNodeContent{
21+
id: string
1922
name?: string
2023
desc?: string
2124
childNode?: IWorkFlowNode
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { Action } from "../actions";
1+
import { Action, ActionType, SetErrorsAction } from "../actions";
22
import { IErrors } from "../interfaces/state";
33

44
export function errorsReducer(state: IErrors, action: Action): IErrors {
5+
switch (action.type) {
6+
case ActionType.SET_ERRORS: {
7+
return (action as SetErrorsAction).payload?.errors
8+
}
9+
}
510
return state
611
}

0 commit comments

Comments
 (0)