Extract replaceWithInlineTextToggle in text toggle file

This commit is contained in:
relhamdi
2026-06-22 00:48:37 +02:00
parent fc071b44c2
commit 2f3de22cd5
2 changed files with 39 additions and 39 deletions
+2 -38
View File
@@ -4,14 +4,12 @@ import {
drawGroupBorder,
drawWidgetOutline,
findWidget,
hideWidget,
hideWidgetInput,
hookWidget,
registerNode,
setInputDotColor,
waitForWidgets,
} from "./utils.js";
import { InlineTextToggleWidget } from "./widgets/inline_text_toggle_widget.js";
import { replaceWithInlineTextToggle } from "./widgets/inline_text_toggle_widget.js";
// --- Constants ---
@@ -50,40 +48,6 @@ const BOOL_FIELDS = [
// --- Replace native widget pairs with InlineTextToggleWidget ---
const convertToInlineTextToggle = (node, textWidget, modeWidget) => {
const helper = new InlineTextToggleWidget(
textWidget.name,
textWidget,
modeWidget,
);
// Build a brand new custom widget object
const newWidget = {
name: textWidget.name,
type: "custom",
value: textWidget.value, // Kept for compatibility, not used for render
get value() {
return textWidget.value;
},
set value(v) {
textWidget.value = v;
},
draw: (ctx, n, width, posY, height) =>
helper.draw(ctx, n, width, posY, height),
mouse: (event, pos, n) => helper.mouse(event, pos, n),
computeSize: () => helper.computeSize(),
serializeValue: textWidget.serializeValue,
};
const idx = node.widgets.indexOf(textWidget);
node.widgets[idx] = newWidget;
hideWidget(modeWidget, true);
hideWidgetInput(node, modeWidget);
return helper;
};
const replaceWithToggleWidgets = (node) => {
const toggleWidgets = {};
@@ -92,7 +56,7 @@ const replaceWithToggleWidgets = (node) => {
const modeWidget = findWidget(node, `${field}_mode`);
if (!textWidget || !modeWidget) continue;
toggleWidgets[field] = convertToInlineTextToggle(
toggleWidgets[field] = replaceWithInlineTextToggle(
node,
textWidget,
modeWidget,
+37 -1
View File
@@ -1,5 +1,5 @@
import { COLORS } from "../constants.js";
import { fitString } from "../utils.js";
import { fitString, hideWidget, hideWidgetInput } from "../utils.js";
import { app } from "/scripts/app.js";
// --- Constants ---
@@ -116,3 +116,39 @@ export class InlineTextToggleWidget {
return x >= hit[0] && x <= hit[0] + hit[1];
}
}
// -- Extra --
export const replaceWithInlineTextToggle = (node, textWidget, modeWidget) => {
const helper = new InlineTextToggleWidget(
textWidget.name,
textWidget,
modeWidget,
);
// Build a brand new custom widget object
const newWidget = {
name: textWidget.name,
type: "custom",
value: textWidget.value, // Kept for compatibility, not used for render
get value() {
return textWidget.value;
},
set value(v) {
textWidget.value = v;
},
draw: (ctx, n, width, posY, height) =>
helper.draw(ctx, n, width, posY, height),
mouse: (event, pos, n) => helper.mouse(event, pos, n),
computeSize: () => helper.computeSize(),
serializeValue: textWidget.serializeValue,
};
const idx = node.widgets.indexOf(textWidget);
node.widgets[idx] = newWidget;
hideWidget(modeWidget, true);
hideWidgetInput(node, modeWidget);
return helper;
};