Skip to content

Commit 38990db

Browse files
committed
refactor: move to undo plugin
1 parent 916a232 commit 38990db

File tree

10 files changed

+172
-104
lines changed

10 files changed

+172
-104
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist": "yarn eslint && yarn webpack:p",
99
"dev": "webpack-dev-server --open",
1010
"eslint": "eslint src/**/*.js",
11-
"graph": "madge --circular src/js/app.js",
11+
"graph": "madge --circular --extensions js src/js/layout/components",
1212
"prettier:check": "prettier --check \"src/**/*.js\" \"src/**/*.scss\" \"test/*.js\"",
1313
"prettier:write": "prettier --write \"src/**/*.js\" \"src/**/*.scss\" \"test/*.js\"",
1414
"pre-commit": "yarn prettier:check && yarn eslint && yarn test",

src/js/app.js

+6-87
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,15 @@ import { DivaServices } from "divaservices-utils";
1111
import { initWebservices } from "./constants/globals";
1212
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "./constants/constants";
1313
import { addElementFromName, addLinkFromLink } from "./elements/addElement";
14-
import { deleteElementByBoxId, deleteLink } from "./elements/deleteElement";
15-
import { resizeElements } from "./elements/resizeElement";
16-
import {
17-
equalObjects,
18-
findDifferenceBy,
19-
getDeletedElements
20-
} from "./utils/utils";
14+
import { deleteElementByBoxId } from "./elements/deleteElement";
15+
import { findDifferenceBy } from "./utils/utils";
2116
import {
2217
selectedElements,
2318
copiedElements,
2419
deletedElements,
2520
currentElements,
2621
currentDataElements
2722
} from "./store/modules/utils";
28-
import { moveElements } from "./elements/moveElement";
2923
import { addDataBox } from "./elements/addDataElement";
3024
import { initSplit } from "./layout/split";
3125
import { initKeyboardEvents } from "./events/keyboardEvents";
@@ -39,7 +33,6 @@ import {
3933
MESSAGE_COPY_ERROR
4034
} from "./constants/messages";
4135
import { saveWorkflow } from "./workflows/saveWorkflow";
42-
import {setInputValueInElement, setSelectValueInElement} from "./layout/inputs"
4336

4437
export let app;
4538

@@ -88,16 +81,6 @@ export let app;
8881
return { boxId, position };
8982
});
9083
},
91-
resizedElements() {
92-
return this.elements.map(({ boxId, size }) => {
93-
return { boxId, size };
94-
});
95-
},
96-
defaultParamsElements() {
97-
return this.elements.map(({ boxId, defaultParams }) => {
98-
return { boxId, defaultParams };
99-
});
100-
},
10184
dataElements() {
10285
return this.dataTest;
10386
},
@@ -146,14 +129,10 @@ export let app;
146129
undo() {
147130
UndoRedoHistory.undo();
148131
},
149-
150-
addElementToSelection(cellView) {
151-
this.$addElementToSelection(cellView);
152-
},
153-
addLinkFromApp(payload) {
132+
addLink(payload) {
154133
this.$addLink({ ...payload });
155134
},
156-
deleteLinkFromApp(payload) {
135+
deleteLink(payload) {
157136
this.$deleteLink({ ...payload });
158137
},
159138
deleteElementByCellView(cellView) {
@@ -162,7 +141,6 @@ export let app;
162141
elements: [this.elements.find(el => el.boxId === boxId)]
163142
});
164143
},
165-
166144
resizeElementByBoxId(boxId, size) {
167145
const element = this.elements.find(el => el.boxId === boxId);
168146
this.$resizeElement({ element, size });
@@ -194,7 +172,7 @@ export let app;
194172
* add new elements, remove deleted elements
195173
*/
196174
currentElements: {
197-
handler(newValue, oldValue) {
175+
handler(newValue) {
198176
// if boxId element does not exist in the graph, we add it
199177
for (const el of Graph.getNewElements(newValue)) {
200178
const { type, category } = el;
@@ -209,12 +187,6 @@ export let app;
209187
console.log("ERROR ADDING EL");
210188
}
211189
}
212-
213-
// remove element removed from arr
214-
// cannot use arr.includes because states are deep cloned
215-
for (const el of getDeletedElements(oldValue, newValue)) {
216-
deleteElementByBoxId(el.boxId);
217-
}
218190
}
219191
},
220192

@@ -235,70 +207,17 @@ export let app;
235207
}
236208
}
237209
},
238-
239-
/**
240-
* watch parameters of elements
241-
* on direct changes, nothing changes (mechanic operation)
242-
* only apply changes on undo-redo
243-
*/
244-
defaultParamsElements: {
245-
deep: true,
246-
handler(newValue, oldValue) {
247-
const difference = findDifferenceBy(
248-
newValue,
249-
oldValue,
250-
"defaultParams"
251-
);
252-
for (const box of difference) {
253-
setSelectValueInElement(box);
254-
setInputValueInElement(box);
255-
}
256-
}
257-
},
258210

