File size: 1,624 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class EditorTab {
    constructor (runtime, extensionId, tabId, name, uri) {
        this.runtime = runtime;
        this.extensionId_ = extensionId;
        this.tabId_ = tabId;
        this.element_ = null;

        this.name = name;
        this.icon = uri;
    }

    setName (name) {
        this.name = name;
        this.update();
    }
    setIcon (iconUri) {
        this.icon = iconUri;
        this.update();
    }

    setDOM (element) {
        this.element_ = element;
        this.update();
    }
    update () {
        this.runtime.emit('EDITOR_TABS_UPDATE');
    }
}

/**
 * Class responsible for managing tabs created by extensions.
 * These tabs are editor tabs near the Code, Costumes and Sounds tabs.
 */
class TabManager {
    constructor (runtime) {
        this.runtime = runtime;
        this.tabs_ = {};
    }

    /**
     * Register a new editor tab.
     * @param {string} extensionId ID of this extension.
     * @param {string} tabId ID for this tab.
     * @param {string} name Name of the editor tab.
     * @returns {EditorTab}
     */
    register (extensionId, tabId, name, uri) {
        const fullTabId = `${extensionId}_${tabId}`;
        // check if this tab exists
        if (fullTabId in this.tabs_) {
            console.warn('Tab', tabId, 'for', extensionId, 'already exists.');
            return this.tabs_[fullTabId];
        }
        // create tab
        const tab = new EditorTab(this.runtime, extensionId, tabId, name, uri);
        this.tabs_[fullTabId] = tab;
        this.runtime.emit('EDITOR_TABS_NEW', tab);
        return tab;
    }
}

module.exports = TabManager;