Skip to content

Commit 785e0df

Browse files
committed
refactor: highlight as plugin
1 parent 96289b7 commit 785e0df

File tree

8 files changed

+111
-104
lines changed

8 files changed

+111
-104
lines changed

src/js/app.js

-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import components from "./layout/components";
99
import store from "./store/store";
1010
import { DivaServices } from "divaservices-utils";
1111
import { initWebservices } from "./constants/globals";
12-
import { highlightSelection, unHighlight } from "./store/modules/highlight";
1312
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "./constants/constants";
1413
import { addElementFromName, addLinkFromLink } from "./elements/addElement";
1514
import { deleteElementByBoxId, deleteLink } from "./elements/deleteElement";
@@ -277,28 +276,6 @@ export let app;
277276
deleteElementByBoxId(element.boxId);
278277
}
279278
},
280-
/**
281-
* watches selected elements
282-
*/
283-
selectedElements(newValue, oldValue) {
284-
// highlight current selection
285-
for (const { boxId } of newValue) {
286-
const cellView = Paper.findViewInPaper(
287-
Graph.getElementByBoxId(boxId)
288-
);
289-
highlightSelection(cellView);
290-
}
291-
// remove unselected element if not deleted
292-
for (const { boxId } of oldValue.filter(el => !newValue.includes(el))) {
293-
const el = Graph.getElementByBoxId(boxId);
294-
295-
// on delete, these might be still be selected
296-
if (el) {
297-
const cellView = Paper.findViewInPaper(el);
298-
unHighlight(cellView);
299-
}
300-
}
301-
},
302279
/**
303280
* watches paper scale
304281
*/

src/js/classes/Paper.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Paper {
133133
app.elements
134134
);
135135

136-
if (selectedElements) {
136+
if (selectedElements.length) {
137137
app.$selectElements({ elements: selectedElements });
138138
}
139139
}
@@ -215,10 +215,10 @@ class Paper {
215215
// the current selection is reset
216216

217217
if (!ctrlDown) {
218-
app.$unSelectAllElements();
218+
app.$addUniqueElementToSelection(cellView);
219+
} else {
220+
app.addElementToSelection(cellView);
219221
}
220-
221-
app.addElementToSelection(cellView);
222222
} else {
223223
app.$initResize(e);
224224
}
@@ -274,9 +274,6 @@ class Paper {
274274
-bbox.x + canvasDimensions.width / 2 - bbox.width / 2,
275275
-bbox.y + canvasDimensions.height / 2 - bbox.height / 2
276276
);
277-
278-
// highlight element
279-
//this.$addUniqueElementToSelection(el.findView(this._paper));
280277
}
281278

