Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (43 loc) · 2.01 KB
/
Copy pathindex.js
File metadata and controls
50 lines (43 loc) · 2.01 KB
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
/**
* G-code Explorer - ncSender plugin
* Entry for the Node-based ncSender platforms (v1 / pro-v1).
*
* The plugin's actual logic lives in commands.js, which is written to
* run inside the pro-v2 (C#) embedded JS engine where a global
* `pluginContext` is injected. This wrapper adapts the v1 `ctx` API to
* that contract so both platforms share one implementation.
*/
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { onBeforeCommand, buildInitialConfig } from './commands.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const resolveServerPort = (appSettings = {}) => {
const appPort = Number.parseInt(appSettings?.senderPort, 10);
return Number.isFinite(appPort) ? appPort : 8090;
};
export async function onLoad(ctx) {
ctx.log('G-code Explorer plugin loaded');
// commands.js expects the pro-v2 global; adapt the v1 ctx to it.
globalThis.pluginContext = {
log: (...args) => ctx.log(...args),
showDialog: (title, html, options) => ctx.showDialog(title, html, options),
getTools: () => (typeof ctx.getTools === 'function' ? ctx.getTools() : [])
};
ctx.registerEventHandler('onBeforeCommand', async (commands, context) => {
const settings = buildInitialConfig(ctx.getSettings() || {});
return onBeforeCommand(commands, context, settings);
});
ctx.registerToolMenu('G-code Explorer', async () => {
const appSettings = typeof ctx.getAppSettings === 'function' ? (ctx.getAppSettings() || {}) : {};
let html = readFileSync(join(__dirname, 'config.html'), 'utf-8');
// split/join = replace ALL occurrences, matching the pro-v2 server's behavior
html = html
.split('__TOOL_MENU_LABEL__').join('G-code Explorer')
.split('__SERVER_PORT__').join(String(resolveServerPort(appSettings)));
ctx.showDialog('G-code Explorer', html, { closable: true });
}, { icon: 'icon.png' });
}
export function onUnload() {
console.log('[PLUGIN:com.cncdocs.gcode-reference] G-code Explorer plugin unloaded');
}