/
TestEditorEvents.js
146 lines (107 loc) · 3.02 KB
/
TestEditorEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership.
*
* Camunda licenses this file to you under the MIT; you may not use this file
* except in compliance with the MIT License.
*/
import { PureComponent } from 'camunda-modeler-plugin-helpers/react';
import BpmnModelerExtension from './bpmn-modeler-extension';
import DmnModelerExtension from './dmn-modeler-extension';
/**
* An extension that shows how to hook into
* editor events to accomplish the following:
*
* - hook into <bpmn.modeler.configure> to provide a bpmn.modeler extension
* - hook into <bpmn.modeler.created> to register for bpmn.modeler events
* - hook into <tab.saved> to perform a post-safe action
*
*/
export default class TestEditorEvents extends PureComponent {
constructor(props) {
super(props);
const {
subscribe
} = props;
subscribe('bpmn.modeler.configure', (event) => {
const {
tab,
middlewares
} = event;
log('Creating editor for tab', tab);
middlewares.push(addModule(BpmnModelerExtension));
});
subscribe('dmn.modeler.configure', (event) => {
const {
tab,
middlewares
} = event;
log('Creating editor for tab', tab);
middlewares.push(addDmnModule(DmnModelerExtension));
});
const onModelerCreated = event => {
const {
tab,
modeler,
} = event;
log('Modeler created for tab', tab);
modeler.on('saveXML.start', (event) => {
const {
definitions
} = event;
log('Saving XML with definitions', definitions, tab);
});
};
subscribe('bpmn.modeler.created', onModelerCreated);
subscribe('dmn.modeler.created', onModelerCreated);
subscribe('tab.saved', (event) => {
const {
tab
} = event;
log('Tab saved', tab);
});
}
render() {
return null;
}
}
// helpers //////////////
function log(...args) {
console.log('[TestEditorEvents]', ...args);
}
/**
* Returns a bpmn.modeler.configure middleware
* that adds the specific module.
*
* @param {didi.Module} extensionModule
*
* @return {Function}
*/
function addModule(extensionModule) {
return (config) => {
const additionalModules = config.additionalModules || [];
return {
...config,
additionalModules: [
...additionalModules,
extensionModule
]
};
};
}
function addDmnModule(extensionModule) {
return (config) => {
const newConfig = { ...config };
for (const viewer of [ 'drd', 'decisionTable', 'literalExpression' ]) {
newConfig[viewer] = newConfig[viewer] || {};
const additionalModules = (newConfig[viewer] && newConfig[viewer].additionalModules) || [];
newConfig[viewer].additionalModules = [
...additionalModules,
extensionModule
];
}
return newConfig;
};
}