282279
translate(newX, newY) {
@@ -288,6 +285,10 @@ class Paper {
288285
return el.findView(this._paper);
289286
}
290287

288+
getViewFromBoxId(boxId) {
289+
return this.findViewInPaper(Graph.getElementByBoxId(boxId));
290+
}
291+
291292
// helper function to find an empty position to add
292293
// an element without overlapping other ones
293294
// it tries to fill the canvas view first horizontally

src/js/layout/components/toolsbar.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
import { mapState, mapActions } from "vuex";
2424
import { TOOLSBAR } from "../../constants/selectors";
2525
import { shortcutToString } from "../../utils/utils";
26-
import Graph from "../../classes/Graph";
2726
import Paper from "../../classes/Paper";
2827

2928
const Toolsbar = Vue.component("Toolsbar", {
@@ -131,8 +130,8 @@ const Toolsbar = Vue.component("Toolsbar", {
131130
{
132131
resize: {
133132
action: () => {
134-
const cellView = Paper.findViewInPaper(
135-
Graph.getElementByBoxId(this.selectedElements[0].boxId)
133+
const cellView = Paper.getViewFromBoxId(
134+
this.selectedElements[0].boxId
136135
);
137136
this.$createResizer(this.elements, cellView);
138137
},

src/js/store/modules/highlight.js

-69
This file was deleted.
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* This file contains highlight-related functions
3+
* Highlights appear when a box is selected
4+
*/
5+
6+
import { BOX_HIGHLIGHTERS } from "../../constants/constants";
7+
import Paper from "../../classes/Paper";
8+
9+
const HighlightPlugin = store => {
10+
store.subscribe(({ type }, { Interface }) => {
11+
// @TODO optimize with only changed values
12+
switch (type) {
13+
case "Interface/ADD_ELEMENT_TO_SELECTION":
14+
case "Interface/UNSELECT_ALL_ELEMENTS":
15+
case "Interface/ADD_UNIQUE_ELEMENT_TO_SELECTION":
16+
case "Interface/ADD_ELEMENTS_TO_SELECTION": {
17+
console.log(type, Interface);
18+
for (const element of Interface.elements) {
19+
const cellView = Paper.getViewFromBoxId(element.boxId);
20+
if (element.selected) {
21+
highlightSelection(cellView);
22+
} else {
23+
unHighlight(cellView);
24+
}
25+
}
26+
break;
27+
}
28+
default:
29+
break;
30+
}
31+
});
32+
};
33+
34+
export default HighlightPlugin;
35+
36+
function highlightSelection(cellView) {
37+
if (cellView) {
38+
for (const highlight of BOX_HIGHLIGHTERS) {
39+
cellView.highlight(null, highlight);
40+
}
41+
42+
// if(!cellView.el.querySelector(`.${HIGHLIGHT_TOOLSBAR}`)) {
43+
44+
// appendHighlightToolsbar(cellView)
45+
46+
// }
47+
}
48+
}
49+
50+
/**
51+
* remove highlight on given cellView
52+
*
53+
* @param {cellView} cellView
54+
*/
55+
function unHighlight(cellView) {
56+
for (const highlight of BOX_HIGHLIGHTERS) {
57+
cellView.unhighlight(null, highlight);
58+
}
59+
60+
// removeHighlightToolsbar(cellView)
61+
}
62+
63+
/**
64+
* remove highlight for all given elements
65+
*
66+
* @param {array} elements
67+
*/
68+
// function unHighlightAllElements(elements) {
69+
// for (const el of elements) {
70+
// unHighlight(el);
71+
// }
72+
// }
73+
74+
/**
75+
* reset highlight on element
76+
*
77+
* @param {element} element
78+
*/
79+
// const resetHighlight = element => {
80+
// unHighlight(element);
81+
// highlightSelection(element);
82+
// };
83+
84+
/**
85+
* reset highlight on all elements
86+
*
87+
* @param {araay} elements
88+
*/
89+
// function resetHighlightAllElements(elements) {
90+
// for (const el of elements) {
91+
// resetHighlight(el);
92+
// }
93+
// }

src/js/store/plugins/UndoRedoPlugin.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { cloneDeep } from "lodash";
66
import UndoRedoHistory from "./UndoRedoHistory";
77

88
// operations which can be undo/redo
9+
// @TODO messages constants
910
const savedOperations = [
1011
"Interface/INIT_GRAPH",
1112
"Interface/INIT_PAPER",

src/js/store/modules/highlightToolsbar.js src/js/store/plugins/highlightToolsbar.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { app } from "../../app";
22
const HIGHLIGHT_TOOLSBAR = "highlight-toolsbar";
33
const MARGIN = 15;
44

5+
// const removeHighlightToolsbar = (cellView) => {
6+
// cellView.el.querySelector(`.${HIGHLIGHT_TOOLSBAR}`).remove();
7+
// }
8+
59
/**
610
* Append a highlight toolbar
711
* state: on development

src/js/store/plugins/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import undoRedoPlugin from "./UndoRedoPlugin";
1+
import UndoRedoPlugin from "./UndoRedoPlugin";
2+
import HighlightPlugin from "./HighlightPlugin";
23

3-
export default [undoRedoPlugin];
4+
export default [UndoRedoPlugin, HighlightPlugin];

0 commit comments

Comments
 (0)