Files
2024-10-02 20:05:45 +08:00

57 lines
2.2 KiB
JavaScript

import { app } from "../../scripts/app.js";
import { ComfyWidgets } from "../../scripts/widgets.js";
app.registerExtension({
name: "org.example.TextDisplay",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeData.name === "TextDisplay") {
function populate(text) {
if (this.widgets) {
for (let i = 1; i < this.widgets.length; i++) {
this.widgets[i].onRemove?.();
}
this.widgets.length = 1;
}
// 确保文本是字符串,如果是数组则合并
const displayText = Array.isArray(text) ? text.join('') : String(text);
const w = ComfyWidgets["STRING"](this, "", ["STRING", { multiline: true }], app).widget;
w.inputEl.readOnly = true;
w.inputEl.style.opacity = 0.6;
w.inputEl.style.whiteSpace = 'pre-wrap';
w.inputEl.style.wordBreak = 'break-word';
w.inputEl.style.overflowWrap = 'break-word';
w.value = displayText;
requestAnimationFrame(() => {
const sz = this.computeSize();
if (sz[0] < this.size[0]) {
sz[0] = this.size[0];
}
if (sz[1] < this.size[1]) {
sz[1] = this.size[1];
}
this.onResize?.(sz);
app.graph.setDirtyCanvas(true, false);
});
}
const onExecuted = nodeType.prototype.onExecuted;
nodeType.prototype.onExecuted = function (message) {
onExecuted?.apply(this, arguments);
if (message && message.text !== undefined) {
populate.call(this, message.text);
}
};
const onConfigure = nodeType.prototype.onConfigure;
nodeType.prototype.onConfigure = function () {
onConfigure?.apply(this, arguments);
if (this.widgets_values?.length) {
populate.call(this, this.widgets_values[0]);
}
};
}
},
});