From 21eb41743b8c1fa584c91646fef088aa276b5de7 Mon Sep 17 00:00:00 2001 From: cgjgh <160297365+cgjgh@users.noreply.github.com> Date: Wed, 7 May 2025 07:26:09 -0500 Subject: [PATCH 1/3] Add onEmitConfig hook for plugins --- nodes/config/ui_base.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/nodes/config/ui_base.js b/nodes/config/ui_base.js index 7256b2cdf..57881f77e 100644 --- a/nodes/config/ui_base.js +++ b/nodes/config/ui_base.js @@ -473,8 +473,9 @@ module.exports = function (RED) { if (!handshakeEditKey || !meta?.wysiwyg?.editKey || handshakeEditKey !== meta.wysiwyg.editKey) { delete meta.wysiwyg } - // pass the connected UI the UI config - socket.emit('ui-config', node.id, { + + // Prepare the UI configuration + const uiConfig = { meta, dashboards: Object.fromEntries(node.ui.dashboards), heads: Object.fromEntries(node.ui.heads), @@ -482,7 +483,21 @@ module.exports = function (RED) { themes: Object.fromEntries(node.ui.themes), groups: Object.fromEntries(node.ui.groups), widgets: Object.fromEntries(node.ui.widgets) + } + + // Apply plugin modifications + let finalConfig = uiConfig + RED.plugins.getByType('node-red-dashboard-2').forEach(plugin => { + if (plugin.hooks?.onEmitConfig) { + const modifiedConfig = plugin.hooks.onEmitConfig(socket, finalConfig) + if (modifiedConfig) { + finalConfig = modifiedConfig + } + } }) + + // Pass the connected UI the UI config + socket.emit('ui-config', node.id, finalConfig) } // remove event handler socket listeners for a given socket connection From c9ca67ea1f41ef571cbb7786d5992666a33e752a Mon Sep 17 00:00:00 2001 From: cgjgh <160297365+cgjgh@users.noreply.github.com> Date: Wed, 7 May 2025 13:01:44 -0500 Subject: [PATCH 2/3] Add msg with connection credentials --- nodes/config/ui_base.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nodes/config/ui_base.js b/nodes/config/ui_base.js index 57881f77e..d291f3a35 100644 --- a/nodes/config/ui_base.js +++ b/nodes/config/ui_base.js @@ -485,11 +485,13 @@ module.exports = function (RED) { widgets: Object.fromEntries(node.ui.widgets) } - // Apply plugin modifications + // Apply plugin modifications with connection credentials let finalConfig = uiConfig + let msg = {} + msg = addConnectionCredentials(RED, msg, socket, node) RED.plugins.getByType('node-red-dashboard-2').forEach(plugin => { if (plugin.hooks?.onEmitConfig) { - const modifiedConfig = plugin.hooks.onEmitConfig(socket, finalConfig) + const modifiedConfig = plugin.hooks.onEmitConfig(socket, finalConfig, msg) if (modifiedConfig) { finalConfig = modifiedConfig } From 0f660f01e6af00876c5f8d812a1316b8c8f6afa3 Mon Sep 17 00:00:00 2001 From: cgjgh <160297365+cgjgh@users.noreply.github.com> Date: Wed, 7 May 2025 13:01:51 -0500 Subject: [PATCH 3/3] Update index.md --- docs/contributing/plugins/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/contributing/plugins/index.md b/docs/contributing/plugins/index.md index db62fb320..c810d0abc 100644 --- a/docs/contributing/plugins/index.md +++ b/docs/contributing/plugins/index.md @@ -114,6 +114,19 @@ module.exports = function(RED) { } } }, + /** + * onEmitConfig - called when the UI configuration is about to be sent to a client + * @param {object} socket - SocketIO connection object to the client + * @param {object} config - The UI configuration object that will be sent + * @param {object} msg - A msg object, populated with connection credentials by `addConnectionCredentials` + * @returns {object} - The (potentially modified) UI configuration object + */ + onEmitConfig: (socket, config, msg) => { + // modify config in any way you like + // e.g. config.myCustomProperty = "Hello World" + // You can also use data from msg, e.g. msg._client.socketIp + return config + }, /** * onInput - called when a node receives a message * @param {object} msg - Node-RED msg object