This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
216 lines (196 loc) · 5.96 KB
/
main.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import type * as BlocklyObject from "./node_modules/blockly/core/blockly.js";
type BlocklyRuntime = typeof BlocklyObject;
interface manifestObject {
id: string;
name: string;
version: string;
description: string;
author: string;
homepage: string;
main: string;
}
interface PluginInfo {
baseUrl: string;
manifest: manifestObject;
getUrl(url: string): string;
initializeWorkspace(): void;
}
interface PluginObject {
getPlugin(id: string): PluginInfo;
}
interface BF2042PortalRuntimeSDK {
Plugins: PluginObject;
}
type ToolBoxBlockItem = {
blockxml: XMLDocument;
displayName: string;
kind: string;
type: string;
};
export type FlyoutSectionLabel = {
kind: string;
text: string;
};
export const BF2042SDK = BF2042Portal.Plugins.getPlugin(
"1650e7b6-3676-4858-8c9c-95b9381b7f8c"
);
import { Logger } from "./lib/logger";
export const logger = new Logger(BF2042SDK.manifest.name);
import { leftPluginPaneManager } from "./lib/leftPaneManager";
import { searchWithCategory } from "./plugins/searchWithCategory";
import { menuInitlizer } from "./lib/menu";
import { startTess, tessOCR, loadCoordinateReader } from "./plugins/coordinatReader.js";
import { mutationObserverWrapper, showStartupBanner } from "./lib/helper";
import Tesseract from "tesseract.js";
declare global {
interface Window {
modBlock: BlocklyObject.Block;
allBlocks: { [id: string]: ToolBoxBlockItem[] };
mainWorkspace: BlocklyObject.Workspace;
Blockly: BlocklyRuntime;
coolPlugins: {
tess: {
scheduler: Tesseract.Scheduler;
worker: Tesseract.Worker;
};
};
_Blockly: BlocklyRuntime;
tessOCR: typeof tessOCR;
startTess: typeof startTess;
loadCoordinateReader: typeof loadCoordinateReader;
}
}
declare var BF2042Portal: BF2042PortalRuntimeSDK, _Blockly: BlocklyRuntime;
const flyout_show_event = new CustomEvent("flyout_show_event", {
detail: true,
}),
flyout_hide_event = new CustomEvent("flyout_hide_event", { detail: false });
export let Blockly: BlocklyRuntime = undefined;
let mainWorkspace: BlocklyObject.Workspace = undefined,
modBlock: BlocklyObject.Block = undefined,
allBlocks: { [id: string]: ToolBoxBlockItem[] } = {},
blocklyMutationObserver: MutationObserver = undefined,
blockly_plugins_loaded = false,
og_flyout_show = undefined;
function setBlocklyBaseVars() {
logger.info("Setting Blockly Plugins Base variables...");
(Blockly = _Blockly),
(mainWorkspace = Blockly.getMainWorkspace() || undefined);
if (mainWorkspace) {
(modBlock = _Blockly
.getMainWorkspace()
.getBlocksByType("modBlock", false)[0]),
(allBlocks = {});
(mainWorkspace as any)
.getToolbox()
.toolboxDef_.contents.forEach((element) => {
if (
element.kind === "SEP" ||
[
"SEARCH RESULTS",
"VARIABLES",
"SUBROUTINES",
"CONTROL ACTIONS",
].includes(element.name)
) {
return;
}
element.contents.forEach((block: ToolBoxBlockItem) => {
if (block.kind === "LABEL") {
return;
}
if (!(element.name in allBlocks)) {
allBlocks[element.name] = [block];
} else {
allBlocks[element.name].push(block);
}
});
});
let flyout = (mainWorkspace as any).getFlyout();
og_flyout_show = flyout.setVisible;
flyout.setVisible = showflyout;
(window.modBlock = modBlock),
(window.allBlocks = allBlocks),
(window.mainWorkspace = mainWorkspace);
(window.startTess = startTess);
(window.tessOCR = tessOCR);
(window.loadCoordinateReader = loadCoordinateReader);
(window.coolPlugins) = {
tess: {
scheduler: undefined,
worker: undefined,
}
}
window.Blockly = Blockly;
}
}
function setGlobalPluginBaseVars() {
logger.info("Setting Global Plugins Base variables...");
}
function showflyout(show: boolean) {
og_flyout_show.call(this, show);
window.dispatchEvent(show ? flyout_show_event : flyout_hide_event);
}
function blocklyPluginMain() {
if (blocklyMutationObserver == undefined) {
mutationObserverWrapper("app-rules", function (mutationList, observer) {
blocklyMutationObserver = observer;
for (const mutation of mutationList) {
if (mutation.type === "childList") {
if (!document.getElementsByTagName("app-blockly").length) {
// required to build dynamic html again as DOM is recreated
if (blockly_plugins_loaded) {
blockly_plugins_loaded = false;
observer.disconnect();
blocklyMutationObserver = undefined;
logger.warn(
"DOM recreated.... waiting to reload blockly specific plugins...."
);
}
}
}
}
});
}
blockly_plugins_loaded = true;
}
function loadSubPlugins() {
setGlobalPluginBaseVars();
if (!blockly_plugins_loaded) {
try {
setBlocklyBaseVars();
if (mainWorkspace) {
leftPluginPaneManager();
menuInitlizer();
searchWithCategory();
}
blocklyPluginMain();
} catch (error) {
logger.critical("Failed loading plugin... " + error);
}
}
logger.info("coolnesss loaded");
(window as any).cool_plugin_loaded = true;
}
function loadJquery() {
// Load the script
if (typeof (window as any).jQuery === "undefined") {
const script = document.createElement("script");
script.src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
script.type = "text/javascript";
script.addEventListener("load", () => {
logger.info(`jQuery ${$.fn.jquery} has been loaded successfully!`);
loadSubPlugins();
});
document.head.appendChild(script);
}
showStartupBanner();
}
BF2042SDK.initializeWorkspace = function () {
if(typeof (window as any).jQuery === "undefined")
loadJquery();
else
loadSubPlugins();
blockly_plugins_loaded = false;
};