From a07b2fdf91798c9212737a90469950dcbe889f19 Mon Sep 17 00:00:00 2001 From: Sun Yi-Ran Date: Sun, 25 Feb 2024 17:16:13 +0800 Subject: [PATCH] fix afterChange may override other extensions (#15) --- extensions/colors.js | 50 ++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/extensions/colors.js b/extensions/colors.js index c5b5648..1e4c42c 100644 --- a/extensions/colors.js +++ b/extensions/colors.js @@ -184,45 +184,40 @@ function colorPositiveNegative(app) { * Colors */ let afterChange; +let colorModeValue; +function invokeAfterChange() { + switch (colorModeValue) { + case 1: + uncolor(app); + break; + case 2: + rainbowify(app); + break; + case 3: + colorByType(app); + break; + default: + break; + } + if (getPosNegValue()) { + colorPositiveNegative(app); + } + return afterChange?.apply(this, arguments); +} + function setColorMode(value, app) { + colorModeValue = value; switch (value) { case 1: - app.graph.afterChange = function () { - uncolor(app); - if (getPosNegValue()) { - colorPositiveNegative(app); - } - return afterChange?.apply(this, arguments); - }; uncolor(app); break; case 2: - app.graph.afterChange = function () { - rainbowify(app); - if (getPosNegValue()) { - colorPositiveNegative(app); - } - return afterChange?.apply(this, arguments); - }; rainbowify(app); break; case 3: - app.graph.afterChange = function () { - colorByType(app); - if (getPosNegValue()) { - colorPositiveNegative(app); - } - return afterChange?.apply(this, arguments); - }; colorByType(app); break; default: - app.graph.afterChange = function () { - if (getPosNegValue()) { - colorPositiveNegative(app); - } - return afterChange?.apply(this, arguments); - }; app.graph._nodes.forEach((node) => { node.bgcolor = node._bgcolor ?? node.bgcolor; node.color = node._color ?? node.color; @@ -275,6 +270,7 @@ app.registerExtension({ }, async init(app) { afterChange = app.graph.afterChange; + app.graph.afterChange = invokeAfterChange; const onMenuNodeColors = LGraphCanvas.onMenuNodeColors; LGraphCanvas.onMenuNodeColors = function (value, options, e, menu, node) { const response = onMenuNodeColors.apply(this, arguments);