259-
/**
260-
* watch moved elements
261-
* on direct move operation, nothing changes (mechanic operation)
262-
* only apply changes on undo-redo
263-
*/
264-
movedElements: {
265-
deep: true,
266-
handler(newValue, oldValue) {
267-
const difference = findDifferenceBy(newValue, oldValue, "position");
268-
moveElements(difference);
269-
}
270-
},
271-
/**
272-
* watch moved elements
273-
* on direct resize operation, nothing changes (mechanic operation)
274-
* only apply changes on undo-redo
275-
*/
276-
resizedElements: {
277-
deep: true,
278-
handler(newValue, oldValue) {
279-
const difference = findDifferenceBy(newValue, oldValue, "size");
280-
resizeElements(difference);
281-
}
282-
},
283211
/**
284212
* watch links
285213
* on direct operation, nothing changes (mechanic operation)
286214
* on start, add parsed links
287215
* apply changes on undo-redo
288216
*/
289-
links(newValue, oldValue) {
217+
links(newValue) {
290218
for (const l of Graph.getNewLinks(newValue)) {
291219
addLinkFromLink(l);
292220
}
293-
294-
// remove links removed from arr
295-
// cannot use arr.includes because states are deep cloned
296-
for (const el of oldValue.filter(
297-
el => newValue.filter(v => equalObjects(v, el)).length === 0
298-
)) {
299-
const link = Graph.getLinkBySourceTarget(el.source, el.target);
300-
deleteLink(link);
301-
}
302221
},
303222
/**
304223
* watches deleted elements

src/js/classes/Graph.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Graph {
3131
return arr.filter(({ boxId }) => !this.getElementByBoxId(boxId));
3232
}
3333

34+
getLinkById(id) {
35+
return this._graph.getCell(id);
36+
}
37+
3438
/**
3539
* return links which exist in links, but not in the graph
3640
*

src/js/classes/Paper.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Paper {
174174
distance: -30,
175175
action: function() {
176176
this.model.remove({ ui: true, tool: this.cid });
177-
app.deleteLinkFromApp({ link: this.model });
177+
app.deleteLink({ link: this.model });
178178
}
179179
})
180180
]
@@ -187,7 +187,7 @@ class Paper {
187187
});
188188

189189
this._paper.on("link:connect", linkView => {
190-
app.addLinkFromApp({ link: linkView.model.attributes });
190+
app.addLink({ link: linkView.model.attributes });
191191
});
192192

193193
this._paper.on(
@@ -206,18 +206,18 @@ class Paper {
206206
/**SELECTION*/
207207

208208
this._paper.on("element:pointerdown", (cellView, e) => {
209-
if (!app.$resizing) {
210-
// remove resizer if exists
211-
app.$removeResizer();
212-
}
213209
if (cellView.model.attributes.class != "resizer") {
210+
if (!app.$resizing) {
211+
// remove resizer if exists
212+
app.$removeResizer();
213+
}
214+
214215
// if control key is not hold, a different
215216
// the current selection is reset
216-
217217
if (!ctrlDown) {
218218
app.$addUniqueElementToSelection(cellView);
219219
} else {
220-
app.addElementToSelection(cellView);
220+
app.$addElementToSelection(cellView);
221221
}
222222
} else {
223223
app.$initResize(e);
@@ -242,7 +242,7 @@ class Paper {
242242
app.$unSelectAllElements();
243243
}
244244

245-
app.addElementToSelection(cellView);
245+
app.$addElementToSelection(cellView);
246246
contextMenu.setPositionToContextMenu(CONTEXT_MENU_ELEMENT, { x, y });
247247
return false;
248248
});

src/js/elements/deleteElement.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ export const deleteLink = link => {
2828
link.remove();
2929
}
3030
};
31+
32+
export const deleteLinkById = id => {
33+
const link = Graph.getLinkById(id);
34+
deleteLink(link);
35+
};

src/js/store/modules/Interface.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
addLinktoLinks,
1919
removeLinksWithDeletedElements,
2020
unSelectAllElements,
21-
findElementByBoxId
21+
findElementByBoxId,
22+
buildLinkForStore
2223
} from "./utils";
2324
import {
2425
ADD_ELEMENT,
@@ -187,8 +188,9 @@ const Interface = {
187188
$setTextValueInElement({ commit }, payload) {
188189
commit(SET_INPUT_VALUE, { type: Types.TEXT.type, ...payload });
189190
},
190-
$addLink({ commit }, payload) {
191-
commit(ADD_LINK, payload);
191+
$addLink({ commit }, { link }) {
192+
const l = buildLinkForStore(link);
193+
commit(ADD_LINK, { link: l });
192194
},
193195
$deleteLink({ commit }, payload) {
194196
commit(DELETE_LINK, payload);

src/js/store/modules/utils.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ export const deleteLink = (links, link) => {
8282
* @param {graph} graph
8383
*/
8484
export const addLinktoLinks = (links, link) => {
85-
const l = buildLinkForStore(link);
86-
links.push(l);
85+
links.push(link);
8786
};
8887

8988
/**

src/js/store/plugins/ElementPlugin.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { addElementFromName, addLinkFromLink } from "../../elements/addElement";
2+
import { addDataBox } from "../../elements/addDataElement";
3+
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "../../constants/constants";
4+
import { deleteLinkById } from "../../elements/deleteElement";
5+
6+
const ElementPlugin = store => {
7+
store.subscribe(({ type, payload }) => {
8+
// @TODO optimize with only changed values
9+
switch (type) {
10+
case "Interface/ADD_ELEMENT": {
11+
addElement(payload);
12+
break;
13+
}
14+
case "Interface/ADD_ELEMENTS": {
15+
for (const el of payload.elements) {
16+
addElement(el);
17+
}
18+
break;
19+
}
20+
case "Interface/ADD_LINK": {
21+
addLinkFromLink(payload.link);
22+
break;
23+
}
24+
case "Interface/DELETE_LINK": {
25+
deleteLinkById(payload.link);
26+
break;
27+
}
28+
case "Interface/OPEN_WORKFLOW": {
29+
console.log(payload);
30+
const { elements, links } = payload;
31+
for (const el of elements) {
32+
addElement(el);
33+
}
34+
for (const link of links) {
35+
addLinkFromLink(link);
36+
}
37+
break;
38+
}
39+
default:
40+
break;
41+
}
42+
});
43+
};
44+
45+
const addElement = el => {
46+
const { type, category } = el;
47+
switch (category) {
48+
case CATEGORY_SERVICE:
49+
addElementFromName(type, el);
50+
break;
51+
case CATEGORY_DATATEST:
52+
addDataBox(el);
53+
break;
54+
default:
55+
console.log("ERROR ADDING EL");
56+
}
57+
};
58+
59+
export default ElementPlugin;

src/js/store/plugins/HighlightPlugin.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const HighlightPlugin = store => {
1414
case "Interface/UNSELECT_ALL_ELEMENTS":
1515
case "Interface/ADD_UNIQUE_ELEMENT_TO_SELECTION":
1616
case "Interface/ADD_ELEMENTS_TO_SELECTION": {
17-
for (const element of Interface.elements) {
17+
for (const element of Interface.elements.filter(
18+
({ deleted }) => !deleted
19+
)) {
1820
const cellView = Paper.getViewFromBoxId(element.boxId);
1921
if (element.selected) {
2022
highlightSelection(cellView);

0 commit comments

Comments
 (0)