Skip to content

Commit

Permalink
feat: 解决数据双向同步的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fengxiaotx committed Dec 21, 2023
1 parent e6bac93 commit 27e743c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
13 changes: 7 additions & 6 deletions examples/approval-process-designer-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import './App.css';
import {
ApprovalProcessDesigner, GlobalStore,
Expand All @@ -10,10 +10,7 @@ import {Watermark} from "antd";
import * as Icons from "./activities/Icons"

function App() {
const handleOnChange = (value: any) => {
console.log("[processNode]", value)
}
const processNode: IProcessNode = {
const [data,setData] = useState<IProcessNode>({
type: 'START',
componentName: 'StartActivity',
title: '发起人',
Expand Down Expand Up @@ -51,13 +48,17 @@ function App() {
]
}
}
})
const handleOnChange = (value: any) => {
console.log("[processNode]", value)
setData(value)
}

GlobalStore.registerIcons(Icons);
return (
<div>
<Watermark style={{height: '100%'}} content={['书阙', '北斗开源']}>
<ApprovalProcessDesigner value={processNode} onChange={handleOnChange}>
<ApprovalProcessDesigner value={data} onChange={handleOnChange}>
<StudioPanel>
<ProcessWidget activities={{
StartActivity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {FC, useEffect} from "react"
import React, {FC, useEffect, useMemo, useState} from "react"
import {ApprovalProcessContext} from "../context";
import {ApprovalProcessEngine} from "../model/ApprovalProcessEngine";
import {IProcessNode, ProcessNode} from "../model";
import {IProcessNode} from "../model";
import _ from "lodash";

type ApprovalProcessDesignerProps = {
children?: React.ReactNode;
Expand All @@ -16,20 +17,30 @@ export const ApprovalProcessDesigner: FC<ApprovalProcessDesignerProps> = ({
value,
onChange
}) => {
let scopeEngine = engine;
if (!scopeEngine) {
scopeEngine = new ApprovalProcessEngine();
}
const [scopeValue, setScopeValue] = useState(value)


let designerEngine = useMemo(() => {
let scopeEngine = engine;
if (!engine) {
scopeEngine = new ApprovalProcessEngine({value: value});
}
return scopeEngine
}, [engine])

designerEngine?.setOnchange((value: any) => {
setScopeValue(value)
onChange?.(value)
})

scopeEngine?.setOnchange(onChange)

useEffect(() => {
if (value) {
scopeEngine.process.from(value)
if (value && !_.isEqual(value, scopeValue)) {
designerEngine.process.from(value)
}
}, [value])

return <ApprovalProcessContext.Provider value={scopeEngine}>
return <ApprovalProcessContext.Provider value={designerEngine}>
{children}
</ApprovalProcessContext.Provider>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {ProcessNode} from "./ProcessNode";
import {IProcessNode, ProcessNode} from "./ProcessNode";
import {define, observable} from "@formily/reactive";
import {GlobalStore} from "../store";
import {DesignerCore} from "../util";
import _ from "lodash";

interface IApprovalProcessEngine {

value?: IProcessNode
}

export class ApprovalProcessEngine {
Expand All @@ -18,7 +18,10 @@ export class ApprovalProcessEngine {
type: 'START',
componentName: 'StartActivity',
title: '开始'
});
})
if (engine?.value) {
this.process.from(engine.value)
}
this.makeObservable()
}

Expand All @@ -29,9 +32,9 @@ export class ApprovalProcessEngine {
})
}

handleChange = _.debounce((msg: any)=>{
handleChange = _.debounce((msg: any) => {
this.onChange?.(DesignerCore.transformToSchema(this.process))
},100)
}, 100)

setOnchange(fn: (value: any) => void) {
this.onChange = fn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const ActivityWidget: FC<ActivityWidgetProps> = observer(({
processNode,
...props
}) => {
console.log("ss", processNode.id)
console.log("ss", processNode.id,processNode)
const activities = useActivities()
const handleRender = () => {
const Activity: ActivityFC<any> = _.get(activities, [processNode.componentName]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const AddActivityItemWidget: FC<ActivityCardWidgetProps> = ({
const handleClick = () => {
onClick?.(processNode)
const activity = GlobalStore.getActivityResource(resource?.componentName)
debugger
processNode.setNextNode(activity?.node.clone(processNode))
}
return <ActivityCardWidgetStyled onClick={handleClick}>
Expand Down

0 comments on commit 27e743c

Please sign in to comment.