diff --git a/docs/en/contributing/plugins/index.md b/docs/en/contributing/plugins/index.md index cf2b0ab84..3cec4c170 100644 --- a/docs/en/contributing/plugins/index.md +++ b/docs/en/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 diff --git a/nodes/config/ui_base.js b/nodes/config/ui_base.js index 028125d68..b055f3b1f 100644 --- a/nodes/config/ui_base.js +++ b/nodes/config/ui_base.js @@ -503,8 +503,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), @@ -512,7 +513,23 @@ 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 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, msg) + 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