sequential-workflow-designer-react 0.5.2

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 ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 N4NO.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ ![Sequential Workflow Designer for React](../.github/cover.png)
2
+
3
+ # Sequential Workflow Designer for React
4
+
5
+ [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fb4rtaz%2Fsequential-workflow-designer%2Fbadge%3Fref%3Dmain&style=flat-square)](https://actions-badge.atrox.dev/b4rtaz/sequential-workflow-designer/goto?ref=main) [![License: MIT](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](/LICENSE) [![View this project on NPM](https://img.shields.io/npm/v/sequential-workflow-designer-react.svg?style=flat-square)](https://npmjs.org/package/sequential-workflow-designer-react)
6
+
7
+ React wrapper for the [Sequential Workflow Designer](https://github.com/nocode-js/sequential-workflow-designer) component.
8
+
9
+ ## 🚀 Installation
10
+
11
+ Install the following packages by [NPM](https://www.npmjs.com/) command:
12
+
13
+ `npm i sequential-workflow-designer sequential-workflow-designer-react`
14
+
15
+ Import CSS files:
16
+
17
+ ```tsx
18
+ import 'sequential-workflow-designer/css/designer.css';
19
+ import 'sequential-workflow-designer/css/designer-light.css';
20
+ import 'sequential-workflow-designer/css/designer-dark.css';
21
+ ```
22
+
23
+ Import types:
24
+
25
+ ```tsx
26
+ import {
27
+ Definition,
28
+ ToolboxConfiguration,
29
+ StepsConfiguration
30
+ } from 'sequential-workflow-designer';
31
+ import {
32
+ SequentialWorkflowDesigner,
33
+ wrapDefinition,
34
+ useGlobalEditor,
35
+ useStepEditor
36
+ } from 'sequential-workflow-designer-react';
37
+ ```
38
+
39
+ Create or load your definition of a workflow.
40
+
41
+ ```ts
42
+ const startDefinition: Definition = { /* ... */ };
43
+ ```
44
+
45
+ Wrap the start definition and memorize it.
46
+
47
+ ```tsx
48
+ const [definition, setDefinition] = useState(() => wrapDefinition(startDefinition));
49
+ ```
50
+
51
+ Configure the designer.
52
+
53
+ ```ts
54
+ const toolboxConfiguration: ToolboxConfiguration = { /* ... */ };
55
+ const stepsConfiguration: StepsConfiguration = { /* ... */ };
56
+ ```
57
+
58
+ Create the global editor component:
59
+
60
+ ```tsx
61
+ function GlobalEditor() {
62
+ const { properties, setProperty } = useGlobalEditor();
63
+
64
+ function onSpeedChanged(e) {
65
+ setProperty('speed', e.target.value);
66
+ }
67
+
68
+ return (
69
+ <>
70
+ <h3>Speed</h3>
71
+ <input value={properties['speed'] || ''} onChange={onSpeedChanged} />
72
+ </>
73
+ );
74
+ }
75
+ ```
76
+
77
+ Create the step editor component:
78
+
79
+ ```tsx
80
+ function StepEditor() {
81
+ const { type, componentType, name, setName, properties, setProperty } = useStepEditor();
82
+
83
+ function onNameChanged(e) {
84
+ setName(e.target.value);
85
+ }
86
+
87
+ return (
88
+ <>
89
+ <h3>Name</h3>
90
+ <input value={name} onChange={onNameChanged} />
91
+ </>
92
+ );
93
+ }
94
+ ```
95
+
96
+ At the end attach the designer.
97
+
98
+ ```tsx
99
+ <SequentialWorkflowDesigner
100
+ definition={definition}
101
+ onDefinitionChange={setDefinition}
102
+ toolboxConfiguration={toolboxConfiguration}
103
+ stepsConfiguration={stepsConfiguration}
104
+ globalEditor={<GlobalEditor />}
105
+ stepEditor={<StepEditor />}>
106
+ />
107
+ ```
108
+
109
+ Check the [demo project](../demos/react-app/).
110
+
111
+ ## 💡 License
112
+
113
+ This project is released under the MIT license.
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { Definition, GlobalEditorContext, Properties, PropertyValue } from 'sequential-workflow-designer';
3
+ export interface GlobalEditorWrapper {
4
+ readonly properties: Properties;
5
+ setProperty(name: string, value: PropertyValue): void;
6
+ }
7
+ export declare function useGlobalEditor(): GlobalEditorWrapper;
8
+ export declare function GlobalEditorWrapperContext(props: {
9
+ children: JSX.Element;
10
+ definition: Definition;
11
+ context: GlobalEditorContext;
12
+ }): JSX.Element;
@@ -0,0 +1,39 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { createContext, useContext, useState } from 'react';
14
+ var globalEditorWrapperContext = createContext(null);
15
+ export function useGlobalEditor() {
16
+ var wrapper = useContext(globalEditorWrapperContext);
17
+ if (!wrapper) {
18
+ throw new Error('Cannot find global editor context');
19
+ }
20
+ return wrapper;
21
+ }
22
+ export function GlobalEditorWrapperContext(props) {
23
+ var _a = useState(function () { return createWrapper(); }), wrapper = _a[0], setWrapper = _a[1];
24
+ function createWrapper() {
25
+ return {
26
+ properties: props.definition.properties,
27
+ setProperty: setProperty
28
+ };
29
+ }
30
+ function forward() {
31
+ setWrapper(createWrapper());
32
+ }
33
+ function setProperty(name, value) {
34
+ props.definition.properties[name] = value;
35
+ props.context.notifyPropertiesChanged();
36
+ forward();
37
+ }
38
+ return _jsx(globalEditorWrapperContext.Provider, __assign({ value: wrapper }, { children: props.children }));
39
+ }
@@ -0,0 +1,18 @@
1
+ /// <reference types="react" />
2
+ import { ToolboxConfiguration, StepsConfiguration, DesignerExtension } from 'sequential-workflow-designer';
3
+ import { WrappedDefinition } from './WrappedDefinition';
4
+ export interface SequentialWorkflowDesignerProps {
5
+ definition: WrappedDefinition;
6
+ onDefinitionChange: (state: WrappedDefinition) => void;
7
+ selectedStepId?: string | null;
8
+ onSelectedStepIdChanged?: (stepId: string | null) => void;
9
+ isReadonly?: boolean;
10
+ globalEditor: JSX.Element;
11
+ stepEditor: JSX.Element;
12
+ theme?: string;
13
+ undoStackSize?: number;
14
+ stepsConfiguration: StepsConfiguration;
15
+ toolboxConfiguration: ToolboxConfiguration;
16
+ extensions?: DesignerExtension[];
17
+ }
18
+ export declare function SequentialWorkflowDesigner(props: SequentialWorkflowDesignerProps): JSX.Element;
@@ -0,0 +1,139 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import ReactDOM from 'react-dom/client';
14
+ import { useEffect, useRef, useState } from 'react';
15
+ import { Designer } from 'sequential-workflow-designer';
16
+ import { GlobalEditorWrapperContext } from './GlobalEditorWrapper';
17
+ import { StepEditorWrapperContext } from './StepEditorWrapper';
18
+ import { wrapDefinition } from './WrappedDefinition';
19
+ export function SequentialWorkflowDesigner(props) {
20
+ var _a = useState(null), placeholder = _a[0], setPlaceholder = _a[1];
21
+ var onDefinitionChangeRef = useRef(props.onDefinitionChange);
22
+ var onSelectedStepIdChangedRef = useRef(props.onSelectedStepIdChanged);
23
+ var globalEditorRef = useRef(props.globalEditor);
24
+ var stepEditorRef = useRef(props.stepEditor);
25
+ var designerRef = useRef(null);
26
+ var editorRootRef = useRef(null);
27
+ var definition = props.definition;
28
+ var selectedStepId = props.selectedStepId;
29
+ var isReadonly = props.isReadonly;
30
+ var theme = props.theme;
31
+ var undoStackSize = props.undoStackSize;
32
+ var stepsConfiguration = props.stepsConfiguration;
33
+ var toolboxConfiguration = props.toolboxConfiguration;
34
+ var extensions = props.extensions;
35
+ function forwardDefinition() {
36
+ if (designerRef.current) {
37
+ var def = wrapDefinition(designerRef.current.getDefinition(), designerRef.current.isValid());
38
+ onDefinitionChangeRef.current(def);
39
+ }
40
+ }
41
+ function globalEditorProvider(def, context) {
42
+ return editorProvider(editorRootRef, _jsx(GlobalEditorWrapperContext, __assign({ definition: def, context: context }, { children: globalEditorRef.current })));
43
+ }
44
+ function stepEditorProvider(step, context) {
45
+ return editorProvider(editorRootRef, _jsx(StepEditorWrapperContext, __assign({ step: step, context: context }, { children: stepEditorRef.current })));
46
+ }
47
+ useEffect(function () {
48
+ onDefinitionChangeRef.current = props.onDefinitionChange;
49
+ }, [props.onDefinitionChange]);
50
+ useEffect(function () {
51
+ onSelectedStepIdChangedRef.current = props.onSelectedStepIdChanged;
52
+ }, [props.onSelectedStepIdChanged]);
53
+ useEffect(function () {
54
+ globalEditorRef.current = props.globalEditor;
55
+ }, [props.globalEditor]);
56
+ useEffect(function () {
57
+ stepEditorRef.current = props.stepEditor;
58
+ }, [props.stepEditor]);
59
+ useEffect(function () {
60
+ if (!placeholder) {
61
+ return;
62
+ }
63
+ if (designerRef.current) {
64
+ var isNotChanged = definition.value === designerRef.current.getDefinition();
65
+ if (isNotChanged) {
66
+ if (selectedStepId !== undefined && selectedStepId !== designerRef.current.getSelectedStepId()) {
67
+ if (selectedStepId) {
68
+ designerRef.current.selectStepById(selectedStepId);
69
+ }
70
+ else {
71
+ designerRef.current.clearSelectedStep();
72
+ }
73
+ // console.log('sqd: selected step updated');
74
+ }
75
+ if (isReadonly !== undefined && isReadonly !== designerRef.current.isReadonly()) {
76
+ designerRef.current.setIsReadonly(isReadonly);
77
+ // console.log('sqd: readonly updated');
78
+ }
79
+ return;
80
+ }
81
+ designerRef.current.destroy();
82
+ designerRef.current = null;
83
+ }
84
+ var designer = Designer.create(placeholder, definition.value, {
85
+ theme: theme,
86
+ undoStackSize: undoStackSize,
87
+ toolbox: toolboxConfiguration,
88
+ steps: stepsConfiguration,
89
+ editors: {
90
+ isHidden: false,
91
+ globalEditorProvider: globalEditorProvider,
92
+ stepEditorProvider: stepEditorProvider
93
+ },
94
+ extensions: extensions
95
+ });
96
+ if (selectedStepId) {
97
+ designer.selectStepById(selectedStepId);
98
+ }
99
+ if (isReadonly) {
100
+ designer.setIsReadonly(isReadonly);
101
+ }
102
+ // console.log('sqd: designer rendered');
103
+ designer.onReady.subscribe(forwardDefinition);
104
+ designer.onDefinitionChanged.subscribe(forwardDefinition);
105
+ designer.onSelectedStepIdChanged.subscribe(function (stepId) {
106
+ if (onSelectedStepIdChangedRef.current) {
107
+ onSelectedStepIdChangedRef.current(stepId);
108
+ }
109
+ });
110
+ designerRef.current = designer;
111
+ }, [placeholder, definition, selectedStepId, isReadonly, theme, undoStackSize, toolboxConfiguration, stepsConfiguration, extensions]);
112
+ useEffect(function () {
113
+ return function () {
114
+ if (editorRootRef.current) {
115
+ var oldRoot_1 = editorRootRef.current;
116
+ editorRootRef.current = null;
117
+ setTimeout(function () { return oldRoot_1.unmount(); });
118
+ }
119
+ if (designerRef.current) {
120
+ designerRef.current.destroy();
121
+ designerRef.current = null;
122
+ // console.log('sqd: designer destroyed');
123
+ }
124
+ };
125
+ }, []);
126
+ return _jsx("div", { ref: setPlaceholder, className: "sqd-designer-react" });
127
+ }
128
+ function editorProvider(rootRef, element) {
129
+ if (rootRef.current) {
130
+ var oldRoot_2 = rootRef.current;
131
+ rootRef.current = null;
132
+ setTimeout(function () { return oldRoot_2.unmount(); });
133
+ }
134
+ var container = document.createElement('div');
135
+ container.className = 'sqd-editor-react';
136
+ rootRef.current = ReactDOM.createRoot(container);
137
+ rootRef.current.render(element);
138
+ return container;
139
+ }
@@ -0,0 +1,20 @@
1
+ /// <reference types="react" />
2
+ import { ComponentType, Properties, PropertyValue, Step, StepEditorContext } from 'sequential-workflow-designer';
3
+ export interface StepEditorWrapper {
4
+ readonly id: string;
5
+ readonly type: string;
6
+ readonly componentType: ComponentType;
7
+ readonly name: string;
8
+ readonly properties: Properties;
9
+ readonly step: Step;
10
+ setName(name: string): void;
11
+ setProperty(name: string, value: PropertyValue): void;
12
+ notifyPropertiesChanged(): void;
13
+ notifyChildrenChanged(): void;
14
+ }
15
+ export declare function useStepEditor(): StepEditorWrapper;
16
+ export declare function StepEditorWrapperContext(props: {
17
+ children: JSX.Element;
18
+ step: Step;
19
+ context: StepEditorContext;
20
+ }): JSX.Element;
@@ -0,0 +1,62 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { createContext, useContext, useState } from 'react';
14
+ var globalEditorWrapperContext = createContext(null);
15
+ export function useStepEditor() {
16
+ var wrapper = useContext(globalEditorWrapperContext);
17
+ if (!wrapper) {
18
+ throw new Error('Cannot find step editor context');
19
+ }
20
+ return wrapper;
21
+ }
22
+ export function StepEditorWrapperContext(props) {
23
+ var _a = useState(function () { return createWrapper(); }), wrapper = _a[0], setWrapper = _a[1];
24
+ function createWrapper() {
25
+ return {
26
+ id: props.step.id,
27
+ type: props.step.type,
28
+ componentType: props.step.componentType,
29
+ name: props.step.name,
30
+ properties: props.step.properties,
31
+ step: props.step,
32
+ setName: setName,
33
+ setProperty: setProperty,
34
+ notifyPropertiesChanged: notifyPropertiesChanged,
35
+ notifyChildrenChanged: notifyChildrenChanged
36
+ };
37
+ }
38
+ function forward() {
39
+ setWrapper(createWrapper());
40
+ }
41
+ function setName(name) {
42
+ props.step.name = name;
43
+ notifyNameChanged();
44
+ }
45
+ function setProperty(name, value) {
46
+ props.step.properties[name] = value;
47
+ notifyPropertiesChanged();
48
+ }
49
+ function notifyNameChanged() {
50
+ props.context.notifyNameChanged();
51
+ forward();
52
+ }
53
+ function notifyPropertiesChanged() {
54
+ props.context.notifyPropertiesChanged();
55
+ forward();
56
+ }
57
+ function notifyChildrenChanged() {
58
+ props.context.notifyChildrenChanged();
59
+ forward();
60
+ }
61
+ return _jsx(globalEditorWrapperContext.Provider, __assign({ value: wrapper }, { children: props.children }));
62
+ }
@@ -0,0 +1,6 @@
1
+ import { Definition } from 'sequential-workflow-designer';
2
+ export interface WrappedDefinition {
3
+ readonly value: Definition;
4
+ readonly isValid: boolean | undefined;
5
+ }
6
+ export declare function wrapDefinition(value: Definition, isValid?: boolean): WrappedDefinition;
@@ -0,0 +1,6 @@
1
+ export function wrapDefinition(value, isValid) {
2
+ return {
3
+ value: value,
4
+ isValid: isValid
5
+ };
6
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './GlobalEditorWrapper';
2
+ export * from './SequentialWorkflowDesigner';
3
+ export * from './StepEditorWrapper';
4
+ export * from './WrappedDefinition';
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './GlobalEditorWrapper';
2
+ export * from './SequentialWorkflowDesigner';
3
+ export * from './StepEditorWrapper';
4
+ export * from './WrappedDefinition';
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "sequential-workflow-designer-react",
3
+ "description": "React wrapper for Sequential Workflow Designer component.",
4
+ "version": "0.5.2",
5
+ "main": "./lib/index.js",
6
+ "types": "./lib/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/nocode-js/sequential-workflow-designer.git"
10
+ },
11
+ "files": [
12
+ "lib/"
13
+ ],
14
+ "publishConfig": {
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "author": "N4NO.com",
18
+ "license": "MIT",
19
+ "scripts": {
20
+ "prepare": "cp ../LICENSE LICENSE",
21
+ "clean": "rm -rf lib",
22
+ "start": "tsc --watch",
23
+ "build": "yarn clean && tsc",
24
+ "prettier": "prettier --check ./src",
25
+ "prettier:fix": "prettier --write ./src",
26
+ "eslint": "eslint ./src --ext .ts",
27
+ "test:single": "jest",
28
+ "test": "jest --watchAll"
29
+ },
30
+ "dependencies": {
31
+ "react": "^18.2.0",
32
+ "react-dom": "^18.2.0",
33
+ "sequential-workflow-designer": "^0.5.2"
34
+ },
35
+ "devDependencies": {
36
+ "@typescript-eslint/eslint-plugin": "^5.47.0",
37
+ "@typescript-eslint/parser": "^5.47.0",
38
+ "eslint": "^8.30.0",
39
+ "@types/react": "^18.0.26",
40
+ "@testing-library/react": "^13.4.0",
41
+ "@testing-library/jest-dom": "^5.16.5",
42
+ "jest": "^29.3.1",
43
+ "jest-environment-jsdom": "^29.3.1",
44
+ "@types/jest": "^29.2.5",
45
+ "ts-jest": "^29.0.3",
46
+ "prettier": "^2.8.2",
47
+ "typescript": "^4.9.4"
48
+ },
49
+ "keywords": [
50
+ "workflow",
51
+ "designer",
52
+ "builder",
53
+ "nocode",
54
+ "lowcode",
55
+ "flow",
56
+ "react",
57
+ "reactjs"
58
+ ]
59
+ }