Skip to content

Commit 4011a43

Browse files
committed
feat: step changes detection example.
1 parent 389f5c8 commit 4011a43

3 files changed

Lines changed: 234 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Features:
3535
- [📜 Scrollable Page](https://nocode-js.github.io/sequential-workflow-designer/examples/scrollable-page.html)
3636
- [🌵 Multi-Conditional Switch](https://nocode-js.github.io/sequential-workflow-designer/examples/multi-conditional-switch.html)
3737
- [🌀 Auto-Select](https://nocode-js.github.io/sequential-workflow-designer/examples/auto-select.html)
38+
- [🔄 Step Changes Detection](https://nocode-js.github.io/sequential-workflow-designer/examples/step-changes-detection.html)
3839
- [Angular Demo](https://nocode-js.github.io/sequential-workflow-designer/angular-app/)
3940
- [React Demo](https://nocode-js.github.io/sequential-workflow-designer/react-app/)
4041
- [Svelte Demo](https://nocode-js.github.io/sequential-workflow-designer/svelte-app/)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* global document, sequentialWorkflowDesigner */
2+
3+
const uid = sequentialWorkflowDesigner.Uid.next;
4+
5+
function createTaskStep(name) {
6+
return {
7+
id: uid(),
8+
name,
9+
componentType: 'task',
10+
type: 'task',
11+
properties: {}
12+
};
13+
}
14+
15+
function createIfStep(name, t, f) {
16+
return {
17+
id: uid(),
18+
name,
19+
componentType: 'switch',
20+
type: 'switch',
21+
properties: {},
22+
branches: {
23+
True: t,
24+
False: f
25+
}
26+
};
27+
}
28+
29+
const configuration = {
30+
theme: 'soft',
31+
undoStackSize: 10,
32+
steps: {
33+
iconUrlProvider: () => './assets/icon-trigger.svg'
34+
},
35+
toolbox: {
36+
groups: [
37+
{
38+
name: 'Steps',
39+
steps: [createIfStep('If', [], []), createTaskStep('Pear'), createTaskStep('Cucumber'), createTaskStep('Tomato')]
40+
}
41+
]
42+
},
43+
editors: false,
44+
controlBar: true
45+
};
46+
47+
class ChangeDetector {
48+
constructor(definition, onChange) {
49+
this.walker = new sequentialWorkflowDesigner.DefinitionWalker();
50+
this.lastMap = this.extractMap(definition);
51+
this.onChange = onChange;
52+
}
53+
54+
extractMap(definition) {
55+
const map = new Map();
56+
this.walker.forEach(definition, step => map.set(step.id, step.name));
57+
return map;
58+
}
59+
60+
detect(newDefinition) {
61+
const newMap = this.extractMap(newDefinition);
62+
const added = [];
63+
const removed = [];
64+
for (const [id, name] of newMap) {
65+
if (!this.lastMap.has(id)) {
66+
added.push([id, name]);
67+
}
68+
}
69+
for (const [id, name] of this.lastMap) {
70+
if (!newMap.has(id)) {
71+
removed.push([id, name]);
72+
}
73+
}
74+
this.lastMap = newMap;
75+
this.onChange({ added, removed });
76+
}
77+
}
78+
79+
const startDefinition = {
80+
properties: {},
81+
sequence: [createTaskStep('Apple'), createIfStep('If', [createTaskStep('Banana')], [createTaskStep('Lemon')])]
82+
};
83+
84+
const placeholder = document.getElementById('designer');
85+
const recentChanges = document.getElementById('recentChanges');
86+
const designer = sequentialWorkflowDesigner.Designer.create(placeholder, startDefinition, configuration);
87+
88+
const detector = new ChangeDetector(startDefinition, changes => {
89+
let diff = '';
90+
if (changes.added.length > 0) {
91+
diff += `📗 Added: ${changes.added.map(r => r[1]).join(', ')}`;
92+
}
93+
if (changes.removed.length > 0) {
94+
diff += `❌ Removed: ${changes.removed.map(r => r[1]).join(', ')}`;
95+
}
96+
if (!diff) {
97+
diff = 'No changes';
98+
}
99+
recentChanges.textContent = diff;
100+
});
101+
designer.onDefinitionChanged.subscribe(event => detector.detect(event.definition));
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
6+
7+
<title>🔄 Step Changes Detection - Sequential Workflow Designer</title>
8+
<meta property="og:title" content="🔄 Step Changes Detection - Sequential Workflow Designer" />
9+
<meta
10+
name="description"
11+
content="Example showing how to detect what changed in a workflow definition, including which steps were added and which were removed."
12+
/>
13+
<meta
14+
property="og:description"
15+
content="Example showing how to detect what changed in a workflow definition, including which steps were added and which were removed."
16+
/>
17+
<meta property="og:image" content="https://nocode-js.com/img/social-card.png" />
18+
<meta name="twitter:card" content="summary_large_image" />
19+
<meta name="twitter:title" content="🔄 Step Changes Detection - Sequential Workflow Designer" />
20+
<meta
21+
name="twitter:description"
22+
content="Example showing how to detect what changed in a workflow definition, including which steps were added and which were removed."
23+
/>
24+
<meta name="twitter:image" content="https://nocode-js.com/img/social-card.png" />
25+
26+
<link rel="icon" href="./assets/favicon.ico" />
27+
<link rel="stylesheet" href="./assets/common.css" />
28+
<style>
29+
html,
30+
body {
31+
width: 100%;
32+
height: 100%;
33+
}
34+
body {
35+
display: flex;
36+
flex-direction: column;
37+
overflow: hidden;
38+
}
39+
.content {
40+
flex: 1;
41+
display: flex;
42+
min-height: 0;
43+
}
44+
#designer {
45+
flex: 1;
46+
min-width: 0;
47+
}
48+
.info-panel {
49+
width: 20%;
50+
min-width: 260px;
51+
padding: 24px;
52+
box-sizing: border-box;
53+
background: #f9fafb;
54+
border-left: 1px solid #e5e7eb;
55+
overflow: auto;
56+
}
57+
.info-panel h1 {
58+
margin: 0 0 12px;
59+
font-size: 1.4rem;
60+
line-height: 1.2;
61+
}
62+
.info-panel p {
63+
margin: 0;
64+
line-height: 1.5;
65+
color: #334155;
66+
}
67+
.info-panel .section-title {
68+
margin: 24px 0 10px;
69+
font-size: 1rem;
70+
line-height: 1.2;
71+
}
72+
.changes-output {
73+
margin: 0;
74+
padding: 12px;
75+
background: #ffffff;
76+
border: 1px solid #dbe3ea;
77+
border-radius: 8px;
78+
color: #111827;
79+
line-height: 1.5;
80+
white-space: pre-wrap;
81+
word-break: break-word;
82+
}
83+
84+
@media (max-width: 900px) {
85+
.content {
86+
flex-direction: column;
87+
height: 100%;
88+
}
89+
#designer {
90+
flex: 0 0 70%;
91+
height: 70%;
92+
}
93+
.info-panel {
94+
width: 100%;
95+
height: 30%;
96+
flex: 0 0 30%;
97+
min-width: 0;
98+
border-left: 0;
99+
border-top: 1px solid #e5e7eb;
100+
overflow: auto;
101+
}
102+
}
103+
</style>
104+
</head>
105+
<body>
106+
<header class="title-bar">
107+
<div class="column flex-1">
108+
<h1>🔄 Step Changes Detection</h1>
109+
</div>
110+
<div class="column text-end flex-1 text-gray">
111+
<a href="https://github.com/nocode-js/sequential-workflow-designer" target="_blank">GitHub</a> |
112+
<a href="https://nocode-js.com" target="_blank">NoCode JS</a>
113+
</div>
114+
</header>
115+
116+
<div class="content">
117+
<div id="designer"></div>
118+
<aside class="info-panel">
119+
<h1>Step Changes Detection</h1>
120+
<p>
121+
This example demonstrates how to detect what changed in the workflow definition. It focuses on identifying which steps
122+
were added and which steps were removed.
123+
</p>
124+
<h2 class="section-title">Recent Changes</h2>
125+
<pre id="recentChanges" class="changes-output">No changes</pre>
126+
</aside>
127+
</div>
128+
129+
<script src="./assets/lib.js"></script>
130+
<script src="./assets/step-changes-detection.js"></script>
131+
</body>
132+
</html>

0 commit comments

Comments
 (0)