-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.js
60 lines (52 loc) · 1.65 KB
/
selection.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
class SelectionMonitor {
constructor() {
this._selection = [];
this.listeners = [];
this.onSelectionUpdated = this.onSelectionUpdated.bind(this);
miro.onReady(() => {
// subscribe on user selected widgets
miro.addListener(miro.enums.event.SELECTION_UPDATED, this.onSelectionUpdated)
this.onSelectionUpdated()
})
}
async onSelectionUpdated() {
// Get selected widgets
let widgets = await miro.board.selection.get()
this._selection = widgets;
console.log("widgets", widgets, this);
for(let i in this.listeners)
{
const listener = this.listeners[i];
try {
// console.log("calling listener",i, widgets);
listener(widgets);
} catch (e) {
console.error("error", e);
}
}
}
addSelectionListener(listener) {
if (typeof listener === "function" && !this.listeners.some((item) => item === listener)) {
this.listeners.push(listener);
return true;
} else {
return false;
}
}
removeSelectionListener(listener) {
if (typeof listener === "function" && this.listeners.some((item) => item === listener)) {
const index = this.listeners.indexOf(listener);
if (index > -1) {
this.listeners.splice(index, 1);
return true;
}
return false;
} else {
return false;
}
}
getSelection() {
return this._selection;
}
}
const BoardSelection = new SelectionMonitor()