-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
120 lines (105 loc) Β· 4.29 KB
/
index.ts
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
import type { RoomPlugin } from "types";
import infoWindow from "./base/infoWindow";
import names from "./base/names";
import objectContextMenu from "./base/objectContextMenu";
import objectLoader from "./base/objectLoader";
import objectPreview from "./base/objectPreview";
import objectProperties from "./base/objectProperties";
import objectSnapping from "./base/objectSnapping";
import peerCursors from "./base/peerCursors";
import peerRelaying from "./base/peerRelaying";
import peerSpectate from "./base/peerSpectate";
import pluginManager from "./base/pluginManager";
import roomSharing from "./base/roomSharing";
import theme from "./base/theme";
import viewport from "./base/viewport";
import introDialogue from "./addons/introDialogue";
import presetLoader from "./addons/presetLoader";
import panning from "./base/panning";
import userEvents from "./base/userEvents";
export class PluginManager {
/**
* Global Reference to available plugins
*/
static availablePlugins: RoomPlugin[] = [];
/**
* Loads default and external Plugins
* @param externalPlugins URLs of external plugin files
* @returns a list of RoomPlugins
*/
static async loadPlugins(externalPlugins: string[]): Promise<RoomPlugin[]> {
const plugins = [
viewport,
infoWindow,
introDialogue,
objectContextMenu,
names,
peerCursors,
objectProperties,
objectLoader,
objectPreview,
objectSnapping,
peerSpectate,
peerRelaying,
presetLoader,
pluginManager,
roomSharing,
theme,
panning,
userEvents,
];
if (externalPlugins.length > 0) {
console.info("%c" + this.tag("downloading external plugins..."), "color: #f72");
for (const pluginURL of externalPlugins) {
console.info("%c\t" + this.tag(`downloading ${pluginURL}...`), "color: #d7d");
try {
const pluginCode: string = await (await fetch(pluginURL)).text();
const pluginModule: { default: RoomPlugin } = await import(
/* @vite-ignore */ "data:text/javascript," + pluginCode
);
console.info("%c\t" + this.tag(`successfully downloaded [${pluginModule.default.name}]`), "color: #d7d");
plugins.push(pluginModule.default);
} catch (e) {
console.error(e);
}
}
}
if (plugins.length > 0) {
console.info("%c" + this.tag("validating and ordering plugins..."), "color: #d45");
this.availablePlugins = this.availablePlugins.concat(this.getSortedPlugins(plugins));
const orderList = this.availablePlugins
.map((plugin, index) => `\t${index + 1}.\t${plugin.name}`)
.join("\n");
console.info("%c" + this.tag(`loading order resolved: \n${orderList}`), "color: #0dd");
}
console.info("%c" + this.tag("finished loading plugins!"), "color: #0e0");
return this.availablePlugins;
}
/**
* Sort a list of Plugins according to the order they should be loaded
* @param plugins RoomPlugins
* @returns an array of the input plugins sorted by dependencies
*/
private static getSortedPlugins(plugins: RoomPlugin[]): RoomPlugin[] {
// The behavior here is that duplicate plugins will be ignored
const pluginMap = plugins.reduce((p, c) => p.set(c.name, c), new Map<string, RoomPlugin>());
const sortedPlugins: RoomPlugin[] = [];
function checkDep(dep: string): boolean {
if (!sortedPlugins.find((plugin) => plugin.name === dep)) {
const plugin = pluginMap.get(dep);
if (plugin) {
plugin.dependencies?.forEach(checkDep);
sortedPlugins.push(plugin);
return true;
}
console.error(`missing dependency[${dep}]`);
}
return false;
}
plugins.forEach((plugin) => checkDep(plugin.name));
return sortedPlugins;
}
private static tag(msg: unknown): string {
return "[PluginManager] " + msg;
}
}