-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathManager.js
200 lines (155 loc) · 4.94 KB
/
Manager.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { destroy } from "mobx-state-tree";
import { guidGenerator } from "../utils/unique";
/** @type {Map<any, ToolsManager>} */
const INSTANCES = new Map();
let root = null;
class ToolsManager {
static getInstance({ name } = {}) {
if (!name) return;
if (INSTANCES.has(name)) {
return INSTANCES.get(name);
}
const instance = new ToolsManager({ name });
INSTANCES.set(name, instance);
return instance;
}
static allInstances() {
return Array.from(INSTANCES.values());
}
static setRoot(rootStore) {
root = rootStore;
}
static removeAllTools() {
INSTANCES.forEach((manager) => manager.removeAllTools());
INSTANCES.clear();
}
constructor({ name } = {}) {
this.name = name;
this.tools = {};
this._default_tool = null;
this._prefix = guidGenerator();
}
get preservedTool() {
return window.localStorage.getItem(`selected-tool:${this.name}`);
}
get root() {
return root;
}
get obj() {
return root.annotationStore.names.get(this.name);
}
addTool(toolName, tool, removeDuplicatesNamed = null, prefix = guidGenerator()) {
if (tool.smart && tool.control?.smartonly) return;
// todo: It seems that key is used only for storing,
// but not for finding tools, so may be there might
// be an array instead of an object
const name = tool.toolName ?? toolName;
const key = `${prefix ?? this._prefix}#${name}`;
if (removeDuplicatesNamed && toolName === removeDuplicatesNamed) {
const findme = new RegExp(`^.*?#${name}.*$`);
if (Object.keys(this.tools).some((entry) => findme.test(entry))) {
console.log(
`Ignoring duplicate tool ${name} because it matches removeDuplicatesNamed ${removeDuplicatesNamed}`,
);
return;
}
}
this.tools[key] = tool;
if (tool.default && !this._default_tool) this._default_tool = tool;
if (this.preservedTool && tool.shouldPreserveSelectedState) {
if (tool.fullName === this.preservedTool && tool.setSelected) {
this.unselectAll();
this.selectTool(tool, true, true);
return;
}
}
if (this._default_tool && !this.hasSelected) {
this.selectTool(this._default_tool, true, true);
}
}
unselectAll() {
// when one of the tool get selected you need to unselect all
// other active tools
Object.values(this.tools).forEach((t) => {
if (typeof t.selected !== "undefined") t.setSelected(false);
});
const stage = this.obj?.stageRef;
if (stage) {
stage.container().style.cursor = "default";
}
}
selectTool(tool, selected, isInitial = false) {
const currentTool = this.findSelectedTool();
const newSelection = tool?.group;
// if there are no tools selected, there are no specific labels to unselect
// also this will skip annotation init
if (currentTool && newSelection === "segmentation") {
const toolType = tool.control.type.replace(/labels$/, "");
const currentLabels = tool.obj.activeStates();
// labels of different types; we can't create regions with different tools simultaneously, so we have to unselect them
const unrelatedLabels = currentLabels.filter((tag) => {
const type = tag.type.replace(/labels$/, "");
if (tag.type === "labels") return false;
if (type === toolType) return false;
return true;
});
unrelatedLabels.forEach((tag) => tag.unselectAll());
}
currentTool?.handleToolSwitch?.(tool);
if (selected) {
this.unselectAll();
tool.setSelected?.(true, isInitial);
} else {
const drawingTool = this.findDrawingTool();
this.selectTool(drawingTool ?? this._default_tool, true);
}
}
selectDefault() {
const tool = this.findSelectedTool();
if (this._default_tool && tool?.dynamic === true) {
this.unselectAll();
this._default_tool.setSelected(true);
}
}
allTools() {
return Object.values(this.tools);
}
addToolsFromControl(s) {
if (s.tools) {
const t = s.tools;
Object.keys(t).forEach((k) => {
this.addTool(k, t[k], s.removeDuplicatesNamed, s.name || s.id);
});
}
}
findSelectedTool() {
return Object.values(this.tools).find((t) => t.selected);
}
findDrawingTool() {
return Object.values(this.tools).find((t) => t.isDrawing);
}
event(name, ev, ...args) {
// if there is an active tool, dispatch there
const selectedTool = this.findSelectedTool();
if (selectedTool) {
selectedTool.event(name, ev, args);
return;
}
}
reload({ name } = {}) {
INSTANCES.delete(this.name);
INSTANCES.set(name, this);
this.removeAllTools();
this.name = name;
}
removeAllTools() {
Object.values(this.tools).forEach((t) => destroy(t));
this.tools = {};
this._default_tool = null;
}
get hasSelected() {
return Object.values(this.tools).some((t) => t.selected);
}
}
window.ToolManager = ToolsManager;
export default ToolsManager;